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

Feature Idea - :ancestor action option #710

Open
NakajimaTakuya opened this issue Aug 1, 2023 · 0 comments
Open

Feature Idea - :ancestor action option #710

NakajimaTakuya opened this issue Aug 1, 2023 · 0 comments

Comments

@NakajimaTakuya
Copy link
Contributor

NakajimaTakuya commented Aug 1, 2023

There are many cases where a child element wants to listen to events of its ancestor element and act on them.
In such cases, it is common to use @document or @window.
However, it is often necessary to care whether or not the event was originated by its own ancestor.
How about providing some assurance of that in the action option?
If it looks good I will make a PR.

code (Code that reproduces the function using registerActionOption)

app.registerActionOption('ancestor', ({ event, element }) => {
  return event.target.contains(element);
});

syntax:

data-action="disclousre:open@document->content-loader#load:ancestor:once"

example:

https://codepen.io/Takuya-Nakajima/pen/eYQbVwa?editors=1010

usecase:

This helps to keep controllers healthy that are used as descendant elements of a generic controller.

For example, suppose we have a modal that displays a map. This is very useful when you want to load the map when the modal is opened, and when you want to reset the center location when the map is closed.

<div ... data-controller="modal">
  <div ... data-controller="map"
    data-action="modal:open@document->map#load:ancestor:once modal:close@document->map#resetCenter:ancestor"
  />
</div>

It is easy to imagine more than one modal controller on a page.
In such a case, it is very convenient to use a global listener for only the ancestor of the map.

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

No branches or pull requests

1 participant