Blocks Design System

Themes

Tokens follow the reader’s light or dark preference. Any element can choose a theme for its subtree with data-ds-theme: "light", "dark", or "system" to return to the preference inside a forced theme. Only the semantic --ds-color-* tokens change between themes.

html
<aside data-ds-theme="dark">…</aside>

Always light

Accent text

Always dark

Accent text

Source · themes/adjacent-themes.tsx
themes/adjacent-themes.tsx
const panel = {
  padding: 'var(--ds-space-lg)',
  color: 'var(--ds-color-text)',
  backgroundColor: 'var(--ds-color-bg)',
  border: 'var(--ds-border-width-sm) solid var(--ds-color-border)',
  borderRadius: 'var(--ds-border-radius-md)',
}

export default function AdjacentThemes() {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(12rem, 1fr))', gap: 16 }}>
      <section data-ds-theme="light" aria-label="Light theme" style={panel}>
        <p style={{ margin: 0 }}>Always light</p>
        <p style={{ margin: 0, color: 'var(--ds-color-accent-text)' }}>Accent text</p>
      </section>
      <section data-ds-theme="dark" aria-label="Dark theme" style={panel}>
        <p style={{ margin: 0 }}>Always dark</p>
        <p style={{ margin: 0, color: 'var(--ds-color-accent-text)' }}>Accent text</p>
      </section>
    </div>
  )
}
Adjacent themes

Popups in a themed subtree

Select, Modal, Popover, Tooltip and Menu render their popups in a portal at the end of document.body, which is outside any data-ds-theme subtree. Pass container to their content part with an element inside the subtree, and the popup inherits its theme. Select and Modal show it.

Embed in another page

An editor or widget that ships into someone else’s page should not set variables on the host’s :root or claim the host’s font names. Build its stylesheet with renderTokensCss from @blocks/design-system/tokens at build time: every token is scoped to your root selector, themes work on that root and its descendants, and font families can be renamed.

themes/build-editor-css.ts
// Builds a stylesheet for an embedded editor. Every token lives on
// .example-editor instead of :root, and the editor's font has a private
// family name, so the host page keeps its own colors and fonts.
import { writeFileSync } from 'node:fs'
import { renderTokensCss } from '@blocks/design-system/tokens'

const css = renderTokensCss({
  selector: '.example-editor',
  banner: '/* Example editor tokens, generated by build-editor-css.ts. */',
  fontFaces: [{ family: 'Serif', src: 'local("Georgia"), local("Times New Roman")' }],
  fontFamilyNames: { Serif: 'Example Editor Serif' },
  fonts: { body: '"Serif", Georgia, serif', heading: '"Serif", Georgia, serif' },
})

const editorRules = `
.example-editor {
  font-family: var(--ds-font-body);
  color: var(--ds-color-text);
  background-color: var(--ds-color-bg);
  border: var(--ds-border-width-sm) solid var(--ds-color-border);
  border-radius: var(--ds-border-radius-md);
  padding: var(--ds-space-lg);
}
`

writeFileSync(new URL('./editor.css', import.meta.url), `${css}${editorRules}`)

This paragraph belongs to the host page and keeps its own color and font.

The editor follows the reader’s light or dark preference.

This editor is always dark.

Source · themes/embedded-editor.tsx
themes/embedded-editor.tsx
import './editor.css'

export default function EmbeddedEditor() {
  return (
    <section
      aria-label="Host page"
      style={{ fontFamily: '"Courier New", monospace', color: 'rgb(120, 40, 0)' }}
    >
      <p>This paragraph belongs to the host page and keeps its own color and font.</p>
      <section className="example-editor" aria-label="Embedded editor">
        <p>The editor follows the reader’s light or dark preference.</p>
      </section>
      <section
        className="example-editor"
        data-ds-theme="dark"
       
        aria-label="Embedded editor, dark"
        style={{ marginTop: 16 }}
      >
        <p>This editor is always dark.</p>
      </section>
    </section>
  )
}
An embedded editor in a host page

renderTokensCss options:

OptionPurpose
selectorThe element that owns the tokens. Defaults to :root.
fontsReplaces body, heading or monospace font stacks.
fontFaces@font-face rules to include.
fontFamilyNamesPrivate names for families, applied to faces and font tokens.
assetBaseUrlPrefix for relative font url()s, e.g. "./fonts/".
bannerThe comment at the top of the stylesheet.