Get started
Hooks
The library exports the hooks it's built with, plus tested hooks for everyday UI work: open and close state, keyboard shortcuts, saved preferences, the clipboard, timers, and long lists.
Using the hooks
Every hook is exported from the package root, next to the components. They're safe in server rendering and tree-shakable: each one costs only itself, mostly well under 1 KB.
import { useDisclosure, useHotkeys } from "@vjdesigns/vjdesigns-ui";
All hooks
| Hook | What it does |
|---|---|
useDisclosure(initial?) | Open and closed state with stable open, close and toggle handlers, for a Modal, Drawer or Popover. |
useToggle(values?) | Cycles through values: a boolean, or a list like ["list", "grid"]. |
usePrevious(value) | The value from before its last change. |
useDebouncedValue(value, ms) | The value, updated only after it stops changing (search as you type). |
useDebouncedCallback(fn, ms) | A function that runs once after calls stop, with cancel and flush. |
useThrottledCallback(fn, ms) | A function that runs at most once per interval (scroll, resize). |
useInterval(fn, ms | null) / useTimeout(fn, ms | null) | Timers that always call the latest function; null pauses them. |
useLocalStorage(key, default) / useSessionStorage | State saved in storage, synced across components and tabs. It never throws when storage is blocked. |
useClipboard() | copy(text) with a copied flag, and a fallback when the Clipboard API is blocked. |
useHotkeys([[keys, fn]]) / getHotkeyHandler | Page shortcuts. mod is ⌘ or Ctrl. Single keys don't fire while typing. getHotkeyHandler does the same for one element. |
useEventListener(type, fn, target?) | An event listener that always calls the latest handler. |
useClickOutside(fn) | Calls a function on a press or a focus move outside an element. |
useIntersection(options) | Whether an element is in view (lazy loading, entrance effects). |
useElementSize() | An element's size, kept up to date as it resizes. |
useVirtualizer(options) | Renders only the visible items of a long list, with variable heights and scrollToIndex. VirtualList is built on it. |
useMediaQuery(query) / useResponsiveValue(value) | A synced media query, or a responsive value resolved for the current viewport. |
useHydrated() | False during server rendering and true after, to avoid hydration mismatches. |
useReducedMotion() / useColorScheme() | The user's reduced-motion and color-scheme preferences. |
useOnline() / useDocumentVisibility() / useViewportSize() | The connection, tab visibility and viewport size. |
Open and close an overlay
useDisclosure holds open and closed state with stable open, close, toggle and set handlers, ready for a Modal, Drawer or Popover.
import { Button, Modal, useDisclosure } from "@vjdesigns/vjdesigns-ui";
function DeleteGame() {
const [opened, { open, set }] = useDisclosure();
return (
<>
<Button onClick={open}>Delete game</Button>
<Modal open={opened} onOpenChange={set}>
…
</Modal>
</>
);
}
Keyboard shortcuts
useHotkeys adds page-wide shortcuts. mod is ⌘ on a Mac and Ctrl elsewhere. Single keys like / don't fire while someone types in a field, so shortcuts never get in the way of text entry (WCAG 2.1.4). Show each shortcut in the interface too, for example with Kbd.
import { useHotkeys } from "@vjdesigns/vjdesigns-ui";
useHotkeys([
["mod+k", () => setPaletteOpen(true)],
["/", () => searchRef.current?.focus()],
]);
Remember a preference
useLocalStorage is state saved in local storage and synced across components and browser tabs. It never throws when storage is blocked; it falls back to memory. useSessionStorage keeps the value until the tab closes.
import { SegmentedControl, useLocalStorage } from "@vjdesigns/vjdesigns-ui";
const [view, setView] = useLocalStorage<"list" | "grid">("roster-view", "list");
Copy to the clipboard
useClipboard gives you copy(text) and a copied flag for a short "Copied" label, with a fallback when the Clipboard API is blocked. Announce the result to screen readers too, for example in a role="status" element.
import { Button, VisuallyHidden, useClipboard } from "@vjdesigns/vjdesigns-ui";
function CopyLink({ url }: { url: string }) {
const { copy, copied } = useClipboard();
return (
<>
<Button onClick={() => copy(url)}>{copied ? "Copied" : "Copy link"}</Button>
<VisuallyHidden role="status">{copied ? "Link copied" : ""}</VisuallyHidden>
</>
);
}
Building blocks
The hooks the components are built on are exported too, for your own components: useControllableState for props that can be controlled or uncontrolled, useFocusTrap and useScrollLock for dialogs, and useFormValidation for forms (see ErrorSummary). useVirtualizer is what VirtualList uses.