Colors

Mocha's color tokens, read from its theme.css, in light and dark mode. Components use them through Tailwind, like bg-primary or text-muted-foreground.

Palette

Preview
colors.tsx
import { useSyncExternalStore } from "react"

import { Inline } from "~/components/ui/inline"
import { Stack } from "~/components/ui/stack"
import { Text } from "~/components/ui/text"

// Each surface with the text color meant for it.
const pairs = [
  {
    name: "background",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-background text-foreground",
  },
  {
    name: "surface",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-surface text-surface-foreground",
  },
  {
    name: "card",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-card text-card-foreground",
  },
  {
    name: "overlay",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-overlay text-overlay-foreground",
  },
  {
    name: "primary",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-primary text-primary-foreground",
  },
  {
    name: "secondary",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-secondary text-secondary-foreground",
  },
  {
    name: "muted",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-muted text-muted-foreground",
  },
  {
    name: "accent",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-accent text-accent-foreground",
  },
  {
    name: "destructive",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-destructive text-destructive-foreground",
  },
  {
    name: "warning",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-warning text-warning-foreground",
  },
  {
    name: "success",
    className:
      "flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-success text-success-foreground",
  },
]

// Colors for lines and states, with no text on them.
const lines = [
  { name: "border", className: "size-4 rounded-sm ring-1 ring-border bg-border" },
  { name: "input", className: "size-4 rounded-sm ring-1 ring-border bg-input" },
  { name: "ring", className: "size-4 rounded-sm ring-1 ring-border bg-ring" },
  { name: "backdrop", className: "size-4 rounded-sm ring-1 ring-border bg-backdrop" },
]

const names = [...pairs, ...lines].map((color) => color.name)

/** The colors this system's theme sets, space-separated: a system may not have every one. */
function definedNames(): string {
  const style = getComputedStyle(document.documentElement)
  return names.filter((name) => style.getPropertyValue(`--${name}`).trim() !== "").join(" ")
}

// The theme doesn't change while the page is open, so there's nothing to subscribe to.
const subscribe = () => () => {}

export default function Example() {
  const defined = new Set(
    useSyncExternalStore(subscribe, definedNames, () => names.join(" ")).split(" "),
  )
  return (
    <Stack gap="xl" className="w-full">
      <div className="grid grid-cols-2 gap-sm sm:grid-cols-3 lg:grid-cols-4">
        {pairs
          .filter((color) => defined.has(color.name))
          .map((color) => (
            <div key={color.name} className={color.className}>
              <Text size="lg" weight="semibold" family="heading">
                Aa
              </Text>
              <Text size="xs" family="mono">
                {color.name}
              </Text>
            </div>
          ))}
      </div>
      <Inline gap="lg">
        {lines
          .filter((color) => defined.has(color.name))
          .map((color) => (
            <Inline key={color.name} gap="xs">
              <span className={color.className} />
              <Text render={<span />} size="xs" family="mono" tone="muted">
                {color.name}
              </Text>
            </Inline>
          ))}
      </Inline>
    </Stack>
  )
}

Tokens

Each color's light and dark value. Tokens without a dark value stay the same in dark mode.

TokenLightDark
--background#f5e1c9#20130d
--foreground#20130d#f5e1c9
--surface#f2d9b8#2a1a12
--surface-foreground#20130d#f5e1c9
--overlay#f7e8d6#2c1c14
--overlay-foreground#20130d#f5e1c9
--primary#382419#f0b557
--primary-foreground#f5e1c9#20130d
--secondary#ecc790#382419
--secondary-foreground#20130d#f5e1c9
--muted#f1d7b2#382419
--muted-foreground#605145#b5a391
--accent#ecc790#382419
--accent-foreground#20130d#f5e1c9
--destructive#dc2626#7f1d1d
--destructive-foreground#f5e1c9#f5e1c9
--border#d5c2ad#403229
--input#cab8a3#4b3c33
--ring#382419#f0b557
--backdrop#20130d99#000000b3