Docs
useClickAway
Detect clicks outside of specific component with useClickAway.
Loading...
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-click-away
Usage
"use client"
import * as React from "react"
import { useClickAway } from "@/hooks/use-click-away"
import { closeIcon } from "./icons"
export function Component() {
const [isOpen, setIsOpen] = React.useState(false)
const ref = useClickAway(() => {
setIsOpen(false)
})
const handleOpenModal = () => {
if (isOpen === false) {
setIsOpen(true)
}
}
return (
<>
<section>
<h1>useClickAway</h1>
<button className="link" onClick={handleOpenModal}>
Open Modal
</button>
</section>
{isOpen && (
<dialog ref={ref}>
<button onClick={() => setIsOpen(false)}>{closeIcon}</button>
<h2>Modal</h2>
<p>
Click outside the modal to close (or use the button) whatever you
prefer.
</p>
</dialog>
)}
</>
)
}
API Reference
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
callback | Function | The callback function that is provided as an argument to useClickAway . This function is invoked whenever a click event is detected outside of the referenced element. The event object from the click is passed to this callback function. | - | No |
Return Values
Name | Type | Description |
---|---|---|
ref | React ref | This is a ref object returned by the hook. It should be attached to a React element to monitor click events. The ref provides a way to access the properties of the element it is attached to. |