Integration guide

Next.js

Set up a theme boundary, load the compiled CSS, and extract your own Trousers styles.

Install the packages you use

Precursor packages target React 19. In a Next.js App Router project, install the theme and the components you want.

sh
npm install @precursor/theme @precursor/stack @precursor/heading @precursor/text @precursor/button @precursor/spinner

Set up a client theme boundary

Keep the root layout as a Server Component. Put the provider in a small Client Component and pass the rest of the application through children.

tsx
// app/providers.tsx
'use client';

import { Suspense, type ReactNode } from 'react';
import { ThemeProvider } from '@precursor/theme';

export function Providers({ children }: { children: ReactNode }) {
    return (
        <Suspense fallback={<p>Loading theme…</p>}>
            <ThemeProvider selection={{ colorMode: 'auto' }}>
                {children}
            </ThemeProvider>
        </Suspense>
    );
}
tsx
// app/layout.tsx
import type { ReactNode } from 'react';
import { Providers } from './providers';
import '@precursor/theme/styles.css';
import '@precursor/stack/styles.css';
import '@precursor/heading/styles.css';
import '@precursor/text/styles.css';
import '@precursor/button/styles.css';
import '@precursor/spinner/styles.css';

export default function RootLayout({ children }: { children: ReactNode }) {
    return (
        <html lang="en">
            <body>
                <Providers>{children}</Providers>
            </body>
        </html>
    );
}

Import the stylesheet for each component that enters your server-rendered tree, including styled dependencies such as Button’s Spinner. Some client bundlers pick up the package’s browser CSS automatically. Explicit imports in the layout keep the CSS available to HTML that Next.js renders on the server.

Compose the page

Put event handlers, React state, and components that use client hooks inside a Client Component. The page can render it from the server and pass serializable props.

tsx
// app/intro.tsx
'use client';

import Button from '@precursor/button';
import Heading from '@precursor/heading';
import Stack from '@precursor/stack';
import Text from '@precursor/text';

export function Intro() {
    return (
        <Stack gap="s">
            <Heading as="h1">Design foundations</Heading>
            <Text>Start with a theme and a few primitives.</Text>
            <Button primary onClick={() => window.print()}>
                Print this page
            </Button>
        </Stack>
    );
}
tsx
// app/page.tsx
import { Intro } from './intro';

export default function Page() {
    return <Intro />;
}

Extract your own Trousers styles

Precursor ships compiled styles. For application-owned Trousers styles, use the Trousers esbuild plugin in a separate step before Next.js runs. Keep the styles in a dedicated module and let Next.js consume the generated JavaScript and CSS.

sh
npm install trousers
npm install -D esbuild @trousers/esbuild
js
// app/styles/callout.styles.js
import { css } from 'trousers';

export const callout = css('Callout', {
    backgroundColor: 'var(--color-surface-subtle)',
    padding: 'var(--spacing-inset-m)',
    borderRadius: 'var(--shape-radius-m)',
});
js
// scripts/extract-styles.mjs
import { build } from 'esbuild';
import trousers from '@trousers/esbuild';

await build({
    entryPoints: ['app/styles/callout.styles.js'],
    bundle: true,
    packages: 'external',
    format: 'esm',
    outdir: 'app/generated',
    outExtension: { '.js': '.mjs' },
    plugins: [trousers()],
});

Run node scripts/extract-styles.mjs before both next dev and next build. Rerun it after editing the source style module, or use esbuild’s watch API during development. Ignore app/generated/ in Git.

tsx
// app/callout.tsx
import type { ReactNode } from 'react';
import { classnames } from 'trousers';
import { callout } from './generated/callout.styles.mjs';
import './generated/callout.styles.css';

export function Callout({ children }: { children: ReactNode }) {
    return <aside className={classnames(callout)}>{children}</aside>;
}

Use static Trousers style definitions and named modifiers. CSS custom properties connect them to the active Precursor theme.

Further reading