MitchellJeppson

MitchellJeppson

Triggering focus on mobile via JS module

Using the JS.focus() function to set the focus to a text field does not work as expected on mobile.

I have the following simplified live view:

defmodule ExampleWeb.Live.ExampleLive do
  use ExampleWeb, :live_view

  def render(assigns) do
    ~H"""
    <.button phx-click={JS.focus(to: "#my_text_area")}>Click me to focus</.button>

    <input type="text" id="my_text_area" />
    """
  end
end

When I click the button via my desktop browser it works as expected, I get a blinking cursor in the text box and it is outlined in blue to signal it is selected. But when I do the same on my mobile I only get the blue outline, no blinking cursor and the keyboard doesn’t pop up like you would expect if you had selected a text field.

Tried on chrome and safari, same behavior.

The only thing I can find that might be the cause is here https://stackoverflow.com/questions/54049029/focus-doesnt-work-on-chrome-mobile-when-not-activated-with-click-workaround

essentially: mobile browsers require an actual user interaction to have happened not too long before the focus is programmatically triggered

Maybe something with how LiveView is handling the event makes the browser think it is no longer valid and won’t allow programatic focus anymore?

Any input is appreciated :heart:

First Post!

alexanders

alexanders

Looks like that’s just the way it works on iOS. Programmatically triggered focus doesn’t show the keyboard, but apply focus styles. I don’t think it has anything to do with LiveView because if you use plain JS, it will do exactly the same. And recent interaction doesn’t seem to help because if you warp focus into setTimeout it still doesn’t work. On Android it shows keyboard.

Most Liked

codeanpeace

codeanpeace

This reminds me of another post on how WebKit used to require a whole rigamarole of propagating user gestures to call the WebAuthn API. Funnily enough, that discussion was also prompted by one of your questions. Requiring "native click listener" within LiveView for webauthn

Check out this answer on StackOverflow, the key bit is that HTMLElement.focus() needs to be called synchronously from the event handler like you’re doing in that hook. And from skimming the LiveView source code, I suspect the debouncing logic to LiveView bindings might be causing some unintended side effects here.
https://github.com/phoenixframework/phoenix_live_view/blob/d1250dc6541684fd19f5c8dd3aea692ad4b57950/assets/js/phoenix_live_view/live_socket.js#L658-L663

arcanemachine

arcanemachine

Just so we’re all clear here, when you say “mobile”, you mean “iOS”? Or are you testing on Android as well?

arcanemachine

arcanemachine

OK so I tested out your example code on a couple devices, and my experiences (on iOS) match the issues you are describing.

Using JS.focus/2

iPhone 7, iOS 15, Safari: Does not focus (only applies focus CSS style to text box)

iPad 7th gen, iOS 17, Safari: Does not focus (only applies focus CSS style to text box)

Pixel 7, Android 13, Chrome: Focuses the text box and brings up the keyboard

Using hooks

iPhone 7, iOS 15, Safari: Focuses the text box and brings up the keyboard

iPad 7th gen, iOS 17, Safari: Focuses the text box and brings up the keyboard

Pixel 7, Android 13, Chrome: Focuses the text box and brings up the keyboard


It is worth mentioning that Chrome on iOS (like all iOS browsers) still uses Safari’s rendering engine (although not in the EU as of iOS 17.4 apparently), so iOS Chrome will likely have the same issue as Safari.


Based on my (limited) understanding of the situation, I would say that your suspicions are correct. At the very least, this situation warrants a new GitHub issue (I couldn’t find one on the topic, but I might not have looked hard enough).

Last Post!

MitchellJeppson

MitchellJeppson

@arcanemachine thank you for testing both versions on iOS and android :heart:

@codeanpeace hey! :wave: good to chat again! Thanks for digging into that.

I created a bug ticket here, feel free to pipe in if I left anything out.

Appreciate you both, the elixir community rules :metal:

Where Next?

Popular in Questions Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement