Giter Site home page Giter Site logo

ooade / react-click-away-listener Goto Github PK

View Code? Open in Web Editor NEW
170.0 3.0 13.0 1.42 MB

:paw_prints: Tiny React Click Away Listener built with React Hooks

License: MIT License

JavaScript 6.34% TypeScript 93.66%
click-away react react-hooks event-handler click-outside

react-click-away-listener's Introduction

~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

react-click-away-listener's People

Contributors

ahkohd avatar calebdeji avatar dependabot[bot] avatar mrlika avatar ooade avatar rishabh-ink avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-click-away-listener's Issues

Add classname to the returned element

Currently the click away listener doesn't have a class name and adds a naked element. There should be a classname before props are spread to indicate in dev tools that this naked element is actually the click away listener. Since props are spread any classname that is provided will override it. This will just help clarify what this naked element is when no classname prop is provided.

onClickAway should return the fired event

Currently the Mouse/Touch Event that is fired from clicking away is not passed to the function passed for onClickAway.

I feel this should be changed so that you could prevent event bubbling by using event.preventDefault or to do other stuff with the event.

I can submit a PR if you wish.

Let click-away event do not report if we click / focus the children of the element.

Describe the bug

  1. When I click a button, the website will show me a list. And it uses react-click-away-listener.
  2. I want to choose one clickable element by my Tab key.
  3. Ok. Now I choose it - a child of the list.
  4. Oh no. The list get the click-away event and close itself - and its children.

To Reproduce
๐Ÿ‘€

Expected behavior
I hope if I try to focus the children, (even click the children), it will never get the click-away event.

Screenshots
๐Ÿ‘€

Desktop (please complete the following information):

  • OS: Manjaro
  • Browser Firefox
  • Version 99.0.1

Smartphone (please complete the following information):
๐Ÿ‘€

Additional context
I try to use focusEvent="none" to disable. It works now. But not good.

Portal

Do you have any plans for supporting portals?

v1.1.0 does not appear to be passing Event

Hi Again!

v1.1.0 does not appear to be passing the event to the onClickAway function. I copied the source to my application and it seems to be fine. I'm wondering if perhaps there was an issue with the build process. I do have the updates types!

Would you be able to take a look into this?

Warning when used within a form

image

Building a custom select dropdown with ClickAwayListener wrapping the component.

Right now the structure looks something like this

<form onSubmit={e => e.preventDefault()}>
       <ClickAwayListener>
               <button onClick={e => e.preventDefault()}> test </button>
       </ClickAwayListener>
       <button type='submit'>submit</button>
</form>

Memory Leak

The react-click-away-listener fires the following memory leak FYI.

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
Header@webpack-internal:///./components/modules/Header.tsx:24:74
div
Default@webpack-internal:///./components/layouts/Default/Default.tsx:22:18

overrideMethod react_devtools_backend.js:2557
    React 5
    handleClickAway Header.tsx:18
    handleEvents ClickAwayListener.tsx:92
    (Async: EventListener.handleEvent)
    ClickAwayListener ClickAwayListener.tsx:95

It's implemented as below:

<ClickAwayListener onClickAway={handleClickAway}>
  <button type="button" 
    onClick={() => {
      setOpenProfile(!openProfile);
    }}>
      <span>See Profile</span>
    </button>
</ClickAwayListener>

v2.2.0 introduced a breaking change by dropping React 17 support

Describe the bug

In #59 a change has been introduced that changes peerDependencies of react and react-dom to only allow versions ^18.0.0. This introduced a breaking change by dropping support for React 17.

I suggest setting peerDependencies to ^17.0.0 || ^18.0.0 to fix a breaking change.

And actually, while we're at it, it looks like your code should run perfectly in ^16.8.0 as well. So perhaps even set it to ^16.8.0 || ^17.0.0 || ^18.0.0?

OnClickAway trigger when I switch browser tab

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. switch tab
  2. back to current tab
  3. onClickAway triggerd

Expected behavior
onClickAway not trigger

Chrome

an error appear in console when you add touchEvent

Describe the bug
A clear and concise description of what the bug is.
the bug is some error appear in console when you add touchEvent

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

image

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.
image

Desktop (please complete the following information):

  • OS: [e.g. iOS] windows
  • Browser [e.g. chrome, safari] chrome
  • Version [e.g. 22] 100

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Click inside of div on react-icons icon calls OnClickAway

image
When I click on these items, the popup is closed, but it shouldn't be. For example if I click on the embed code copy button then the menu should remain open.

<ClickAwayListener onClickAway={() => mapDetailsOpen && setMapDetailsOpen(false)}>
  <div></div>
</ClickAwayListener/>

Using the latest pre-react 17 version that I could find, 1.4.4.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.