dvartic

dvartic

Link/1 action on mousedown instead of click

Would there be interest to implement a link/1 that gives to option to operate on other events?

So I implemented a simple link/1 function that instead of working on click works on mousedown (or whatever you want, based on a hook).

Just took a look at Lieview code, copied the link/1 function, and added a hook onmount that calls click which, based on also Lieview code, already has an event and event checks for if(this.pendingLink === href) {return;} so I don’t need to prevent the next click event after the mouse is lifted. Works really well.

  attr :id, :string, required: true
  attr :navigate, :string
  attr :patch, :string
  attr :href, :any
  attr :replace, :boolean, default: false
  attr :method, :string, default: "get"
  attr :csrf_token, :any, default: true
  attr :rest, :global, include: ~w(download hreflang referrerpolicy rel target type)
  slot :inner_block, required: true

  def fast_link(%{navigate: to} = assigns) when is_binary(to) do
    ~H"""
    <a
      id={@id}
      href={@navigate}
      data-phx-link="redirect"
      data-phx-link-state={if @replace, do: "replace", else: "push"}
      phx-hook="FastLink"
      {@rest}
    >
      <%= render_slot(@inner_block) %>
    </a>
    """
  end

Repeat the others for push and href.

The hook is just:

export const FastLink = {
  mounted() {
    const linkEl = this.el;

    linkEl.addEventListener("mousedown", (e) => {
      e.preventDefault();
      e.stopImmediatePropagation();
      linkEl.click();
    });
  },
};

Most Liked

BartOtten

BartOtten

I always like it when people experiment with unofficial solutions. But….

You might want to buy a bit more from people who actually studied usability patterns. For me: If I touch a button and it fires before I lift my finger..I get really irritated. Maybe I want to open the link in another tab (I can’t), select the text (I can’t) and other issues you have probably never thought of as you do not know all use cases. You as developer making the decision if it should be a fast link or a normal one is killing (_blank and _new are considered bad practice too)

There are usability guidelines for a reason; so you don’t have to learn all reasons why we came up with them in the first place.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you want a link to behave like a link, please don’t do this.

If you want to use a link for other purposes then sure, but please don’t do this to actual hrefs. As others have stated, the loss in consistency, predictability, and accessibility isn’t worth it.

BartOtten

BartOtten

My ‘opinion’ is so strong because I actually studied usability. So I do not guess, I know. And I share that information with you.

Your logic is broken. Pushing things forward might be accomplished by breaking the rules. That does not mean the other way around is also true. I could, for example, break the rules of this forum by using CAPS-LOCK ALL THE TIME. BUT WE BOTH AGREE IT DOES NOT PUSH ANYTHING, EXCEPT THE WRONG BUTTONS (pun intended).

You asked for opinions; you got opinions. Don’t act like all who replied are narrow sighted and suggest they don’t like going forward.

Last Post!

BartOtten

BartOtten

Just to be clear: you did find the information but you were still not convinced? Ah well…you do you as they say :upside_down_face:

Where Next?

Popular in Proposals: Ideas Top

hst337
Elixir compiler and language specification Purpose of the proposal Elixir language is in mature state and no breaking or heavy changes ar...
New
c4710n
This is an idea for improving the workflow of iex -S mix phx.server The Problem and The Solution in use When running multiple Phoenix en...
New
rmoorman
Current situation Currently, the structure of the HTML returned by phoenix is determined by the layouts (components/layouts/[root,app].ht...
New
dkuku
This is a proposal to make the map key mismatch errors a bit better: Every time I have a typo It’s very challenging for me even when I u...
New
bartblast
This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc,...
New
byhemechi
Many web frameworks (e.g. Remix, Gatsby) have an option for their link components that begins the navigation request on hover so that whe...
New
eagle-head
Hi everyone, I’ve been researching Content Security Policy Level 3 support in Phoenix and wanted to share my findings and a proposal for...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement