ModeToggle
One icon button that switches between the light and the dark scheme - the sun in light, the moon in dark, no menu.
Mode is the colour-scheme button of an application header: the sun while the light scheme is
shown, the moon while the dark one is, and a click switches to the other. There is no menu and no
"system" choice to pick.
// apps/docs/examples/ui/mode-toggle.tsx'use client';
import {ModeToggle} from '@workspace/active-ui/components/composed/mode-toggle';import {TooltipProvider} from '@workspace/active-ui/components/primitives/tooltip';
/** * The colour-scheme button of an application header, under this site's * theme provider: a click switches the whole site between light and dark, * like the button in the header above. */export default function ModeToggleExample() { return ( <TooltipProvider> <ModeToggle label="Colour scheme" schemeLabels={{light: 'Light', dark: 'Dark'}} showTooltip /> </TooltipProvider> );}What the composition adds#
One click, one switch: it stores the opposite of the scheme in effect through
useof the theme provider.Theme() A first visit follows the device: until the reader clicks, the provider keeps the mode the host gave it. A host that starts in
system(no stored choice yet) shows the device's scheme; the first click stores light or dark.The right icon before hydration: the sun and the moon follow the
darkclass on the document, so the icon is right as soon as the page paints.A name with the state:
labelfollowed by the scheme shown ("Colour scheme: Dark");showshowsTooltip labelas a tooltip. Put aTooltipabove it to set the delay.Provider
Usage#
import {ModeToggle} from '@workspace/active-ui/components/composed/mode-toggle';
<ModeToggle label="Colour scheme" schemeLabels={{light: 'Light', dark: 'Dark'}} showTooltip/>;What your application provides#
A
Themeabove it, with the persistence of your choice (a cookie your server reads, so a reload shows the stored scheme without a flash).Provider The texts, translated:
labeland the two names inscheme.Labels Whether a reader can return to "follow the device": this button never chooses
system. Offer it elsewhere if your product wants it.
Accessibility and keyboard#
A <button>, reached with Tab and pressed with Enter or Space. Its accessible name says the
scheme in effect, so a screen reader hears the change after a press.
API#
Import from @workspace/
Props
- Type
string
The name of the control, supplied translated: "Colour scheme". Shown as the tooltip and, followed by the scheme in effect, as the accessible name ("Colour scheme: Dark").
- Type
{ light: string; dark: string; }
The names of the two schemes, supplied translated, said after label in the accessible name.
- Type
string
Classes for the button.
- Type
boolean- Default
false
Also shows label as a tooltip. Off by default, for the reason Full gives: the delay lives on the caller's Tooltip..
Takes only the props listed.
Types
The props of Mode, each described in its prop list on this page.
interface ModeToggleProps {
label: string;
schemeLabels: {light: string; dark: string};
showTooltip?: boolean;
className?: string;
}