@lukeboyle/when-clicked-outside
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

Add a click listener to fire a callback for everywhere on the window except your chosen element.

Installation

  • run npm install @lukeboyle/when-clicked-outside

Basic Usage

// ES6
import whenClickedOutside from '@lukeboyle/when-clicked-outside';
// CommonJs
const whenClickedOutside = require('@lukeboyle/when-clicked-outside');

whenClickedOutside('#element', whenClickedOutsideCallback, {});

function whenClickedOutsideCallback(event) {
    console.log('hello');
}

Advanced Usage

// ES6
import whenClickedOutside from '@lukeboyle/when-clicked-outside';
// CommonJs
const whenClickedOutside = require('@lukeboyle/when-clicked-outside');

const listener = whenClickedOutside('#element', whenClickedOutsideCallback, {});

function whenClickedOutsideCallback(event) {
    console.log('hello');
    listener.destroy();
}

API

whenClickedOutside

Arguments

Element

Type: querySelector String OR HTMLElement

e.g. 'body', '.class-name', '#id', document.getElementById('id')

Callback

Type: Function

Config

Type: Object

To set the standard addEventListener, add an 'options' element to the config object

Returns

Returns an object with a reference to your element, and a destroy function to remove the listener.

Changelog

2.0.5

  • fix: add missing dependency

2.0.4

  • fix: remove reference to window.event to address a bug in firefox

2.0.3

  • fix: add explicit reference to the MouseEvent in click verification function

2.0.2

  • docs: update the readme with commonJs require

2.0.1

  • fix: build issue when publishing

2.0.0

  • breaking change: Export whenClickedOutside by default

1.0.2

  • refactor: tidy verifyElementExists function

1.0.1

  • fix: add error handling for incorrect types
  • fix: add error handling for non-existent elements

Readme

Keywords

none

Package Sidebar

Install

npm i @lukeboyle/when-clicked-outside

Weekly Downloads

25

Version

2.1.1

License

MIT

Unpacked Size

16.6 kB

Total Files

16

Last publish

Collaborators

  • lukeboyle