Dark mode
How to use dark mode in your application
Casper UI supports dark mode, as it extends the built in support from Chakra UI.
By default the theme is set to light mode. In RMS apps it should follow the navigation, other apps toggle it with the useColorMode-hook.
The default Casper UI color theme includes dark mode colors. But when using custom theme colors
(type of the colors-prop provided to <ThemeProvider>) they must include a dark mode version per color to work as expected.
RMS apps
Section titled “RMS apps”In RMS, the shared navigation keeps the color mode and persists it. Follow it with getTheme and subscribeTheme from @caspeco/navigation-core (3.22.0 or later) and the Theme Provider’s colorMode-prop:
import { ThemeProvider } from "@caspeco/casper-ui-library";import { getTheme, subscribeTheme } from "@caspeco/navigation-core";import { useSyncExternalStore } from "react";
function App() { const colorMode = useSyncExternalStore(subscribeTheme, getTheme); return ( <ThemeProvider colorMode={colorMode}> {/* Your app consuming Casper UI components goes here */} </ThemeProvider> );}The Theme Provider then follows the prop: useColorMode returns it, and setColorMode/toggleColorMode have no effect, and nothing is duplicated or overridden in localStorage.
Apps outside RMS can pass a color mode they keep themselves the same way.
Read and Toggle dark and light mode using the useColorMode-hook
Section titled “Read and Toggle dark and light mode using the useColorMode-hook”Import the useColorMode-hook from the theme to control the color mode in your application.
- To toggle between dark and light mode, call the
toggleColorMode-function returned from the hook. - To get the current color mode, use the
colorMode-property returned from the hook.
Example usage:
import { useColorMode, FormControl, FormLabel, Switch } from "@caspeco/casper-ui-library";
function ToggleDarkModeSwitch() { const { colorMode, setColorMode } = useColorMode(); return ( <FormControl display="flex"> <FormLabel>Use Dark Mode</FormLabel> <Switch isChecked={colorMode === "dark"} onCheckedChange={({ checked }) => setColorMode(checked ? "dark" : "light")} /> </FormControl> );}