[ Utilities ]
Hooks & Utilities
The same building blocks the components use internally, exported for your own components: open/close state, outside-click detection, controlled/uncontrolled state and a class-name helper.
Import#
import { useDisclosure, useClickOutside, useControllableState, cn } from 'reusify-ui';When to use#
- useDisclosure: any open/close UI such as Modal, Drawer or a custom popover.
- useClickOutside: closing custom menus and popovers.
- useControllableState: building your own components that support both value and defaultValue.
- cn: joining class names conditionally.
Examples#
useDisclosure#
Returns { isOpen, open, close, toggle, setIsOpen }. All functions are stable, so they are safe in dependency arrays.
falseimport { useDisclosure, Modal, Button } from 'reusify-ui';
function Example() {
const { isOpen, open, close, toggle } = useDisclosure(false);
return (
<>
<Button onClick={open}>Open</Button>
<Modal open={isOpen} onClose={close} title="Hello">…</Modal>
</>
);
}useClickOutside#
useClickOutside(ref, handler, enabled). The third argument lets you only listen while something is open.
useControllableState#
useControllableState(value, defaultValue, onChange) returns [state, setState]. When value is undefined the hook keeps its own state; otherwise the parent is in control. onChange is called either way.
import { useControllableState } from 'reusify-ui';
function Toggle({ value, defaultValue = false, onChange, label }) {
const [on, setOn] = useControllableState(value, defaultValue, onChange);
return <button onClick={() => setOn(!on)}>{label}: {on ? 'On' : 'Off'}</button>;
}
<Toggle label="Uncontrolled" />
<Toggle label="Controlled" value={on} onChange={setOn} />cn#
Joins class names and skips falsy values. Handy when passing className to Reusify components.
cn('a', false && 'b', 'c') → "a c"import { cn } from 'reusify-ui';
cn('btn', isActive && 'btn--active', null, undefined); // "btn btn--active"
<Button className={cn('w-full', compact && 'h-8')}>Save</Button>API reference#
useDisclosure(initialOpen?)
| Prop | Type | Default | Description |
|---|---|---|---|
initialOpen | boolean | false | Initial state. |
returns | { isOpen, open, close, toggle, setIsOpen } | - | State and stable setters. |
initialOpenfalsebooleanInitial state.
returns-{ isOpen, open, close, toggle, setIsOpen }State and stable setters.
useClickOutside(ref, handler, enabled?)
| Prop | Type | Default | Description |
|---|---|---|---|
ref* | RefObject<HTMLElement> | - | Element to watch. |
handler* | () => void | - | Called on a pointer press outside the element. |
enabled | boolean | true | Only listen while true. |
ref*-RefObject<HTMLElement>Element to watch.
handler*-() => voidCalled on a pointer press outside the element.
enabledtruebooleanOnly listen while true.
useControllableState(value, defaultValue, onChange?)
| Prop | Type | Default | Description |
|---|---|---|---|
value | T | undefined | - | Controlled value. undefined means uncontrolled. |
defaultValue* | T | - | Initial value when uncontrolled. |
onChange | (value: T) => void | - | Called whenever the value changes. |
returns | [value, setValue] | - | Current value and setter. |
value-T | undefinedControlled value. undefined means uncontrolled.
defaultValue*-TInitial value when uncontrolled.
onChange-(value: T) => voidCalled whenever the value changes.
returns-[value, setValue]Current value and setter.
cn(...classes)
| Prop | Type | Default | Description |
|---|---|---|---|
classes | Array<string | false | null | undefined> | - | Class names; falsy values are ignored. |
classes-Array<string | false | null | undefined>Class names; falsy values are ignored.
Accessibility#
- useClickOutside listens to pointerdown, so it works for mouse, touch and pen.