Document Click Listener
Easily catch outside of element clicks with enable/disable functionality.
This javascript module especially useful if you have multiple UI elements that needs to track where user clicked in the terms of inside/outside of itself.
Install
npm install document-click-listener
Import
This package exports multiple builds. Developer has the responsibility to choose the one that best fits to her/his needs.
- Minified (default)
This is basically the source code itself but minified. There are no compilation and polyfills inside. Use it if you already have a compiler (like babel) and polyfills in your project. (which is mostly is the case.)
// just 827 bytesconst documentClickListener =
- Polyfilled and minified
The source code compiled with babel (with the configuration that can be found inside babel.config.js
file) and minified. This build contains also polyfills which increases the size of the package dramatically. Polyfills added by the configuration that can be found inside the .browserlistrc
file.
const documentClickListener =
- Bundle for browsers
The source code bundled with browserify
to generate a UMD
bundle. This bundle can be imported by html script tag. No compilation and polyfills. The variable attached to the browser's window
object is DocumentClickListener
.
- Polyfilled bundle for browsers
The source code compiled with babel and bundled with browserify
to generate a UMD
bundle. This bundle can be imported by html script tag. The variable attached to the browser's window
object is DocumentClickListener
.
Use
Call listenOutsideClicks
method where UI element needs to take action on outside click.
const documentClickListener = // example case: hiding dropdown menu if user clicks outside of itconst listenerID = 'dropdownMenu' { // tell module which dom element(s) this is about return document } { // triggered if user clicks outside of the .dropdown-menu-wrapper const menu = document menustyledisplay = 'none' // also don't listen anymore since we hide the dropdown documentClickListener} documentClickListener
Babel Polyfills Report
This module uses the following polyfills in its polyfilled builds.
es.array.filter
es.array.map
es.object.values