useOutsideClick
useOutsideClick
is a custom hook that detects click events outside of an element.
Import
import { useOutsideClick } from "@yamada-ui/react"
Usage
Editable example
const ref = useRef<HTMLDivElement>(null) const [isOpen, setIsOpen] = useState(false) useOutsideClick({ ref, handler: () => setIsOpen(false), }) return ( <> {isOpen ? ( <Center ref={ref} h="10" px="4" bg="danger" color="white" rounded="md"> Hey, Click anywhere outside of me to close. </Center> ) : ( <Button onClick={() => setIsOpen(true)}>Please Click</Button> )} </> )
Edit this page on GitHub