Skip to content
View as Markdown

useTheme ​

React hook to access and control the current theme (light/dark mode).

Signature ​

typescript
import { type ColorTheme, type ResolvedColorTheme } from "@tailor-platform/app-shell";

const useTheme: () => {
  theme: ColorTheme;
  resolvedTheme: ResolvedColorTheme;
  setTheme: (theme: ColorTheme) => void;
};

Return Value ​

theme ​

  • Type: ColorTheme ("light" | "dark" | "system")
  • Description: Current user preference. "system" means the OS setting is followed.

resolvedTheme ​

  • Type: ResolvedColorTheme ("light" | "dark")
  • Description: Concrete color mode after resolving "system". Always "light" or "dark".

setTheme() ​

  • Type: (theme: ColorTheme) => void
  • Description: Set the color theme. Persisted to localStorage under the key appshell-ui-theme.

Usage ​

Display Current Theme ​

Import Text and useTheme from @tailor-platform/app-shell.

tsx
function ThemeDisplay() {
  const { theme } = useTheme();
  return <Text.p>Current theme: {theme}</Text.p>;
}

Theme Toggle ​

typescript
function ThemeToggle() {
  const { theme, setTheme } = useTheme();

  const toggleTheme = () => {
    setTheme(theme === "dark" ? "light" : "dark");
  };

  return <button onClick={toggleTheme}>Toggle Theme</button>;
}

Theme Selector ​

typescript
function ThemeSelector() {
  const { theme, setTheme } = useTheme();

  return (
    <select value={theme} onChange={(e) => setTheme(e.target.value)}>
      <option value="light">Light</option>
      <option value="dark">Dark</option>
      <option value="system">System</option>
    </select>
  );
}

Conditional Rendering ​

Use resolvedTheme when you need a concrete "light" or "dark" value — it already accounts for "system":

The preview uses Moon and Sun from lucide-react instead of application-specific logo assets. Change the documentation theme to see the rendered logo change.

tsx
function Logo() {
  const { resolvedTheme } = useTheme();
  return (
    <div role="img" aria-label={`${resolvedTheme} mode logo`} className="flex items-center gap-2">
      {resolvedTheme === "dark" ? (
        <Moon aria-hidden className="size-6" />
      ) : (
        <Sun aria-hidden className="size-6" />
      )}
      <Text.span>{resolvedTheme === "dark" ? "Night workspace" : "Day workspace"}</Text.span>
    </div>
  );
}

Theme Persistence ​

Theme preference is automatically saved to localStorage and restored on page load.