Git Product home page Git Product logo

svelte-click-outside's Introduction

svelte-click-outside

Purpose

This is a small helper component that lets you listen for clicks outside of an element. For example, if you want to close a popup when the user clicks outside of it.

Installation

npm install --save svelte-click-outside

Basic Usage

Wrap the element in the <ClickOutside> component and listen for the clickoutside event:

  <script>
    function onClickOutside() {
      console.log('Clicked outside!');
    }
  </script>

  <ClickOutside on:clickoutside={onClickOutside}>
    <div>Click Outside Me</div>
  </ClickOutside>

Exclusions

By default, clicking on any element outside of the wrapped element will cause the event to trigger. You can specify excluded elements that will not trigger the event. For example, a button that triggers a popup must be excluded. Otherwise, it will immediately close the popup when it is opened.

The ClickOutside component has an exclude prop that expects an array of DOM nodes. Clicks on those nodes or their children will be ignored.

Example: Show/hide panel

  <script>
    import ClickOutside from 'svelte-click-outside';

    let triggerEl;
    let panelVisible = false;

    function togglePanel() {
      panelVisible = !panelVisible;
    }

    function hidePanel() {
      panelVisible = false;
    }
  </script>

  <button bind:this={triggerEl} on:click={togglePanel}>Click Me</button>

  <ClickOutside on:clickoutside={hidePanel} exclude={[triggerEl]}>
    <div hidden={!panelVisible}>I'm a panel!</div>
  </ClickOutside>

svelte-click-outside's People

Contributors

joeattardi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

svelte-click-outside's Issues

<ClickOutside> is not a valid SSR component

I'm getting

<ClickOutside> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

Using

Sapper: 0.27.9
Svelte: 3.12.1

Component disappears when its content is changed

I have a simple component that changes its content when

element is clicked. When I click on that element (and change the content of the component) the whole element disappears, because it's treated as an outside element.

`
{#if addNew === 'project'}
<ClickOutside
on:clickoutside={() => (addNew = null)}
exclude={[triggerEl]}>

      <div class="box">
        {#if newBox === 'one'}
          <p on:click={() => (newBox = 'two')}>One</p>
        {:else if newBox === 'two'}
          <p>Two</p>
        {/if}
      </div>
{/if}`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.