Skip to content

A very easy hook solution for React to handle outside clicks of elements or components

License

Notifications You must be signed in to change notification settings

uykalkan/react-use-outside-click

Repository files navigation

npm version npm version npm licence

Installation

yarn add react-use-outside-click

It's Easy

import { useOutsideClick } from 'react-use-outside-click'

useOutsideClick("my-excluded-css-class", () => {
  alert("OUTSIDE CLICKED")
})

Example

import React, {useState} from 'react';
import { useOutsideClick } from "react-use-outside-click";

const ExampleComponent = () => {
    const [popoverIsVisible, setPopoverIsVisible] = useState(false);

    useOutsideClick("my-popover", () => {
        setPopoverIsVisible(false);
    })

    const togglePopover = () => {
        setPopoverIsVisible(!popoverIsVisible);
    }

    return (
        <div>
            <button type="button" onClick={togglePopover}>Toggle Popover</button>
            <div className="my-popover">
                Popover is Here
            </div>
        </div>
    );
};

export default ExampleComponent;

About

A very easy hook solution for React to handle outside clicks of elements or components

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published