O3 Framework / useOnClickOutside
useOnClickOutside<
T>(handler,active):RefObject<T>
Defined in: packages/framework/esm-react-utils/src/useOnClickOutside.ts:31
A React hook that detects clicks outside of a referenced element. Useful for implementing dropdown menus, modals, or any component that should close when clicking outside of it.
T extends HTMLElement = HTMLElement
The type of HTML element the ref will be attached to.
(event) => void
A callback function invoked when a click occurs outside the referenced element.
boolean = true
Whether the outside click detection is active. Defaults to true.
Set to false to temporarily disable the detection.
RefObject<T>
A React ref object to attach to the element you want to detect outside clicks for.
import { useOnClickOutside } from '@openmrs/esm-framework';
function Dropdown() {
const [isOpen, setIsOpen] = useState(false);
const ref = useOnClickOutside<HTMLDivElement>(() => setIsOpen(false), isOpen);
return (
<div ref={ref}>
{isOpen && <ul>...</ul>}
</div>
);
}