2.3.0 • Published 6 months ago

react-click-away-listener v2.3.0

Weekly downloads
4,713
License
MIT
Repository
github
Last release
6 months ago

Installation

npm install react-click-away-listener

or

yarn add react-click-away-listener
  • It's quite small in size! Just Bundlephobia minified, or Bundlephobia minified & gzipp’d.
  • It's built with TypeScript.
  • It works with React Portals (v2.0.0 onwards).
  • It supports mouse, touch and focus events.

Usage

import ClickAwayListener from 'react-click-away-listener';

const App = () => {
	const handleClickAway = () => {
		console.log('Maybe close the popup');
	};

	return (
		<div id="app">
			<ClickAwayListener onClickAway={handleClickAway}>
				<div>
					{' '}
					Triggers whenever a click event is registered outside this div element{' '}
				</div>
			</ClickAwayListener>
		</div>
	);
};

Caveats

v2.0.0 has breaking changes which uses the React.Children.only API.

Thus, the following caveats apply for the children of the <ClickAwayListener> component:

  1. You may pass only one child to the <ClickAwayListener> component.
  2. You may not pass plain text nodes to the <ClickAwayListener> component.

Violating the above caveats will result in the following error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `ClickAwayListener`.

If you have multiple child components to pass, you can simply wrap them around a React Fragment like so:

// Assume the `handleClickAway` function is defined.
<ClickAwayListener onClickAway={handleClickAway}>
	<>
		<p>First paragraph</p>
		<button>Example Button</button>
		<p>Second paragraph</p>
	</>
</ClickAwayListener>

Or if you only have text nodes, you can also wrap them in a React Fragment like so:

// Assume the `handleClickAway` function is defined.
<ClickAwayListener onClickAway={handleClickAway}>
	<>First text node Second text node</>
</ClickAwayListener>

Props

NameTypeDefaultDescription
onClickAway(event) => voidFires when a user clicks outside the click away component
mouseEvent'click' |'mousedown' |'mouseup''click'The mouse event type that gets fired on ClickAway
touchEvent'touchstart' |'touchend''touchend'The touch event type that gets fired on ClickAway
focusEvent'focusin' |'focusout''focusin'The focus event type that gets fired on ClickAway

Examples

LICENSE

MIT

2.2.4

7 months ago

2.3.0

6 months ago

2.2.3

2 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.5

4 years ago

2.0.5-beta.1

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.1.0-beta.1

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

2.0.0

4 years ago

1.6.1-beta.2

4 years ago

1.6.1-beta.1

4 years ago

1.5.3

4 years ago

2.0.0-alpha.6

4 years ago

2.0.0-alpha.4

4 years ago

2.0.0-alpha.5

4 years ago

2.0.0-alpha.3

4 years ago

2.0.0-alpha.1

4 years ago

2.0.0-alpha.2

4 years ago

1.5.2

4 years ago

2.0.0-alpha.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.5.0-beta.1

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago