Skip to content

3stacks/when-clicked-outside

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

JavaScript micro-library: pass in an element and a callback and this will trigger when you click anywhere other than the element

Resources

Stars

Watchers

Forks

Packages

No packages published