@dax-ui/nextjs
v0.8.0
Published
dax-ui setup for Next.js App Router: UIProvider with server-rendered styles
Maintainers
Readme
@dax-ui/nextjs
dax-ui setup for Next.js App Router (Next 13 to 16). Next.js apps use this package's
UIProvider instead of the one in @dax-ui/theme. On top of the theme, it:
- collects component styles into
<head>during server rendering, instead of a separate<style>tag beside every component; - applies the saved light/dark mode before the page paints, so it never flashes the wrong mode.
Everything MUI/Emotion needs for this is included; the app only needs Next.js and React.
Installation
pnpm add @dax-ui/nextjsThen add the components you use, e.g. pnpm add @dax-ui/button @dax-ui/text-field.
Usage
Render UIProvider once, directly inside <body> of the root layout:
// app/layout.tsx
import { UIProvider } from '@dax-ui/nextjs';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" suppressHydrationWarning>
<body>
<UIProvider>{children}</UIProvider>
</body>
</html>
);
}suppressHydrationWarning on <html> is required: the color-mode script sets a class on
it before React loads.
Components work in server and client components alike:
// app/page.tsx (a server component)
import { Button } from '@dax-ui/button';
export default function Page() {
return <Button>Get started</Button>;
}Custom theme
A theme object can't be passed from a server component (it contains functions), so build it in a small client file:
// app/providers.tsx
'use client';
import { UIProvider, createAppTheme } from '@dax-ui/nextjs';
const appTheme = createAppTheme({ shape: { borderRadius: 12 } });
export function Providers({ children }: { children: React.ReactNode }) {
return <UIProvider theme={appTheme}>{children}</UIProvider>;
}and render <Providers> in app/layout.tsx instead of <UIProvider>.
With Tailwind CSS or plain CSS
Set enableCssLayer so the app's own styles override MUI's without !important:
<UIProvider enableCssLayer>{children}</UIProvider>Light/dark toggle (in a client component)
'use client';
import { useColorScheme } from '@dax-ui/nextjs';
export function ModeToggle() {
const { mode, setMode } = useColorScheme();
return <button onClick={() => setMode(mode === 'dark' ? 'light' : 'dark')}>Toggle mode</button>;
}Exports
| Export | What it is |
| --- | --- |
| UIProvider | The Next.js App Router provider; props below |
| useColorScheme, createAppTheme, styled, useTheme, Typography, theme, themeOptions, Theme, ThemeOptions | Re-exported from @dax-ui/theme, so Next.js apps don't need to install it separately |
Pages Router isn't supported yet.
Props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children (required) | ReactNode | | The app. Every @dax-ui component must render inside the provider. |
| defaultMode | "light" \| "dark" \| "system" | system | Color mode on first visit. system follows the OS setting. After that, the user's choice made through useColorScheme().setMode() is remembered. |
| disableCssBaseline | boolean | false | Skips MUI's CSS reset (CssBaseline), for apps that already ship their own. |
| enableCssLayer | boolean | false | Wraps MUI's generated styles in @layer mui, so the app's own CSS or Tailwind utilities override them without !important. |
| theme | Theme | theme | Replaces the default theme, e.g. createAppTheme({ ...overrides }). |
