praliedutzel

praliedutzel

Handling focus with JS commands - is there a way to conditionally trigger the phx-window-keydown only if that modal is open?

I have a function component for a modal that uses JS commands to handle opening and closing the modal. When the modal is closed, I want to return focus to the trigger that was used to open the modal initially. I have this working except when a user presses the Escape key to close the modal when there are multiple modals on the same page.

Here’s a simplified version of my function component and the JS commands:

def modal(assigns) do
  ~H"""
  <.focus_wrap id={@id} class="modal">
    <div role="dialog" aria-modal="true" phx-window-keydown={close_modal(@id)} phx-key="escape">
      <%= render_slot(@inner_block) %>
    </div>
  </.focus_wrap>
  """
end

def close_modal(id) do
  %JS{}
  |> JS.remove_class("overflow-hidden", to: "body")
  |> JS.transition("is-closing", to: "##{id}")
  |> JS.remove_class("is-opened", to: "##{id}")
  |> JS.focus_first(to: "[data-role=#{id}-trigger]")
end

Because there are multiple modals on the same page and they all have phx-window-keydown, they all get run when a user hits the Escape key. It seems like the last modal on the page is the one that gets the focus moves to its trigger, even if it wasn’t the modal that was opened.

Is there a way to conditionally trigger the phx-window-keydown only if that modal is open? I haven’t had any luck so far. I’m using phx-window-keydown since I want it to fire from any element inside the modal, but maybe that’s not the right way of going about it? Open to any ideas! :thinking:

I know I could dispatch a custom JS event to handle this on the JavaScript side, but was hoping to leverage the same close_modal function I’m using on the close button (not shown in the code above) so it’s consistent.

Marked As Solved

trisolaran

trisolaran

Have you tried using push_focus and pop_focus to push the focus to the modal when opening it from the trigger and popping it back to the trigger on modal close?

I’ve never used these functions, but they seem worth a try in this case.

Also Liked

cmo

cmo

I recently had the same problem while migrating my dropdowns from Alipine to JS commands.

I ended up removing the phx-window-keydown and in the hook, adding an event listener on keydown when key is Escape. Then event triggered the JS I had defined for phx-click-away on the dropdown panel, using this.liveSocket.execJS.

If you look at livebeats they click the document body and focus the element in the menu(?) Hook.

praliedutzel

praliedutzel

I had initially tried push_focus and pop_focus and they didn’t work, but I must have had something else wrong; just tried again this morning and they worked for what I need!

Here are the open and close functions for reference:

def open_modal(id) do
  %JS{}
  |> JS.push_focus()
  |> JS.add_class("overflow-hidden", to: "body")
  |> JS.remove_class("is-closing", to: "##{id}")
  |> JS.transition("is-opening", to: "##{id}")
  |> JS.add_class("is-opened", to: "##{id}")
  |> JS.focus_first(to: "##{id}")
end
  
def close_modal(id) do
  %JS{}
  |> JS.remove_class("overflow-hidden", to: "body")
  |> JS.transition("is-closing", to: "##{id}")
  |> JS.remove_class("is-opened", to: "##{id}")
  |> JS.pop_focus()
end

Last Post!

praliedutzel

praliedutzel

I had initially tried push_focus and pop_focus and they didn’t work, but I must have had something else wrong; just tried again this morning and they worked for what I need!

Here are the open and close functions for reference:

def open_modal(id) do
  %JS{}
  |> JS.push_focus()
  |> JS.add_class("overflow-hidden", to: "body")
  |> JS.remove_class("is-closing", to: "##{id}")
  |> JS.transition("is-opening", to: "##{id}")
  |> JS.add_class("is-opened", to: "##{id}")
  |> JS.focus_first(to: "##{id}")
end
  
def close_modal(id) do
  %JS{}
  |> JS.remove_class("overflow-hidden", to: "body")
  |> JS.transition("is-closing", to: "##{id}")
  |> JS.remove_class("is-opened", to: "##{id}")
  |> JS.pop_focus()
end

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement