useReadingPrefs hook

@interlace/ui — reads and writes the reader's text size (`data-text-size`) and contrast (`data-contrast`) on <html>, persists both, and follows `prefers-contrast` when no choice is stored.

foundationclientlibv1.0.0
Visual examples + interactive variants
View useReadingPrefs hook on Storybook
No embeddable story for this item yet.
→

Install

Two equivalent paths — the URL works in any shadcn-CLI setup; the alias works once you've registered @interlace in your components.json.

Via shadcn URL
npx shadcn@latest add https://ds.interlace.tools/r/use-reading-prefs.json
With the @interlace alias
npx shadcn@latest add @interlace/use-reading-prefs

History

This component is at v1.0.0, first shipped in @interlace/ui 2.0.0. The version is stamped as a banner into the file the install writes, so the copy in your tree says which one you have — compare it with the number above before deciding whether to re-run the install.

@interlace/ui 2.0.01 entry

  • Addedminor

    Reading settings for the reader: a text size control and a More contrast toggle. TextSizeControl is a labelled group of three buttons: smaller, the current size (which resets), and larger. It sets the root font size to 100, 112, 125 or 150% of the browser's own default through data-text-size on <html>, so type, spacing, control heights and containers all grow together and the layout holds. At the ends the buttons stay focusable, so a keyboard user is never dropped to the top of the page. ContrastToggle turns More contrast on or off. Off stores an explicit normal, which also overrides prefers-contrast: more. useReadingPrefs is the hook behind both: it persists the choices, keeps every control in the document and every open tab in step, and follows the OS when nothing is stored. THEME_SCRIPT now restores both choices before first paint, so a returning reader never sees the page reflow. Every word in both controls is a prop.

Import

5 named exports — pull the parts you need.

Public API
import { ContrastPreference, applyReadingPrefs, readStoredContrast, readStoredTextSize, useReadingPrefs } from '@/hooks/use-reading-prefs';

Accessibility

Every story for this component is rendered headlessly and checked with axe-core (wcag2aa, wcag22aa, best-practice, ACT) on every PR. That gate has no continue-on-error, so what ships has zero known violations.

What follows is what static analysis can see. Axe cannot press a key and never sees an overlay open, so the operable-without-a-mouse claim lives in Behavior instead, where the keyboard path is replayed step by step.

Focus + keyboard behaviour
Native element semantics — no interaction layer to get wrong.
Focus ring (WCAG 2.2 SC 2.4.13)
Not focusable — no focus indicator required.
Reduced motion
No animation to gate.
ARIA in the source
None — semantics come from the element or the Base UI primitive rather than hand-written ARIA.

Dependencies

Base UI primitive
Native / no Base UI dependency
Lucide icons
none
NPM dependencies
none
Registry dependencies

Source

The full implementation — hooks/use-reading-prefs.ts once installed.

189 lines · TypeScript
'use client';

import { useCallback, useEffect, useState } from 'react';

// @interlace/use-reading-prefs v1.0.0 — Interlace design system.
// Docs, props and live preview: https://ds.interlace.tools/c/use-reading-prefs
// What changed since: https://ds.interlace.tools/c/use-reading-prefs#history
// Generated banner — keep it, the upgrade diff reads this version.

/**
 * @interlace/ui — useReadingPrefs.
 *
 * The two reading-comfort axes, beside the theme and scheme that `useTheme`
 * owns:
 *
 *   text size → `data-text-size="lg" | "xl" | "2xl"`  (absent = 100%)
 *   contrast  → `data-contrast="more" | "normal"`      (absent = follow the OS)
 *
 * Both are written to `<html>`. Text size has to be: it sets the ROOT font
 * size, and every rem in the system (type, spacing, control heights,

…169 more lines…

Registry JSON

The raw registry record — what the shadcn CLI fetches.