Skip to content

Commit 0f0d15a

Browse files
committed
feat(tokens): a 12-step gray ramp under the neutral color tokens
Each theme now defines --mu-gray-1 to --mu-gray-12, and every neutral color token points at a step instead of its own hex. Values that sat within a few levels of each other share a step, so a few colors move slightly: - Light: canvas and key-hover #fafafa (was #f9f9f9, #fbfbfb), hover #ececec (#ededed), line #dedede (#e0e0e0), primary #1a1a1a (#1f1f1f). - Dark: well, hover and line #252525 (#262626, #242424, #262626), edge #343434 (#353535), on-primary #0f0f0f (#111111), primary-hover #ffffff (#f5f5f5). The ramp is internal, like space and radius: apps read a step for their own parts but don't override it, and themeTokens is unchanged. Steps 7-9 are free. docs/color.md documents the ramp; tokens.test.ts checks the 12 steps and that no neutral token is a raw gray hex, and contrast.test.ts resolves the var() references.
1 parent aa0956d commit 0f0d15a

9 files changed

Lines changed: 116 additions & 49 deletions

File tree

‎docs/color.md‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Memos UI is ink on paper: pure-gray neutrals, `primary` ink for emphasis, one qu
3030

3131
There are two themes: **Light** (the default) and **Dark**.
3232

33-
- Each theme sets every color token and every shadow token to an explicit value. Nothing is derived at runtime. The package's tests check the values against the tables on this page and in [Depth](depth.md), and check the contrast promised here.
33+
- Each theme sets the [gray ramp](#gray-ramp), every color token and every shadow token. Neutral color tokens point at a ramp step; nothing else is derived at runtime. The package's tests check the values against the tables on this page and in [Depth](depth.md), and check the contrast promised here.
3434
- Dark keeps the same depth model. Wells go darker than `canvas`, keys go lighter, `key-sheen` drops to 8%, and `primary` flips to near-white with a dark `on-primary`.
3535
- Non-text contrast: `edge` is about 1.5:1 in Light and Dark. That is the soft border people expect from a calm interface, and it is below WCAG's 3:1 for control boundaries. An app that needs 3:1 boundaries defines its own [app theme](#app-themes) with a darker `edge` and outlined keys.
3636

@@ -44,7 +44,7 @@ The package does not read `prefers-color-scheme` itself. The app does, so that a
4444

4545
An app sets only these tokens:
4646

47-
- the 26 color tokens in the [table below](#color-tokens),
47+
- the 27 color tokens in the [table below](#color-tokens),
4848
- the five shadow tokens in [Depth](depth.md#shadow-tokens),
4949
- the three font tokens in [Typography](typography.md#fonts): `font-sans`, `font-display` and `font-mono`.
5050

@@ -66,7 +66,7 @@ An app can offer more themes than the two the package ships, such as a warm Pape
6666
```
6767

6868
- Set every color token in the table below and the five shadow tokens in [Depth](depth.md). A token the theme leaves out keeps its Light value, so a partial theme mixes two palettes.
69-
- The package exports `themeTokens`, the custom property names of those 31 tokens (`--mu-canvas` to `--mu-shadow-pop`). Check in the app's tests that its theme sets each one:
69+
- The package exports `themeTokens`, the custom property names of those 32 tokens (`--mu-canvas` to `--mu-shadow-pop`). Check in the app's tests that its theme sets each one:
7070

7171
```ts
7272
import { themeTokens } from "@usememos/ui";
@@ -81,25 +81,48 @@ An app can offer more themes than the two the package ships, such as a warm Pape
8181
- The tokens sit in the `mu` [cascade layer](building.md#cascade-layer), so an unlayered app theme wins over them at any specificity.
8282
- An app theme is a choice the user makes. It is not a way to restyle the package per product: an app's default theme is Light or Dark.
8383

84+
## Gray ramp
85+
86+
Every neutral color comes from one 12-step gray ramp per theme. Step 1 is the page and step 12 is the strongest text; Light runs light to dark and Dark runs dark to light. The neutral color tokens below point at a step (`hover` is `var(--mu-gray-3)`); only white and the hues are written out.
87+
88+
- The ramp is internal, like space and radius. Apps read a step for their own parts (`var(--mu-gray-7)` for a chart grid line) but don't override it, and an [app theme](#app-themes) sets the color tokens, not the ramp.
89+
- Steps 1 to 6 are fills and lines, 7 to 9 are free for app parts, 10 to 12 are text. A step marked free has no token on it yet.
90+
- A new neutral token points at an existing step. Add a step only with a written reason here.
91+
92+
| Step | Light | Dark | Used by |
93+
| --- | --- | --- | --- |
94+
| `gray-1` | `#fafafa` | `#0f0f0f` | Light: `canvas`, `key-hover`. Dark: `canvas`, `on-primary` |
95+
| `gray-2` | `#f4f4f4` | `#1a1a1a` | Light: free. Dark: `surface` |
96+
| `gray-3` | `#ececec` | `#252525` | Light: `hover`. Dark: `well`, `hover`, `line` |
97+
| `gray-4` | `#e4e4e4` | `#343434` | Light: `well`. Dark: `key`, `edge` |
98+
| `gray-5` | `#dedede` | `#3c3c3c` | Light: `line`. Dark: `key-hover` |
99+
| `gray-6` | `#d0d0d0` | `#4d4d4d` | Light: `edge`. Dark: free |
100+
| `gray-7` | `#b5b5b5` | `#636363` | Light: free. Dark: free |
101+
| `gray-8` | `#949494` | `#7c7c7c` | Light: free. Dark: free |
102+
| `gray-9` | `#767676` | `#959595` | Light: free. Dark: free |
103+
| `gray-10` | `#5e5e5e` | `#adadad` | Light: `ink-muted`. Dark: `ink-muted` |
104+
| `gray-11` | `#333333` | `#e6e6e6` | Light: `primary-hover`. Dark: `primary` |
105+
| `gray-12` | `#1a1a1a` | `#ededed` | Light: `ink`, `primary`. Dark: `ink` |
106+
84107
## Color tokens
85108

86109
| Token | Light | Dark | Usage |
87110
| --- | --- | --- | --- |
88-
| `canvas` | `#f9f9f9` | `#0f0f0f` | Page background |
89-
| `surface` | `#ffffff` | `#1a1a1a` | One lightness step above canvas: inputs, menus, popovers, dialogs, toasts, table headers |
90-
| `well` | `#e4e4e4` | `#262626` | Recessed parts that hold a state: switch, tab, checkbox and radio wells, pressed toggles, inline code. Paired with `edge` and `shadow-track`. Not for text entry |
91-
| `hover` | `#ededed` | `#242424` | Hover on rows, ghost buttons and wells; the highlighted row in menus and selects |
92-
| `key` | `#ffffff` | `#343434` | Raised parts you press: neutral buttons, select triggers, the selected tab, switch knobs, kbd. Paired with `shadow-key` or `shadow-raised` |
93-
| `key-hover` | `#fbfbfb` | `#3c3c3c` | Hover on a neutral key |
111+
| `canvas` | `var(--mu-gray-1)` | `var(--mu-gray-1)` | Page background |
112+
| `surface` | `#ffffff` | `var(--mu-gray-2)` | One lightness step above canvas: inputs, menus, popovers, dialogs, toasts, table headers |
113+
| `well` | `var(--mu-gray-4)` | `var(--mu-gray-3)` | Recessed parts that hold a state: switch, tab, checkbox and radio wells, pressed toggles, inline code. Paired with `edge` and `shadow-track`. Not for text entry |
114+
| `hover` | `var(--mu-gray-3)` | `var(--mu-gray-3)` | Hover on rows, ghost buttons and wells; the highlighted row in menus and selects |
115+
| `key` | `#ffffff` | `var(--mu-gray-4)` | Raised parts you press: neutral buttons, select triggers, the selected tab, switch knobs, kbd. Paired with `shadow-key` or `shadow-raised` |
116+
| `key-hover` | `var(--mu-gray-1)` | `var(--mu-gray-5)` | Hover on a neutral key |
94117
| `key-sheen` | `rgba(255,255,255,0.18)` | `rgba(255,255,255,0.08)` | Top light on keys: a gradient from `key-sheen` at the top edge to clear at 60% of the height |
95-
| `edge` | `#d0d0d0` | `#353535` | 1px border of inputs and wells. About 1.5:1 on canvas |
96-
| `line` | `#e0e0e0` | `#262626` | Separators, table rules and menu dividers. 1.25:1 in Light, so structural lines also need spacing or a heading |
118+
| `edge` | `var(--mu-gray-6)` | `var(--mu-gray-4)` | 1px border of inputs and wells. About 1.5:1 on canvas |
119+
| `line` | `var(--mu-gray-5)` | `var(--mu-gray-3)` | Separators, table rules and menu dividers. 1.25:1 in Light, so structural lines also need spacing or a heading |
97120
| `overlay` | `rgba(0,0,0,0.32)` | `rgba(0,0,0,0.6)` | Backdrop behind dialogs and sheets |
98-
| `ink` | `#1a1a1a` | `#ededed` | Primary text and icons on canvas, surface, well and key (13:1 or more in both themes) |
99-
| `ink-muted` | `#5e5e5e` | `#adadad` | Secondary text: descriptions, placeholders, unselected tabs and toggles, shortcuts, timestamps (4.9:1 or more on every surface) |
100-
| `primary` | `#1f1f1f` | `#e6e6e6` | The one emphasized action and every on state: primary buttons, checked boxes and radios, switches when on, tooltips. Ink, not a hue |
101-
| `primary-hover` | `#333333` | `#f5f5f5` | Hover on a primary key |
102-
| `on-primary` | `#ffffff` | `#111111` | Text, icons and the switch knob on `primary` (16:1 Light, 15:1 Dark) |
121+
| `ink` | `var(--mu-gray-12)` | `var(--mu-gray-12)` | Primary text and icons on canvas, surface, well and key (13:1 or more in both themes) |
122+
| `ink-muted` | `var(--mu-gray-10)` | `var(--mu-gray-10)` | Secondary text: descriptions, placeholders, unselected tabs and toggles, shortcuts, timestamps (4.9:1 or more on every surface) |
123+
| `primary` | `var(--mu-gray-12)` | `var(--mu-gray-11)` | The one emphasized action and every on state: primary buttons, checked boxes and radios, switches when on, tooltips. Ink, not a hue |
124+
| `primary-hover` | `var(--mu-gray-11)` | `#ffffff` | Hover on a primary key |
125+
| `on-primary` | `#ffffff` | `var(--mu-gray-1)` | Text, icons and the switch knob on `primary` (16:1 Light, 15:1 Dark) |
103126
| `accent` | `oklch(0.5 0.13 255)` | `oklch(0.78 0.11 255)` | The only hue at rest: links, the text cursor and selection. 5.8:1 on canvas in Light. Never a fill |
104127
| `selection` | `rgba(40,99,171,0.18)` | `rgba(135,186,253,0.28)` | Text selection and the selected range in an editor |
105128
| `focus` | `var(--mu-primary)` | `var(--mu-primary)` | Keyboard focus ring: 2px outline, 1px offset, on `:focus-visible` only |

‎docs/custom-components.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ An app on Tailwind CSS v4 loads `tailwind.css` after the package's other stylesh
119119
@import "@usememos/ui/tailwind.css";
120120
```
121121

122-
It maps the 26 color tokens, five shadow tokens, seven radius tokens and three font tokens to utilities with an `mu-` prefix, so they never clash with an app's own colors:
122+
It maps the 27 color tokens, five shadow tokens, seven radius tokens and three font tokens to utilities with an `mu-` prefix, so they never clash with an app's own colors:
123123

124124
| Token | Utility |
125125
| --- | --- |

‎e2e/button.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ test('shows the ink focus ring on keyboard focus only', async ({ page }) => {
2121
await expect(button).toBeFocused();
2222
await expect(button).toHaveCSS('outline-style', 'solid');
2323
await expect(button).toHaveCSS('outline-width', '2px');
24-
// `focus` is `primary` ink (#1f1f1f in Light), 1px outside the edge.
25-
await expect(button).toHaveCSS('outline-color', 'rgb(31, 31, 31)');
24+
// `focus` is `primary` ink (#1a1a1a in Light), 1px outside the edge.
25+
await expect(button).toHaveCSS('outline-color', 'rgb(26, 26, 26)');
2626
await expect(button).toHaveCSS('outline-offset', '1px');
2727
});
2828

‎e2e/foundations.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, test } from '@playwright/test';
22
import { expectNoAxeViolations, open, themes } from './helpers';
33

4-
const canvas = { light: 'rgb(249, 249, 249)', dark: 'rgb(15, 15, 15)' };
4+
const canvas = { light: 'rgb(250, 250, 250)', dark: 'rgb(15, 15, 15)' };
55

66
for (const theme of themes) {
77
test(`applies the ${theme} theme`, async ({ page }) => {

‎e2e/interop.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test("the app's own variables don't reach the components", async ({ page }) => {
1111
await page.goto('/interop.html');
1212
const button = page.locator('#primary');
1313
// The app sets --primary, --radius-md, --font-sans and --space-3; the button reads only --mu- tokens.
14-
await expect(button).toHaveCSS('background-color', 'rgb(31, 31, 31)');
14+
await expect(button).toHaveCSS('background-color', 'rgb(26, 26, 26)');
1515
await expect(button).toHaveCSS('border-top-left-radius', '6px');
1616
await expect(button).toHaveCSS('padding-left', '12px');
1717
expect(await button.evaluate((element) => getComputedStyle(element).fontFamily)).toMatch(/^system-ui,/);
@@ -86,8 +86,8 @@ for (const theme of themes) {
8686

8787
test("an app's own parts show states from attributes", async ({ page }) => {
8888
await page.goto('/interop.html');
89-
// A menu row the app highlights itself gets `hover` (#ededed in Light).
90-
await expect(page.locator('#menu-custom-active')).toHaveCSS('background-color', 'rgb(237, 237, 237)');
89+
// A menu row the app highlights itself gets `hover` (#ececec in Light).
90+
await expect(page.locator('#menu-custom-active')).toHaveCSS('background-color', 'rgb(236, 236, 236)');
9191
await expect(page.locator('#menu-custom-item')).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
9292

9393
// A field's edge turns 1.5px `focus-edge` ink (#1a1a1a in Light) while anything inside it has focus.

‎e2e/overlays.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ test('Menu: keyboard opens it, moves through items, and returns focus on close',
1111
const menu = page.getByRole('menu');
1212
await expect(menu).toBeVisible();
1313
await expect(trigger).toHaveAttribute('data-popup-open', '');
14-
// The popup takes focus without a ring; the highlighted item shows the `hover` fill (#ededed in Light), not a ring.
14+
// The popup takes focus without a ring; the highlighted item shows the `hover` fill (#ececec in Light), not a ring.
1515
await expect(menu).toHaveCSS('outline-style', 'none');
1616
await page.keyboard.press('ArrowDown');
1717
const edit = page.getByRole('menuitem', { name: /Edit/ });
1818
await expect(edit).toHaveAttribute('data-highlighted', '');
19-
await expect(edit).toHaveCSS('background-color', 'rgb(237, 237, 237)');
19+
await expect(edit).toHaveCSS('background-color', 'rgb(236, 236, 236)');
2020
await expect(edit).toHaveCSS('outline-style', 'none');
2121
await page.keyboard.press('Escape');
2222
await expect(menu).toBeHidden();

‎packages/ui/src/styles/tokens.css‎

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,32 @@
1616
:where(:root, :host, .mu-root:not([data-theme] *)),
1717
[data-theme="light"] {
1818
color-scheme: light;
19-
--mu-canvas: #f9f9f9;
19+
--mu-gray-1: #fafafa;
20+
--mu-gray-2: #f4f4f4;
21+
--mu-gray-3: #ececec;
22+
--mu-gray-4: #e4e4e4;
23+
--mu-gray-5: #dedede;
24+
--mu-gray-6: #d0d0d0;
25+
--mu-gray-7: #b5b5b5;
26+
--mu-gray-8: #949494;
27+
--mu-gray-9: #767676;
28+
--mu-gray-10: #5e5e5e;
29+
--mu-gray-11: #333333;
30+
--mu-gray-12: #1a1a1a;
31+
--mu-canvas: var(--mu-gray-1);
2032
--mu-surface: #ffffff;
21-
--mu-well: #e4e4e4;
22-
--mu-hover: #ededed;
33+
--mu-well: var(--mu-gray-4);
34+
--mu-hover: var(--mu-gray-3);
2335
--mu-key: #ffffff;
24-
--mu-key-hover: #fbfbfb;
36+
--mu-key-hover: var(--mu-gray-1);
2537
--mu-key-sheen: rgba(255,255,255,0.18);
26-
--mu-edge: #d0d0d0;
27-
--mu-line: #e0e0e0;
38+
--mu-edge: var(--mu-gray-6);
39+
--mu-line: var(--mu-gray-5);
2840
--mu-overlay: rgba(0,0,0,0.32);
29-
--mu-ink: #1a1a1a;
30-
--mu-ink-muted: #5e5e5e;
31-
--mu-primary: #1f1f1f;
32-
--mu-primary-hover: #333333;
41+
--mu-ink: var(--mu-gray-12);
42+
--mu-ink-muted: var(--mu-gray-10);
43+
--mu-primary: var(--mu-gray-12);
44+
--mu-primary-hover: var(--mu-gray-11);
3345
--mu-on-primary: #ffffff;
3446
--mu-accent: oklch(0.5 0.13 255);
3547
--mu-selection: rgba(40,99,171,0.18);
@@ -50,21 +62,33 @@
5062
--mu-shadow-pop: 0 8px 24px rgba(0,0,0,0.10), 0 1px 3px rgba(0,0,0,0.08), 0 0 0 0.5px rgba(0,0,0,0.08), inset 0 1px 0 rgba(255,255,255,0.8);
5163
}
5264
[data-theme="dark"] {
53-
--mu-canvas: #0f0f0f;
54-
--mu-surface: #1a1a1a;
55-
--mu-well: #262626;
56-
--mu-hover: #242424;
57-
--mu-key: #343434;
58-
--mu-key-hover: #3c3c3c;
65+
--mu-gray-1: #0f0f0f;
66+
--mu-gray-2: #1a1a1a;
67+
--mu-gray-3: #252525;
68+
--mu-gray-4: #343434;
69+
--mu-gray-5: #3c3c3c;
70+
--mu-gray-6: #4d4d4d;
71+
--mu-gray-7: #636363;
72+
--mu-gray-8: #7c7c7c;
73+
--mu-gray-9: #959595;
74+
--mu-gray-10: #adadad;
75+
--mu-gray-11: #e6e6e6;
76+
--mu-gray-12: #ededed;
77+
--mu-canvas: var(--mu-gray-1);
78+
--mu-surface: var(--mu-gray-2);
79+
--mu-well: var(--mu-gray-3);
80+
--mu-hover: var(--mu-gray-3);
81+
--mu-key: var(--mu-gray-4);
82+
--mu-key-hover: var(--mu-gray-5);
5983
--mu-key-sheen: rgba(255,255,255,0.08);
60-
--mu-edge: #353535;
61-
--mu-line: #262626;
84+
--mu-edge: var(--mu-gray-4);
85+
--mu-line: var(--mu-gray-3);
6286
--mu-overlay: rgba(0,0,0,0.6);
63-
--mu-ink: #ededed;
64-
--mu-ink-muted: #adadad;
65-
--mu-primary: #e6e6e6;
66-
--mu-primary-hover: #f5f5f5;
67-
--mu-on-primary: #111111;
87+
--mu-ink: var(--mu-gray-12);
88+
--mu-ink-muted: var(--mu-gray-10);
89+
--mu-primary: var(--mu-gray-11);
90+
--mu-primary-hover: #ffffff;
91+
--mu-on-primary: var(--mu-gray-1);
6892
--mu-accent: oklch(0.78 0.11 255);
6993
--mu-selection: rgba(135,186,253,0.28);
7094
--mu-success: oklch(0.78 0.14 150);

‎packages/ui/test/contrast.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ function colorTable() {
2626
if (!name || !light || !dark || !/^[\w-]+$/.test(name) || name === 'Token') continue;
2727
table.set(name, { light, dark });
2828
}
29+
// Neutral tokens point at the gray ramp: resolve var(--mu-gray-N) to its value.
30+
for (const values of table.values()) {
31+
for (const theme of themes) {
32+
const ref = values[theme].match(/^var\(--mu-([\w-]+)\)$/)?.[1];
33+
if (ref) values[theme] = table.get(ref)?.[theme] ?? values[theme];
34+
}
35+
}
2936
return table;
3037
}
3138

‎packages/ui/test/tokens.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,21 @@ describe('tokens.css', () => {
9494
}
9595
});
9696

97+
it('points every neutral color token at a step of the gray ramp, or at white', () => {
98+
const grays = tableRows(colorDocs).filter(([name]) => /^`gray-\d+`$/.test(name ?? ''));
99+
expect(grays.map(([name]) => unquote(name))).toEqual(Array.from({ length: 12 }, (_, i) => `gray-${i + 1}`));
100+
for (const theme of themes) {
101+
for (const [name, value] of themeRule[theme]) {
102+
if (name.startsWith('--mu-gray-') || !/^#([0-9a-f])\1{5}$|^#(?:([0-9a-f]{2})\2\2)$/i.test(value)) continue;
103+
expect(value, `${name} in ${theme} should be var(--mu-gray-N) or #ffffff`).toBe('#ffffff');
104+
}
105+
}
106+
});
107+
97108
it('exports the color and shadow tokens as themeTokens, and every theme sets each one', () => {
98-
const colors = tableRows(colorDocs).map(([name]) => `--mu-${unquote(name)}`);
109+
const colors = tableRows(colorDocs)
110+
.map(([name]) => `--mu-${unquote(name)}`)
111+
.filter((name) => !name.startsWith('--mu-gray-'));
99112
const shadows = [...depthDocs.matchAll(/^\| `(shadow-[\w-]+)` \|/gm)].map(([, name = '']) => `--mu-${name}`);
100113
expect(shadows).toHaveLength(5);
101114
expect([...themeTokens]).toEqual([...colors, ...shadows]);

0 commit comments

Comments
 (0)