useTheme

Custom hook to switch between the light and dark mode of the used theme.
Requires no custom JS logic to handle dark mode colors
Supports server side rendering


Usage

import { useTheme } from "@uicapsule/components";

In UIC, we don't treat light and dark modes like two different themes; instead, we have a separate colorMode definition.

function Example() {
const { setColorMode, invertColorMode, colorMode } = useTheme();
return (
<View direction="row" align="center" gap={3}>
<View.Item>Mode: {colorMode}</View.Item>
<Button onClick={invertColorMode}>Invert mode</Button>
<Button onClick={() => setColorMode("dark")}>Switch to dark mode</Button>
</View>
);
}

This means there is always an easy way to toggle the modes even if you're nesting multiple themes into each other using ThemeProvider. Try switching the mode in the previous demo and see how it affects the rendering of these cards:

<View gap={3} direction="row">
<Card>I am using the current color mode</Card>
<ThemeProvider colorMode="inverted">
<Card>I am using the inverted color mode</Card>
</ThemeProvider>
</View>

Inverted color mode is a concept that we use in UIC components like Tooltip to avoid introducing additional design tokens while keeping the content accessible.

Color mode is applicable for the whole application, so its default value can be set using UIC provider. By default, the value is set to light mode.

<UIC defaultColorMode="dark" />

To support the color mode value with server-side rendering, pass the className from the theme to the body element.

import '@uicapsule/components/themes/@uicapsule/components/theme.css';
<body data-uic-theme="uicapsule-light">

Arguments and return value

() => ({
colorMode: "dark" | "light",
setColorMode: (mode: "dark" | "light") => {},
invertColorMode: () => {},
});