• Stars
    star
    174
  • Rank 219,104 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿพ Tiny React Click Away Listener built with 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

More Repositories

1

NextSimpleStarter

๐Ÿณ Simple and Accessible PWA boilerplate with Nextjs 12 and MUI
JavaScript
938
star
2

vuex-examples

๐Ÿป Simple Examples on using Vuex to build Real World Apps
JavaScript
258
star
3

next-apollo-auth

Authentication Boilerplate with Next.js and Apollo GraphQL
JavaScript
203
star
4

react-rough

๐Ÿ‡ React Components for Rough.js
TypeScript
177
star
5

awesome-apollo-graphql

A curated list of amazingly awesome things regarding Apollo GraphQL ecosystem ๐ŸŒŸ
149
star
6

use-fetch-hook

A custom hook to fetch and cache data
JavaScript
113
star
7

pimg

๐Ÿ“ท Mini Image Lazy Loader for P(R)eact and Vue
JavaScript
99
star
8

PreactSimpleStarter

PWA Simple Starter with Preact, Preact-mdl and Webpack2 ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ
JavaScript
64
star
9

Advanced-JavaScript

Documentation based on John Resig's website on Advanced JavaScript
JavaScript
55
star
10

array-explorer-cli

โšก A CLI package to help figure out what JavaScript array method would be best to use at any given time
JavaScript
47
star
11

yooda

๐Ÿง˜โ€โ™‚๏ธ600b validation library with support for custom handlers and messages.
TypeScript
33
star
12

object-explorer-cli

โšก A CLI package to help figure out what JavaScript object method would be best to use at any given time
JavaScript
17
star
13

state-machines-workshop

Unstack Conf workshop on State machines and State charts
JavaScript
15
star
14

next-react-toolbox

React Toolbox integration with Next.js ๐Ÿ“ฆ
JavaScript
12
star
15

ApolloSimpleStarter

๐ŸŒŸ Bare Minimum implementation of Apollo Server & Client with MERN
JavaScript
11
star
16

ng-states

~400 Bytes Nigeria State list with its respective Local Government Areas
JavaScript
10
star
17

react-library-starter

A simple boilerplate to help get started in creating production ready react libraries
JavaScript
9
star
18

cors

Bypass CORS
JavaScript
8
star
19

global-context-example

Using Contexts to properly manage global states
JavaScript
8
star
20

PreactStatic

Working out SSR with Preact
JavaScript
8
star
21

always-treeshake-mui

An example on how to properly tree-shake mui with next.js
JavaScript
8
star
22

css2object

Converts css to object/json (fun thang!) ๐Ÿค”
TypeScript
8
star
23

fetch-gifs

Fetch GIFS for your nodeJS app in just few steps
JavaScript
7
star
24

30-days-of-code

Daily log of the things I read and learn during week days, and maybe weekends
7
star
25

micro-typeof

โœ๐ŸฟGet the exact type of a value
TypeScript
7
star
26

vortal

Voice Portal System for information management
JavaScript
6
star
27

ReactAndPreact

Repo to Compare React to React with Preact Compat in terms of speed and performance ๐Ÿ”ฅ ๐Ÿ”ฅ
JavaScript
6
star
28

ademola.adegbuyi.me

Ademola's Portfolio
JavaScript
6
star
29

react-hooks-playground

React Hooks Playground! Yay!!!
JavaScript
5
star
30

component-library

A component library for my article on Smashing Magazine.
JavaScript
5
star
31

react-gif-finder

Example Implemention of Fetch Gif With React
JavaScript
4
star
32

portfolio-2018

My Portfolio
JavaScript
4
star
33

web-workers-demo

Examples using web workers
HTML
3
star
34

react-101

Intro to React class
JavaScript
3
star
35

portfolio-2019-cli

A CLI package for my porfolio ๐Ÿ˜…
JavaScript
3
star
36

react-integrations

A React library to help integrate Vanilla JS libraries to your app
JavaScript
3
star
37

minifycss

Tiny ~200B minimal css minifier.
JavaScript
3
star
38

headerParser

Header Parser Microservice FCC Challenge
JavaScript
3
star
39

todo-mvc

A todomvc.com example built on RelayJS with @RisingStack/graffiti-mongoose Schema
JavaScript
3
star
40

node-mvrd

๐Ÿ˜… Motor Vehicle Registration Data???
JavaScript
3
star
41

react-mdl-css

Use a custom style from https://getmdl.io/customize in your react-mdl app ๐Ÿ”ฅ
CSS
2
star
42

fonsole

Format your browser's console - Add some craziness to it ๐Ÿ˜
JavaScript
2
star
43

booktrade

Booktrade BackEnd Project
JavaScript
2
star
44

http2-example

Certificate gen and serving files....
JavaScript
2
star
45

vuex-examples-server

The server used for the gallery vuex example
JavaScript
2
star
46

mnml-calc-cli

A simple CLI calculator app with minimist.
JavaScript
2
star
47

simple-calc-cli

A simple CLI calculator app with commander
JavaScript
2
star
48

Timestamp-Microservice

An Api Project For FreeCodeCamp
JavaScript
2
star
49

fcc-roguelike

FreeCodeCamp RougeLike React Challenge
JavaScript
1
star
50

emotion-force-filter-unset

Chrome extension to forcefully remove filters from emotion documentation
JavaScript
1
star
51

Minidium

Medium Home Clone built with Preact and Webpack 3 for my IO extended session
JavaScript
1
star
52

Auth

BoilerPlate Project For Server Side Authentication
JavaScript
1
star
53

votepush

This is a voting app (NodeJs, Angular, Mongodb)
JavaScript
1
star
54

nightLife

A nightLife Coordination app for FreeCodeCamp BaseJumps
CSS
1
star
55

stockstock

A Realtime Stock Market App (MEAN)
JavaScript
1
star
56

Pinterest-Clone

Real time Pinterest Clone - Built with Meteor and ReactJS
JavaScript
1
star