Leave Yamada UI a star

Star
Yamada UIYamada UIv1.6.4

useOutsideClick

useOutsideClick is a custom hook that detects click events outside of an element.

Source@yamada-ui/use-outside-click

Import

import { useOutsideClick } from "@yamada-ui/react"
Copied!

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>
    )}
  </>
)
Copied!

Edit this page on GitHub

PrevioususeOSNextusePrevious