Skip to content

Configuration

Theme

The Cherry theme is a TypeScript object that defines various aspects of your project. Whether you're working on media queries, spacing sizes, colors, font sizes, or line heights, the Cherry theme has got you covered. Cherry also offers a Figma plugin to extract design tokens - check out the Figma documentation for details.

Wrap your entire application with CherryThemeProvider to ensure all components have access to the theme:

tsx
import {
  CherryThemeProvider,
  theme,
  themeDark,
} from "cherry-styled-components";

export default function App({ Component, pageProps }) {
  return (
    <CherryThemeProvider theme={theme} themeDark={themeDark}>
      <Component {...pageProps} />
    </CherryThemeProvider>
  );
}

Cherry exports two ready-made theme objects to start from: theme (light) and themeDark (dark). You pass both as plain objects with real color values, and the provider turns them into one theme for both modes whose colors and shadows are CSS variables (see Theming through CSS variables). themeDark is optional: without it the page stays light. To switch modes from your own code, use ThemeContext (toggleTheme and setPreference) or the useColorMode() hook. See the Dark Mode documentation for details.

Theme Object

The theme object is type safe and uses TypeScript. The Theme interface has seven keys, the sections below. There is no isDark flag: which theme is light and which is dark comes from the provider prop you pass it to (theme or themeDark):

These sections make up the exported theme and themeDark objects:

tsx
export const theme: Theme = {
  breakpoints,
  spacing,
  colors,
  shadows,
  fonts,
  fontSizes,
  lineHeights,
};

export const themeDark: Theme = {
  breakpoints,
  spacing,
  colors: colorsDark,
  shadows: shadowsDark,
  fonts,
  fontSizes,
  lineHeights,
};

Using Shadows

Every theme ships five shadow levels (xs, sm, md, lg, xl) under theme.shadows. Reference them in your styled components to add elevation. When you pass themeDark to the provider, theme.shadows.md is a CSS variable whose value is the light shadow on :root and the shadowsDark one in dark mode, so elevation adapts to light and dark mode automatically:

tsx
import styled from "styled-components";

const Card = styled.div`
  background: ${({ theme }) => theme.colors.light};
  border-radius: ${({ theme }) => theme.spacing.radius.lg};
  box-shadow: ${({ theme }) => theme.shadows.md};
`;

See the Shadows foundation page for the full list of values.

Theming through CSS variables

Inside styled components, the theme is one object for both modes. The providers pass your light and dark theme objects to createThemeVariables, which hands styled-components a theme whose colors and shadows are CSS variable references, var(--cherry-color-<key>) and var(--cherry-shadow-<key>), and renders the values in a plain <style data-cherry-theme> element: the light ones on :root, the dark ones on :root.dark. Everything else in the theme (breakpoints, spacing, fonts, font sizes, line heights) stays as you wrote it.

tsx
const Card = styled.div`
  /* Renders as background: var(--cherry-color-light); */
  background: ${({ theme }) => theme.colors.light};
  box-shadow: ${({ theme }) => theme.shadows.md};
`;

The mode is the dark class on <html>. Switching it flips the class and the browser repaints: no styled component renders again, and the page changes at once instead of fading control by control. For the frame that paints a switch, a .theme-switching class on <html> turns transitions off.

Two things follow from this model:

  • A style cannot branch on the mode in JavaScript, since the same theme object serves both. Use a mode token (below), a :root.dark & rule, or the modes() helper.
  • A theme color is a var() string, not a hex value, so JavaScript color math on it (polished's darken(), rgba(), readableColor() and the like) breaks. Use the native color-mix() helpers alpha, shade and tint, or read real values from useResolvedTheme().

Mode tokens

Some colors have a default that differs between light and dark mode. The providers always fill these mode tokens in, per mode, with the default below, so Cherry's components read them as CSS variables that follow the mode. Set one on either theme object to override it:

TokenUsed forUnset (light / dark)
colors.accentHover and emphasis text (Prose links on hover, ChatSources, ChatLauncher, the ThemeToggle glyph on hover)primaryDark / primaryLight
colors.linkLinks in GlobalStylesprimary / dark
colors.onFilledThe label on a filled control that is not primary (a secondary, tertiary or error Button, a colored Avatar), and on a primary fill when there is no onPrimarylight / dark
colors.primaryTintHoverThe tint behind a hovered Dropzone or AvatarDropzoneprimary at 4% / 10%
colors.primaryTintDropThe tint behind a Dropzone or AvatarDropzone while a file is dragged over itprimary at 5% / 12%
colors.primaryTintActiveThe tint behind an active ($active) IconButtonprimary at 10% / 18%
shadows.toastThe Toast's shadowxs / sm

The optional fill tokens follow the same rule: once either theme sets primaryFill or primaryFillHover, both modes get a value (the theme's own, else primary and the default hover shade), and the same goes for onPrimary, so one mode never inherits the other's. The mixins accentColor, linkColor, primaryTintColor, filledTextColor and primaryFillHoverColor read these tokens for your own components.

The Callout's alert tints are fixed colors, not tokens, so it follows the dark class on <html> and the nearest data-theme="dark" or data-theme="light" ancestor.

createThemeVariables

The providers call createThemeVariables(theme, themeDark?) for you. It is server-safe, so you can call it yourself to render the stylesheet in server code or to read the resolved palettes. It returns:

  • theme - the theme for both modes, with every color and shadow as a CSS variable.
  • light and dark - the two palettes with real values and every mode token filled in, each with an isDark flag. dark is the light one when no dark theme is given.
  • css - the stylesheet behind the variables. Render it as a plain <style>, not through styled-components, whose CSS preprocessor drops the empty custom properties modeValue() relies on.
tsx
import { createThemeVariables } from "cherry-styled-components";
import { theme, themeDark } from "./theme";

const { light, dark } = createThemeVariables(theme, themeDark);

light.colors.accent; // "#194569"
dark.colors.accent; // "#79C5FF"

Extending the Theme

When extending the Cherry theme, adhere to established patterns. Cherry uses the following size patterns: xs, sm, md, lg, xl, xxl, and xxxl. These patterns define not only media queries but also various other sizes.

If you extend the theme, ensure that you add the necessary type definitions to maintain consistency. All theme section interfaces (Theme, Breakpoints, Spacing, Colors, Shadows, Fonts, FontSizes, LineHeights) are exported, so you can extend them in TypeScript.