Skip to content

Does petitle-vue support @click.away? #9

Answered by Aferz
pthethanh asked this question in Q&A
Discussion options

You must be logged in to vote

That'd be a nice addition because either Vue or Petite Vue don't allow custom event modifiers.

In the meantime, for future reference, here is a basic directive that does the trick.

<div v-scope="{ count: 0 }">
    {{ count }}
    <button v-click-away="count++">inc</button>
</div>

<script src="https://unpkg.com/petite-vue"></script>

<script>
  function clickAwayDirective({ el, get }) {
    const listener = (event) => {
      if (!el.contains(event.target)) {
        get();
      }
    };

    document.addEventListener("click", listener);

    return () => {
      document.removeEventListener(listener);
    };
  }

  PetiteVue.createApp().directive("click-away", clickAwayDirective).mount();

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@pthethanh
Comment options

@niutech
Comment options

Answer selected by pthethanh
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants