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

add click away on event #5138

Closed
goldcoders opened this issue Jul 13, 2020 · 4 comments
Closed

add click away on event #5138

goldcoders opened this issue Jul 13, 2020 · 4 comments

Comments

@goldcoders
Copy link

Is there a way you can add this little helper directive on svelte?
on:click|away , i think this is doable... and easy to implement

https://github.com/alpinejs/alpine/blob/master/src/directives/on.js#L7-L26

@antony
Copy link
Member

antony commented Jul 13, 2020

Please use, not delete the issue template in future. You can implement this as an action, please see the API documentation on actions. This will not be implemented as an event modifier for on:click because it isn't an event modifier.

@antony antony closed this as completed Jul 13, 2020
@goldcoders
Copy link
Author

Documentation is very vague on this topic, i think this should be easy to implemenet like alpineJS...
Can you share a snippets a very quick and dirty example that would trigger an event for example clickAway

@antony
Copy link
Member

antony commented Jul 13, 2020

See sveltejs/rfcs#24

@goldcoders
Copy link
Author

For Other that might be looking for solution on this

U need to use bind this
first define an element u will use i.e. dropdownEl then add the event listener for click

let dropdownEl;
document.addEventListener("click", function (e) {
   
    if (dropdownEl != undefined) {
      if (dropdownEl.contains(e.target)) return;
      // Don't do anything if this element isn't currently visible.
      if (dropdownEl.offsetWidth < 1 && dropdownEl.offsetHeight < 1) return;
      dropdown = false;
      console.log("dropdown-close");
    }
  });

on your html Important U Add stopPropagation event modifier on click

<button on:click|stopPropagation={toggleDropdown}>
</button

<!-- YOUR DROPDOWN -->
<div  bind:this={dropdownEl}>
 </div>
``

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

No branches or pull requests

2 participants