@tangible/ui 0.2.41 → 0.2.42

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.
Files changed (58) hide show
  1. package/components/CommandPalette/CommandPalette.d.ts +30 -0
  2. package/components/CommandPalette/CommandPalette.js +409 -0
  3. package/components/CommandPalette/CommandPaletteContext.d.ts +31 -0
  4. package/components/CommandPalette/CommandPaletteContext.js +8 -0
  5. package/components/CommandPalette/index.d.ts +3 -0
  6. package/components/CommandPalette/index.js +2 -0
  7. package/components/CommandPalette/shortcut.d.ts +15 -0
  8. package/components/CommandPalette/shortcut.js +83 -0
  9. package/components/CommandPalette/types.d.ts +152 -0
  10. package/components/CommandPalette/types.js +16 -0
  11. package/components/IconTile/IconTile.d.ts +16 -0
  12. package/components/IconTile/IconTile.js +17 -0
  13. package/components/IconTile/index.d.ts +2 -0
  14. package/components/IconTile/index.js +1 -0
  15. package/components/IconTile/types.d.ts +29 -0
  16. package/components/IconTile/types.js +1 -0
  17. package/components/SegmentedControl/SegmentedControl.js +2 -2
  18. package/components/SegmentedControl/types.d.ts +10 -0
  19. package/components/Table/DataTable.js +1 -1
  20. package/components/Table/types.d.ts +6 -0
  21. package/components/Table/types.js +3 -0
  22. package/components/index.d.ts +4 -0
  23. package/components/index.js +2 -0
  24. package/figma-tokens.json +101 -3
  25. package/icons/icons.svg +1 -1
  26. package/icons/sprite-version.js +1 -1
  27. package/icons/sprite.php +1 -1
  28. package/package.json +13 -1
  29. package/skills/building-with-tui/SKILL.md +15 -1
  30. package/styles/all.css +1 -1
  31. package/styles/all.expanded.css +514 -63
  32. package/styles/all.expanded.unlayered.css +514 -63
  33. package/styles/all.unlayered.css +1 -1
  34. package/styles/components/_bundle.scss +8 -0
  35. package/styles/components/avatar.scss +6 -23
  36. package/styles/components/button.scss +15 -15
  37. package/styles/components/chip.scss +35 -11
  38. package/styles/components/command-palette.scss +314 -0
  39. package/styles/components/content-indicator.scss +6 -1
  40. package/styles/components/date-picker.scss +1 -1
  41. package/styles/components/icon-button.scss +6 -7
  42. package/styles/components/icon-tile.scss +89 -0
  43. package/styles/components/input/index.scss +4 -0
  44. package/styles/components/multi-select.scss +1 -1
  45. package/styles/components/progress.scss +23 -0
  46. package/styles/components/segmented-control.scss +11 -1
  47. package/styles/components/step-indicator.scss +1 -1
  48. package/styles/components/tabs.scss +1 -1
  49. package/styles/core.css +1 -1
  50. package/styles/core.unlayered.css +1 -1
  51. package/styles/system/_constants.scss +5 -0
  52. package/styles/system/_palettes.scss +1 -0
  53. package/styles/system/_tokens.scss +40 -1
  54. package/tui-manifest.json +166 -2
  55. package/utils/color/theme.d.ts +66 -0
  56. package/utils/color/theme.js +232 -0
  57. package/vanilla/styles.css +1 -1
  58. package/vanilla/styles.unlayered.css +1 -1
@@ -0,0 +1,66 @@
1
+ import type { ColorScheme } from '../color-scheme';
2
+ /** The resolved page mode: `ColorScheme` without `'auto'`, which needs a system read first. */
3
+ export type ThemeMode = Exclude<ColorScheme, 'auto'>;
4
+ /**
5
+ * Custom property name → value. Every `--tui-*` value is hex, except
6
+ * `initial`. The dark map also carries two private `--_fill-*` names, set to
7
+ * `initial` to undo the dark palette's darker primary hover.
8
+ */
9
+ export type ThemeTokens = Record<string, string>;
10
+ /**
11
+ * The colours a theme is derived from. Only each colour's hue and chroma are
12
+ * used: lightness comes from the contrast targets, so `#2942d1` and a paler
13
+ * blue of the same hue give the same theme.
14
+ */
15
+ export type ThemeColors = {
16
+ /** The brand accent: primary buttons, links, the current item, the focus ring */
17
+ primary: string;
18
+ /** Secondary chrome: secondary buttons, chips, switches. Omit to keep TUI's neutral grey. */
19
+ secondary?: string;
20
+ /**
21
+ * The paper the theme is printed on: surfaces, text, borders and fills take
22
+ * this colour's hue, faintly. Omit and they take the primary's, more faintly still.
23
+ */
24
+ neutral?: string;
25
+ /**
26
+ * How much colour the neutral puts on the page, 0 to 1. At 0 (the default)
27
+ * neutrals stay as grey as TUI's own: the neutral only sets their
28
+ * temperature. At 1 the page takes the neutral's own colour, as loud as it
29
+ * is. Contrast holds at any setting: text, lines and accents are solved
30
+ * against the page as it actually comes out.
31
+ */
32
+ vibrance?: number;
33
+ };
34
+ /** One colour (the primary) or all three */
35
+ export type ThemeSource = string | ThemeColors;
36
+ /**
37
+ * Every colour token for one mode.
38
+ *
39
+ * @param source - The primary colour, or `{ primary, secondary, neutral }`.
40
+ * Any colour `parseColor` reads (hex, rgb(), hsl(), oklch()…).
41
+ * @param mode - Which page the tokens are solved against.
42
+ * @returns `--tui-*` → hex, or `null` when a colour can't be parsed.
43
+ *
44
+ * Sets the page grounds, neutrals, text, `fg-on-accent`, the seven primary
45
+ * steps, `theme-primary-on-base` and the focus ring colour, plus the seven
46
+ * secondary steps and `theme-secondary-on-base` when a secondary is given.
47
+ * In dark mode an accent's base is light (it sits 4.5:1 above a dark page),
48
+ * so text on it is a near-black of its hue, and the dark palette's darker
49
+ * primary hover is reset to `initial` so fills go lighter on hover instead,
50
+ * gaining contrast with that dark text.
51
+ */
52
+ export declare function deriveThemeTokens(source: ThemeSource, mode: ThemeMode): ThemeTokens | null;
53
+ export type DeriveThemeCssOptions = {
54
+ /**
55
+ * The interface selector. Default `.tui-interface`. Light tokens go on it;
56
+ * dark ones on its dark and auto forms, one specificity step heavier than
57
+ * TUI's own dark block, and on nested interfaces under a dark ancestor.
58
+ */
59
+ selector?: string;
60
+ };
61
+ /**
62
+ * A derived theme as a stylesheet: light on the interface, dark under
63
+ * `data-theme="dark"`, and under `data-theme="auto"` when the system is dark.
64
+ * Returns an empty string when a colour can't be parsed.
65
+ */
66
+ export declare function deriveThemeCss(source: ThemeSource, options?: DeriveThemeCssOptions): string;
@@ -0,0 +1,232 @@
1
+ // =============================================================================
2
+ // Derived themes: a whole TUI colour theme from one colour, solved by contrast
3
+ // =============================================================================
4
+ //
5
+ // The grounds (bg, surface, muted, elevated) are fixed lightnesses, tinted
6
+ // faintly with the colour's hue. Everything read against them (borders, fills,
7
+ // text, the seven primary steps) is solved for its WCAG contrast with the
8
+ // page, so every step clears its target in both modes by construction, for
9
+ // any hue.
10
+ //
11
+ // For theme tooling: a theme picker, a preview, a settings panel that writes
12
+ // `--tui-*` overrides. Like the rest of `utils/color`, it is not for a
13
+ // component's own styling.
14
+ import { parseColor, rgbToHex, rgbToOklch, hsvToRgb } from './color.js';
15
+ const STEPS = ['subtlest', 'subtle', 'soft', 'base', 'strong', 'stronger', 'strongest'];
16
+ /**
17
+ * Contrast of each primary step with the page. `base` at 4.5 makes the base a
18
+ * text colour in either mode; `soft` at 1.9 stays a tint; `strongest` at 13
19
+ * is near the fg.
20
+ */
21
+ const PRIMARY_STEP_TARGETS = {
22
+ subtlest: 1.08,
23
+ subtle: 1.25,
24
+ soft: 1.9,
25
+ base: 4.5,
26
+ strong: 6,
27
+ stronger: 9,
28
+ strongest: 13,
29
+ };
30
+ /** Contrast of each neutral role with the page, in the order they're solved. */
31
+ const NEUTRAL_TARGETS = {
32
+ 'fill-subtle': 1.08,
33
+ divider: 1.15,
34
+ border: 1.35,
35
+ fill: 1.35,
36
+ 'fill-strong': 2.6,
37
+ /** WCAG 1.4.11: a control's boundary needs 3:1 */
38
+ 'border-control': 3,
39
+ 'fg-muted': 4.8,
40
+ 'fg-secondary': 7.5,
41
+ };
42
+ /**
43
+ * How much colour (OKLCH chroma) a neutral may carry, per role and mode, from
44
+ * TUI's own greys: light surfaces ~0, borders ~0.009, grey text ~0.024; dark
45
+ * grounds, lines and text ~0.02–0.03.
46
+ */
47
+ const NEUTRAL_CHROMA = {
48
+ light: { ground: 0.003, muted: 0.006, line: 0.01, text: 0.02 },
49
+ dark: { ground: 0.017, muted: 0.02, line: 0.022, text: 0.018 },
50
+ };
51
+ /** The most colour a neutral may carry per role at full vibrance (gamut permitting) */
52
+ const VIVID_CHROMA = {
53
+ light: { ground: 0.2, muted: 0.2, line: 0.12, text: 0.06 },
54
+ dark: { ground: 0.07, muted: 0.08, line: 0.08, text: 0.05 },
55
+ };
56
+ /** Page lightnesses (OKLCH L) per mode */
57
+ const GROUNDS = {
58
+ light: { bg: 0.966, surface: 0.978, muted: 0.946, elevated: 0.992 },
59
+ dark: { bg: 0.17, surface: 0.13, muted: 0.22, elevated: 0.2 },
60
+ };
61
+ function oklchToLinear(L, C, h) {
62
+ const a = C * Math.cos((h * Math.PI) / 180);
63
+ const b = C * Math.sin((h * Math.PI) / 180);
64
+ const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
65
+ const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
66
+ const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
67
+ return [
68
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
69
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
70
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
71
+ ];
72
+ }
73
+ const inGamut = (lin) => lin.every((v) => v >= -1e-4 && v <= 1 + 1e-4);
74
+ const clip01 = (v) => Math.min(1, Math.max(0, v));
75
+ const linearToSrgb = (c) => (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055);
76
+ const luminance = ([r, g, b]) => 0.2126 * clip01(r) + 0.7152 * clip01(g) + 0.0722 * clip01(b);
77
+ const ratio = (a, b) => (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
78
+ /** Most chroma (up to `want`) the hue can carry at this lightness, in sRGB */
79
+ function fitChroma(L, want, h) {
80
+ if (inGamut(oklchToLinear(L, want, h)))
81
+ return want;
82
+ let lo = 0;
83
+ let hi = want;
84
+ for (let i = 0; i < 24; i++) {
85
+ const mid = (lo + hi) / 2;
86
+ if (inGamut(oklchToLinear(L, mid, h)))
87
+ lo = mid;
88
+ else
89
+ hi = mid;
90
+ }
91
+ return lo;
92
+ }
93
+ const lumOf = (L, C, h) => luminance(oklchToLinear(L, fitChroma(L, C, h), h));
94
+ function toHex(L, C, h) {
95
+ const [r, g, b] = oklchToLinear(L, fitChroma(L, C, h), h).map((v) => clip01(linearToSrgb(clip01(v))));
96
+ return rgbToHex({ r, g, b });
97
+ }
98
+ /** The lightness on this hue that sits `target`:1 from the page: darker in light mode, lighter in dark */
99
+ function solve(target, bgLum, dark, chroma, h) {
100
+ let lo = 0;
101
+ let hi = 1;
102
+ for (let k = 0; k < 32; k++) {
103
+ const L = (lo + hi) / 2;
104
+ const lum = lumOf(L, chroma, h);
105
+ const enough = (dark ? lum > bgLum : lum < bgLum) && ratio(lum, bgLum) >= target;
106
+ if (dark === enough)
107
+ hi = L;
108
+ else
109
+ lo = L;
110
+ }
111
+ return toHex((lo + hi) / 2, chroma, h);
112
+ }
113
+ function toRgb(color) {
114
+ const parsed = parseColor(color);
115
+ return parsed.kind === 'color' ? hsvToRgb(parsed.hsva) : null;
116
+ }
117
+ function hueChroma(color) {
118
+ if (color === undefined)
119
+ return null;
120
+ const rgb = toRgb(color);
121
+ return rgb ? rgbToOklch(rgb) : null;
122
+ }
123
+ /**
124
+ * Every colour token for one mode.
125
+ *
126
+ * @param source - The primary colour, or `{ primary, secondary, neutral }`.
127
+ * Any colour `parseColor` reads (hex, rgb(), hsl(), oklch()…).
128
+ * @param mode - Which page the tokens are solved against.
129
+ * @returns `--tui-*` → hex, or `null` when a colour can't be parsed.
130
+ *
131
+ * Sets the page grounds, neutrals, text, `fg-on-accent`, the seven primary
132
+ * steps, `theme-primary-on-base` and the focus ring colour, plus the seven
133
+ * secondary steps and `theme-secondary-on-base` when a secondary is given.
134
+ * In dark mode an accent's base is light (it sits 4.5:1 above a dark page),
135
+ * so text on it is a near-black of its hue, and the dark palette's darker
136
+ * primary hover is reset to `initial` so fills go lighter on hover instead,
137
+ * gaining contrast with that dark text.
138
+ */
139
+ export function deriveThemeTokens(source, mode) {
140
+ const colors = typeof source === 'string' ? { primary: source } : source;
141
+ const primary = hueChroma(colors.primary);
142
+ const secondary = hueChroma(colors.secondary);
143
+ const neutral = hueChroma(colors.neutral);
144
+ if (!primary || (colors.secondary !== undefined && !secondary) || (colors.neutral !== undefined && !neutral)) {
145
+ return null;
146
+ }
147
+ const dark = mode === 'dark';
148
+ // The paper. Neutrals carry no more colour than TUI's own greys do, role by
149
+ // role: light surfaces are all but white, borders and grey text carry the
150
+ // hint of hue, and dark grounds a little more (measured from the default
151
+ // palette). The neutral sets the hue and how far toward those ceilings it
152
+ // goes; without one, the primary's hue at half strength.
153
+ const strength = neutral ? Math.min(1, neutral.c / 0.02) : 0.5 * Math.min(1, primary.c / 0.16);
154
+ const paper = { h: neutral ? neutral.h : primary.h };
155
+ const ceiling = NEUTRAL_CHROMA[mode];
156
+ // Vibrance blends from those ceilings toward the colour itself: the page and
157
+ // lines up to its own chroma, grey text half of it, within each mode's limit.
158
+ const vibrance = Math.min(1, Math.max(0, colors.vibrance ?? 0));
159
+ const own = (neutral ?? primary).c;
160
+ const vivid = VIVID_CHROMA[mode];
161
+ const chroma = (role) => {
162
+ const tame = ceiling[role] * strength;
163
+ const loud = Math.min(role === 'text' ? own / 2 : own, vivid[role]);
164
+ return tame + (Math.max(tame, loud) - tame) * vibrance;
165
+ };
166
+ const ground = GROUNDS[mode];
167
+ const groundChroma = (key) => chroma(key === 'muted' ? 'muted' : 'ground');
168
+ const bgLum = lumOf(ground.bg, groundChroma('bg'), paper.h);
169
+ // Text and boundaries (targets of 3:1 and up) must hold on every ground they
170
+ // sit on, not just the page: popovers are elevated, panels muted. So they're
171
+ // solved against the hardest ground: the darkest in light mode, the
172
+ // lightest in dark. Tints and fills are decoration and solve against the page.
173
+ const groundLums = Object.keys(ground).map((key) => lumOf(ground[key], groundChroma(key), paper.h));
174
+ const hardestLum = dark ? Math.max(...groundLums) : Math.min(...groundLums);
175
+ // A hair above each target, so 8-bit rounding never lands a step under it
176
+ const at = (target, c, h) => solve(target * 1.015, target >= 3 ? hardestLum : bgLum, dark, c, h);
177
+ const flat = (key) => toHex(ground[key], groundChroma(key), paper.h);
178
+ const onAccent = (h) => (dark ? toHex(0.13, 0.03, h) : '#ffffff');
179
+ const tokens = {
180
+ '--tui-color-bg': flat('bg'),
181
+ '--tui-color-bg-surface': flat('surface'),
182
+ '--tui-color-bg-muted': flat('muted'),
183
+ '--tui-color-bg-elevated': flat('elevated'),
184
+ '--tui-color-fg': at(dark ? 15 : 16, chroma('text'), paper.h),
185
+ '--tui-color-fg-on-accent': onAccent(primary.h),
186
+ };
187
+ for (const [role, target] of Object.entries(NEUTRAL_TARGETS)) {
188
+ // Fills and lines take the line ceiling; grey text the text one
189
+ tokens[`--tui-color-${role}`] = at(target, chroma(target >= 4.5 ? 'text' : 'line'), paper.h);
190
+ }
191
+ const ladder = (name, { c, h }) => {
192
+ STEPS.forEach((step, i) => {
193
+ // Tints near the page carry less colour, as a real tint would
194
+ const chroma = c * (i < 2 ? 0.45 : i === 2 ? 0.8 : 1);
195
+ tokens[`--tui-theme-${name}-${step}`] = at(PRIMARY_STEP_TARGETS[step], chroma, h);
196
+ });
197
+ tokens[`--tui-theme-${name}-on-base`] = onAccent(h);
198
+ };
199
+ ladder('primary', primary);
200
+ if (secondary)
201
+ ladder('secondary', secondary);
202
+ // The focus ring follows the brand. The base clears 4.5:1 on every ground,
203
+ // above the 3:1 a focus indicator needs.
204
+ tokens['--tui-focus-ring-color'] = tokens['--tui-theme-primary-base'];
205
+ if (dark) {
206
+ tokens['--_fill-interact-primary'] = 'initial';
207
+ tokens['--_fill-selected-hover-primary'] = 'initial';
208
+ }
209
+ return tokens;
210
+ }
211
+ /**
212
+ * A derived theme as a stylesheet: light on the interface, dark under
213
+ * `data-theme="dark"`, and under `data-theme="auto"` when the system is dark.
214
+ * Returns an empty string when a colour can't be parsed.
215
+ */
216
+ export function deriveThemeCss(source, options = {}) {
217
+ const light = deriveThemeTokens(source, 'light');
218
+ const dark = deriveThemeTokens(source, 'dark');
219
+ if (!light || !dark)
220
+ return '';
221
+ const sel = options.selector ?? '.tui-interface';
222
+ const block = (selector, tokens, indent = '') => `${indent}${selector} {\n${Object.entries(tokens)
223
+ .map(([name, value]) => `${indent} ${name}: ${value};`)
224
+ .join('\n')}\n${indent}}`;
225
+ const darkSel = `${sel}[data-theme="dark"],\n[data-theme="dark"] ${sel}:not([data-theme])`;
226
+ const autoSel = ` ${sel}[data-theme="auto"],\n [data-theme="auto"] ${sel}:not([data-theme])`;
227
+ return [
228
+ block(sel, light),
229
+ block(darkSel, dark),
230
+ `@media (prefers-color-scheme: dark) {\n${block(autoSel.trimStart(), dark, ' ')}\n}`,
231
+ ].join('\n\n');
232
+ }