@dax-ui/theme
v0.8.0
Published
dax-ui MUI theme and UIProvider
Maintainers
Readme
@dax-ui/theme
The dax-ui theme and UIProvider. Every app wraps its tree in UIProvider once;
every other @dax-ui component needs it.
Installation
pnpm add @dax-ui/themeMUI and Emotion are included; the app only needs React 18 or 19. Don't add
@mui/material to the app as well: a second copy wouldn't share the dax-ui theme.
Usage
Wrap the app once, at the root:
import { UIProvider } from '@dax-ui/theme';
export function App({ children }) {
return <UIProvider defaultMode="system">{children}</UIProvider>;
}Next.js App Router
Use @dax-ui/nextjs instead: its UIProvider adds server-side style
collection and the color-mode script, and it re-exports everything else from this package.
Other server rendering
Render ColorSchemeScript first inside <body> so the page doesn't flash the wrong
color mode before React loads:
<body>
<ColorSchemeScript />
<UIProvider>{children}</UIProvider>
</body>Light/dark toggle
import { useColorScheme } from '@dax-ui/theme';
function ModeToggle() {
const { mode, setMode } = useColorScheme();
return (
<button onClick={() => setMode(mode === 'dark' ? 'light' : 'dark')}>
Switch to {mode === 'dark' ? 'light' : 'dark'} mode
</button>
);
}Your app's brand colors
Every app can brand the whole library. Pass colors to createAppTheme; anything you
leave out keeps the dax-ui default (the DAX palette). The subtle/soft tints and the
highlight border follow primary and secondary automatically:
import { UIProvider, createAppTheme } from '@dax-ui/theme';
const appTheme = createAppTheme({
colors: { primary: '#6D28D9', secondary: '#1E1B4B' },
});
<UIProvider theme={appTheme}>{children}</UIProvider>;Create the theme once, outside components (or in a 'use client' module in Next.js).
The full set of colors, each optional:
| Group | Keys |
| --- | --- |
| Brand | primary, secondary |
| background | base, surface, disabled, primarySubtle (primary 5%), primarySoft (primary 10%), secondarySubtle, secondarySoft |
| text | heading, body, supporting, disabled, muted, inverse |
| status | success, warning, info, neutral, error |
| border | primary, secondary (defaults to primary) |
Dark mode has its own defaults and follows the app's primary and status colors; set
darkColors to change anything else in dark mode. Other MUI theme options are deep-merged
on top: createAppTheme({ colors, shape: { borderRadius: 12 } }).
Typography
The Figma type scale is built into the theme as Typography variants (sizes in px):
| Group | Variants |
| --- | --- |
| Headings (600) | h1 24/32, h2 20/28, h3 18/26, h4 16/24, h5 14/20, h6 12/18 |
| Body (400) | bodyLg 16/24, bodyMd 14/20, bodySm 12/18, bodyXs 10/14 |
| Label (500) | labelLg 14/18, labelMd 12/16, labelSm 10/14 |
| Caption (400) | caption 12/16, captionSm 10/14 |
| Button (500) | buttonLg 14/20, button 14/20, buttonSm 12/16 |
| Metric | metricXl 36/44 and metricLg 24/32 (600), metricMd 20/28, metricSm 16/24, metricXs 12/16 (500) |
import { Typography } from '@dax-ui/theme';
<Typography variant="labelMd">Status</Typography>
<Typography variant="bodyMd" sx={{ fontWeight: 'fontWeightMedium' }}>Emphasized</Typography>MUI's body1 and body2 are Body Medium and Body Small, so the page and MUI components use
the scale too. The variants are also on theme.typography, for styled:
...theme.typography.labelMd.
Apps change the font, or any style, with typography:
createAppTheme({ typography: { fontFamily: '"Roboto", sans-serif', h1: { fontSize: '2rem' } } });Styling your own elements
styled and useTheme read the same theme. Prefer theme.vars.palette: it's a CSS variable,
so it follows the color mode and the app's colors without re-rendering:
import { styled, useTheme } from '@dax-ui/theme';
const Panel = styled('section')(({ theme }) => ({
padding: theme.spacing(2),
borderRadius: theme.shape.borderRadius,
color: theme.vars.palette.text.heading,
background: theme.vars.palette.background.primarySubtle,
border: `1px solid ${theme.vars.palette.border.primary}`,
}));
function Example() {
const theme = useTheme();
return <p style={{ color: theme.palette.text.secondary }}>...</p>;
}Every dax-ui component also accepts an sx prop: <Button sx={{ mt: 2 }}>Save</Button>.
Exports
| Export | What it is |
| --- | --- |
| UIProvider | The provider; props below |
| ColorSchemeScript | Prevents a color-mode flash with server rendering; props below |
| useColorScheme | { mode, setMode } for reading and changing the color mode |
| createAppTheme({ colors, darkColors, ...overrides }) | The dax-ui theme in the app's colors, with other MUI options deep-merged on top |
| resolveThemeColors(colors, darkColors) | The final light and dark colors after merging the app's over the defaults |
| styled, useTheme | Style custom elements / read the theme in components |
| Typography | MUI's Typography, with the dax-ui variants (bodyMd, labelMd, metricLg, ...) |
| theme, themeOptions | The default theme and the options it's built from |
| Theme, ThemeOptions | Types |
UIProvider 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. |
| theme | Theme | createAppTheme() | Replaces the default theme, e.g. createAppTheme({ ...overrides }). |
ColorSchemeScript props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| defaultMode | "light" \| "dark" \| "system" | system | Color mode on first visit. Must match UIProvider's defaultMode. |
