# Theming URL: /react/getting-started/styling/theming Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/getting-started/styling/theming.mdx Learn how to understand and use Crispy's theme system. ## Overview Crispy provides a theme system that supports light and dark modes. Themes are controlled through the `data-seed-color-mode` and `data-seed-user-color-scheme` attributes on HTML elements. Learn the roles and differences of these two attributes, and how to set up global theme configuration and override themes for specific areas. ## Understanding Theme Attributes Crispy's theme system uses two `data-*` attributes. ### `data-seed-color-mode` An attribute that sets the application's theme policy. It can have three values. - `system`: Automatically switches between light/dark mode based on user's system settings. - `light-only`: Always fixed to light mode. - `dark-only`: Always fixed to dark mode. ```html ``` ### `data-seed-user-color-scheme` An attribute that represents the actual color scheme of the user's device. It has two values. - `light`: User's system is set to light mode - `dark`: User's system is set to dark mode This value is only meaningful when `data-seed-color-mode="system"` and is automatically updated through the `prefers-color-scheme` media query. ```html ``` ### How It Works Crispy CSS applies appropriate color tokens through the combination of both attributes: ```css /* When light mode is applied */ :root, :root[data-seed-color-mode="system"][data-seed-user-color-scheme="light"], :root[data-seed-color-mode="light-only"], :root [data-seed-color-mode="light-only"] { /* Light mode color tokens */ } /* When dark mode is applied */ :root[data-seed-color-mode="system"][data-seed-user-color-scheme="dark"], :root[data-seed-color-mode="dark-only"], :root [data-seed-color-mode="dark-only"] { /* Dark mode color tokens */ } ``` ## Applying Global Theme Refer to the Installation documentation for how to apply themes to the entire application. - [Vite](/react/getting-started/installation/vite) - [Rsbuild](/react/getting-started/installation/rsbuild) - [Webpack](/react/getting-started/installation/webpack) - [Manual](/react/getting-started/installation/manual) You can check methods using bundler plugins or directly adding attributes and scripts to HTML. ## Overriding Theme for Specific Elements When you want to apply a different theme to a specific area or component, set `data-seed-color-mode` on that element with `light-only`, `dark-only`, or `system` values. ```tsx import { ActionButton } from "seed-design/ui/action-button"; import { actionButtonVariantMap } from "@seed-design/css/recipes/action-button"; import { Text, VStack } from "@seed-design/react"; function Demo() { return ( <> {actionButtonVariantMap.variant.map((variant) => ( {variant} ))} ); } export default function ThemingColorModeOverride() { return (
라이트 모드 다크 모드
); } ``` ## Detecting Current Theme Info from Client Sometimes components need to perform different actions based on the current theme settings. In such cases, you can detect and use the `data-seed-color-mode` and `data-seed-user-color-scheme` attributes. Here's an example of creating a custom hook to detect current theme information: ```tsx "use client"; import { useEffect, useState } from "react"; export type ColorMode = "system" | "light-only" | "dark-only"; export type UserColorScheme = "light" | "dark"; export interface ThemeInfo { colorMode: ColorMode; userColorScheme: UserColorScheme; } function readThemeInfo(): ThemeInfo { if (typeof document === "undefined") { return { colorMode: "system", userColorScheme: "light", }; } const colorMode = document.documentElement.getAttribute( "data-seed-color-mode" ) as ColorMode; const userColorScheme = document.documentElement.getAttribute( "data-seed-user-color-scheme" ); return { colorMode: colorMode || "system", userColorScheme: userColorScheme === "dark" ? "dark" : "light", }; } export function useTheme(): ThemeInfo { const [themeInfo, setThemeInfo] = useState(readThemeInfo); useEffect(() => { if (typeof document === "undefined") { return; } const observer = new MutationObserver(() => { setThemeInfo(readThemeInfo()); }); observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-seed-color-mode", "data-seed-user-color-scheme"], }); return () => observer.disconnect(); }, []); return themeInfo; } ``` The hook can be used as follows: ```tsx "use client"; function MyComponent() { const { colorMode, userColorScheme } = useTheme(); return (

Theme policy: {colorMode}

Current color scheme: {userColorScheme}

{userColorScheme === "dark" ? ( ) : ( )}
); } ``` ### How It Works - Uses `MutationObserver` to detect changes to `data-seed-color-mode` and `data-seed-user-color-scheme` attributes. - `colorMode` represents the application's theme policy (`system`, `light-only`, `dark-only`). - `userColorScheme` represents the actual color scheme currently applied (`light` or `dark`). - State is automatically updated when system settings change or when the theme is switched.