Skip to the page
Supply UI

Preferences

Shows preferences on data-* attributes, so CSS styles by them from the first paint: global ones on <html>, the rest on its own element, which has display: contents. On the server it renders one inline script as its first child, which puts every saved value on its attribute as the page loads, before hydration. Content that reads a preference, through usePreference, catches up after hydration.

Preview

  • Harbour Room
  • Quay Room
  • Garden Room

Pick one and reload the page: it stays

Use

The code of the preview above
import { useState } from 'react';
import { Grid, List, ListItem, PatternAvatar, Row, Segmented, Stack } from '@/components/ui';

function PreferencesDemo() {
  const [view] = useState(() =>
    createPreference({ values: ['list', 'grid'], defaultValue: 'list', attribute: 'data-view', storage: createLocalStorage('rooms-view') }),
  );
  const { value } = usePreference(view);
  const rooms = ['Harbour Room', 'Quay Room', 'Garden Room'];
  return (
    <Preferences preferences={[view]}>
      <Stack gap={4}>
        <Segmented
          aria-label="View"
          value={value}
          onValueChange={(next) => view.set(next as 'list' | 'grid')}
          items={[
            { value: 'list', label: 'List' },
            { value: 'grid', label: 'Grid' },
          ]}
        />
        {value === 'grid' ? (
          <Row key="grid" gap={4} className="ui-appear">
            {rooms.map((room) => (
              <Stack key={room} gap={2}>
                <PatternAvatar name={room} size="lg" />
                <span className="ui-small">{room}</span>
              </Stack>
            ))}
          </Row>
        ) : (
          <List key="list" className="ui-appear">
            {rooms.map((room) => (
              <ListItem key={room}>{room}</ListItem>
            ))}
          </List>
        )}
        <p className="ui-small ui-muted">Pick one and reload the page: it stays</p>
      </Stack>
    </Preferences>
  );
}

This page as text, for an AI

Options

No options of its own.

It also takes the 281 attributes of the element or UI SDK part it draws: className, aria-label, events and the rest.

Helpers: states, do and don’t

A choice the person makes once and keeps in their browser: list or grid view, an open sidebar. Not for settings that belong to the account or the site.

Hooks and functions the components are made of, passed on so a project uses the same ones: keyboard shortcuts, options loaded while typing, search matching, saved preferences, screen-reader announcements, smooth sizes, the theme.

Do

  • Use useShortcut for app-wide keys, and show the key with formatShortcut so it reads right on each computer.
  • Use announce() to tell a screen reader what changed when nothing visible takes focus ("3 results").

Don't

  • Don't turn off the smooth sizes Toaster starts; boxes would jump when their content changes.