useDisclosure
An open/closed boolean with open, close and toggle handlers, for driving Collapse, a panel or any boolean UI from a trigger of your own.
useDisclosure tracks an open/closed boolean and hands back open, close and toggle. Reach for it when a trigger of your own drives something that takes an open prop, such as Collapse, without pulling in a whole disclosure component.
import { useDisclosure } from '@42-central/ui-react';
import { Collapse } from '@42-central/ui-react/collapse';
const [opened, { toggle }] = useDisclosure();
<button aria-expanded={opened} aria-controls="details" onClick={toggle}>
Details
</button>
<Collapse id="details" open={opened}>…</Collapse>It is uncontrolled only: there's no value prop, the state is yours and lives in this hook. To give a component of your own the kit's open / defaultOpen / onOpenChange props, build it on useUncontrolled instead.
Usage
The trigger sets aria-expanded and aria-controls itself, since Collapse only takes the boolean. onOpen and onClose fire on a real transition only: pressing open() while it's already open does nothing.
Revealed by a button you own. Collapse only takes the boolean.
onOpen fired 0×, onClose fired 0×
Code
const [opened, { open, close, toggle }] = useDisclosure(false, {
onOpen: () => setOpened((n) => n + 1),
onClose: () => setClosed((n) => n + 1),
});
<Button aria-expanded={opened} aria-controls="details" onClick={toggle}>
Details
</Button>
<Button variant="light" onClick={open}>open()</Button>
<Button variant="light" onClick={close}>close()</Button>
<Collapse id="details" open={opened}>…</Collapse>The callbacks run in the handler, never inside a state updater, so Strict Mode's double-invoked updaters don't fire them twice.
API
Prop
Type
Returns [opened, { open, close, toggle }].
Prop
Type
The handlers keep their identity across renders as long as onOpen and onClose do. Pass stable callbacks (useCallback, or functions defined outside the component) when a handler sits in an effect's dependency list.
usePagination
The page math behind Pagination, without the bar. The current page, the page count, the items to draw with their ellipses, the item range on the page, and the moves between pages.
useLocalStorage
Persists a piece of state to localStorage, kept in sync across every component using the same key — within the tab and across tabs.