• Stars
    star
    546
  • Rank 81,396 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

๐Ÿ˜Ž ๐Ÿ–ฑ React hook to listen for clicks outside of the component(s).

REACT COOL ONCLICKOUTSIDE

This is a React hook to trigger callback when user clicks outside of the target component(s) area. It's a useful logic for UI interaction design (IxD) like dismiss a dropdown menu, modal or tooltip etc. You can check the features section to learn more.

โšก๏ธ Live demo: https://react-cool-onclickoutside.netlify.app

โค๏ธ it? โญ๏ธ it on GitHub or Tweet about it.

build status coverage status npm version npm downloads npm downloads gzip size All Contributors PRs welcome Twitter URL

Features

Requirement

To use react-cool-onclickoutside, you must use [email protected] or greater which includes hooks.

Installation

This package is distributed via npm.

$ yarn add react-cool-onclickoutside
# or
$ npm install --save react-cool-onclickoutside

Usage

Common use case.

import { useState } from "react";
import useOnclickOutside from "react-cool-onclickoutside";

const Dropdown = () => {
  const [openMenu, setOpenMenu] = useState(false);
  const ref = useOnclickOutside(() => {
    setOpenMenu(false);
  });

  const handleClickBtn = () => {
    setOpenMenu(!openMenu);
  };

  return (
    <div>
      <button onClick={handleClickBtn}>Button</button>
      {openMenu && <div ref={ref}>Menu</div>}
    </div>
  );
};

Edit useOnclickOutside demo

Support multiple refs. Callback only be triggered when user clicks outside of the registered components.

import { useState } from "react";
import useOnclickOutside from "react-cool-onclickoutside";

const App = () => {
  const [showTips, setShowTips] = useState(true);
  const ref = useOnclickOutside(() => {
    setShowTips(false);
  });

  return (
    <div>
      {showTips && (
        <>
          <div ref={ref}>Tooltip 1</div>
          <div ref={ref}>Tooltip 2</div>
        </>
      )}
    </div>
  );
};

Ignore Elements by CSS Class Name

You can tell react-cool-onclickoutside to ignore certain elements during the event loop by the ignore-onclickoutside CSS class name. If you want explicit control over the class name, use the ignoreClass option.

import { useState } from "react";
import useOnclickOutside from "react-cool-onclickoutside";

// Use the default CSS class name
const App = () => {
  const ref = useOnclickOutside(() => {
    // Do something...
  });

  return (
    <div>
      <div ref={ref}>I'm a ๐Ÿ•</div>
      <div>Click me will trigger the event's callback</div>
      <div className="ignore-onclickoutside">
        Click me won't trigger the event's callback
      </div>
    </div>
  );
};

// Use your own CSS class name
const App = () => {
  const ref = useOnclickOutside(
    () => {
      // Do something...
    },
    {
      ignoreClass: "my-ignore-class", // Or ["class-1", "class-2"]
    }
  );

  return (
    <div>
      <div ref={ref}>I'm a ๐Ÿ•</div>
      <div>Click me will trigger the event's callback</div>
      <div className="my-ignore-class">
        Click me won't trigger the event's callback
      </div>
    </div>
  );
};

Disabling the Event Listener

In case you want to disable the event listener for performance reasons or fulfill some use cases. We provide the disabled option for you. Once you set it to true, the callback wonโ€™t be triggered.

import { useState } from "react";
import useOnclickOutside from "react-cool-onclickoutside";

const App = () => {
  const [disabled, setDisabled] = useState(false);
  const ref = useOnclickOutside(
    () => {
      // Do something...
    },
    { disabled }
  );

  const handleBtnClick = () => {
    setDisabled(true);
  };

  return (
    <div>
      <button onClick={handleBtnClick}>
        Stop listening for outside clicks
      </button>
      <div ref={ref}>I'm a ๐ŸŽ</div>
    </div>
  );
};

Use Your Own ref

In case of you had a ref already or you want to share a ref for other purposes. You can pass in the ref instead of using the one provided by this hook.

const ref = useRef();

useOnclickOutside(
  () => {
    // Do something...
  },
  { refs: [ref] }
);

Detecting Iframe Clicks

Clicks on an <iframe> element won't trigger document.documentElement listeners, because it's literally different page with different security domain. However, when clicking on an iframe moves focus to its content's window that triggers the main window.blur event. react-cool-onclickoutside in conjunction the blur event with document.activeElement to detect if an iframe is clicked, and execute the provided callback.

The above-mentioned workaround has its caveats:

  • Clicks on an iframe will only trigger the provided callback once. Subsequent clicks on iframe will not trigger the callback until focus has been moved back to main window.
  • Move focus to iframe via keyboard navigation also triggers the provided callback.

For our convenience, this feature is enabled by default. You can optionally disable it by setting the detectIFrame to false if you find it conflicting with your use-case.

API

const ref = useOnclickOutside(callback: (event: Event) => void, options?: object);

You must register the ref and pass the callback to use this hook. Moreover you can access the event object via the callback's parameter, default will be MouseEvent or TouchEvent.

const callback = (event) => {
  console.log("Event: ", event);
};

The options object contains the following keys.

Key Type Default Description
refs Array For some reasons, you can pass in your own ref(s) instead of using the built-in.
disabled boolean false Enable/disable the event listener.
eventTypes Array ['mousedown', 'touchstart'] Which events to listen for.
excludeScrollbar boolean false Whether or not to listen (ignore) to browser scrollbar clicks.
ignoreClass string | string[] ignore-onclickoutside To ignore certain elements during the event loop by the CSS class name that you defined.
detectIFrame boolean true To disable the feature of detecting iframe clicks.

Articles / Blog Posts

๐Ÿ’ก If you have written any blog post or article about react-cool-onclickoutside, please open a PR to add it here.

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Welly

๐Ÿ’ป ๐Ÿ“– ๐Ÿšง

DmitryScaletta

๐Ÿ›

vardani

๐Ÿ›

Alexey Cherepanov

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

react-cool-inview

๐Ÿ˜Ž ๐Ÿ–ฅ๏ธ React hook to monitor an element enters or leaves the viewport (or another element).
TypeScript
1,462
star
2

react-cool-starter

๐Ÿ˜Ž ๐Ÿฃ A starter boilerplate for a universal web app with the best development experience and a focus on performance and best practices.
TypeScript
1,306
star
3

use-web-animations

๐Ÿ˜Ž ๐Ÿฟ React hook for highly-performant and manipulable animations using Web Animations API.
TypeScript
1,255
star
4

use-places-autocomplete

๐Ÿ˜Ž ๐Ÿ“ React hook for Google Maps Places Autocomplete.
TypeScript
1,206
star
5

react-cool-virtual

๐Ÿ˜Ž โ™ป๏ธ A tiny React hook for rendering large datasets like a breeze.
TypeScript
1,195
star
6

react-cool-dimensions

๐Ÿ˜Ž ๐Ÿ“ React hook to measure an element's size and handle responsive components.
TypeScript
941
star
7

react-cool-img

๐Ÿ˜Ž ๐Ÿž A React <Img /> component let you handle image UX and performance as a Pro!
TypeScript
779
star
8

react-cool-portal

๐Ÿ˜Ž ๐Ÿ’ React hook for Portals, which renders modals, dropdowns, tooltips etc. to <body> or else.
TypeScript
747
star
9

react-cool-form

๐Ÿ˜Ž ๐Ÿ“‹ React hooks for forms state and validation, less code more performant.
TypeScript
248
star
10

eslint-config-welly

๐Ÿ˜Ž โš™๏ธ ESLint configuration for React projects that I do. Feel free to use this!
JavaScript
21
star
11

use-transition-state

๐Ÿ˜Ž ๐Ÿ‘Ÿ useTransition + useState = useTransitionState
JavaScript
12
star
12

wellyshen

๐Ÿค“ Hello I'm Welly.
3
star
13

welly-exchange

๐Ÿ’ฐ A currency exchange app made by React.
TypeScript
2
star
14

about-me

๐Ÿค“ About me.
HTML
2
star
15

wellyshen.github.io

LeadFit Privacy Policy
HTML
1
star