@tenphi/glaze 2.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,487 @@
1
+ ---
2
+ title: Getting started
3
+ description: Install Glaze, build a first theme step by step, and export it.
4
+ ---
5
+
6
+ import { Callout, Card, CardGrid, CodeGroup } from '@tenphi/cookbook/components';
7
+ import Example from '../docs-site/components/Example.astro';
8
+ import Swatches from '../docs-site/components/Swatches.tsx';
9
+ import UiPreview from '../docs-site/components/UiPreview.tsx';
10
+ import ContrastSamples from '../docs-site/components/home/ContrastSamples.tsx';
11
+
12
+ This guide builds a small, complete theme — a surface, text, an accent, a
13
+ border, and a card shadow — and exports it as CSS. Every example renders live in
14
+ the scheme you're viewing, so switch the site's appearance to dark or high
15
+ contrast as you go.
16
+
17
+ ## Install
18
+
19
+ <CodeGroup
20
+ label="Install Glaze"
21
+ items={[
22
+ { label: 'pnpm', language: 'shell', code: 'pnpm add @tenphi/glaze' },
23
+ { label: 'npm', language: 'shell', code: 'npm install @tenphi/glaze' },
24
+ { label: 'yarn', language: 'shell', code: 'yarn add @tenphi/glaze' },
25
+ ]}
26
+ />
27
+
28
+ Glaze has no runtime dependencies. It ships ESM and CommonJS builds with type
29
+ definitions and runs in Node.js 20+, browsers, and edge runtimes.
30
+
31
+ ## Build a theme
32
+
33
+ ### 1. Seed and surface
34
+
35
+ A theme starts from a seed: a hue (0–360) and a saturation (0–100). Colors are
36
+ then defined on the **tone** scale, where 0 is black and 100 is white and equal
37
+ steps give equal contrast for neutrals (see [Tone and OKHST](./okhst.mdx)). The
38
+ page surface is a **root** color: it places itself with an absolute tone.
39
+
40
+ <Example hue={280} saturation={80} colors={{ surface: { tone: 97, saturation: 0.15 } }} />
41
+
42
+ A color's `saturation` is a factor of the seed: `0.15` is 15% of 80. It
43
+ defaults to `1`, the full seed saturation.
44
+
45
+ Each swatch prints the tone the current scheme renders. Ordinary light and dark
46
+ schemes fit tones into a window, so tone 97 renders as 97.1 in light; dark mode
47
+ inverts the scale, so the surface turns dark. High-contrast schemes use the
48
+ full range. [Light, dark, and high contrast](./schemes.mdx) covers the details.
49
+
50
+ ### 2. Text with a contrast floor
51
+
52
+ Text is a **dependent** color: it names a `base` and a signed **tone delta**
53
+ from it. `'-1'` means one tone darker than the surface — unreadable on its own.
54
+ The `contrast` floor fixes that: Glaze moves the color away from its base until
55
+ the WCAG ratio passes (`'AA'` is 4.5:1, `'AAA'` is 7:1), and stops at the floor
56
+ or just past it.
57
+
58
+ ```ts
59
+ theme.colors({
60
+ 'surface-text': {
61
+ base: 'surface',
62
+ tone: '-1',
63
+ saturation: 0.4,
64
+ contrast: 'AAA',
65
+ },
66
+ 'surface-text-soft': {
67
+ base: 'surface',
68
+ tone: '-1',
69
+ saturation: 0.3,
70
+ contrast: 'AA',
71
+ },
72
+ });
73
+ ```
74
+
75
+ <ContrastSamples
76
+ hue={280}
77
+ saturation={80}
78
+ colors={{
79
+ surface: { tone: 97, saturation: 0.15 },
80
+ 'surface-text': {
81
+ base: 'surface',
82
+ tone: '-1',
83
+ saturation: 0.4,
84
+ contrast: 'AAA',
85
+ },
86
+ 'surface-text-soft': {
87
+ base: 'surface',
88
+ tone: '-1',
89
+ saturation: 0.3,
90
+ contrast: 'AA',
91
+ },
92
+ }}
93
+ samples={[
94
+ { fg: 'surface-text', bg: 'surface', note: "contrast: 'AAA'" },
95
+ { fg: 'surface-text-soft', bg: 'surface', note: "contrast: 'AA'" },
96
+ ]}
97
+ caption="Each tile measures the resolved text against the surface in the scheme you're viewing."
98
+ />
99
+
100
+ `theme.colors()` merges: it adds new colors and replaces existing ones by name,
101
+ so a theme can grow over several calls.
102
+
103
+ The delta follows its base into every scheme. In dark mode the scale inverts, so
104
+ one tone darker becomes about 0.87 tones lighter (the dark window is a little
105
+ narrower than the full scale) and the text turns light. In high contrast,
106
+ `'AA'` rises to `'AAA'`, so both colors meet 7:1. For APCA instead of WCAG,
107
+ write the floor as `{ apca: 'body' }` — see
108
+ [Contrast solving](./contrast.mdx).
109
+
110
+ ### 3. An accent that keeps its contrast
111
+
112
+ A primary button needs a fill and a label that read well together in every
113
+ scheme. Pin the label, then solve the fill against it:
114
+
115
+ ```ts
116
+ theme.colors({
117
+ 'accent-surface-text': { tone: 100, mode: 'fixed' },
118
+ 'accent-surface': {
119
+ base: 'accent-surface-text',
120
+ tone: '-1',
121
+ contrast: 'AA',
122
+ mode: 'fixed',
123
+ },
124
+ 'accent-text': { base: 'surface', tone: '-1', contrast: 'AA' },
125
+ });
126
+ ```
127
+
128
+ <ContrastSamples
129
+ hue={280}
130
+ saturation={80}
131
+ colors={{
132
+ surface: { tone: 97, saturation: 0.15 },
133
+ 'accent-surface-text': { tone: 100, mode: 'fixed' },
134
+ 'accent-surface': {
135
+ base: 'accent-surface-text',
136
+ tone: '-1',
137
+ contrast: 'AA',
138
+ mode: 'fixed',
139
+ },
140
+ 'accent-text': { base: 'surface', tone: '-1', contrast: 'AA' },
141
+ }}
142
+ samples={[
143
+ { fg: 'accent-surface-text', bg: 'accent-surface', note: 'button label on fill' },
144
+ { fg: 'accent-text', bg: 'surface', note: 'link on the surface' },
145
+ ]}
146
+ />
147
+
148
+ - The label sits at tone 100: white in light and high contrast, and near-white
149
+ (tone 95.9) in dark, where the dark tone window caps it. The fill starts one
150
+ tone below the label and moves down until the pair clears 4.5:1, landing
151
+ just past the floor at about 4.55:1 (the solver keeps a 1% margin). Because
152
+ the fill depends on the label, the pair passes in every scheme — in high
153
+ contrast the fill darkens further, until the pair clears 7:1.
154
+ - `mode: 'fixed'` keeps both colors on their authored side of the tone scale in
155
+ dark mode, so the button stays a saturated fill with a light label. The
156
+ default, `mode: 'auto'`, inverts.
157
+ - `accent-text` is a link color for the page. It uses the full seed saturation
158
+ and the same `'AA'` floor as the soft text.
159
+
160
+ ### 4. A border with a high-contrast pair
161
+
162
+ ```ts
163
+ theme.colors({
164
+ border: {
165
+ base: 'surface',
166
+ tone: ['-8', '-16'],
167
+ saturation: 0.2,
168
+ },
169
+ });
170
+ ```
171
+
172
+ <Swatches
173
+ hue={280}
174
+ saturation={80}
175
+ colors={{
176
+ surface: { tone: 97, saturation: 0.15 },
177
+ border: { base: 'surface', tone: ['-8', '-16'], saturation: 0.2 },
178
+ }}
179
+ />
180
+
181
+ Many values accept a `[normal, highContrast]` pair. The border sits 8 tones
182
+ from the surface in light, about 7 in dark (the dark tone window scales relative
183
+ steps down a little), and exactly 16 in both high-contrast schemes. It has no
184
+ contrast floor: a border is a visible step, not text.
185
+
186
+ ### 5. A card shadow
187
+
188
+ ```ts
189
+ theme.colors({
190
+ 'card-shadow': {
191
+ type: 'shadow',
192
+ bg: 'surface',
193
+ intensity: 8,
194
+ },
195
+ });
196
+ ```
197
+
198
+ A `shadow` is a translucent overlay, black by default, whose opacity Glaze
199
+ solves so that once the browser composites it over `bg`, the surface lands
200
+ exactly `intensity` tones darker, in every scheme where it has room for the
201
+ step. `bg` is the surface the shadow falls on. Add `fg` — the element casting
202
+ the shadow — and a darker object casts a deeper shadow.
203
+
204
+ That completes the theme. Here it is on a small product card, in the scheme
205
+ you're viewing:
206
+
207
+ <UiPreview
208
+ hue={280}
209
+ saturation={80}
210
+ colors={{
211
+ surface: { tone: 97, saturation: 0.15 },
212
+ 'surface-text': {
213
+ base: 'surface',
214
+ tone: '-1',
215
+ saturation: 0.4,
216
+ contrast: 'AAA',
217
+ },
218
+ 'surface-text-soft': {
219
+ base: 'surface',
220
+ tone: '-1',
221
+ saturation: 0.3,
222
+ contrast: 'AA',
223
+ },
224
+ 'accent-surface-text': { tone: 100, mode: 'fixed' },
225
+ 'accent-surface': {
226
+ base: 'accent-surface-text',
227
+ tone: '-1',
228
+ contrast: 'AA',
229
+ mode: 'fixed',
230
+ },
231
+ 'accent-text': { base: 'surface', tone: '-1', contrast: 'AA' },
232
+ border: { base: 'surface', tone: ['-8', '-16'], saturation: 0.2 },
233
+ 'card-shadow': { type: 'shadow', bg: 'surface', intensity: 8 },
234
+ }}
235
+ roles={{
236
+ page: 'surface',
237
+ card: 'surface',
238
+ border: 'border',
239
+ text: 'surface-text',
240
+ textSoft: 'surface-text-soft',
241
+ link: 'accent-text',
242
+ accent: 'accent-surface',
243
+ accentLabel: 'accent-surface-text',
244
+ shadow: 'card-shadow',
245
+ }}
246
+ />
247
+
248
+ <Callout kind="note" title="The shadow in dark high contrast">
249
+ The dark high-contrast surface renders at tone 3, which leaves room for only
250
+ 3 tones of shadow. Glaze uses the strongest shadow it can — opaque black —
251
+ and logs a one-time console warning; on a surface that is already black, the
252
+ shadow would be transparent. Dark interfaces usually show elevation with a
253
+ `glow`, the lightening mirror of a shadow; see
254
+ [Shadows and glows](./shadows-and-glows.mdx).
255
+ </Callout>
256
+
257
+ ## See all four schemes
258
+
259
+ Glaze resolves every color in four **scheme variants**: `light`, `dark`,
260
+ `lightContrast`, and `darkContrast`. Here is the whole theme in one place, with
261
+ each scheme on its own background:
262
+
263
+ <Example
264
+ hue={280}
265
+ saturation={80}
266
+ visual="schemes"
267
+ colors={{
268
+ surface: { tone: 97, saturation: 0.15 },
269
+ 'surface-text': {
270
+ base: 'surface',
271
+ tone: '-1',
272
+ saturation: 0.4,
273
+ contrast: 'AAA',
274
+ },
275
+ 'surface-text-soft': {
276
+ base: 'surface',
277
+ tone: '-1',
278
+ saturation: 0.3,
279
+ contrast: 'AA',
280
+ },
281
+ 'accent-surface-text': { tone: 100, mode: 'fixed' },
282
+ 'accent-surface': {
283
+ base: 'accent-surface-text',
284
+ tone: '-1',
285
+ contrast: 'AA',
286
+ mode: 'fixed',
287
+ },
288
+ 'accent-text': { base: 'surface', tone: '-1', contrast: 'AA' },
289
+ border: { base: 'surface', tone: ['-8', '-16'], saturation: 0.2 },
290
+ 'card-shadow': { type: 'shadow', bg: 'surface', intensity: 8 },
291
+ }}
292
+ roles={{
293
+ page: 'surface',
294
+ card: 'surface',
295
+ border: 'border',
296
+ text: 'surface-text',
297
+ textSoft: 'surface-text-soft',
298
+ link: 'accent-text',
299
+ accent: 'accent-surface',
300
+ accentLabel: 'accent-surface-text',
301
+ shadow: 'card-shadow',
302
+ }}
303
+ />
304
+
305
+ `css()` always returns all four variants. The other output methods include
306
+ `light` and `dark` by default; turn on the high-contrast variants once for the
307
+ whole app:
308
+
309
+ ```ts
310
+ glaze.configure({
311
+ modes: { dark: true, highContrast: true },
312
+ });
313
+ ```
314
+
315
+ or for one call, with `theme.tokens({ modes: { highContrast: true } })`.
316
+
317
+ ## Export it
318
+
319
+ Output methods resolve the theme and return the colors in the shape your stack
320
+ consumes. Colors are native `oklch()` by default; pass `format: 'rgb'` or
321
+ `'hsl'` for other notations.
322
+
323
+ ### CSS custom properties
324
+
325
+ `theme.css()` returns one block of declarations per scheme variant. Each color
326
+ becomes a `--<name>-color` property (pass `suffix: ''` to drop `-color`). Wrap
327
+ the blocks in the selectors that switch your schemes — here, the light block on
328
+ `:root` and the dark block in a media query:
329
+
330
+ <Example
331
+ hue={280}
332
+ saturation={80}
333
+ visual="none"
334
+ output="css"
335
+ label="styles.ts"
336
+ colors={{
337
+ surface: { tone: 97, saturation: 0.15 },
338
+ 'surface-text': {
339
+ base: 'surface',
340
+ tone: '-1',
341
+ saturation: 0.4,
342
+ contrast: 'AAA',
343
+ },
344
+ 'surface-text-soft': {
345
+ base: 'surface',
346
+ tone: '-1',
347
+ saturation: 0.3,
348
+ contrast: 'AA',
349
+ },
350
+ 'accent-surface-text': { tone: 100, mode: 'fixed' },
351
+ 'accent-surface': {
352
+ base: 'accent-surface-text',
353
+ tone: '-1',
354
+ contrast: 'AA',
355
+ mode: 'fixed',
356
+ },
357
+ 'accent-text': { base: 'surface', tone: '-1', contrast: 'AA' },
358
+ border: { base: 'surface', tone: ['-8', '-16'], saturation: 0.2 },
359
+ 'card-shadow': { type: 'shadow', bg: 'surface', intensity: 8 },
360
+ }}
361
+ code={[
362
+ "const css = theme.css({ format: 'rgb' });",
363
+ '',
364
+ 'const stylesheet = `',
365
+ ':root {',
366
+ '${css.light}',
367
+ '}',
368
+ '',
369
+ '@media (prefers-color-scheme: dark) {',
370
+ ' :root {',
371
+ '${css.dark}',
372
+ ' }',
373
+ '}`;',
374
+ ].join('\n')}
375
+ />
376
+
377
+ Components then read the variables, for example
378
+ `background: var(--surface-color)`.
379
+
380
+ ### Other formats
381
+
382
+ Each export is one call:
383
+
384
+ ```ts
385
+ theme.tokens(); // scheme → color → value
386
+ theme.json(); // color → scheme → value
387
+ theme.tasty(); // Tasty '#color' tokens with state maps
388
+ theme.tailwind(); // Tailwind CSS v4 @theme block + overrides
389
+ theme.dtcg(); // one W3C DTCG token tree per scheme
390
+ theme.dtcgResolver(); // one DTCG Resolver-Module document
391
+ ```
392
+
393
+ - `tokens()` and `json()` return plain JavaScript data, grouped by scheme or by
394
+ color.
395
+ - `tasty()` returns [Tasty](https://tasty.style) `#token` bindings keyed by
396
+ state, and also accepts Tasty's own `okhsl` and `okhst` formats.
397
+ - `tailwind()` returns a Tailwind CSS v4 `@theme` block plus scheme overrides,
398
+ so `bg-surface` and `text-surface-text` work out of the box.
399
+ - `dtcg()` returns one W3C design-token tree per scheme for Figma, Tokens
400
+ Studio, Style Dictionary, and other DTCG tools; `dtcgResolver()` puts every
401
+ scheme in one document.
402
+
403
+ To save the definitions themselves rather than resolved colors, call
404
+ `theme.export()` and restore the snapshot with `glaze.themeFrom()`.
405
+ [Exporting and integration](./exporting.mdx) covers choosing an export, wiring
406
+ it into your app, and multi-theme palettes;
407
+ [Output formats](./reference/output.md) lists every option.
408
+
409
+ ## One color without a theme
410
+
411
+ When you need a single adaptive color, or a pair with a contrast floor, skip the
412
+ theme and use `glaze.color()`:
413
+
414
+ ```ts
415
+ import { glaze } from '@tenphi/glaze';
416
+
417
+ const surface = glaze.color('#f6f5fa');
418
+ const brandText = glaze.color({
419
+ from: '#7a4dbf',
420
+ base: surface,
421
+ contrast: 'AA',
422
+ });
423
+
424
+ brandText.css({ name: 'brand-text' });
425
+ // → {
426
+ // light: '--brand-text-color: oklch(0.5276 0.172 298.52);',
427
+ // dark: '--brand-text-color: oklch(0.6588 0.1367 298.52);',
428
+ // lightContrast: '…',
429
+ // darkContrast: '…',
430
+ // }
431
+ ```
432
+
433
+ The light variant reproduces `#7a4dbf` exactly because it already passes 4.5:1
434
+ on that surface. The dark and high-contrast variants adapt, and the floor holds
435
+ in each of them. See [Color tokens](./reference/color-tokens.md).
436
+
437
+ ## Concepts at a glance
438
+
439
+ 1. **A theme has one hue and saturation seed**, optionally with a separate pair
440
+ for the dark schemes ([dark seed](./schemes.mdx#dark-seed)). Status themes
441
+ come from `extend()`: they inherit the same relationships with a different
442
+ hue. See [Designing a palette](./methodology.mdx).
443
+ 2. **A color is a root or a dependent.** Roots use an absolute `tone` or the
444
+ scheme extremes `'max'` / `'min'`. Dependents name a `base` and may use a
445
+ signed tone delta such as `'-8'` plus a `contrast` floor.
446
+ 3. **Tone is an authoring axis, not a contrast guarantee.** Equal tone deltas
447
+ are exact WCAG steps for neutrals. Chromatic colors can drift in rendered
448
+ luminance, so Glaze measures contrast floors against the resolved colors.
449
+ See [Tone and OKHST](./okhst.mdx).
450
+ 4. **`contrast` is a floor.** A bare number or preset means WCAG; `{ wcag }`
451
+ and `{ apca }` pick the metric explicitly. The solver keeps the requested
452
+ tone whenever it already passes with a small margin (1% over a WCAG ratio,
453
+ 0.5 Lc over an APCA target). See [Contrast solving](./contrast.mdx).
454
+ 5. **Each color chooses its dark adaptation.** `mode: 'auto'` inverts tone
455
+ (`100 − t`), `'fixed'` keeps the authored side of the scale, and `'static'`
456
+ skips scheme adaptation. See
457
+ [Light, dark, and high contrast](./schemes.mdx).
458
+ 6. **Tone windows bound the ordinary schemes.** `lightTone` and `darkTone` set
459
+ the light and dark tone windows; high-contrast variants use the full range.
460
+ Values that accept `[normal, highContrast]` pairs can tighten deliberately in
461
+ high contrast.
462
+
463
+ Output `modes` choose which scheme variants an export includes. In Tasty, the
464
+ `states` option chooses how those variants are activated in your app.
465
+
466
+ ## Next steps
467
+
468
+ <CardGrid>
469
+ <Card title="Design a palette" href="/methodology">
470
+ Surface ladders, accents, disabled states, effects, and status themes,
471
+ built up step by step.
472
+ </Card>
473
+ <Card title="Export and integrate" href="/exporting">
474
+ Pick an export shape and wire it into CSS, Tailwind, Tasty, JSON, or
475
+ design-token tools.
476
+ </Card>
477
+ <Card title="Concepts" href="/schemes">
478
+ How schemes, contrast floors, shadows and glows, and mixes work, down to
479
+ the tone model under them.
480
+ </Card>
481
+ <Card title="API reference" href="/reference">
482
+ Every function, method, option, and type.
483
+ </Card>
484
+ </CardGrid>
485
+
486
+ To explore the tone axis interactively, try the
487
+ [ramp explorer](./okhst.mdx#ramp-explorer).