Open in a new tab

Define colour and spacing tokens with light and dark values, scope them to one panel, and switch light, dark or follow-the-system.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

Your app has a light and a dark look, and the user may force one or follow the system. Tokens as CSS custom properties keep the stylesheet the same for both; only the values change.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html, main.js and style.css next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.
  4. Drop scope (it defaults to the document) to theme the whole page.

What it shows

  • themeTokens.create({ scope, prefix }) and define({ name: { light, dark } }): each token becomes a --theme-<name> custom property on the scope; a plain value (space-3) is the same in both modes.
  • mode('light' | 'dark' | 'auto'), current() for the effective mode (auto follows prefers-color-scheme), get(name) for a value, and onChange(fn) to follow the switches.
  • style.css reads only the tokens: no colour is written twice.

Code

main.js

javascriptApache-2.027 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { themeTokens } from '@awacloud/fw/dom/rendering/themeTokens.js';

fw.runtime.registerAllDeep([themeTokens]);

/**
 * Design tokens as CSS custom properties (`--theme-*`), with light and dark
 * values, scoped to the preview panel so this page's own look stays as is.
 */
export const theme = fw.runtime.resolve('themeTokens').create({ scope: '#preview', prefix: 'theme-' });
theme.define({
    'color-bg': { light: '#ffffff', dark: '#111318' },
    'color-fg': { light: '#1a1a1a', dark: '#f2f2f2' },
    'color-accent': { light: '#1d4ed8', dark: '#93c5fd' },
    'space-3': '12px',
});

const mode = /** @type {HTMLSelectElement} */ (document.getElementById('mode'));
const resolved = /** @type {HTMLElement} */ (document.getElementById('resolved'));

/** Show the effective mode: `auto` follows the system, the others force it. */
const showMode = () => { resolved.textContent = theme.current().resolved; };
theme.onChange(showMode);
mode.addEventListener('change', () => theme.mode(/** @type {'light'|'dark'|'auto'} */ (mode.value)));
theme.mode(/** @type {'light'|'dark'|'auto'} */ (mode.value));
showMode();

index.html

htmlApache-2.032 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Light and dark design tokens</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <link rel="stylesheet" href="./style.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Light and dark design tokens</h1>
    <label for="mode">Theme of the preview</label>
    <select id="mode">
      <option value="light" selected>Light</option>
      <option value="dark">Dark</option>
      <option value="auto">Follow the system</option>
    </select>
    <p>Resolved mode: <output id="resolved" for="mode"></output></p>
    <section id="preview" aria-label="Preview">
      <h2>Preview</h2>
      <p>Text and <a href="#preview">a link</a> drawn from the <code>--theme-*</code> tokens.</p>
    </section>
  </main>
</body>
</html>

style.css

cssApache-2.09 lines

/* SPDX-License-Identifier: Apache-2.0 */
/* The preview reads only the tokens the page defines with themeTokens. */
#preview {
    background: var(--theme-color-bg);
    color: var(--theme-color-fg);
    padding: var(--theme-space-3);
    border-radius: 8px;
}
#preview a { color: var(--theme-color-accent); }