|Kit

Hooks

What the kit's hooks are for, the contracts every one of them keeps, and a catalogue grouped by what you'd reach for them to do.

Components cover the common case: a Popover opens, a DataTable sorts, a FileUpload takes a drop. Hooks expose the state and behaviour underneath, so you can build what a prop can't express: your own trigger, your own layout, your own upload transport.

That split is deliberate. When a component has behaviour worth reusing, the kit exposes it as state plus a hook, not as a growing list of props that each switch one behaviour on. Collapse takes a single open boolean, and useDisclosure is how you drive it from a trigger of your own:

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>

Schedule works the same way. It has no navigation prop, because what a click means is yours to decide; view, date and selected are controlled pairs, and useSchedule holds them for you and hands back goTo handlers.

Contracts every hook keeps

SSR-safe first render

A server can't know the visitor's viewport, stored preferences or clock. So every hook that reads the browser returns a fixed value on the server and on the first client render, and reads the real one right after hydration. The server HTML and the hydrating render always agree, so there's never a hydration mismatch.

const isNarrow = useMediaQuery('(width < 640px)');
// Server HTML and first client render: false.
// Right after hydration: the real match. On a phone, this re-renders once.

That's why a value can flip on mount: useMediaQuery, useContainerQuery, useReducedMotion and useApplePlatform start at false, useNow starts at a placeholder you can test with isNowPending, useLocalStorage starts at its defaultValue, useObjectUrl returns undefined for a Blob until its effect has made the URL, and useInView is undefined until its first observation. To hold back output of your own until after hydration, use useHydrated.

Visuals belong in CSS

A flip is harmless for behaviour (whether to autoplay, which way to scroll) but visible when it decides what gets painted. For anything visual, prefer the CSS equivalent, which is right from the first frame: a responsive Tailwind variant, motion-reduce:, or dark: instead of useTheme().resolvedTheme.

Same naming as the components

The kit's components all take value / defaultValue / onChange, or open / defaultOpen / onOpenChange for something that opens. Both pairs are built on useUncontrolled, so a component of your own built on it is controlled and uncontrolled the same way a kit one is:

function Panel({ open, defaultOpen, onOpenChange }) {
  const [isOpen, setOpen] = useUncontrolled({
    value: open,
    defaultValue: defaultOpen,
    finalValue: false,
    onChange: onOpenChange,
  });
  // …
}

useDisclosure is the lighter tool for state you own outright: an uncontrolled boolean with open / close / toggle, whose value you pass to a component's open.

Headless: the I/O stays yours

A hook that touches the outside world runs the state machine and leaves the network to you. useFileQueue decides when each file uploads, tracks progress, cancel and retry, but never makes a request: you pass the upload function.

const queue = useFileQueue({
  upload: (file, { signal, onProgress }) => send(file, { signal, onProgress }),
});

The same goes for data: DataTable's sorting, filters and pagination are plain controlled state, so a useState or your query library owns them.

Concrete values

useLocale and useTimeZone never return "auto" or undefined. They resolve the component's own prop, then the nearest IntlProvider, then the default, and hand back a tag and a zone you can pass straight to Intl:

const locale = useLocale(props.locale); // "fr-FR", never "auto"
const timeZone = useTimeZone(props.timeZone); // "Europe/Paris", never "auto"

"auto" follows the browser, so it flips on mount like any other browser read: the server and the first client render report "en-US" and "UTC".

Client-only, imported from the root

Hooks only run in Client Components, so the file that calls one starts with "use client". The general-purpose hooks are exported from the package root. A hook that belongs to one component lives on that component's subpath, next to it:

'use client';

import { useMediaQuery } from '@42-central/ui-react';
import { useSchedule } from '@42-central/ui-react/schedule';

Catalogue

Environment

Intl

State

DOM & refs

Interaction

Files

Integration

Dev

Component hooks

These belong to one component and are documented on its page.

A few compound components also expose a context hook, which reads the component's state from a part of your own rendered inside it: useCameraContext, usePhotoCaptureContext, useImageCropperContext and useAppShell.

Changelog

v0.30.0
  • New usePagination hook and paginationRange helper: the page math without the bar

    #478
  • New useScrollSpy hook tracks the heading being read and every section in view

    #485
v0.20.0
  • useFileQueue({ upload, concurrency, batch, autoStart, retryable, onSettled }): a network-free upload queue — you supply the transport; it handles progress, cancel, retry, retryFailed, reset and batches, and FileQueueSummary makes one polite announcement per run

    #367
  • useHotkey(keys, handler, options) binds a chord like "mod+k" (⌘ on Apple, Ctrl elsewhere) or an array, and decides when it fires: inputs, scope + hover, layer (yields under an open overlay), repeat, event: "keyup" and when

    #396
  • <Kbd hotkey="mod+k" /> draws a useHotkey string for the visitor's platform as one key cap per key with icons and spoken names; formatHotkey() and hotkeyKeys() return the same keys as text

    #401
  • useReducedMotion() follows the reduced-motion setting live, false on the server

    #342
  • OpenIndexProvider, useOpenIndex and useOpenIndexTrigger(index, reveal?) let any element open a viewer at an index, the way Gallery tiles and Carousel slides open Lightbox

    #349
  • useObjectUrl(blob) returns an object URL for a Blob or File, revoked when it changes or unmounts, and only for allowlisted types

    #355
  • useApplePlatform, useMergedRefs and useNow are public (with isApplePlatform, mergeRefs, isNowPending), and the new useHydrated() is false on the server and during hydration

    #394
  • useDebouncedValue(value, delay) and useDebouncedCallback(fn, delay): SSR-safe trailing debounces that drop a pending call on unmount

    #397
  • useDocumentVisibility() returns "visible" or "hidden" live ("visible" on the server)

    #398
  • useInView(ref, { root, margin, threshold, once }) wraps IntersectionObserver, undefined until observed so a reveal never hides server content

    #399
  • useElementSize(ref, { box, enabled }) gives an element's live width and height through ResizeObserver, undefined until measured

    #402

On this page