useDisclosure
Open and closed state with open, close and toggle functions, and callbacks when it changes.
Source@monoline/react 0.4.0Markdown for AI toolsstable
- No DOM output. Pair with aria-expanded and aria-controls on the trigger.
- Callbacks run only when the state actually changes, not on mount.
Import
import '@monoline/css';
import {useDisclosure} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add use-disclosure
Usage
import {Button, useDisclosure} from '@monoline/react';
function DisclosureDemo() {
const {isOpen, toggle} = useDisclosure();
return (
<div>
<Button variant="line" aria-expanded={isOpen} onClick={toggle}>{isOpen ? 'Hide details' : 'Show details'}</Button>
{isOpen && <p>Details appear here.</p>}
</div>
);
}
<DisclosureDemo />
Examples
Props
UseDisclosureOptions
| Prop | Type | Default | Description |
|---|---|---|---|
initialOpen | boolean | false | Open on first render. Default `false`. |
onOpen | (() => void) | – | Called when the state changes from closed to open. Not called on mount. |
onClose | (() => void) | – | Called when the state changes from open to closed. Not called on mount. |
UseDisclosureReturn
| Prop | Type | Default | Description |
|---|---|---|---|
isOpen required | boolean | – | |
open required | () => void | – | |
close required | () => void | – | |
toggle required | () => void | – |
Behaviour
- No DOM output. Pair with aria-expanded and aria-controls on the trigger.
- Callbacks run only when the state actually changes, not on mount.