Skip to content

Next.js

Works with Next.js 15 and 16, App Router (primary) and Pages Router, on Turbopack or webpack. The library ships compiled ESM, so no transpilePackages is needed.

The /next export surface

@windmorph/react/next exports getThemeFromCookies (with CookieReader and GetThemeFromCookiesOptions), NextRouterProvider (with NextRouterProviderProps), useNavigationGuard and useGuardedNavigate (with NavigationAttempt and NavigationGuardOptions), and re-exports themeAttributes and the ThemeState type. Note: ThemeScript is not here — it comes from @windmorph/react.

With a server you can read the theme cookie and render the right data-* attributes on <html> before any JS runs, so there is no theme flash. The async root layout reads getThemeFromCookies(await cookies()), spreads themeAttributes(theme), loads Roboto Flex, and wraps children in NextRouterProvider and ThemeProvider.

Pass every value to ThemeProvider as its default* prop, density included. A value the server leaves out is applied right after hydration instead, and React renders any Suspense boundary still streaming (a loading.tsx page) on the client, dropping its server HTML. In development, ThemeProvider warns when that happens and names the values.

LocaleProvider takes the language the page is written in. The server has no browser to ask, so without it a date or time field is rendered in en-US and changes to the browser's language after hydration. With it the server and the browser write dates, times and numbers the same way, in the page's language. On a site with several languages, pass the route's.

app/layout.tsx
// app/layout.tsx (App Router — cookie, no-flash path)
import { LocaleProvider, ThemeProvider } from '@windmorph/react';
import { NextRouterProvider, getThemeFromCookies, themeAttributes } from '@windmorph/react/next';
import { Roboto_Flex } from 'next/font/google';
import { cookies } from 'next/headers';

const robotoFlex = Roboto_Flex({
  subsets: ['latin'],
  axes: ['wdth', 'opsz', 'GRAD'],
  variable: '--md-ref-typeface-brand',
  display: 'swap',
});

export default async function RootLayout({ children }) {
  const theme = getThemeFromCookies(await cookies());
  return (
    <html lang="en" {...themeAttributes(theme)} className={robotoFlex.variable}>
      <body style={{ '--md-ref-typeface-plain': 'var(--md-ref-typeface-brand)' }}>
        <NextRouterProvider>
          <ThemeProvider
            defaultTheme={theme.theme}
            defaultMode={theme.mode}
            defaultContrast={theme.contrast}
            defaultMotion={theme.motion}
            defaultDensity={theme.density}
          >
            <LocaleProvider locale="en-US">{children}</LocaleProvider>
          </ThemeProvider>
        </NextRouterProvider>
      </body>
    </html>
  );
}

Pages Router (script, no flash)

Render ThemeScript from @windmorph/react inside <Head> with suppressHydrationWarning on <Html>, then wrap the app in ThemeProvider.

pages/_document.tsx
// pages/_document.tsx (Pages Router — ThemeScript, no-flash path)
import { ThemeScript } from '@windmorph/react';
import { Head, Html, Main, NextScript } from 'next/document';

export default function Document() {
  return (
    <Html lang="en" suppressHydrationWarning>
      <Head>
        <ThemeScript />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}
pages/_app.tsx
// pages/_app.tsx
import { ThemeProvider } from '@windmorph/react';

export default function App({ Component, pageProps }) {
  return (
    <ThemeProvider>
      <Component {...pageProps} />
    </ThemeProvider>
  );
}

Server & client boundaries

  • Every export of @windmorph/react, /primitives and /next can be imported into a Server Component (checked 2026-10-13: a test page imports them all into one and renders it under next start and next dev). Interactive component files ship with "use client", so a Server Component renders them as client components.
  • Every function, class and constant runs on the server: cn, tv, createTheme, the theme state helpers (getThemeFromCookies, themeAttributes, getThemeScriptSource, …), the tokens, getM3Spring, every *Styles recipe, SnackbarHostState and the shape library. Hooks (use*) run only in client components.
  • Pass only serialisable props from a Server Component: text, numbers, plain objects and elements such as icons. Event handlers, render functions and class instances (a SnackbarHostState, @internationalized/date values) can't cross to the client, so render a component that needs one (such as SnackbarHost) from a client component. Contexts such as ButtonContext render as providers from a Server Component.
  • No window or document access at module load; overlays portal only on the client.
  • Build the items of a Menu, Select, Autocomplete, Tabs or interactive List (MenuItem, MenuGroup, SelectItem, Tab, ListItem, …) in a client component, including a menu passed to SplitButton. React Aria reads each item's element type, and items created in a Server Component arrive as client references it can't read. In development the library throws an error saying so. A static List (no onAction, selection or nested items) takes its ListItem and ListSection elements from a Server Component.
  • Client links are one line: NextRouterProvider wraps React Aria’s RouterProvider with the Next router’s push. Give it basePath and trailingSlash when next.config sets them, so a link’s address is the one Next’s own Link writes.

Leaving with unsaved changes

useNavigationGuard asks before a page with unsaved changes is left. While when is true, library links inside NextRouterProvider call onAttempt instead of navigating: show your own confirm dialog and call proceed() if the person leaves. Reloading or closing the tab shows the browser's own prompt. For navigation in code, use useGuardedNavigate() instead of router.push. Plain next/link elements and the browser's Back and Forward buttons aren't covered: the App Router gives no way to stop them.

unsaved-changes.tsx
'use client';
import { Button, Dialog, DialogActions, DialogTitle } from '@windmorph/react';
import { useNavigationGuard } from '@windmorph/react/next';
import { useState } from 'react';

export function UnsavedChanges({ dirty }: { dirty: boolean }) {
  const [leaving, setLeaving] = useState<(() => void) | null>(null);
  useNavigationGuard({ when: dirty, onAttempt: (proceed) => setLeaving(() => proceed) });
  return (
    <Dialog open={leaving !== null} onOpenChange={(open) => !open && setLeaving(null)} role="alertdialog">
      <DialogTitle>Discard your changes?</DialogTitle>
      <DialogActions>
        <Button variant="text" onPress={() => setLeaving(null)}>Keep editing</Button>
        <Button variant="text" onPress={() => leaving?.()}>Discard</Button>
      </DialogActions>
    </Dialog>
  );
}

Static export (this site)

This documentation site uses output: 'export', so there is no server to read cookies. It therefore uses the script path (the ThemeScript source from getThemeScriptSource, in the root layout’s <head>) rather than the cookie path the Next.js playground app uses. Pick the cookie path when you have a server, and the script path for static or exported sites.