Stoop

GitHub
/useTheme

useTheme

#

Hook to access current theme and toggle themes

Usage

#
import { useTheme } from "./theme";

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

  return (
    <button onClick={toggleTheme}>
      Current theme: {themeName}
    </button>
  );
}

Returns

#
PropertyTypeDescription
themeNamestringCurrent theme name
themeThemeCurrent theme object
setTheme(name: string) => voidSet theme by name
toggleTheme() => voidToggle between themes
availableThemesreadonly string[]Array of all available theme names

Examples

#

Basic Theme Toggle

#
function ThemeToggle() {
  const { themeName, toggleTheme } = useTheme();

  return (
    <button onClick={toggleTheme}>
      Current theme: {themeName}
    </button>
  );
}

Theme Selector Dropdown

#
function ThemeSelector() {
  const { themeName, setTheme, availableThemes } = useTheme();

  return (
    <select value={themeName} onChange={(e) => setTheme(e.target.value)}>
      {availableThemes.map((theme) => (
        <option key={theme} value={theme}>
          {theme.charAt(0).toUpperCase() + theme.slice(1)}
        </option>
      ))}
    </select>
  );
}

Accessing Theme Values

#
function MyComponent() {
  const { theme } = useTheme();

  return (
    <div style={{ backgroundColor: theme.colors.background }}>
      Current background: {theme.colors.background}
    </div>
  );
}

Complete Example

#
function ThemeSwitcher() {
  const { themeName, theme, setTheme, toggleTheme, availableThemes } = useTheme();

  return (
    <div>
      <p>Current theme: {themeName}</p>
      <p>Available themes: {availableThemes.join(", ")}</p>

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

      <div>
        {availableThemes.map((name) => (
          <button
            key={name}
            onClick={() => setTheme(name)}
            style={{
              backgroundColor: name === themeName ? theme.colors.primary : "transparent",
            }}
          >
            {name}
          </button>
        ))}
      </div>
    </div>
  );
}