Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

zhouzi/react-click-outside

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@zhouzi/react-click-outside

Listen to clicks happening outside a given container. The work here is mostly an update of https://github.com/tj/react-click-outside

Installation

npm install @zhouzi/react-click-outside

Usage

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useClickOutside } from '@zhouzi/react-click-outside';

function Modal() {
    const [isOpen, setOpen] = React.useState(false);
    const { ref } = useClickOutside(() => {
        if (isOpen) {
            setOpen(false);
        }
    });

    return (
        <>
            <button onClick={() => setOpen(true)}>
                Open
            </button>
            {isOpen && <div ref={ref}>Modal is open</div>}
        </>
    );
}

ReactDOM.render(
    <Modal />,
    window.document.getElementById('root')
);

About

Listen to clicks happening outside a given container

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published