# Theme

> The Cherry theme object defines media queries, spacing, colors, font sizes, line heights, and shadows.

Source: https://docs.cherry.al/v1/code/theme

> For the complete documentation index, see [llms.txt](https://docs.cherry.al/llms.txt).

# 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](/v1/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](#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](/v1/code/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`):

<Accordion title="1. Breakpoints">

```tsx
export const breakpoints: Breakpoints = {
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
  xxl: 1440,
  xxxl: 1920,
};

export function mq(minWidth: keyof Breakpoints) {
  return `@media screen and (min-width: ${breakpoints[minWidth]}px)`;
}
```

The `mq()` helper turns a breakpoint key into a min-width media query. Use it inside your styled components to apply styles from that breakpoint upwards:

```tsx
import styled from "styled-components";
import { mq } from "cherry-styled-components";

const Wrapper = styled.div`
  padding: 20px;

  ${mq("lg")} {
    padding: 40px;
  }
`;
```

</Accordion>

<Accordion title="2. Spacing">

```tsx
export const spacing: Spacing = {
  maxWidth: { xs: "1280px", xxxl: "1440px" },
  padding: { xs: "20px", lg: "40px" },
  radius: { xs: "6px", lg: "12px", xl: "30px" },
  gridGap: { xs: "20px", lg: "40px" },
};
```

</Accordion>

<Accordion title="3. Colors">

```tsx
export const colors: Colors = {
  primaryLight: "#91aec4",
  primary: "#4d6f8b",
  primaryDark: "#194569",
  secondaryLight: "#a4b17b",
  secondary: "#5c6e46",
  secondaryDark: "#354c2b",
  tertiaryLight: "#ebccb9",
  tertiary: "#816b5a",
  tertiaryDark: "#675445",
  grayLight: "#e5e7eb",
  gray: "#9ca3af",
  grayDark: "#4b5563",
  success: "#84cc16",
  error: "#ef4444",
  warning: "#eab308",
  info: "#06b6d4",
  dark: "#000000",
  light: "#ffffff",
};
```

A dark counterpart, `colorsDark`, is also exported and used by `themeDark`. It flips `dark`/`light`, swaps the grays for dark-surface values, and brightens the primary palette:

```tsx
export const colorsDark: Colors = {
  primaryLight: "#79C5FF",
  primary: "#6198C6",
  primaryDark: "#339DF4",
  secondaryLight: "#a4b17b",
  secondary: "#5c6e46",
  secondaryDark: "#354c2b",
  tertiaryLight: "#ebccb9",
  tertiary: "#816b5a",
  tertiaryDark: "#675445",
  grayLight: "#1a1a1a",
  gray: "#454444",
  grayDark: "#808080",
  success: "#84cc16",
  error: "#ef4444",
  warning: "#eab308",
  info: "#06b6d4",
  dark: "#ffffff",
  light: "#000000",
};
```

`Colors` also takes optional keys, unset in both built-in themes. Three of them are for a primary that cannot carry a label:

- `onPrimary`: the label color on a primary fill (the primary [Button](/v1/code/button), the sent [ChatMessage](/v1/code/chat-messages) bubble, the primary [Avatar](/v1/code/avatar)). Set it when neither `light` nor `dark` reads on your primary, such as a bright dark-mode primary. Unset, the label takes the palette's contrasting end.
- `primaryFill`: the fill of those same solid primary surfaces, whose label is `onPrimary`. Set it when `primary` is too bright to carry a label but right for accents: the fills take the deeper shade, and links, rings and outlines keep `primary`. Unset, the fills use `primary`.
- `primaryFillHover`: the hover shade of that solid primary fill. Unset, it is `primaryFill` mixed 10% toward black when the theme sets `primaryFill`, else `primaryDark`.

```tsx
import { themeDark as baseDark, type Theme } from "cherry-styled-components";

export const themeDark: Theme = {
  ...baseDark,
  colors: {
    ...baseDark.colors,
    primary: "#ff7a1a", // accents: links, rings, accent text
    primaryFill: "#ba440d", // buttons, sent bubbles, avatars
    primaryFillHover: "#9a3a0b", // primaryFill on hover
    onPrimary: "#faf8f6", // the label on primaryFill
  },
};
```

The rest are mode tokens, covered in [Theming through CSS variables](#theming-through-css-variables) below.

</Accordion>

<Accordion title="4. Shadows">

```tsx
export const shadows: Shadows = {
  xs: "0px 4px 4px 0px rgba(18, 18, 18, 0.04), 0px 1px 3px 0px rgba(39, 41, 45, 0.02)",
  sm: "0px 4px 4px 0px rgba(18, 18, 18, 0.08), 0px 1px 3px 0px rgba(39, 41, 45, 0.04)",
  md: "0px 8px 8px 0px rgba(18, 18, 18, 0.16), 0px 2px 3px 0px rgba(39, 41, 45, 0.06)",
  lg: "0px 16px 24px 0px rgba(18, 18, 18, 0.20), 0px 2px 3px 0px rgba(39, 41, 45, 0.08)",
  xl: "0px 24px 32px 0px rgba(18, 18, 18, 0.24), 0px 2px 3px 0px rgba(39, 41, 45, 0.12)",
};
```

`Shadows` also takes an optional `toast` key, the [Toast](/v1/code/toast)'s shadow. Unset, the toast uses `xs` in light mode and `sm` in dark mode.

A dark counterpart, `shadowsDark`, is also exported and used by `themeDark`. It uses white shadows at the same offsets and opacities so elevation stays visible on dark surfaces:

```tsx
export const shadowsDark: Shadows = {
  xs: "0px 4px 4px 0px rgba(255, 255, 255, 0.04), 0px 1px 3px 0px rgba(255, 255, 255, 0.02)",
  sm: "0px 4px 4px 0px rgba(255, 255, 255, 0.08), 0px 1px 3px 0px rgba(255, 255, 255, 0.04)",
  md: "0px 8px 8px 0px rgba(255, 255, 255, 0.16), 0px 2px 3px 0px rgba(255, 255, 255, 0.06)",
  lg: "0px 16px 24px 0px rgba(255, 255, 255, 0.20), 0px 2px 3px 0px rgba(255, 255, 255, 0.08)",
  xl: "0px 24px 32px 0px rgba(255, 255, 255, 0.24), 0px 2px 3px 0px rgba(255, 255, 255, 0.12)",
};
```

</Accordion>

<Accordion title="5. Fonts">

```tsx
export const fonts: Fonts = {
  text: "Inter",
  head: "Inter",
  mono: "monospace",
};
```

</Accordion>

<Accordion title="6. Font Sizes">

```tsx
export const fontSizes: FontSizes = {
  hero1: { xs: "72px", lg: "128px" },
  hero2: { xs: "60px", lg: "96px" },
  hero3: { xs: "36px", lg: "72px" },
  h1: { xs: "40px", lg: "60px" },
  h2: { xs: "30px", lg: "36px" },
  h3: { xs: "28px", lg: "30px" },
  h4: { xs: "26px", lg: "24px" },
  h5: { xs: "18px", lg: "20px" },
  h6: { xs: "16px", lg: "18px" },
  text: { xs: "14px", lg: "16px" },
  strong: { xs: "14px", lg: "16px" },
  small: { xs: "12px", lg: "14px" },
  blockquote: { xs: "16px", lg: "18px" },
  code: { xs: "14px", lg: "16px" },
  button: { xs: "16px", lg: "16px" },
  buttonBig: { xs: "18px", lg: "18px" },
  buttonSmall: { xs: "14px", lg: "14px" },
  input: { xs: "16px", lg: "16px" },
  inputBig: { xs: "18px", lg: "18px" },
  inputSmall: { xs: "14px", lg: "14px" },
};
```

</Accordion>

<Accordion title="7. Line Heights">

```tsx
export const lineHeights: LineHeights = {
  hero1: { xs: "1.10", lg: "1.10" },
  hero2: { xs: "1.10", lg: "1.10" },
  hero3: { xs: "1.20", lg: "1.10" },
  h1: { xs: "1.50", lg: "1.40" },
  h2: { xs: "1.50", lg: "1.50" },
  h3: { xs: "1.30", lg: "1.50" },
  h4: { xs: "1.30", lg: "1.50" },
  h5: { xs: "1.60", lg: "1.50" },
  h6: { xs: "1.60", lg: "1.60" },
  text: { xs: "1.70", lg: "1.70" },
  strong: { xs: "1.70", lg: "1.70" },
  small: { xs: "1.70", lg: "1.70" },
  blockquote: { xs: "1.70", lg: "1.70" },
  code: { xs: "1.70", lg: "1.70" },
  button: { xs: "1.00", lg: "1.00" },
  buttonBig: { xs: "1.00", lg: "1.00" },
  buttonSmall: { xs: "1.00", lg: "1.00" },
  input: { xs: "1.00", lg: "1.00" },
  inputBig: { xs: "1.00", lg: "1.00" },
  inputSmall: { xs: "1.00", lg: "1.00" },
};
```

</Accordion>

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};
`;
```

<Preview bleed>
<iframe className="light-only" src="https://v1.cherry.al/preview/shadows?theme=light" title="Shadow elevations" loading="lazy" style={{ width: "100%", height: "500px" }} />
<iframe className="dark-only" src="https://v1.cherry.al/preview/shadows?theme=dark" title="Shadow elevations" loading="lazy" style={{ width: "100%", height: "500px" }} />
</Preview>

See the [Shadows](/v1/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()`](/v1/code/dark-mode#styles-that-differ-by-mode) 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`](/v1/code/mixins#color-helpers), or read real values from [`useResolvedTheme()`](/v1/code/dark-mode#real-color-values).

### 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:

| Token | Used for | Unset (light / dark) |
| --- | --- | --- |
| `colors.accent` | Hover and emphasis text (Prose links on hover, ChatSources, ChatLauncher, the ThemeToggle glyph on hover) | `primaryDark` / `primaryLight` |
| `colors.link` | Links in `GlobalStyles` | `primary` / `dark` |
| `colors.onFilled` | The 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 `onPrimary` | `light` / `dark` |
| `colors.primaryTintHover` | The tint behind a hovered [Dropzone](/v1/code/dropzone) or [AvatarDropzone](/v1/code/avatar-dropzone) | `primary` at 4% / 10% |
| `colors.primaryTintDrop` | The tint behind a Dropzone or AvatarDropzone while a file is dragged over it | `primary` at 5% / 12% |
| `colors.primaryTintActive` | The tint behind an active (`$active`) [IconButton](/v1/code/icon-button) | `primary` at 10% / 18% |
| `shadows.toast` | The [Toast](/v1/code/toast)'s shadow | `xs` / `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](/v1/code/mixins#mode-token-helpers) `accentColor`, `linkColor`, `primaryTintColor`, `filledTextColor` and `primaryFillHoverColor` read these tokens for your own components.

The [Callout](/v1/code/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.

<Button href="https://github.com/cherry-design-system/styled-components/blob/main/src/lib/utils/theme.ts" icon="code" iconPosition="left">
  View Source
</Button>
