Initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# useDisclosure
|
||||
|
||||
Manages a boolean open/closed state — useful for modals, drawers, dropdowns, and any show/hide UI pattern.
|
||||
|
||||
## Signature
|
||||
|
||||
```ts
|
||||
function useDisclosure(initial?: boolean): UseDisclosureControl;
|
||||
```
|
||||
|
||||
| Parameter | Type | Default | Description |
|
||||
| --------- | --------- | ------- | ------------------------- |
|
||||
| `initial` | `boolean` | `false` | Initial open/closed state |
|
||||
|
||||
### Returns
|
||||
|
||||
```ts
|
||||
interface UseDisclosureControl {
|
||||
isOpen: boolean;
|
||||
open: () => void;
|
||||
close: () => void;
|
||||
toggle: () => void;
|
||||
}
|
||||
```
|
||||
|
||||
| Property | Type | Description |
|
||||
| -------- | ------------ | ------------------------- |
|
||||
| `isOpen` | `boolean` | Current open/closed state |
|
||||
| `open` | `() => void` | Sets `isOpen` to `true` |
|
||||
| `close` | `() => void` | Sets `isOpen` to `false` |
|
||||
| `toggle` | `() => void` | Flips the current state |
|
||||
|
||||
## Usage
|
||||
|
||||
```tsx
|
||||
function ConfirmDialog() {
|
||||
const { isOpen, open, close } = useDisclosure();
|
||||
|
||||
return (
|
||||
<>
|
||||
<button onClick={open}>Delete account</button>
|
||||
<Dialog open={isOpen} onClose={close}>
|
||||
<p>Are you sure?</p>
|
||||
<button onClick={close}>Cancel</button>
|
||||
</Dialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- `open`, `close`, and `toggle` are stable references (`useCallback`) and safe to pass as props without causing unnecessary re-renders.
|
||||
- Client-only (`'use client'`).
|
||||
Reference in New Issue
Block a user