Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support inner: true on JS functions #3396

Closed
wants to merge 2 commits into from
Closed

support inner: true on JS functions #3396

wants to merge 2 commits into from

Conversation

SteffenDE
Copy link
Collaborator

Sometimes it makes sense to only target elements inside of the source element when using JS functions. One example:

~H"""
<.button phx-click={JS.dispatch("click", to: "a", inner: true)}>
  <.link navigate={~p"/posts/new"}>New Post</.link>
</.button>
"""

without inner: true you could only sanely target the inner link by giving it a unique id. This PR adds inner: true as an option to all JS function that accept the :to option, allowing the selector to only target elements inside the source element.

Wdyt?

@greven
Copy link

greven commented Aug 21, 2024

I like this. In my current project since I'm using plain CSS I just add button classes to the link instead of composing a .button with .link inside, but this is a cleaner solution than using an id. 👌

@rktjmp
Copy link
Contributor

rktjmp commented Aug 22, 2024

I did a PR for this kind of idea a while ago, where you would instead give a tuple, {target, scope} selector which meant you could do something similar to this, but also raise up to a container. Eg: you could have your button click "go up" to the nearest .list parent, then down to the nearest .delete button. Then you could have as many .list's without having to generate unique ids for every combination of context * action * object and better make components self-contained.

Not sure if something like that is worth considering, having a way to go up-down and this-down.

#2493

@SteffenDE SteffenDE closed this Sep 16, 2024
@SteffenDE SteffenDE deleted the sd-js-inner branch September 16, 2024 19:21
@SteffenDE
Copy link
Collaborator Author

Superseded by #3433

@chrismccord
Copy link
Member

Note, main now supports to: {:inner, "a"} and to: {:closest, "tr"} for js commands. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants