React

Theming

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.
<!-- Follow system settings -->
<html data-seed-color-mode="system" />

<!-- Fixed to light mode -->
<html data-seed-color-mode="light-only" />

<!-- Fixed to dark mode -->
<html data-seed-color-mode="dark-only" />

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.

<!-- Set to follow system settings, and user is currently using light mode -->
<html data-seed-color-mode="system" data-seed-user-color-scheme="light"></html>

How It Works

Crispy CSS applies appropriate color tokens through the combination of both attributes:

/* 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.

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.

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:

"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<ThemeInfo>(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:

"use client";

function MyComponent() {
  const { colorMode, userColorScheme } = useTheme();

  return (
    <div>
      <p>Theme policy: {colorMode}</p>
      <p>Current color scheme: {userColorScheme}</p>
      {userColorScheme === "dark" ? (
        <DarkModeSpecificComponent />
      ) : (
        <LightModeSpecificComponent />
      )}
    </div>
  );
}

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.

Table of contents