ThemeProvider
Light, night or system theme for the whole app or one section.
Example
Night theme
Only this box switches, because global is false.
import { useState } from "react";
import { Card, CardDescription, CardTitle, Switch, ThemeProvider } from "@porcelana/ui";
export default function Example() {
const [night, setNight] = useState(true);
return (
<ThemeProvider theme={night ? "night" : "light"} global={false} style={{ padding: 20, borderRadius: 28 }}>
<Card>
<CardTitle>{night ? "Night" : "Light"} theme</CardTitle>
<CardDescription>Only this box switches, because global is false.</CardDescription>
<Switch label="Night theme" checked={night} onCheckedChange={setNight} />
</Card>
</ThemeProvider>
);
}Import
import { ThemeProvider } from "@porcelana/ui";
import "@porcelana/ui/styles.css"; // once, at your app rootProps
Generated from the TypeScript types. Standard HTML attributes are passed through and not listed.
ThemeProvider
Wraps content in the porcelana root with the chosen theme.
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | – | Defines a string value that labels the current element. @see aria-labelledby. |
global | boolean | true | Also set data-theme on <html>, so portalled overlays pick up the theme. Default true. |
id | string | – | Element id. Generated automatically when omitted. |
theme | ThemeName | light | "light", "night", or "system" to follow the OS dark mode. |
Accessibility
- Both themes meet WCAG AA text contrast; focus rings are dark blue in light and yellow in night.
- theme="system" follows the operating system's dark mode.
Mobile
- Sets data-theme on <html> by default, so portalled sheets and menus pick up the theme too.