@luxalgo/vela-ui 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +91 -0
- package/LICENSE +201 -0
- package/NOTICE +28 -0
- package/README.md +86 -3
- package/cli/engine.mjs +282 -0
- package/cli/index.mjs +154 -0
- package/dist/badge.d.ts +26 -0
- package/dist/badge.js +44 -0
- package/dist/bar-chart.d.ts +40 -0
- package/dist/bar-chart.js +105 -0
- package/dist/button.d.ts +55 -0
- package/dist/button.js +11 -0
- package/dist/calendar.d.ts +24 -0
- package/dist/calendar.js +1 -0
- package/dist/callout-bubble.d.ts +103 -0
- package/dist/callout-bubble.js +177 -0
- package/dist/chart.d.ts +30 -0
- package/dist/chart.js +1 -0
- package/dist/checkbox.d.ts +60 -0
- package/dist/checkbox.js +12 -0
- package/dist/chunk-2GQIZM5Z.js +28 -0
- package/dist/chunk-3Q32K4PE.js +85 -0
- package/dist/chunk-6QCILIT2.js +52 -0
- package/dist/chunk-7VMN7NKJ.js +133 -0
- package/dist/chunk-A2FJ7Y7D.js +55 -0
- package/dist/chunk-A72NWYNP.js +26 -0
- package/dist/chunk-B5ZSRUDQ.js +227 -0
- package/dist/chunk-BXMWQIBO.js +66 -0
- package/dist/chunk-C3ICYMI3.js +25 -0
- package/dist/chunk-CC3DDNFC.js +117 -0
- package/dist/chunk-E2FE6V2N.js +81 -0
- package/dist/chunk-FN2BUFNM.js +10 -0
- package/dist/chunk-FVH6L7FM.js +18 -0
- package/dist/chunk-G7IQLY3A.js +183 -0
- package/dist/chunk-GVRX3UAV.js +82 -0
- package/dist/chunk-J2QJATCD.js +12 -0
- package/dist/chunk-LZWZV2ZS.js +18 -0
- package/dist/chunk-MJ4RUGX7.js +403 -0
- package/dist/chunk-MMTMOV6R.js +31 -0
- package/dist/chunk-P4MTEMT7.js +23 -0
- package/dist/chunk-Q6WTDVH6.js +31 -0
- package/dist/chunk-S7USVSYP.js +50 -0
- package/dist/chunk-TAKSIV57.js +90 -0
- package/dist/chunk-U3EH42YY.js +72 -0
- package/dist/chunk-V2Q4MRMS.js +573 -0
- package/dist/chunk-VMCR3S2A.js +22 -0
- package/dist/chunk-WBGSYCV2.js +101 -0
- package/dist/chunk-WGULXRWD.js +351 -0
- package/dist/chunk-WXH3EFV5.js +219 -0
- package/dist/chunk-X66RJAIS.js +14 -0
- package/dist/chunk-XBEL4K33.js +142 -0
- package/dist/chunk-Z5PINFQO.js +280 -0
- package/dist/collapsible.d.ts +49 -0
- package/dist/collapsible.js +106 -0
- package/dist/color-picker.d.ts +103 -0
- package/dist/color-picker.js +19 -0
- package/dist/color.d.ts +24 -0
- package/dist/color.js +1 -0
- package/dist/context-menu.d.ts +72 -0
- package/dist/context-menu.js +161 -0
- package/dist/css.d.ts +17 -0
- package/dist/css.js +4 -0
- package/dist/data-table.d.ts +40 -0
- package/dist/data-table.js +88 -0
- package/dist/date-picker.d.ts +90 -0
- package/dist/date-picker.js +257 -0
- package/dist/define-D5u3F5Dj.d.ts +21 -0
- package/dist/dialog.d.ts +89 -0
- package/dist/dialog.js +258 -0
- package/dist/dispose.d.ts +9 -0
- package/dist/dispose.js +2 -0
- package/dist/donut.d.ts +27 -0
- package/dist/donut.js +67 -0
- package/dist/drawer.d.ts +119 -0
- package/dist/drawer.js +326 -0
- package/dist/dropdown-menu.d.ts +114 -0
- package/dist/dropdown-menu.js +18 -0
- package/dist/empty.d.ts +21 -0
- package/dist/empty.js +38 -0
- package/dist/field.d.ts +147 -0
- package/dist/field.js +328 -0
- package/dist/glyph-select.d.ts +57 -0
- package/dist/glyph-select.js +17 -0
- package/dist/h-Bl37qUis.d.ts +24 -0
- package/dist/headless.d.ts +6 -0
- package/dist/headless.js +9 -0
- package/dist/hover-card.d.ts +43 -0
- package/dist/hover-card.js +97 -0
- package/dist/i18n.d.ts +20 -0
- package/dist/i18n.js +1 -0
- package/dist/icons.d.ts +20 -0
- package/dist/icons.js +2 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +13 -0
- package/dist/input-group.d.ts +57 -0
- package/dist/input-group.js +87 -0
- package/dist/input.d.ts +54 -0
- package/dist/input.js +9 -0
- package/dist/item.d.ts +72 -0
- package/dist/item.js +131 -0
- package/dist/kbd.d.ts +19 -0
- package/dist/kbd.js +8 -0
- package/dist/kit-text.d.ts +8 -0
- package/dist/kit-text.js +2 -0
- package/dist/line-chart.d.ts +32 -0
- package/dist/line-chart.js +120 -0
- package/dist/manifest.json +4508 -0
- package/dist/month-grid.d.ts +45 -0
- package/dist/month-grid.js +157 -0
- package/dist/number-input.d.ts +99 -0
- package/dist/number-input.js +14 -0
- package/dist/page.d.ts +7 -0
- package/dist/page.js +1 -0
- package/dist/palette.d.ts +55 -0
- package/dist/palette.js +1 -0
- package/dist/placement.d.ts +98 -0
- package/dist/placement.js +1 -0
- package/dist/popover.d.ts +107 -0
- package/dist/popover.js +15 -0
- package/dist/scatter-chart.d.ts +32 -0
- package/dist/scatter-chart.js +121 -0
- package/dist/select.d.ts +112 -0
- package/dist/select.js +19 -0
- package/dist/slider.d.ts +46 -0
- package/dist/slider.js +111 -0
- package/dist/sort-button.d.ts +41 -0
- package/dist/sort-button.js +88 -0
- package/dist/sparkline.d.ts +22 -0
- package/dist/sparkline.js +72 -0
- package/dist/stat.d.ts +29 -0
- package/dist/stat.js +41 -0
- package/dist/styles.d.ts +22 -0
- package/dist/styles.js +2 -0
- package/dist/surface-events.d.ts +17 -0
- package/dist/surface-events.js +1 -0
- package/dist/surface-exit.d.ts +25 -0
- package/dist/surface-exit.js +1 -0
- package/dist/switch.d.ts +54 -0
- package/dist/switch.js +92 -0
- package/dist/t.d.ts +89 -0
- package/dist/t.js +1 -0
- package/dist/tabs.d.ts +65 -0
- package/dist/tabs.js +155 -0
- package/dist/text-area.d.ts +54 -0
- package/dist/text-area.js +9 -0
- package/dist/theme.d.ts +199 -0
- package/dist/theme.js +8 -0
- package/dist/theme.schema.json +1994 -0
- package/dist/themes/dark.css +3 -0
- package/dist/themes/light.css +3 -0
- package/dist/toggle-group.d.ts +56 -0
- package/dist/toggle-group.js +130 -0
- package/dist/tokens.d.ts +16 -0
- package/dist/tokens.js +3 -0
- package/dist/tooltip.d.ts +47 -0
- package/dist/tooltip.js +74 -0
- package/dist/variants-B4stUBnY.d.ts +16 -0
- package/dist/zag.d.ts +19 -0
- package/dist/zag.js +2 -0
- package/docs/theming.md +217 -0
- package/docs/what-to-change.md +20 -0
- package/docs/writing-a-component.md +250 -0
- package/llms.txt +71 -0
- package/package.json +90 -6
- package/registry/badge.ts +119 -0
- package/registry/bar-chart.ts +215 -0
- package/registry/button.ts +379 -0
- package/registry/callout-bubble.ts +347 -0
- package/registry/checkbox.ts +234 -0
- package/registry/collapsible.ts +271 -0
- package/registry/color-picker.ts +582 -0
- package/registry/context-menu.ts +208 -0
- package/registry/data-table.ts +215 -0
- package/registry/date-picker.ts +483 -0
- package/registry/dialog.ts +535 -0
- package/registry/donut.ts +145 -0
- package/registry/drawer.ts +636 -0
- package/registry/dropdown-menu.ts +762 -0
- package/registry/empty.ts +91 -0
- package/registry/field.ts +585 -0
- package/registry/glyph-select.ts +268 -0
- package/registry/hover-card.ts +222 -0
- package/registry/input-group.ts +229 -0
- package/registry/input.ts +181 -0
- package/registry/item.ts +376 -0
- package/registry/kbd.ts +64 -0
- package/registry/line-chart.ts +218 -0
- package/registry/month-grid.ts +328 -0
- package/registry/number-input.ts +443 -0
- package/registry/popover.ts +368 -0
- package/registry/scatter-chart.ts +221 -0
- package/registry/select.ts +620 -0
- package/registry/slider.ts +259 -0
- package/registry/sort-button.ts +189 -0
- package/registry/sparkline.ts +114 -0
- package/registry/stat.ts +96 -0
- package/registry/switch.ts +205 -0
- package/registry/tabs.ts +426 -0
- package/registry/text-area.ts +186 -0
- package/registry/toggle-group.ts +295 -0
- package/registry/tooltip.ts +156 -0
- package/skills/theming-vela-ui/SKILL.md +137 -0
- package/skills/using-vela-ui-components/SKILL.md +137 -0
- package/skills/writing-vela-ui-components/SKILL.md +163 -0
- package/src/components/badge.ts +119 -0
- package/src/components/bar-chart.ts +215 -0
- package/src/components/button.ts +379 -0
- package/src/components/callout-bubble.ts +347 -0
- package/src/components/checkbox.ts +234 -0
- package/src/components/collapsible.ts +271 -0
- package/src/components/color-picker.ts +582 -0
- package/src/components/context-menu.ts +208 -0
- package/src/components/data-table.ts +215 -0
- package/src/components/date-picker.ts +483 -0
- package/src/components/dialog.ts +535 -0
- package/src/components/donut.ts +145 -0
- package/src/components/drawer.ts +636 -0
- package/src/components/dropdown-menu.ts +762 -0
- package/src/components/empty.ts +91 -0
- package/src/components/field.ts +585 -0
- package/src/components/glyph-select.ts +268 -0
- package/src/components/hover-card.ts +222 -0
- package/src/components/input-group.ts +229 -0
- package/src/components/input.ts +181 -0
- package/src/components/item.ts +376 -0
- package/src/components/kbd.ts +64 -0
- package/src/components/line-chart.ts +218 -0
- package/src/components/month-grid.ts +328 -0
- package/src/components/number-input.ts +443 -0
- package/src/components/popover.ts +368 -0
- package/src/components/scatter-chart.ts +221 -0
- package/src/components/select.ts +620 -0
- package/src/components/slider.ts +259 -0
- package/src/components/sort-button.ts +189 -0
- package/src/components/sparkline.ts +114 -0
- package/src/components/stat.ts +96 -0
- package/src/components/switch.ts +205 -0
- package/src/components/tabs.ts +426 -0
- package/src/components/text-area.ts +186 -0
- package/src/components/toggle-group.ts +295 -0
- package/src/components/tooltip.ts +156 -0
- package/src/index.ts +24 -0
- package/src/lib/calendar.ts +117 -0
- package/src/lib/chart.ts +72 -0
- package/src/lib/color.ts +89 -0
- package/src/lib/component-index.ts +151 -0
- package/src/lib/css.ts +48 -0
- package/src/lib/define.ts +45 -0
- package/src/lib/dispose.ts +36 -0
- package/src/lib/h.ts +55 -0
- package/src/lib/headless.ts +16 -0
- package/src/lib/i18n.ts +30 -0
- package/src/lib/icons.ts +237 -0
- package/src/lib/kit-text.ts +26 -0
- package/src/lib/page.ts +24 -0
- package/src/lib/palette.ts +81 -0
- package/src/lib/placement.ts +188 -0
- package/src/lib/scope.ts +135 -0
- package/src/lib/styles.ts +53 -0
- package/src/lib/surface-events.ts +31 -0
- package/src/lib/surface-exit.ts +87 -0
- package/src/lib/t.ts +68 -0
- package/src/lib/theme.ts +664 -0
- package/src/lib/tokens.ts +111 -0
- package/src/lib/variants.ts +32 -0
- package/src/lib/zag.ts +46 -0
- package/stylelint/index.mjs +60 -0
- package/stylelint/lint.mjs +81 -0
- package/stylelint/plugin.mjs +115 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Calendar names for the date picker in a translator's language: month and weekday names,
|
|
2
|
+
// the year label, the order of month and year in a header. English keeps the picker's own
|
|
3
|
+
// tables (`September`, `Sep`, `Su`); other languages read `Intl`, always on the Gregorian
|
|
4
|
+
// calendar because the picker produces Gregorian dates.
|
|
5
|
+
import type { Translator } from './i18n';
|
|
6
|
+
|
|
7
|
+
export interface CalendarSettings {
|
|
8
|
+
readonly locale: string;
|
|
9
|
+
readonly numberingSystem: string;
|
|
10
|
+
/** The locale's language is English: the picker's own tables apply. */
|
|
11
|
+
readonly english: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const EN_MONTHS_LONG = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
15
|
+
const EN_MONTHS_SHORT = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
16
|
+
const EN_WEEKDAYS_MIN = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
|
|
17
|
+
|
|
18
|
+
const BIDI_MARKS = /[\u200e\u200f\u061c]/g;
|
|
19
|
+
const stripBidiMarks = (text: string): string => text.replace(BIDI_MARKS, '');
|
|
20
|
+
|
|
21
|
+
/** The calendar settings of a translator (English, Latin digits, without one). */
|
|
22
|
+
export function calendarSettings(i18n: Translator | null | undefined): CalendarSettings {
|
|
23
|
+
const locale = i18n?.locale ?? 'en';
|
|
24
|
+
let language = locale.split(/[-_]/)[0] ?? locale;
|
|
25
|
+
try {
|
|
26
|
+
language = new Intl.Locale(locale).language;
|
|
27
|
+
} catch {
|
|
28
|
+
/* an unparsable tag keeps its first subtag */
|
|
29
|
+
}
|
|
30
|
+
return { locale, numberingSystem: i18n?.numberingSystem ?? 'latn', english: language.toLowerCase() === 'en' };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const dateFormats = new Map<string, Intl.DateTimeFormat>();
|
|
34
|
+
|
|
35
|
+
function dateFormat(settings: CalendarSettings, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat {
|
|
36
|
+
const key = `${settings.locale}|${settings.numberingSystem}|${JSON.stringify(options)}`;
|
|
37
|
+
let dtf = dateFormats.get(key);
|
|
38
|
+
if (!dtf) {
|
|
39
|
+
try {
|
|
40
|
+
dtf = new Intl.DateTimeFormat(settings.locale, { ...options, timeZone: 'UTC', calendar: 'gregory', numberingSystem: settings.numberingSystem });
|
|
41
|
+
} catch {
|
|
42
|
+
dtf = new Intl.DateTimeFormat('en', { ...options, timeZone: 'UTC', calendar: 'gregory' });
|
|
43
|
+
}
|
|
44
|
+
dateFormats.set(key, dtf);
|
|
45
|
+
}
|
|
46
|
+
return dtf;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const digitTables = new Map<string, string[] | null>();
|
|
50
|
+
|
|
51
|
+
/** ASCII digits rewritten in the settings' numbering system. */
|
|
52
|
+
function localizeDigits(text: string, settings: CalendarSettings): string {
|
|
53
|
+
const key = `${settings.locale}|${settings.numberingSystem}`;
|
|
54
|
+
let digits = digitTables.get(key);
|
|
55
|
+
if (digits === undefined) {
|
|
56
|
+
let nf: Intl.NumberFormat;
|
|
57
|
+
try {
|
|
58
|
+
nf = new Intl.NumberFormat(settings.locale, { numberingSystem: settings.numberingSystem, useGrouping: false, maximumFractionDigits: 0 });
|
|
59
|
+
} catch {
|
|
60
|
+
nf = new Intl.NumberFormat('en', { useGrouping: false, maximumFractionDigits: 0 });
|
|
61
|
+
}
|
|
62
|
+
const table = Array.from({ length: 10 }, (_, d) => stripBidiMarks(nf.format(d)));
|
|
63
|
+
digits = table.every((d, i) => d === String(i)) ? null : table;
|
|
64
|
+
digitTables.set(key, digits);
|
|
65
|
+
}
|
|
66
|
+
if (!digits) return text;
|
|
67
|
+
const map = digits;
|
|
68
|
+
return text.replace(/[0-9]/g, (d) => map[Number(d)]!);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Twelve month names, January first. */
|
|
72
|
+
export function monthNames(settings: CalendarSettings, width: 'long' | 'short'): string[] {
|
|
73
|
+
if (settings.english) return [...(width === 'long' ? EN_MONTHS_LONG : EN_MONTHS_SHORT)];
|
|
74
|
+
const dtf = dateFormat(settings, { month: width });
|
|
75
|
+
return Array.from({ length: 12 }, (_, m) => stripBidiMarks(dtf.format(Date.UTC(2000, m, 15))));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Seven short weekday names, Sunday first (index = `Date#getDay()`). */
|
|
79
|
+
export function weekdayNames(settings: CalendarSettings): string[] {
|
|
80
|
+
if (settings.english) return [...EN_WEEKDAYS_MIN];
|
|
81
|
+
const dtf = dateFormat(settings, { weekday: 'short' });
|
|
82
|
+
// 2 January 2000 was a Sunday.
|
|
83
|
+
return Array.from({ length: 7 }, (_, d) => stripBidiMarks(dtf.format(Date.UTC(2000, 0, 2 + d))));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A day in words, for a screen reader (`Friday, November 6, 2026`). `month` counts from 0. */
|
|
87
|
+
export function longDate(settings: CalendarSettings, year: number, month: number, day: number): string {
|
|
88
|
+
return stripBidiMarks(dateFormat(settings, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }).format(Date.UTC(year, month, day)));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A year as a header label (`2026`, `2026年`). */
|
|
92
|
+
export function yearLabel(settings: CalendarSettings, year: number): string {
|
|
93
|
+
if (settings.english) return localizeDigits(String(year), settings);
|
|
94
|
+
return stripBidiMarks(dateFormat(settings, { year: 'numeric' }).format(Date.UTC(year, 6, 1)));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** An integer in the settings' digits, without grouping (`2026`, `٢٠٢٦`). */
|
|
98
|
+
export function plainInteger(settings: CalendarSettings, n: number): string {
|
|
99
|
+
return localizeDigits(String(Math.trunc(n)), settings);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const yearFirstCache = new Map<string, boolean>();
|
|
103
|
+
|
|
104
|
+
/** Whether the locale writes the year before the month (`2026年9月`, not `September 2026`). */
|
|
105
|
+
export function yearBeforeMonth(settings: CalendarSettings): boolean {
|
|
106
|
+
if (settings.english) return false;
|
|
107
|
+
const key = `${settings.locale}|${settings.numberingSystem}`;
|
|
108
|
+
let first = yearFirstCache.get(key);
|
|
109
|
+
if (first === undefined) {
|
|
110
|
+
const types = dateFormat(settings, { year: 'numeric', month: 'long' })
|
|
111
|
+
.formatToParts(Date.UTC(2026, 8, 15))
|
|
112
|
+
.map((p) => p.type);
|
|
113
|
+
first = types.indexOf('year') < types.indexOf('month');
|
|
114
|
+
yearFirstCache.set(key, first);
|
|
115
|
+
}
|
|
116
|
+
return first;
|
|
117
|
+
}
|
package/src/lib/chart.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// What the kit's SVG charts share: round axis steps, a value axis with zero on a tick, compact
|
|
2
|
+
// numbers, and the hover tip that sits beside the point it reads.
|
|
3
|
+
|
|
4
|
+
/** A round step for `range` in about `ticks` steps: 1, 2 or 5 times a power of ten. */
|
|
5
|
+
export function niceStep(range: number, ticks: number): number {
|
|
6
|
+
const raw = range / Math.max(1, ticks);
|
|
7
|
+
const mag = 10 ** Math.floor(Math.log10(raw || 1));
|
|
8
|
+
const n = raw / mag;
|
|
9
|
+
return (n <= 1 ? 1 : n <= 2 ? 2 : n <= 5 ? 5 : 10) * mag;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A value axis spanning `lo..hi` in about `count` round steps: its bounds widened to whole
|
|
14
|
+
* steps, and its ticks from bound to bound with zero among them. One step serves both, so no
|
|
15
|
+
* tick falls outside the plot.
|
|
16
|
+
*/
|
|
17
|
+
export function valueAxis(lo: number, hi: number, count: number): { min: number; max: number; step: number; ticks: number[] } {
|
|
18
|
+
const step = niceStep(hi - lo || 1, count);
|
|
19
|
+
const min = Math.floor(lo / step) * step;
|
|
20
|
+
const max = Math.ceil(hi / step) * step || step;
|
|
21
|
+
const n = Math.round((max - min) / step);
|
|
22
|
+
// Stepped by index, rounded to the step's precision: no float drift, and zero lands exactly.
|
|
23
|
+
const digits = Math.max(0, -Math.floor(Math.log10(step)) + 2);
|
|
24
|
+
const ticks = Array.from({ length: n + 1 }, (_, i) => Number((min + i * step).toFixed(digits)) || 0);
|
|
25
|
+
return { min, max, step, ticks };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `1.2k`, `-3.4M`, `0.25`. */
|
|
29
|
+
export function compactNumber(v: number): string {
|
|
30
|
+
const a = Math.abs(v);
|
|
31
|
+
const sign = v < 0 ? '-' : '';
|
|
32
|
+
if (a >= 1e6) return `${sign}${+(a / 1e6).toFixed(1)}M`;
|
|
33
|
+
if (a >= 1e3) return `${sign}${+(a / 1e3).toFixed(1)}k`;
|
|
34
|
+
return `${sign}${+a.toFixed(2)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function escapeXml(s: string): string {
|
|
38
|
+
return s.replace(/[<>&"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', '"': '"' })[c]!);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Fill the tip with a readout and place it beside the point at (x, y), inside the box. */
|
|
42
|
+
export function showTip(tip: HTMLElement, content: string | Node, box: { w: number; h: number }, x: number, y: number): void {
|
|
43
|
+
const wasHidden = tip.hidden;
|
|
44
|
+
if (typeof content === 'string') tip.textContent = content;
|
|
45
|
+
else tip.replaceChildren(content);
|
|
46
|
+
tip.hidden = false;
|
|
47
|
+
// A tip that was hidden appears in place instead of gliding from where it last was.
|
|
48
|
+
tip.toggleAttribute('data-instant', wasHidden);
|
|
49
|
+
const tw = tip.offsetWidth;
|
|
50
|
+
const th = tip.offsetHeight;
|
|
51
|
+
const GAP = 16;
|
|
52
|
+
const left = x + GAP + tw <= box.w ? x + GAP : Math.max(0, x - GAP - tw);
|
|
53
|
+
tip.style.left = `${Math.round(left)}px`;
|
|
54
|
+
tip.style.top = `${Math.round(Math.max(0, Math.min(box.h - th, y - th / 2)))}px`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
58
|
+
|
|
59
|
+
/** A chart's root (sized by its parent), its SVG and its tip, redrawn as the root resizes. */
|
|
60
|
+
export function chartFrame(rootSlot: string, tipSlot: string, className: string | undefined, draw: () => void): { el: HTMLElement; svg: SVGSVGElement; tip: HTMLElement; disconnect(): void } {
|
|
61
|
+
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
62
|
+
const tip = document.createElement('div');
|
|
63
|
+
tip.dataset.slot = tipSlot;
|
|
64
|
+
tip.hidden = true;
|
|
65
|
+
const el = document.createElement('div');
|
|
66
|
+
el.dataset.slot = rootSlot;
|
|
67
|
+
if (className) el.className = className;
|
|
68
|
+
el.append(svg, tip);
|
|
69
|
+
const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(draw) : null;
|
|
70
|
+
ro?.observe(el);
|
|
71
|
+
return { el, svg, tip, disconnect: () => ro?.disconnect() };
|
|
72
|
+
}
|
package/src/lib/color.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Color math shared by every layer (core defaults, renderer chrome, UI kit tokens, widget).
|
|
2
|
+
// Pure string/number work with no DOM dependency, so it is safe for the headless core.
|
|
3
|
+
|
|
4
|
+
interface Rgb {
|
|
5
|
+
r: number;
|
|
6
|
+
g: number;
|
|
7
|
+
b: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Parse `#RGB` / `#RRGGBB` / `#RRGGBBAA` / `rgb()` / `rgba()` into 0-255 channels. */
|
|
11
|
+
export function parseRgb(color: string): Rgb | null {
|
|
12
|
+
const s = color.trim();
|
|
13
|
+
let m = /^#([0-9a-f]{3})$/i.exec(s);
|
|
14
|
+
if (m) {
|
|
15
|
+
const h = m[1]!;
|
|
16
|
+
return {
|
|
17
|
+
r: parseInt(h[0]! + h[0]!, 16),
|
|
18
|
+
g: parseInt(h[1]! + h[1]!, 16),
|
|
19
|
+
b: parseInt(h[2]! + h[2]!, 16),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
m = /^#([0-9a-f]{6})[0-9a-f]{0,2}$/i.exec(s);
|
|
23
|
+
if (m) {
|
|
24
|
+
const h = m[1]!;
|
|
25
|
+
return {
|
|
26
|
+
r: parseInt(h.slice(0, 2), 16),
|
|
27
|
+
g: parseInt(h.slice(2, 4), 16),
|
|
28
|
+
b: parseInt(h.slice(4, 6), 16),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const rgb = /^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/i.exec(s);
|
|
32
|
+
if (rgb) {
|
|
33
|
+
return { r: parseFloat(rgb[1]!), g: parseFloat(rgb[2]!), b: parseFloat(rgb[3]!) };
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Apply an alpha to any parseable color → an `rgba(...)` string. Unparseable input is
|
|
39
|
+
* returned unchanged so callers can pass through `transparent`, `currentColor`, etc. */
|
|
40
|
+
export function withAlpha(color: string, alpha: number): string {
|
|
41
|
+
const c = parseRgb(color);
|
|
42
|
+
if (!c) return color;
|
|
43
|
+
return `rgba(${c.r},${c.g},${c.b},${alpha})`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Flatten `top` at `alpha` over `base` into an OPAQUE color. Panels and menus float above
|
|
47
|
+
* the chart, so their surface must not let candles show through — a translucent wash is not
|
|
48
|
+
* interchangeable with the flattened result here. */
|
|
49
|
+
export function mix(base: string, top: string, alpha: number): string {
|
|
50
|
+
const b = parseRgb(base);
|
|
51
|
+
const t = parseRgb(top);
|
|
52
|
+
if (!b || !t) return base;
|
|
53
|
+
const c = (x: number, y: number) => Math.round(x + (y - x) * alpha);
|
|
54
|
+
return `rgb(${c(b.r, t.r)},${c(b.g, t.g)},${c(b.b, t.b)})`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** `#rrggbb` for any parseable color; `fallback` when it cannot be parsed (native color
|
|
58
|
+
* inputs only accept six-digit hex). */
|
|
59
|
+
export function toHex6(color: string, fallback = '#888888'): string {
|
|
60
|
+
const c = parseRgb(color);
|
|
61
|
+
if (!c) return fallback;
|
|
62
|
+
const h = (n: number) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, '0');
|
|
63
|
+
return `#${h(c.r)}${h(c.g)}${h(c.b)}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Perceived-dark check (relative luminance below mid-gray). Unparseable input counts as
|
|
67
|
+
* dark — the dark palette is Vela's default, so it is the safer assumption. */
|
|
68
|
+
export function isDarkColor(color: string): boolean {
|
|
69
|
+
const c = parseRgb(color);
|
|
70
|
+
if (!c) return true;
|
|
71
|
+
return 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b < 128;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function luminance({ r, g, b }: Rgb): number {
|
|
75
|
+
const lin = (v: number) => {
|
|
76
|
+
const s = v / 255;
|
|
77
|
+
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
78
|
+
};
|
|
79
|
+
return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The WCAG contrast ratio of two colors, from 1 to 21; null when either cannot be parsed. */
|
|
83
|
+
export function contrastRatio(a: string, b: string): number | null {
|
|
84
|
+
const x = parseRgb(a);
|
|
85
|
+
const y = parseRgb(b);
|
|
86
|
+
if (!x || !y) return null;
|
|
87
|
+
const [hi, lo] = [luminance(x), luminance(y)].sort((p, q) => q - p) as [number, number];
|
|
88
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
89
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Generated from each component's declaration by scripts/component-index.mjs, which the build
|
|
2
|
+
// and `new-component` run: do not edit.
|
|
3
|
+
|
|
4
|
+
/** Every kit component's parts and settings, by name: what `checkTheme` checks a theme's
|
|
5
|
+
* `components` against. */
|
|
6
|
+
export const KIT_COMPONENTS: Readonly<Record<string, { readonly parts: readonly string[]; readonly settings: readonly string[] }>> = {
|
|
7
|
+
badge: {
|
|
8
|
+
parts: ['root'],
|
|
9
|
+
settings: ['radius'],
|
|
10
|
+
},
|
|
11
|
+
'bar-chart': {
|
|
12
|
+
parts: ['root', 'grid', 'label', 'bar', 'marker', 'tip'],
|
|
13
|
+
settings: ['tip-radius', 'tip-shadow', 'tip-z', 'tip-bg'],
|
|
14
|
+
},
|
|
15
|
+
button: {
|
|
16
|
+
parts: ['root', 'icon', 'keys'],
|
|
17
|
+
settings: ['radius', 'height', 'height-xs', 'height-sm', 'height-lg', 'font-size', 'font-size-xs', 'font-size-sm'],
|
|
18
|
+
},
|
|
19
|
+
'callout-bubble': {
|
|
20
|
+
parts: ['root', 'panel', 'title', 'text', 'actions', 'button'],
|
|
21
|
+
settings: ['panel-radius', 'button-radius'],
|
|
22
|
+
},
|
|
23
|
+
checkbox: {
|
|
24
|
+
parts: ['root', 'icon'],
|
|
25
|
+
settings: ['radius'],
|
|
26
|
+
},
|
|
27
|
+
collapsible: {
|
|
28
|
+
parts: ['root', 'trigger', 'icon', 'title', 'count', 'chevron', 'content'],
|
|
29
|
+
settings: ['trigger-height', 'radius'],
|
|
30
|
+
},
|
|
31
|
+
'color-picker': {
|
|
32
|
+
parts: ['root', 'trigger', 'swatch', 'content'],
|
|
33
|
+
settings: ['swatch-ring', 'circle-swatch-radius', 'content-radius'],
|
|
34
|
+
},
|
|
35
|
+
'data-table': {
|
|
36
|
+
parts: ['root', 'table', 'head', 'sort', 'row', 'cell', 'actions', 'empty'],
|
|
37
|
+
settings: ['row-height', 'head-height', 'head-z'],
|
|
38
|
+
},
|
|
39
|
+
'date-picker': {
|
|
40
|
+
parts: ['root', 'head', 'title', 'switch', 'nav', 'week', 'grid', 'cells', 'blank', 'day', 'cell'],
|
|
41
|
+
settings: ['radius', 'today-ring'],
|
|
42
|
+
},
|
|
43
|
+
dialog: {
|
|
44
|
+
parts: ['backdrop', 'positioner', 'content', 'header', 'title', 'close', 'body', 'footer'],
|
|
45
|
+
settings: ['radius'],
|
|
46
|
+
},
|
|
47
|
+
donut: {
|
|
48
|
+
parts: ['root', 'ring', 'track', 'segment', 'figure', 'caption', 'legend', 'item', 'swatch', 'label', 'value'],
|
|
49
|
+
settings: ['swatch-radius'],
|
|
50
|
+
},
|
|
51
|
+
drawer: {
|
|
52
|
+
parts: ['backdrop', 'positioner', 'content', 'grab', 'title', 'body'],
|
|
53
|
+
settings: ['radius', 'edge-radius', 'side-width', 'grab-radius'],
|
|
54
|
+
},
|
|
55
|
+
'dropdown-menu': {
|
|
56
|
+
parts: ['content', 'item', 'icon', 'label', 'hint', 'mark', 'badge', 'star', 'switch', 'arrow', 'separator'],
|
|
57
|
+
settings: ['radius', 'switch-radius'],
|
|
58
|
+
},
|
|
59
|
+
empty: {
|
|
60
|
+
parts: ['root', 'icon', 'title', 'description'],
|
|
61
|
+
settings: ['icon-radius'],
|
|
62
|
+
},
|
|
63
|
+
field: {
|
|
64
|
+
parts: ['grid', 'row', 'span', 'label', 'cell', 'bool', 'stacked', 'stacked-head', 'inline', 'inline-item', 'toggle-label', 'section', 'sep'],
|
|
65
|
+
settings: [],
|
|
66
|
+
},
|
|
67
|
+
'glyph-select': {
|
|
68
|
+
parts: ['root', 'value', 'icon', 'content', 'item'],
|
|
69
|
+
settings: ['radius', 'content-radius', 'focus-ring'],
|
|
70
|
+
},
|
|
71
|
+
'hover-card': {
|
|
72
|
+
parts: ['root', 'heading', 'icon', 'title', 'description', 'body', 'hint'],
|
|
73
|
+
settings: ['radius', 'width', 'wide-width', 'icon-radius', 'shadow'],
|
|
74
|
+
},
|
|
75
|
+
input: {
|
|
76
|
+
parts: ['root', 'control'],
|
|
77
|
+
settings: ['radius', 'focus-ring'],
|
|
78
|
+
},
|
|
79
|
+
'input-group': {
|
|
80
|
+
parts: ['root', 'control', 'addon', 'text'],
|
|
81
|
+
settings: ['radius', 'height', 'height-sm', 'focus-ring'],
|
|
82
|
+
},
|
|
83
|
+
item: {
|
|
84
|
+
parts: ['root', 'media', 'content', 'title', 'description', 'meta', 'actions', 'group'],
|
|
85
|
+
settings: ['radius', 'gap'],
|
|
86
|
+
},
|
|
87
|
+
kbd: {
|
|
88
|
+
parts: ['root'],
|
|
89
|
+
settings: ['radius', 'shadow'],
|
|
90
|
+
},
|
|
91
|
+
'line-chart': {
|
|
92
|
+
parts: ['root', 'label', 'area', 'line', 'dot', 'tip'],
|
|
93
|
+
settings: ['tip-radius', 'tip-shadow', 'tip-z', 'tip-bg', 'stroke-width'],
|
|
94
|
+
},
|
|
95
|
+
'month-grid': {
|
|
96
|
+
parts: ['root', 'weekday', 'day', 'date', 'value', 'detail', 'blank'],
|
|
97
|
+
settings: ['radius', 'day-height', 'gap', 'selected-ring'],
|
|
98
|
+
},
|
|
99
|
+
'number-input': {
|
|
100
|
+
parts: ['root', 'input', 'steps', 'step'],
|
|
101
|
+
settings: ['radius', 'step-radius', 'focus-ring'],
|
|
102
|
+
},
|
|
103
|
+
popover: {
|
|
104
|
+
parts: ['content'],
|
|
105
|
+
settings: [],
|
|
106
|
+
},
|
|
107
|
+
'scatter-chart': {
|
|
108
|
+
parts: ['root', 'grid', 'label', 'point', 'trend', 'focus', 'tip'],
|
|
109
|
+
settings: ['tip-radius', 'tip-shadow', 'tip-z', 'tip-bg'],
|
|
110
|
+
},
|
|
111
|
+
select: {
|
|
112
|
+
parts: ['root', 'trigger', 'value', 'icon', 'sizer', 'native', 'content', 'viewport', 'item', 'rail', 'thumb'],
|
|
113
|
+
settings: ['radius', 'content-radius', 'thumb-radius', 'focus-ring'],
|
|
114
|
+
},
|
|
115
|
+
slider: {
|
|
116
|
+
parts: ['root', 'control', 'track', 'range', 'thumb'],
|
|
117
|
+
settings: ['track-height', 'thumb-size', 'radius', 'focus-ring'],
|
|
118
|
+
},
|
|
119
|
+
'sort-button': {
|
|
120
|
+
parts: ['root', 'icon', 'state'],
|
|
121
|
+
settings: ['radius'],
|
|
122
|
+
},
|
|
123
|
+
sparkline: {
|
|
124
|
+
parts: ['root', 'area', 'baseline', 'line'],
|
|
125
|
+
settings: ['stroke-width'],
|
|
126
|
+
},
|
|
127
|
+
stat: {
|
|
128
|
+
parts: ['root', 'label', 'value', 'sub'],
|
|
129
|
+
settings: [],
|
|
130
|
+
},
|
|
131
|
+
switch: {
|
|
132
|
+
parts: ['root', 'control', 'thumb'],
|
|
133
|
+
settings: ['width', 'height', 'thumb-size', 'width-sm', 'height-sm', 'thumb-size-sm'],
|
|
134
|
+
},
|
|
135
|
+
tabs: {
|
|
136
|
+
parts: ['list', 'trigger', 'icon', 'count', 'label', 'description', 'content'],
|
|
137
|
+
settings: ['track-radius', 'trigger-radius', 'filled-radius', 'soft-radius', 'count-radius'],
|
|
138
|
+
},
|
|
139
|
+
'text-area': {
|
|
140
|
+
parts: ['root', 'input'],
|
|
141
|
+
settings: ['radius', 'focus-ring'],
|
|
142
|
+
},
|
|
143
|
+
'toggle-group': {
|
|
144
|
+
parts: ['root', 'item', 'icon'],
|
|
145
|
+
settings: ['radius', 'radius-sm', 'item-radius', 'item-radius-sm'],
|
|
146
|
+
},
|
|
147
|
+
tooltip: {
|
|
148
|
+
parts: ['positioner', 'content'],
|
|
149
|
+
settings: ['radius'],
|
|
150
|
+
},
|
|
151
|
+
};
|
package/src/lib/css.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Component CSS: written in `css` templates (editors highlight them, stylelint checks them
|
|
2
|
+
// through the postcss-lit syntax) and injected once per document or shadow root.
|
|
3
|
+
import { pageState } from './page';
|
|
4
|
+
import { scopeToKit } from './scope';
|
|
5
|
+
import { injectLayerOrder, injectStyles } from './styles';
|
|
6
|
+
|
|
7
|
+
export { injectLayerOrder, LAYER_ORDER } from './styles';
|
|
8
|
+
|
|
9
|
+
// The texts the tag produced: CSS written in code. A theme loaded from data (a saved JSON, a
|
|
10
|
+
// shared file) holds strings no code wrote, so its CSS is told apart and left out by default.
|
|
11
|
+
// Page-wide: a theme written with one copy of the kit is applied by another.
|
|
12
|
+
const written = pageState('written-css@1', () => new Set<string>());
|
|
13
|
+
|
|
14
|
+
/** Joins a template into its CSS text. Interpolations are tokens (`t.*`) and component
|
|
15
|
+
* settings, which print as `var(--vela-…)`. */
|
|
16
|
+
export function css(strings: TemplateStringsArray, ...values: unknown[]): string {
|
|
17
|
+
let out = strings[0] ?? '';
|
|
18
|
+
for (let i = 0; i < values.length; i += 1) out += String(values[i]) + (strings[i + 1] ?? '');
|
|
19
|
+
written.add(out);
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Whether a CSS text was written with the {@link css} tag, or accepted as if it were. */
|
|
24
|
+
export function isWrittenCss(text: string): boolean {
|
|
25
|
+
return written.has(text);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Accept a CSS text from data as if code wrote it (a host's explicit `allowCss`). */
|
|
29
|
+
export function acceptCss(text: string): void {
|
|
30
|
+
written.add(text);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A component's stylesheet: call the result with the root it renders in, at creation, never
|
|
34
|
+
* at import (a component nobody creates injects nothing). Its rules apply inside kit hosts,
|
|
35
|
+
* their floating layers and themed hosts only: the page's own elements may carry the same
|
|
36
|
+
* part names. */
|
|
37
|
+
export function defineStyles(id: string, text: string): (root?: Document | ShadowRoot) => void {
|
|
38
|
+
// The sheet each root got, so later creations skip the lookup while it stays in place.
|
|
39
|
+
const injected = new WeakMap<Document | ShadowRoot, Element>();
|
|
40
|
+
let scoped: string | undefined;
|
|
41
|
+
return (root = document) => {
|
|
42
|
+
if (injected.get(root)?.isConnected) return;
|
|
43
|
+
injectLayerOrder(root);
|
|
44
|
+
injectStyles(id, (scoped ??= scopeToKit(text)), root);
|
|
45
|
+
const sheet = root.getElementById?.(id) ?? (root instanceof Document ? null : root.querySelector(`#${CSS.escape(id)}`));
|
|
46
|
+
if (sheet) injected.set(root, sheet);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A component's declaration: its name, its parts and its settings. Every link between the
|
|
2
|
+
// name and the component's CSS comes from it: the parts' `data-slot` values, and the settings'
|
|
3
|
+
// custom properties, which a theme or a host's CSS can set.
|
|
4
|
+
import { pageState } from './page';
|
|
5
|
+
import type { Token } from './t';
|
|
6
|
+
|
|
7
|
+
export interface ComponentDef<N extends string, P extends string, S extends string> {
|
|
8
|
+
readonly name: N;
|
|
9
|
+
readonly parts: readonly P[];
|
|
10
|
+
/** A part's `data-slot`: the bare name for `root`, `name-part` otherwise. */
|
|
11
|
+
slot(part: P): string;
|
|
12
|
+
/** Each setting as `var(--vela-<name>-<setting>, <default>)`, for the component's CSS. */
|
|
13
|
+
readonly vars: { readonly [K in S]: string };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A declared component's parts and settings, as a theme can name them. */
|
|
17
|
+
export interface DeclaredComponent {
|
|
18
|
+
readonly parts: readonly string[];
|
|
19
|
+
readonly settings: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const declared = (): Map<string, DeclaredComponent> => pageState('component-defs@1', () => new Map());
|
|
23
|
+
|
|
24
|
+
/** Every component declared on the page so far, the kit's and a page's own, by name. */
|
|
25
|
+
export function declaredComponents(): ReadonlyMap<string, DeclaredComponent> {
|
|
26
|
+
return declared();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function defineComponent<const N extends string, const P extends string, const S extends string = never>(def: {
|
|
30
|
+
name: N;
|
|
31
|
+
parts: readonly P[];
|
|
32
|
+
settings?: { readonly [K in S]: Token | string };
|
|
33
|
+
}): ComponentDef<N, P, S> {
|
|
34
|
+
const vars = {} as Record<string, string>;
|
|
35
|
+
for (const [key, fallback] of Object.entries(def.settings ?? {}) as Array<[string, Token | string]>) {
|
|
36
|
+
vars[key] = `var(--vela-${def.name}-${key}, ${String(fallback)})`;
|
|
37
|
+
}
|
|
38
|
+
declared().set(def.name, { parts: def.parts, settings: Object.keys(vars) });
|
|
39
|
+
return {
|
|
40
|
+
name: def.name,
|
|
41
|
+
parts: def.parts,
|
|
42
|
+
slot: (part) => (part === 'root' ? def.name : `${def.name}-${part}`),
|
|
43
|
+
vars: vars as { readonly [K in S]: string },
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Teardown that outlives an element. A control with a running state machine must be stopped,
|
|
2
|
+
// not only detached: some machines register page-wide listeners that keep running, and keep
|
|
3
|
+
// their memory, after the element is gone. Each such control ties its teardown to its root
|
|
4
|
+
// element, so whoever discards a subtree releases every control in it with one call, whether
|
|
5
|
+
// or not it kept the handles. The kit's dialog, drawer and popover do it when destroyed.
|
|
6
|
+
import { pageState } from './page';
|
|
7
|
+
|
|
8
|
+
const owned = pageState('teardowns@1', () => new WeakMap<Element, Set<() => void>>());
|
|
9
|
+
|
|
10
|
+
/** Tie `teardown` to `el` and return its release, which runs it once: a control's `destroy()`
|
|
11
|
+
* calls the release, and so does {@link disposeWithin} on any of its ancestors. */
|
|
12
|
+
export function ownTeardown(el: Element, teardown: () => void): () => void {
|
|
13
|
+
let set = owned.get(el);
|
|
14
|
+
if (!set) owned.set(el, (set = new Set()));
|
|
15
|
+
const own = set;
|
|
16
|
+
const release = (): void => {
|
|
17
|
+
if (!own.delete(release)) return;
|
|
18
|
+
teardown();
|
|
19
|
+
};
|
|
20
|
+
own.add(release);
|
|
21
|
+
return release;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const releaseAt = (el: Element): void => {
|
|
25
|
+
const set = owned.get(el);
|
|
26
|
+
if (set) for (const release of [...set]) release();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Release every kit control inside `root`, `root` included. Call it on a subtree you are
|
|
30
|
+
* discarding: a dialog body you rebuild, a row you remove. A released control no longer
|
|
31
|
+
* responds, so never call it on content you will show again. */
|
|
32
|
+
export function disposeWithin(root: Element | DocumentFragment | null | undefined): void {
|
|
33
|
+
if (!root) return;
|
|
34
|
+
if (root.nodeType === 1) releaseAt(root as Element);
|
|
35
|
+
for (const el of root.querySelectorAll('*')) releaseAt(el);
|
|
36
|
+
}
|
package/src/lib/h.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The DOM vocabulary of kit components: every element a component builds goes through
|
|
2
|
+
// `h()`, typed by its tag. `slot` writes the `data-slot` a host styles against; class names
|
|
3
|
+
// are layout hooks, not part of the styling contract.
|
|
4
|
+
|
|
5
|
+
export type Child = Node | string | number | null | undefined | false | readonly Child[];
|
|
6
|
+
|
|
7
|
+
type AttrValue = string | number | boolean | null | undefined;
|
|
8
|
+
|
|
9
|
+
export interface ElementProps {
|
|
10
|
+
className?: string;
|
|
11
|
+
/** The part's `data-slot` (take it from the component's `defineComponent`). */
|
|
12
|
+
slot?: string;
|
|
13
|
+
/** `data-*` attributes (`{ icon: 'inline-start' }` → `data-icon="inline-start"`). `true`
|
|
14
|
+
* writes an empty value; `false`, `null` and `undefined` write nothing. */
|
|
15
|
+
data?: Record<string, AttrValue>;
|
|
16
|
+
/** Plain attributes, with the same value rules as `data`. */
|
|
17
|
+
attrs?: Record<string, AttrValue>;
|
|
18
|
+
style?: string;
|
|
19
|
+
text?: string;
|
|
20
|
+
on?: { [K in keyof HTMLElementEventMap]?: (e: HTMLElementEventMap[K]) => void };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function h<K extends keyof HTMLElementTagNameMap>(tag: K, props: ElementProps = {}, ...children: Child[]): HTMLElementTagNameMap[K] {
|
|
24
|
+
const el = document.createElement(tag);
|
|
25
|
+
if (props.className) el.className = props.className;
|
|
26
|
+
if (props.slot) el.setAttribute('data-slot', props.slot);
|
|
27
|
+
if (props.data) for (const [k, v] of Object.entries(props.data)) setAttr(el, `data-${k}`, v);
|
|
28
|
+
if (props.attrs) for (const [k, v] of Object.entries(props.attrs)) setAttr(el, k, v);
|
|
29
|
+
if (props.style) el.style.cssText = props.style;
|
|
30
|
+
if (props.text !== undefined) el.textContent = props.text;
|
|
31
|
+
if (props.on) for (const [type, fn] of Object.entries(props.on)) el.addEventListener(type, fn as EventListener);
|
|
32
|
+
append(el, children);
|
|
33
|
+
return el;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function append(el: Node, children: readonly Child[]): void {
|
|
37
|
+
for (const c of children) {
|
|
38
|
+
if (c == null || c === false) continue;
|
|
39
|
+
if (Array.isArray(c)) append(el, c);
|
|
40
|
+
else if (typeof c === 'string' || typeof c === 'number') el.appendChild(document.createTextNode(String(c)));
|
|
41
|
+
else el.appendChild(c as Node);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Add a part to an element's slot, keeping the ones it has: a shell one component owns and
|
|
46
|
+
* another builds on carries both (`data-slot="popover-content select-content"`). */
|
|
47
|
+
export function addSlot(el: Element, slot: string): void {
|
|
48
|
+
const slots = (el.getAttribute('data-slot') ?? '').split(' ').filter(Boolean);
|
|
49
|
+
if (!slots.includes(slot)) el.setAttribute('data-slot', [...slots, slot].join(' '));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function setAttr(el: Element, name: string, v: AttrValue): void {
|
|
53
|
+
if (v == null || v === false) el.removeAttribute(name);
|
|
54
|
+
else el.setAttribute(name, v === true ? '' : String(v));
|
|
55
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// HEADLESS parts for the primitives with no behavior to run (Button, Badge): what a
|
|
2
|
+
// controller hands its root part — the role, ARIA state and `data-*` hooks as one plain
|
|
3
|
+
// record. The vanilla views apply it with {@link applyProps}; another view layer spreads the
|
|
4
|
+
// same record onto its own elements (`aria-*`, `data-*` and `role` keep their names). The
|
|
5
|
+
// interactive primitives (Tabs, ToggleGroup) run Zag machines instead; see `zag.ts`.
|
|
6
|
+
|
|
7
|
+
/** Attributes for one part. `false` and `undefined` mean "absent"; `true` is the empty value. */
|
|
8
|
+
export type PartProps = Readonly<Record<string, string | number | boolean | undefined>>;
|
|
9
|
+
|
|
10
|
+
/** Write a part's attributes onto its element, removing the ones that turned absent. */
|
|
11
|
+
export function applyProps(el: Element, props: PartProps): void {
|
|
12
|
+
for (const [name, value] of Object.entries(props)) {
|
|
13
|
+
if (value === false || value === undefined) el.removeAttribute(name);
|
|
14
|
+
else el.setAttribute(name, value === true ? '' : String(value));
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/lib/i18n.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// The kit's own words (a stepper's name, the color picker's opacity label): a message is a
|
|
2
|
+
// stable id and its English text, and a translator resolves it. Any object with a `t` method
|
|
3
|
+
// fits, Vela's translator included; without one the kit speaks English.
|
|
4
|
+
|
|
5
|
+
export interface MessageDescriptor {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
readonly defaultMessage: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Translator {
|
|
11
|
+
t(message: MessageDescriptor | string): string;
|
|
12
|
+
/** The locale tag (default English). Names months and weekdays in the date picker. */
|
|
13
|
+
readonly locale?: string;
|
|
14
|
+
/** The numbering system of digits (default `latn`). */
|
|
15
|
+
readonly numberingSystem?: string;
|
|
16
|
+
/** The first day of the week in date pickers (0 Sunday, 1 Monday, 6 Saturday). */
|
|
17
|
+
readonly weekStart?: 0 | 1 | 6;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Declare a message. The build extracts every call with literal arguments into the English
|
|
21
|
+
* catalog, so keep both arguments string literals. */
|
|
22
|
+
export function msg(id: string, defaultMessage: string): MessageDescriptor {
|
|
23
|
+
return Object.freeze({ id, defaultMessage });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A message in the translator's language, or its English text without one. */
|
|
27
|
+
export function textOf(i18n: Translator | null | undefined, message: MessageDescriptor | string): string {
|
|
28
|
+
if (i18n) return i18n.t(message);
|
|
29
|
+
return typeof message === 'string' ? message : message.defaultMessage;
|
|
30
|
+
}
|