All Projects → ooade → react-click-away-listener

ooade / react-click-away-listener

Licence: MIT license
🐾 Tiny React Click Away Listener built with React Hooks

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-click-away-listener

focus-outside
📦 一个很棒的 clickOutside 库,它解决了 iframe 无法触发 clickOutside 的问题,并且它支持分组绑定处理。A good clickOutside library, which solves the problem that iframe cannot trigger clickOutside, and it supports grouping binding processing.
Stars: ✭ 74 (-43.51%)
Mutual labels:  click-outside, click-away
react-hook-geolocation
A React hook to access data from the Geolocation API
Stars: ✭ 31 (-76.34%)
Mutual labels:  react-hooks
sharyn
🌹 Sharyn – A collection of JavaScript / TypeScript packages that make your life easier and reduce your boilerplate code
Stars: ✭ 30 (-77.1%)
Mutual labels:  react-hooks
use-fetch-hook
A custom hook to fetch and cache data
Stars: ✭ 95 (-27.48%)
Mutual labels:  react-hooks
MovieCards
React App that uses TMDb API to display movie data. Try it out! ->
Stars: ✭ 38 (-70.99%)
Mutual labels:  react-hooks
acharep
🏠 App to find fraternities near your university
Stars: ✭ 23 (-82.44%)
Mutual labels:  react-hooks
google-place-autocomplete
🏆 Best practice with Google Place Autocomplete API on React
Stars: ✭ 68 (-48.09%)
Mutual labels:  react-hooks
useReduction
useReducer without boilerplate
Stars: ✭ 36 (-72.52%)
Mutual labels:  react-hooks
react-hooks-gatsby
Learn how to use React Hooks, and how they work with Gatsby. Watch the livestream:
Stars: ✭ 18 (-86.26%)
Mutual labels:  react-hooks
pojo-observer
A minimalist object observer with React hooks support. Allows you to separate concerns between presentation and interaction logic
Stars: ✭ 91 (-30.53%)
Mutual labels:  react-hooks
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (-61.83%)
Mutual labels:  react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (-50.38%)
Mutual labels:  react-hooks
use-local-storage
A flexible React Hook for using Local Storage.
Stars: ✭ 57 (-56.49%)
Mutual labels:  react-hooks
react-2048
A React implementation of 2048 game built with typescript and styled-components
Stars: ✭ 66 (-49.62%)
Mutual labels:  react-hooks
use-socket.io-client
https://www.npmjs.com/package/use-socket.io-client
Stars: ✭ 47 (-64.12%)
Mutual labels:  react-hooks
react-antd-admin
基于vite2.x + react17.x + typescript4.x + antd4.x + react-router6.x + mobx6.x编写的一款简单高效的前后端分离的权限管理系统
Stars: ✭ 140 (+6.87%)
Mutual labels:  react-hooks
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-90.08%)
Mutual labels:  react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-90.08%)
Mutual labels:  react-hooks
use-detect-print
A react hook to detect when a page is being printed
Stars: ✭ 55 (-58.02%)
Mutual labels:  react-hooks
7-react-admin-ts
用 ts + react-hooks 实现的管理后台
Stars: ✭ 23 (-82.44%)
Mutual labels:  react-hooks

~700B React Click Away Listener

npm Coverage Status Test downloads/month npm bundle size pullrequest firsttimersonly

Installation

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

Name Type Default Description
onClickAway (event) => void Fires 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].