# @devmoods/ui

> All of the lights. React components, hooks and other utils for building great web applications.

```
npm install @devmoods/ui
```

## Building with an AI agent

The package ships a full usage guide for agents:
[`llms.txt`](llms.txt). It has the rules for good results, every
component with a working example, an app shell, data loading, tables,
schema forms and theming. All examples are type-checked against the
library on every build.

Tell your agent to use it. Add this to the `AGENTS.md` or `CLAUDE.md` of
your app:

```md
## UI

Build the UI with `@devmoods/ui`. Before writing UI code, read
`node_modules/@devmoods/ui/llms.txt` and follow its rules and examples.
```

## Quick start

Import the styles once, and wrap the app in `ThemeProvider`:

```tsx
import '@devmoods/ui/global.css'; // reset, page background, text and spacing classes
import '@devmoods/ui/styles.css'; // component styles
import {
  Button,
  Input,
  Stack,
  ThemeProvider,
  ToastProvider,
  createTheme,
  useToast,
} from '@devmoods/ui';

const theme = createTheme({ colors: { primary: '#2563eb' } });

function Signup() {
  const toast = useToast();
  return (
    <Stack as="form" spacing="m" padding="l">
      <h1 className="dmk-text-title1">Join us</h1>
      <Input label="Email" type="email" />
      <Stack horizontal justifyContent="flex-end" spacing="s">
        <Button variant="text">Cancel</Button>
        <Button
          type="submit"
          onClick={() => toast({ title: 'Welcome!', description: 'Done.' })}
        >
          Sign up
        </Button>
      </Stack>
    </Stack>
  );
}

export function App() {
  return (
    <ThemeProvider theme={theme}>
      <ToastProvider>
        <Signup />
      </ToastProvider>
    </ThemeProvider>
  );
}
```

## What is in the box

| Area     | Components and hooks                                                                                                                                                 |
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Layout   | `Stack`, `Spacer`, `Panel`, `Divider`, `ResponsiveSidebarAppLayout`, `ResponsiveMobileAppLayout`                                                                     |
| Actions  | `Button`, `DropdownMenu`, `ShareButton`, `DarkModeSwitch`, `HamburgerButton`                                                                                         |
| Inputs   | `Input`, `NumberInput`, `Counter`, `SearchInput`, `Select`, `Combobox`, `Checkbox`, `CheckboxGroup`, `RadioGroup`, `Switch`, `Slider`, `Rating`, `FileInput`         |
| Dates    | `Calendar`, `DateRangeCalendar`, `DatePicker`, `TimePicker`                                                                                                          |
| Overlays | `Modal`, `Dialog`, `Popover`, `Tooltip`, `ToastProvider` with `useToast`, `ImageLightbox`                                                                            |
| Display  | `Alert`, `Badge`, `NotificationBadge`, `Avatar`, `Image`, `Skeleton`, `ProgressBar`, `CircularProgress`, `Loader`, `PullToRefresh`, `Tabs`, `Accordion`, `ListGroup` |
| Data     | `Table`, `SmartTable`, `useSorting`, `usePaginationControls`, `useTableRowSelection`, `TableFilterPanel`                                                             |
| Other    | `ErrorBoundary`, `ClientOnly`, `VisuallyHidden`, `useQuery`, `useMutation`, `useStorageState`, `useMedia`, `useResponsiveValue`, `useCopyableText`                   |

Subpaths: `@devmoods/ui/forms` and `@devmoods/ui/forms/widgets` for
schema-driven forms, `@devmoods/ui/format` for date and currency helpers,
and `@devmoods/ui/helmet` for document head tags (needs a `HelmetProvider`
at the root).

The components can be rendered from React Server Components (Next.js App
Router). See "Next.js and Server Components" in the [guide](llms.txt) for
what works where, links with `as={Link}`, and page titles.

The Storybook in this repo (`pnpm start`) shows every component in about 30
themes, with the source code of each example and interactive playgrounds.

## Layout

Build layouts with `Stack` instead of custom flexbox CSS. Spacing props take
a token from the theme (`none`, `xxs`, `xs`, `s`, `m`, `l`, `xl`, `xxl`), and
every prop can change per breakpoint with an array for phone, tablet and
desktop:

```tsx
<Stack horizontal={[false, true, true]} spacing={['s', 'm', 'xl']}>
  <Card />
  <Card />
</Stack>
```

`global.css` also has classes for text (`dmk-text-title1`, `dmk-text-body`,
`dmk-text-caption1`, `dmk-text-muted`, `dmk-text-semibold`, …) and spacing
(`dmk-margin-top-m`, `dmk-padding-l`, …).

## Tips for great results

The [guide](llms.txt) has the full list. The most important ones:

- Use theme tokens for every color, space and radius:
  `var(--colors-primary)`, `var(--spacing-m)`, `var(--border-radius-m)`. Use
  `var(--colors-surface)` for backgrounds, so dark themes work.
- Let components do the work: overlays, menus, tabs and tooltips handle focus,
  Escape and ARIA for you, and many panels can be swiped away on touch
  screens.
- Give every field a `label`, and every icon-only button an `aria-label`.
- Try the app in dark mode (`allowDarkMode` on `ThemeProvider`) and at phone
  width before shipping.

## Theming

Import the component styles, create a theme, and place `ThemeProvider` near the
root of the application:

```tsx
import '@devmoods/ui/styles.css';
import { ThemeProvider, createTheme } from '@devmoods/ui';

const theme = createTheme({
  colors: {
    primary: '#2563eb',
    gray: { 50: '#64748b' },
  },
  spacing: { page: '2rem' },
});

export function App() {
  return <ThemeProvider theme={theme}>{/* application */}</ThemeProvider>;
}
```

Theme values are exposed as CSS custom properties using kebab-case paths, such
as `--colors-primary`, `--colors-gray-50`, and `--spacing-page`. Custom token
groups are supported and retained in the type returned by `createTheme`.
`colors.base` controls the browser theme color and branded header. The page and
component backgrounds use `colors.surface`, which defaults to white; set it
explicitly when creating a dark surface theme.

Components can access typed CSS variable references with `useTokens`. Supply
the application theme type when accessing custom tokens:

```tsx
import { useTokens } from '@devmoods/ui';

type AppTheme = typeof theme;

function Example() {
  const tokens = useTokens<AppTheme>();
  return <div style={{ color: tokens.colors.primary }} />;
}
```

`primary`, `secondary`, and `danger` accept hex values or other CSS colors.
Light and dark variants are generated automatically; explicitly supplied
variants such as `primaryLight` take precedence.

Set `allowDarkMode` to let `useDarkMode` switch between the light theme and the
automatically generated dark theme. Pass a `darkTheme` created with `createTheme`
to specify its colors and other tokens explicitly. `respectDefaultDarkMode` uses
the operating system preference when no saved preference exists.

## Benchmarks

Run `pnpm bench:forms` to measure form validation and update performance. See the
[benchmark guide](src/forms/benchmarks/README.md) for workloads, saved baselines,
and comparison commands.

## Inspiration

In alphabetic order.

- [Ariakit](https://ariakit.org/)
- [Blueprint](https://blueprintjs.com/)
- [Chakra UI](https://chakra-ui.com/)
- [Evergreen](https://evergreen.segment.com/)
- [Grommet](https://v2.grommet.io/)
- [Headless UI](https://headlessui.com/)
- [Material UI](https://mui.com/material-ui/getting-started/overview/)
- [Radix](https://www.radix-ui.com/)
- [React Aria](https://react-spectrum.adobe.com/react-aria/)
- [Reakit](https://reakit.io/)
