@underpin/ui 0.0.0 → 2.1.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/LICENSE +21 -0
- package/README.md +44 -1
- package/components/autocomplete.d.ts +25 -0
- package/components/autocomplete.js +71 -0
- package/components/autocomplete.js.map +1 -0
- package/components/avatar.d.ts +5 -0
- package/components/avatar.js +26 -0
- package/components/avatar.js.map +1 -0
- package/components/badge.d.ts +36 -0
- package/components/badge.js +140 -0
- package/components/badge.js.map +1 -0
- package/components/bar-list.d.ts +133 -0
- package/components/bar-list.js +204 -0
- package/components/bar-list.js.map +1 -0
- package/components/breakdown-card.d.ts +30 -0
- package/components/breakdown-card.js +43 -0
- package/components/breakdown-card.js.map +1 -0
- package/components/bulk-action-bar.d.ts +21 -0
- package/components/bulk-action-bar.js +38 -0
- package/components/bulk-action-bar.js.map +1 -0
- package/components/button-group.d.ts +21 -0
- package/components/button-group.js +32 -0
- package/components/button-group.js.map +1 -0
- package/components/button.d.ts +39 -0
- package/components/button.js +145 -0
- package/components/button.js.map +1 -0
- package/components/calendar.d.ts +27 -0
- package/components/calendar.js +95 -0
- package/components/calendar.js.map +1 -0
- package/components/callout.d.ts +26 -0
- package/components/callout.js +71 -0
- package/components/callout.js.map +1 -0
- package/components/card.d.ts +55 -0
- package/components/card.js +96 -0
- package/components/card.js.map +1 -0
- package/components/chart.d.ts +32 -0
- package/components/chart.js +43 -0
- package/components/chart.js.map +1 -0
- package/components/checkbox.d.ts +15 -0
- package/components/checkbox.js +42 -0
- package/components/checkbox.js.map +1 -0
- package/components/code-block.d.ts +21 -0
- package/components/code-block.js +35 -0
- package/components/code-block.js.map +1 -0
- package/components/collapse.d.ts +14 -0
- package/components/collapse.js +25 -0
- package/components/collapse.js.map +1 -0
- package/components/combobox.d.ts +71 -0
- package/components/combobox.js +155 -0
- package/components/combobox.js.map +1 -0
- package/components/copy-button.d.ts +29 -0
- package/components/copy-button.js +100 -0
- package/components/copy-button.js.map +1 -0
- package/components/copy-input.d.ts +15 -0
- package/components/copy-input.js +51 -0
- package/components/copy-input.js.map +1 -0
- package/components/data-table.d.ts +120 -0
- package/components/data-table.js +336 -0
- package/components/data-table.js.map +1 -0
- package/components/date-picker-trigger.d.ts +12 -0
- package/components/date-picker-trigger.js +30 -0
- package/components/date-picker-trigger.js.map +1 -0
- package/components/date-picker.d.ts +36 -0
- package/components/date-picker.js +70 -0
- package/components/date-picker.js.map +1 -0
- package/components/detail-row.d.ts +51 -0
- package/components/detail-row.js +77 -0
- package/components/detail-row.js.map +1 -0
- package/components/dialog.d.ts +121 -0
- package/components/dialog.js +327 -0
- package/components/dialog.js.map +1 -0
- package/components/disabled-reason.d.ts +31 -0
- package/components/disabled-reason.js +67 -0
- package/components/disabled-reason.js.map +1 -0
- package/components/empty-state.d.ts +41 -0
- package/components/empty-state.js +84 -0
- package/components/empty-state.js.map +1 -0
- package/components/entity-avatar.d.ts +54 -0
- package/components/entity-avatar.js +68 -0
- package/components/entity-avatar.js.map +1 -0
- package/components/form-section.d.ts +22 -0
- package/components/form-section.js +38 -0
- package/components/form-section.js.map +1 -0
- package/components/form.d.ts +199 -0
- package/components/form.js +456 -0
- package/components/form.js.map +1 -0
- package/components/formatted-number.d.ts +21 -0
- package/components/formatted-number.js +13 -0
- package/components/formatted-number.js.map +1 -0
- package/components/heading.d.ts +23 -0
- package/components/heading.js +44 -0
- package/components/heading.js.map +1 -0
- package/components/icon-swap.d.ts +15 -0
- package/components/icon-swap.js +29 -0
- package/components/icon-swap.js.map +1 -0
- package/components/inline-action.d.ts +21 -0
- package/components/inline-action.js +36 -0
- package/components/inline-action.js.map +1 -0
- package/components/input.d.ts +51 -0
- package/components/input.js +52 -0
- package/components/input.js.map +1 -0
- package/components/kbd.d.ts +17 -0
- package/components/kbd.js +34 -0
- package/components/kbd.js.map +1 -0
- package/components/label.d.ts +10 -0
- package/components/label.js +20 -0
- package/components/label.js.map +1 -0
- package/components/menu.d.ts +77 -0
- package/components/menu.js +189 -0
- package/components/menu.js.map +1 -0
- package/components/nav-button.d.ts +34 -0
- package/components/nav-button.js +71 -0
- package/components/nav-button.js.map +1 -0
- package/components/nav.d.ts +39 -0
- package/components/nav.js +68 -0
- package/components/nav.js.map +1 -0
- package/components/number-input.d.ts +16 -0
- package/components/number-input.js +25 -0
- package/components/number-input.js.map +1 -0
- package/components/page.d.ts +14 -0
- package/components/page.js +23 -0
- package/components/page.js.map +1 -0
- package/components/pagination.d.ts +73 -0
- package/components/pagination.js +212 -0
- package/components/pagination.js.map +1 -0
- package/components/ping.d.ts +38 -0
- package/components/ping.js +50 -0
- package/components/ping.js.map +1 -0
- package/components/popover.d.ts +5 -0
- package/components/popover.js +25 -0
- package/components/popover.js.map +1 -0
- package/components/radio-group.d.ts +35 -0
- package/components/radio-group.js +81 -0
- package/components/radio-group.js.map +1 -0
- package/components/relative-time.d.ts +25 -0
- package/components/relative-time.js +51 -0
- package/components/relative-time.js.map +1 -0
- package/components/saved-mark.d.ts +15 -0
- package/components/saved-mark.js +45 -0
- package/components/saved-mark.js.map +1 -0
- package/components/search-input.d.ts +13 -0
- package/components/search-input.js +44 -0
- package/components/search-input.js.map +1 -0
- package/components/section-header.d.ts +83 -0
- package/components/section-header.js +170 -0
- package/components/section-header.js.map +1 -0
- package/components/segmented-control.d.ts +24 -0
- package/components/segmented-control.js +70 -0
- package/components/segmented-control.js.map +1 -0
- package/components/select.d.ts +28 -0
- package/components/select.js +129 -0
- package/components/select.js.map +1 -0
- package/components/shortcut-keys.d.ts +14 -0
- package/components/shortcut-keys.js +32 -0
- package/components/shortcut-keys.js.map +1 -0
- package/components/shortcuts-dialog.d.ts +10 -0
- package/components/shortcuts-dialog.js +92 -0
- package/components/shortcuts-dialog.js.map +1 -0
- package/components/skeleton.d.ts +8 -0
- package/components/skeleton.js +23 -0
- package/components/skeleton.js.map +1 -0
- package/components/sparkline.d.ts +25 -0
- package/components/sparkline.js +70 -0
- package/components/sparkline.js.map +1 -0
- package/components/stack.d.ts +21 -0
- package/components/stack.js +45 -0
- package/components/stack.js.map +1 -0
- package/components/stat-strip.d.ts +43 -0
- package/components/stat-strip.js +151 -0
- package/components/stat-strip.js.map +1 -0
- package/components/steps.d.ts +19 -0
- package/components/steps.js +25 -0
- package/components/steps.js.map +1 -0
- package/components/switch.d.ts +16 -0
- package/components/switch.js +29 -0
- package/components/switch.js.map +1 -0
- package/components/table-toolbar.d.ts +55 -0
- package/components/table-toolbar.js +119 -0
- package/components/table-toolbar.js.map +1 -0
- package/components/table.d.ts +276 -0
- package/components/table.js +572 -0
- package/components/table.js.map +1 -0
- package/components/tabs.d.ts +36 -0
- package/components/tabs.js +120 -0
- package/components/tabs.js.map +1 -0
- package/components/text-link.d.ts +16 -0
- package/components/text-link.js +32 -0
- package/components/text-link.js.map +1 -0
- package/components/textarea.d.ts +10 -0
- package/components/textarea.js +23 -0
- package/components/textarea.js.map +1 -0
- package/components/theme.d.ts +28 -0
- package/components/theme.js +158 -0
- package/components/theme.js.map +1 -0
- package/components/time-series-chart.d.ts +122 -0
- package/components/time-series-chart.js +722 -0
- package/components/time-series-chart.js.map +1 -0
- package/components/toaster.d.ts +3 -0
- package/components/toaster.js +44 -0
- package/components/toaster.js.map +1 -0
- package/components/tooltip.d.ts +64 -0
- package/components/tooltip.js +126 -0
- package/components/tooltip.js.map +1 -0
- package/components/view-picker.d.ts +20 -0
- package/components/view-picker.js +24 -0
- package/components/view-picker.js.map +1 -0
- package/fonts/inter-OFL.txt +93 -0
- package/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/fonts/inter-cyrillic.woff2 +0 -0
- package/fonts/inter-greek-ext.woff2 +0 -0
- package/fonts/inter-greek.woff2 +0 -0
- package/fonts/inter-latin-ext.woff2 +0 -0
- package/fonts/inter-latin.woff2 +0 -0
- package/fonts/inter-vietnamese.woff2 +0 -0
- package/fonts.css +83 -0
- package/lib/animation.d.ts +14 -0
- package/lib/animation.js +20 -0
- package/lib/animation.js.map +1 -0
- package/lib/aria.d.ts +5 -0
- package/lib/aria.js +12 -0
- package/lib/aria.js.map +1 -0
- package/lib/chart-curve.d.ts +13 -0
- package/lib/chart-curve.js +79 -0
- package/lib/chart-curve.js.map +1 -0
- package/lib/classes.d.ts +134 -0
- package/lib/classes.js +152 -0
- package/lib/classes.js.map +1 -0
- package/lib/color.d.ts +25 -0
- package/lib/color.js +149 -0
- package/lib/color.js.map +1 -0
- package/lib/data-table-state.d.ts +87 -0
- package/lib/data-table-state.js +92 -0
- package/lib/data-table-state.js.map +1 -0
- package/lib/day.d.ts +9 -0
- package/lib/day.js +20 -0
- package/lib/day.js.map +1 -0
- package/lib/design-tokens.d.ts +56 -0
- package/lib/design-tokens.js +196 -0
- package/lib/design-tokens.js.map +1 -0
- package/lib/icon.d.ts +17 -0
- package/lib/icon.js +34 -0
- package/lib/icon.js.map +1 -0
- package/lib/icons.d.ts +29 -0
- package/lib/icons.js +34 -0
- package/lib/icons.js.map +1 -0
- package/lib/interactive.d.ts +21 -0
- package/lib/interactive.js +31 -0
- package/lib/interactive.js.map +1 -0
- package/lib/load-more.d.ts +14 -0
- package/lib/load-more.js +27 -0
- package/lib/load-more.js.map +1 -0
- package/lib/media.d.ts +58 -0
- package/lib/media.js +116 -0
- package/lib/media.js.map +1 -0
- package/lib/segmented.d.ts +57 -0
- package/lib/segmented.js +83 -0
- package/lib/segmented.js.map +1 -0
- package/lib/shortcuts.d.ts +94 -0
- package/lib/shortcuts.js +271 -0
- package/lib/shortcuts.js.map +1 -0
- package/lib/slot.d.ts +9 -0
- package/lib/slot.js +18 -0
- package/lib/slot.js.map +1 -0
- package/lib/time-series.d.ts +100 -0
- package/lib/time-series.js +144 -0
- package/lib/time-series.js.map +1 -0
- package/lib/tone.d.ts +6 -0
- package/lib/tone.js +0 -0
- package/lib/variants.d.ts +12 -0
- package/lib/variants.js +14 -0
- package/lib/variants.js.map +1 -0
- package/package.json +122 -3
- package/styles.css +630 -0
- package/tokens.d.ts +104 -0
- package/tokens.js +108 -0
- package/tokens.js.map +1 -0
- package/publish.log +0 -1
package/lib/color.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// src/lib/color.ts
|
|
2
|
+
const THEMES = ["light", "dark"];
|
|
3
|
+
function collapseWhitespace(value) {
|
|
4
|
+
return value.replace(/\s+/g, " ").replace(/\(\s+/g, "(").replace(/\s+\)/g, ")").trim();
|
|
5
|
+
}
|
|
6
|
+
/** The stylesheet without its comments, which quote declarations (`--gray-chroma: 0`) as prose. */
|
|
7
|
+
function stripComments(css) {
|
|
8
|
+
return css.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
9
|
+
}
|
|
10
|
+
function createLookup(css) {
|
|
11
|
+
const code = stripComments(css);
|
|
12
|
+
return (name) => {
|
|
13
|
+
const value = code.match(new RegExp(`--${name}:\\s*([^;]+);`))?.[1];
|
|
14
|
+
if (value === void 0) return;
|
|
15
|
+
return collapseWhitespace(value);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Splits `value` on each top-level `separator` (a comma by default), not those in nested calls. */
|
|
19
|
+
function splitTopLevel(value, separator = ",") {
|
|
20
|
+
const parts = [];
|
|
21
|
+
let depth = 0;
|
|
22
|
+
let current = "";
|
|
23
|
+
for (const character of value) {
|
|
24
|
+
if (character === "(") depth++;
|
|
25
|
+
if (character === ")") depth--;
|
|
26
|
+
if (character === separator && depth === 0) {
|
|
27
|
+
parts.push(current.trim());
|
|
28
|
+
current = "";
|
|
29
|
+
} else current += character;
|
|
30
|
+
}
|
|
31
|
+
parts.push(current.trim());
|
|
32
|
+
return parts;
|
|
33
|
+
}
|
|
34
|
+
/** `value` with every `var(--name)` replaced by what that variable holds, all the way down. */
|
|
35
|
+
function substitute(value, lookup, depth = 0) {
|
|
36
|
+
if (depth > 8) throw new Error(`var() nests too deep in ${value}`);
|
|
37
|
+
return value.replace(/var\(--([a-z0-9-]+)\)/g, (_match, name) => {
|
|
38
|
+
const resolved = lookup(name);
|
|
39
|
+
if (resolved === void 0) throw new Error(`--${name} is not declared`);
|
|
40
|
+
return substitute(resolved, lookup, depth + 1);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** A number, or a `calc()` that multiplies numbers, which is all the stylesheet's colours use. */
|
|
44
|
+
function evaluate(expression) {
|
|
45
|
+
const body = expression.match(/^calc\((.+)\)$/)?.[1];
|
|
46
|
+
const value = (body ? body.split("*") : [expression]).reduce((result, factor) => result * Number.parseFloat(factor.trim()), 1);
|
|
47
|
+
if (Number.isNaN(value)) throw new Error(`Cannot evaluate ${expression}`);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
function readAlpha(value) {
|
|
51
|
+
if (value === void 0) return 1;
|
|
52
|
+
return value.endsWith("%") ? Number.parseFloat(value) / 100 : Number.parseFloat(value);
|
|
53
|
+
}
|
|
54
|
+
/** One side of a colour expression with no `var()` left in it. */
|
|
55
|
+
function parseColor(expression, theme) {
|
|
56
|
+
const [, name, body = ""] = expression.match(/^([a-z-]+)\((.*)\)$/s) ?? [];
|
|
57
|
+
if (name === "light-dark") {
|
|
58
|
+
const [light = "", dark = ""] = splitTopLevel(body);
|
|
59
|
+
return parseColor(theme === "light" ? light : dark, theme);
|
|
60
|
+
}
|
|
61
|
+
if (name === "color-mix") {
|
|
62
|
+
const [space, mixed = "", other] = splitTopLevel(body);
|
|
63
|
+
const [, color = "", strength = ""] = mixed.match(/^(.+)\s+([\d.]+)%$/s) ?? [];
|
|
64
|
+
if (space !== "in oklab" || other !== "transparent" || !color) throw new Error(`Only a colour mixed with transparent in oklab is read: ${expression}`);
|
|
65
|
+
const base = parseColor(color, theme);
|
|
66
|
+
return {
|
|
67
|
+
...base,
|
|
68
|
+
alpha: base.alpha * (Number.parseFloat(strength) / 100)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (name === "oklch") {
|
|
72
|
+
const [channels = "", alpha] = splitTopLevel(body, "/");
|
|
73
|
+
const [lightness = "", chroma = "", hue = ""] = splitTopLevel(channels, " ");
|
|
74
|
+
return {
|
|
75
|
+
lightness: evaluate(lightness),
|
|
76
|
+
chroma: evaluate(chroma),
|
|
77
|
+
hue: evaluate(hue),
|
|
78
|
+
alpha: readAlpha(alpha)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
throw new Error(`Not an oklch colour, pair or mix: ${expression}`);
|
|
82
|
+
}
|
|
83
|
+
/** A token's two sides, following `var()` to a gray setting, a gray step or another token. */
|
|
84
|
+
function readToken(css, name) {
|
|
85
|
+
const lookup = createLookup(css);
|
|
86
|
+
const value = lookup(name);
|
|
87
|
+
if (!value) throw new Error(`--${name} is not declared`);
|
|
88
|
+
const expression = substitute(value, lookup);
|
|
89
|
+
return {
|
|
90
|
+
light: parseColor(expression, "light"),
|
|
91
|
+
dark: parseColor(expression, "dark")
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** OKLCH to gamma-encoded sRGB in 0 to 1, clipped to the gamut. */
|
|
95
|
+
function toSrgb({ lightness, chroma, hue }) {
|
|
96
|
+
const radians = hue * Math.PI / 180;
|
|
97
|
+
const greenRed = chroma * Math.cos(radians);
|
|
98
|
+
const blueYellow = chroma * Math.sin(radians);
|
|
99
|
+
const long = (lightness + .3963377774 * greenRed + .2158037573 * blueYellow) ** 3;
|
|
100
|
+
const medium = (lightness - .1055613458 * greenRed - .0638541728 * blueYellow) ** 3;
|
|
101
|
+
const short = (lightness - .0894841775 * greenRed - 1.291485548 * blueYellow) ** 3;
|
|
102
|
+
return [
|
|
103
|
+
4.0767416621 * long - 3.3077115913 * medium + .2309699292 * short,
|
|
104
|
+
-1.2684380046 * long + 2.6097574011 * medium - .3413193965 * short,
|
|
105
|
+
-.0041960863 * long - .7034186147 * medium + 1.707614701 * short
|
|
106
|
+
].map((channel) => {
|
|
107
|
+
const clipped = Math.min(1, Math.max(0, channel));
|
|
108
|
+
return clipped <= .0031308 ? 12.92 * clipped : 1.055 * clipped ** (1 / 2.4) - .055;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function formatHex(rgb) {
|
|
112
|
+
return `#${rgb.map((channel) => Math.round(channel * 255).toString(16).padStart(2, "0")).join("")}`;
|
|
113
|
+
}
|
|
114
|
+
/** `color` at `alpha` over `ground`, mixed in gamma-encoded sRGB as a browser blends a paint. */
|
|
115
|
+
function blend(color, alpha, ground) {
|
|
116
|
+
return color.map((channel, index) => channel * alpha + (ground[index] ?? 0) * (1 - alpha));
|
|
117
|
+
}
|
|
118
|
+
/** The colour tokens and gray steps the stylesheet declares, in its order. */
|
|
119
|
+
function readColorNames(css) {
|
|
120
|
+
const names = [...stripComments(css).matchAll(/--((?:color-[a-z0-9-]+)|(?:gray-\d+)):/g)].map((match) => match[1] ?? "");
|
|
121
|
+
return [...new Set(names)];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Every opaque colour token and gray step as hex, per theme. A translucent edge is the hex it
|
|
125
|
+
* makes over a card's surface, which is where code that cannot blend (an email) draws its lines.
|
|
126
|
+
* Any other translucent token (a `-subtle` tint, a coloured `-edge`, the scrim) has no single hex
|
|
127
|
+
* and is left out.
|
|
128
|
+
*/
|
|
129
|
+
function readOpaqueTokens(css) {
|
|
130
|
+
const tokens = {
|
|
131
|
+
light: {},
|
|
132
|
+
dark: {}
|
|
133
|
+
};
|
|
134
|
+
const surface = readToken(css, "color-surface");
|
|
135
|
+
for (const name of readColorNames(css)) {
|
|
136
|
+
const value = readToken(css, name);
|
|
137
|
+
const key = name.replace(/^color-/, "");
|
|
138
|
+
const translucent = value.light.alpha < 1 || value.dark.alpha < 1;
|
|
139
|
+
if (translucent && !key.startsWith("edge")) continue;
|
|
140
|
+
for (const theme of THEMES) {
|
|
141
|
+
const color = toSrgb(value[theme]);
|
|
142
|
+
tokens[theme][key] = formatHex(translucent ? blend(color, value[theme].alpha, toSrgb(surface[theme])) : color);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return tokens;
|
|
146
|
+
}
|
|
147
|
+
export { THEMES, formatHex, readColorNames, readOpaqueTokens, readToken, splitTopLevel, toSrgb };
|
|
148
|
+
|
|
149
|
+
//# sourceMappingURL=color.js.map
|
package/lib/color.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"color.js","names":[],"sources":["../../src/lib/color.ts"],"sourcesContent":["/*\n * The stylesheet's colour tokens read as data, for the places that cannot run CSS: an email, an\n * Open Graph image, a JSON config. `styles.css` stays the only place a colour is written.\n */\n\nexport type Rgb = [number, number, number]\nexport type Theme = \"light\" | \"dark\"\n\nexport interface Oklch {\n lightness: number\n chroma: number\n hue: number\n alpha: number\n}\n\n/** What a custom property holds in the stylesheet, by name. */\ntype Lookup = (name: string) => string | undefined\n\nexport const THEMES: Theme[] = [\"light\", \"dark\"]\n\nfunction collapseWhitespace(value: string) {\n return value.replace(/\\s+/g, \" \").replace(/\\(\\s+/g, \"(\").replace(/\\s+\\)/g, \")\").trim()\n}\n\n/** The stylesheet without its comments, which quote declarations (`--gray-chroma: 0`) as prose. */\nfunction stripComments(css: string) {\n return css.replace(/\\/\\*[\\s\\S]*?\\*\\//g, \"\")\n}\n\nfunction createLookup(css: string): Lookup {\n const code = stripComments(css)\n\n return name => {\n const value = code.match(new RegExp(`--${name}:\\\\s*([^;]+);`))?.[1]\n if (value === undefined) return\n return collapseWhitespace(value)\n }\n}\n\n/** Splits `value` on each top-level `separator` (a comma by default), not those in nested calls. */\nexport function splitTopLevel(value: string, separator = \",\") {\n const parts: string[] = []\n let depth = 0\n let current = \"\"\n\n for (const character of value) {\n if (character === \"(\") depth++\n if (character === \")\") depth--\n if (character === separator && depth === 0) {\n parts.push(current.trim())\n current = \"\"\n } else {\n current += character\n }\n }\n\n parts.push(current.trim())\n return parts\n}\n\n/** `value` with every `var(--name)` replaced by what that variable holds, all the way down. */\nfunction substitute(value: string, lookup: Lookup, depth = 0): string {\n if (depth > 8) throw new Error(`var() nests too deep in ${value}`)\n\n return value.replace(/var\\(--([a-z0-9-]+)\\)/g, (_match, name: string) => {\n const resolved = lookup(name)\n if (resolved === undefined) throw new Error(`--${name} is not declared`)\n return substitute(resolved, lookup, depth + 1)\n })\n}\n\n/** A number, or a `calc()` that multiplies numbers, which is all the stylesheet's colours use. */\nfunction evaluate(expression: string) {\n const body = expression.match(/^calc\\((.+)\\)$/)?.[1]\n const factors = body ? body.split(\"*\") : [expression]\n const value = factors.reduce((result, factor) => result * Number.parseFloat(factor.trim()), 1)\n if (Number.isNaN(value)) throw new Error(`Cannot evaluate ${expression}`)\n return value\n}\n\nfunction readAlpha(value: string | undefined) {\n if (value === undefined) return 1\n return value.endsWith(\"%\") ? Number.parseFloat(value) / 100 : Number.parseFloat(value)\n}\n\n/** One side of a colour expression with no `var()` left in it. */\nfunction parseColor(expression: string, theme: Theme): Oklch {\n const [, name, body = \"\"] = expression.match(/^([a-z-]+)\\((.*)\\)$/s) ?? []\n\n if (name === \"light-dark\") {\n const [light = \"\", dark = \"\"] = splitTopLevel(body)\n return parseColor(theme === \"light\" ? light : dark, theme)\n }\n\n // `color-mix(in oklab, <colour> N%, transparent)`: the colour at N% of its strength.\n if (name === \"color-mix\") {\n const [space, mixed = \"\", other] = splitTopLevel(body)\n const [, color = \"\", strength = \"\"] = mixed.match(/^(.+)\\s+([\\d.]+)%$/s) ?? []\n if (space !== \"in oklab\" || other !== \"transparent\" || !color) {\n throw new Error(`Only a colour mixed with transparent in oklab is read: ${expression}`)\n }\n const base = parseColor(color, theme)\n return { ...base, alpha: base.alpha * (Number.parseFloat(strength) / 100) }\n }\n\n if (name === \"oklch\") {\n const [channels = \"\", alpha] = splitTopLevel(body, \"/\")\n const [lightness = \"\", chroma = \"\", hue = \"\"] = splitTopLevel(channels, \" \")\n return {\n lightness: evaluate(lightness),\n chroma: evaluate(chroma),\n hue: evaluate(hue),\n alpha: readAlpha(alpha),\n }\n }\n\n throw new Error(`Not an oklch colour, pair or mix: ${expression}`)\n}\n\n/** A token's two sides, following `var()` to a gray setting, a gray step or another token. */\nexport function readToken(css: string, name: string): Record<Theme, Oklch> {\n const lookup = createLookup(css)\n const value = lookup(name)\n if (!value) throw new Error(`--${name} is not declared`)\n\n const expression = substitute(value, lookup)\n return { light: parseColor(expression, \"light\"), dark: parseColor(expression, \"dark\") }\n}\n\n/** OKLCH to gamma-encoded sRGB in 0 to 1, clipped to the gamut. */\nexport function toSrgb({ lightness, chroma, hue }: Oklch): Rgb {\n const radians = (hue * Math.PI) / 180\n const greenRed = chroma * Math.cos(radians)\n const blueYellow = chroma * Math.sin(radians)\n const long = (lightness + 0.3963377774 * greenRed + 0.2158037573 * blueYellow) ** 3\n const medium = (lightness - 0.1055613458 * greenRed - 0.0638541728 * blueYellow) ** 3\n const short = (lightness - 0.0894841775 * greenRed - 1.291485548 * blueYellow) ** 3\n const linear = [\n 4.0767416621 * long - 3.3077115913 * medium + 0.2309699292 * short,\n -1.2684380046 * long + 2.6097574011 * medium - 0.3413193965 * short,\n -0.0041960863 * long - 0.7034186147 * medium + 1.707614701 * short,\n ]\n\n return linear.map(channel => {\n const clipped = Math.min(1, Math.max(0, channel))\n return clipped <= 0.0031308 ? 12.92 * clipped : 1.055 * clipped ** (1 / 2.4) - 0.055\n }) as Rgb\n}\n\nexport function formatHex(rgb: Rgb) {\n return `#${rgb\n .map(channel =>\n Math.round(channel * 255)\n .toString(16)\n .padStart(2, \"0\"),\n )\n .join(\"\")}`\n}\n\n/** `color` at `alpha` over `ground`, mixed in gamma-encoded sRGB as a browser blends a paint. */\nfunction blend(color: Rgb, alpha: number, ground: Rgb): Rgb {\n return color.map((channel, index) => channel * alpha + (ground[index] ?? 0) * (1 - alpha)) as Rgb\n}\n\n/** The colour tokens and gray steps the stylesheet declares, in its order. */\nexport function readColorNames(css: string) {\n const names = [...stripComments(css).matchAll(/--((?:color-[a-z0-9-]+)|(?:gray-\\d+)):/g)].map(\n match => match[1] ?? \"\",\n )\n return [...new Set(names)]\n}\n\n/**\n * Every opaque colour token and gray step as hex, per theme. A translucent edge is the hex it\n * makes over a card's surface, which is where code that cannot blend (an email) draws its lines.\n * Any other translucent token (a `-subtle` tint, a coloured `-edge`, the scrim) has no single hex\n * and is left out.\n */\nexport function readOpaqueTokens(css: string): Record<Theme, Record<string, string>> {\n const tokens: Record<Theme, Record<string, string>> = { light: {}, dark: {} }\n const surface = readToken(css, \"color-surface\")\n\n for (const name of readColorNames(css)) {\n const value = readToken(css, name)\n const key = name.replace(/^color-/, \"\")\n const translucent = value.light.alpha < 1 || value.dark.alpha < 1\n if (translucent && !key.startsWith(\"edge\")) continue\n\n for (const theme of THEMES) {\n const color = toSrgb(value[theme])\n tokens[theme][key] = formatHex(\n translucent ? blend(color, value[theme].alpha, toSrgb(surface[theme])) : color,\n )\n }\n }\n\n return tokens\n}\n"],"mappings":";AAkBA,MAAa,SAAkB,CAAC,SAAS,MAAM;AAE/C,SAAS,mBAAmB,OAAe;CACzC,OAAO,MAAM,QAAQ,QAAQ,GAAG,CAAC,CAAC,QAAQ,UAAU,GAAG,CAAC,CAAC,QAAQ,UAAU,GAAG,CAAC,CAAC,KAAK;AACvF;;AAGA,SAAS,cAAc,KAAa;CAClC,OAAO,IAAI,QAAQ,qBAAqB,EAAE;AAC5C;AAEA,SAAS,aAAa,KAAqB;CACzC,MAAM,OAAO,cAAc,GAAG;CAE9B,QAAO,SAAQ;EACb,MAAM,QAAQ,KAAK,MAAM,IAAI,OAAO,KAAK,KAAK,cAAc,CAAC,CAAC,GAAG;EACjE,IAAI,UAAU,KAAA,GAAW;EACzB,OAAO,mBAAmB,KAAK;CACjC;AACF;;AAGA,SAAgB,cAAc,OAAe,YAAY,KAAK;CAC5D,MAAM,QAAkB,CAAC;CACzB,IAAI,QAAQ;CACZ,IAAI,UAAU;CAEd,KAAK,MAAM,aAAa,OAAO;EAC7B,IAAI,cAAc,KAAK;EACvB,IAAI,cAAc,KAAK;EACvB,IAAI,cAAc,aAAa,UAAU,GAAG;GAC1C,MAAM,KAAK,QAAQ,KAAK,CAAC;GACzB,UAAU;EACZ,OACE,WAAW;CAE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I,KAC3E;EACF;CACF;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { TableColumnPriority } from "../components/table.js";
|
|
2
|
+
import { ColumnVisibilityState, RowData } from "@tanstack/react-table";
|
|
3
|
+
/**
|
|
4
|
+
* The model behind `DataTable`: its react-table features, the meta a column carries, and the
|
|
5
|
+
* state the app owns. The package never reads the URL: an app keeps `DataTableState` in its
|
|
6
|
+
* router's search params (TanStack Router, nuqs) and hands it back on every change.
|
|
7
|
+
*/
|
|
8
|
+
/** What a column tells the table about its own head and cells. */
|
|
9
|
+
export interface DataTableColumnMeta {
|
|
10
|
+
/** Figures read from the end. */
|
|
11
|
+
align?: "start" | "end";
|
|
12
|
+
/** The column's grid track: `minmax(0,1fr)` (the default) for the name, `6rem` for a figure. */
|
|
13
|
+
width?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Where the column gives way as the table narrows: `medium` below 672px, `low` below 896px,
|
|
16
|
+
* `lowest` below 1152px.
|
|
17
|
+
*/
|
|
18
|
+
priority?: TableColumnPriority;
|
|
19
|
+
/**
|
|
20
|
+
* Turns the column's name into a button that sorts by it. The first click sorts ascending, or
|
|
21
|
+
* descending for `"descending"` (a figure, a date read newest first); the next flips it.
|
|
22
|
+
*/
|
|
23
|
+
sortable?: boolean | "ascending" | "descending";
|
|
24
|
+
/** Whether a skeleton row draws a stand-in here. Off for an actions column. */
|
|
25
|
+
skeleton?: boolean;
|
|
26
|
+
/** The column's name in the column picker, for a column whose header is not a string. */
|
|
27
|
+
label?: string;
|
|
28
|
+
}
|
|
29
|
+
/** The react-table features every `DataTable` registers. */
|
|
30
|
+
export declare const DATA_TABLE_FEATURES: {
|
|
31
|
+
rowSortingFeature: import("@tanstack/react-table").TableFeature;
|
|
32
|
+
rowSelectionFeature: import("@tanstack/react-table").TableFeature;
|
|
33
|
+
columnVisibilityFeature: import("@tanstack/react-table").TableFeature;
|
|
34
|
+
columnMeta: DataTableColumnMeta;
|
|
35
|
+
};
|
|
36
|
+
export type DataTableFeatures = typeof DATA_TABLE_FEATURES;
|
|
37
|
+
/**
|
|
38
|
+
* The column helper for a `DataTable` of `TData` rows. A column's `cell` returns one of the
|
|
39
|
+
* table's cell parts (`TableCell`, `TableIdentityCell`, `TableNumberCell`…), which take the
|
|
40
|
+
* column's `align` and `priority` from its meta.
|
|
41
|
+
*/
|
|
42
|
+
export declare function createDataTableColumnHelper<TData extends RowData>(): import("@tanstack/react-table").ColumnHelper<{
|
|
43
|
+
rowSortingFeature: import("@tanstack/react-table").TableFeature;
|
|
44
|
+
rowSelectionFeature: import("@tanstack/react-table").TableFeature;
|
|
45
|
+
columnVisibilityFeature: import("@tanstack/react-table").TableFeature;
|
|
46
|
+
columnMeta: DataTableColumnMeta;
|
|
47
|
+
}, TData>;
|
|
48
|
+
export interface DataTableSort {
|
|
49
|
+
/** The column's id. */
|
|
50
|
+
id: string;
|
|
51
|
+
desc: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** What a table shows, owned by the app. */
|
|
54
|
+
export interface DataTableState {
|
|
55
|
+
/** 1-based. Read by a paged table, left out by one that loads as it scrolls. */
|
|
56
|
+
page?: number;
|
|
57
|
+
/** Rows on a page. Read by a paged table. */
|
|
58
|
+
perPage?: number;
|
|
59
|
+
/** Left out while the server's own order applies, so no column shows an arrow. */
|
|
60
|
+
sort?: DataTableSort;
|
|
61
|
+
search?: string;
|
|
62
|
+
/** The chosen values of each filtered field, by field. */
|
|
63
|
+
filters?: Record<string, string[]>;
|
|
64
|
+
}
|
|
65
|
+
/** The page size of a paged table whose state names none. */
|
|
66
|
+
export declare const DEFAULT_PER_PAGE = 25;
|
|
67
|
+
/**
|
|
68
|
+
* The state after a change, under the paging rules: a new sort, search, filter or page size
|
|
69
|
+
* starts over on page 1, since the page the reader was on no longer holds the same rows.
|
|
70
|
+
*/
|
|
71
|
+
export declare function changeDataTableState(state: DataTableState, change: Partial<DataTableState>): DataTableState;
|
|
72
|
+
/** How many pages `total` rows fill. An empty table still has its one page. */
|
|
73
|
+
export declare function getPageCount(total: number, perPage: number): number;
|
|
74
|
+
/**
|
|
75
|
+
* The page a table shows for the one its state asks for: page 1 for a page before it, the last
|
|
76
|
+
* page for one past the end (a stale link, rows deleted since).
|
|
77
|
+
*/
|
|
78
|
+
export declare function clampPage(page: number, pageCount: number): number;
|
|
79
|
+
/**
|
|
80
|
+
* A key that changes whenever the rows a table holds stop being the same set: a new page, sort,
|
|
81
|
+
* search or filter. A selection covers one such set.
|
|
82
|
+
*/
|
|
83
|
+
export declare function formatRowSetKey({ page, perPage, sort, search, filters }: DataTableState): string;
|
|
84
|
+
/** The columns the reader hid on this table, saved in this browser. Empty when none are. */
|
|
85
|
+
export declare function readColumnVisibility(storageKey: string): ColumnVisibilityState;
|
|
86
|
+
/** Saves the columns the reader hid. A browser that blocks storage keeps them for the visit. */
|
|
87
|
+
export declare function writeColumnVisibility(storageKey: string, visibility: ColumnVisibilityState): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { columnVisibilityFeature, createColumnHelper, metaHelper, rowSelectionFeature, rowSortingFeature, tableFeatures } from "@tanstack/react-table";
|
|
2
|
+
// src/lib/data-table-state.ts
|
|
3
|
+
/** The react-table features every `DataTable` registers. */
|
|
4
|
+
const DATA_TABLE_FEATURES = tableFeatures({
|
|
5
|
+
rowSortingFeature,
|
|
6
|
+
rowSelectionFeature,
|
|
7
|
+
columnVisibilityFeature,
|
|
8
|
+
columnMeta: metaHelper()
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* The column helper for a `DataTable` of `TData` rows. A column's `cell` returns one of the
|
|
12
|
+
* table's cell parts (`TableCell`, `TableIdentityCell`, `TableNumberCell`…), which take the
|
|
13
|
+
* column's `align` and `priority` from its meta.
|
|
14
|
+
*/
|
|
15
|
+
function createDataTableColumnHelper() {
|
|
16
|
+
return createColumnHelper();
|
|
17
|
+
}
|
|
18
|
+
/** The page size of a paged table whose state names none. */
|
|
19
|
+
const DEFAULT_PER_PAGE = 25;
|
|
20
|
+
function isSameSort(one, other) {
|
|
21
|
+
return one?.id === other?.id && one?.desc === other?.desc;
|
|
22
|
+
}
|
|
23
|
+
function isSameFilters(one, other) {
|
|
24
|
+
return [.../* @__PURE__ */ new Set([...Object.keys(one ?? {}), ...Object.keys(other ?? {})])].every((key) => {
|
|
25
|
+
const left = one?.[key] ?? [];
|
|
26
|
+
const right = other?.[key] ?? [];
|
|
27
|
+
return left.length === right.length && left.every((value) => right.includes(value));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The state after a change, under the paging rules: a new sort, search, filter or page size
|
|
32
|
+
* starts over on page 1, since the page the reader was on no longer holds the same rows.
|
|
33
|
+
*/
|
|
34
|
+
function changeDataTableState(state, change) {
|
|
35
|
+
const next = {
|
|
36
|
+
...state,
|
|
37
|
+
...change
|
|
38
|
+
};
|
|
39
|
+
return "sort" in change && !isSameSort(state.sort, change.sort) || "search" in change && (state.search ?? "") !== (change.search ?? "") || "filters" in change && !isSameFilters(state.filters, change.filters) || "perPage" in change && state.perPage !== change.perPage ? {
|
|
40
|
+
...next,
|
|
41
|
+
page: 1
|
|
42
|
+
} : next;
|
|
43
|
+
}
|
|
44
|
+
/** How many pages `total` rows fill. An empty table still has its one page. */
|
|
45
|
+
function getPageCount(total, perPage) {
|
|
46
|
+
return Math.max(1, Math.ceil(total / perPage));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The page a table shows for the one its state asks for: page 1 for a page before it, the last
|
|
50
|
+
* page for one past the end (a stale link, rows deleted since).
|
|
51
|
+
*/
|
|
52
|
+
function clampPage(page, pageCount) {
|
|
53
|
+
return Math.min(Math.max(1, Math.floor(page)), pageCount);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A key that changes whenever the rows a table holds stop being the same set: a new page, sort,
|
|
57
|
+
* search or filter. A selection covers one such set.
|
|
58
|
+
*/
|
|
59
|
+
function formatRowSetKey({ page, perPage, sort, search, filters }) {
|
|
60
|
+
const filterKey = Object.entries(filters ?? {}).filter(([, values]) => values.length > 0).map(([field, values]) => [field, values.toSorted()]).toSorted(([one], [other]) => String(one).localeCompare(String(other)));
|
|
61
|
+
return JSON.stringify([
|
|
62
|
+
page,
|
|
63
|
+
perPage,
|
|
64
|
+
sort?.id,
|
|
65
|
+
sort?.desc,
|
|
66
|
+
search ?? "",
|
|
67
|
+
filterKey
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
function formatStorageKey(storageKey) {
|
|
71
|
+
return `underpin:data-table:${storageKey}:columns`;
|
|
72
|
+
}
|
|
73
|
+
/** The columns the reader hid on this table, saved in this browser. Empty when none are. */
|
|
74
|
+
function readColumnVisibility(storageKey) {
|
|
75
|
+
try {
|
|
76
|
+
const stored = JSON.parse(localStorage.getItem(formatStorageKey(storageKey)) ?? "{}");
|
|
77
|
+
if (typeof stored !== "object" || stored === null || Array.isArray(stored)) return {};
|
|
78
|
+
return Object.fromEntries(Object.entries(stored).filter((entry) => entry[1] === false));
|
|
79
|
+
} catch {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Saves the columns the reader hid. A browser that blocks storage keeps them for the visit. */
|
|
84
|
+
function writeColumnVisibility(storageKey, visibility) {
|
|
85
|
+
const hidden = Object.fromEntries(Object.entries(visibility).filter(([, shown]) => !shown));
|
|
86
|
+
try {
|
|
87
|
+
localStorage.setItem(formatStorageKey(storageKey), JSON.stringify(hidden));
|
|
88
|
+
} catch {}
|
|
89
|
+
}
|
|
90
|
+
export { DATA_TABLE_FEATURES, DEFAULT_PER_PAGE, changeDataTableState, clampPage, createDataTableColumnHelper, formatRowSetKey, getPageCount, readColumnVisibility, writeColumnVisibility };
|
|
91
|
+
|
|
92
|
+
//# sourceMappingURL=data-table-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-table-state.js","names":[],"sources":["../../src/lib/data-table-state.ts"],"sourcesContent":["import {\n type ColumnVisibilityState,\n columnVisibilityFeature,\n createColumnHelper,\n metaHelper,\n type RowData,\n rowSelectionFeature,\n rowSortingFeature,\n tableFeatures,\n} from \"@tanstack/react-table\"\nimport type { TableColumnPriority } from \"../components/table\"\n\n/**\n * The model behind `DataTable`: its react-table features, the meta a column carries, and the\n * state the app owns. The package never reads the URL: an app keeps `DataTableState` in its\n * router's search params (TanStack Router, nuqs) and hands it back on every change.\n */\n\n/** What a column tells the table about its own head and cells. */\nexport interface DataTableColumnMeta {\n /** Figures read from the end. */\n align?: \"start\" | \"end\"\n /** The column's grid track: `minmax(0,1fr)` (the default) for the name, `6rem` for a figure. */\n width?: string\n /**\n * Where the column gives way as the table narrows: `medium` below 672px, `low` below 896px,\n * `lowest` below 1152px.\n */\n priority?: TableColumnPriority\n /**\n * Turns the column's name into a button that sorts by it. The first click sorts ascending, or\n * descending for `\"descending\"` (a figure, a date read newest first); the next flips it.\n */\n sortable?: boolean | \"ascending\" | \"descending\"\n /** Whether a skeleton row draws a stand-in here. Off for an actions column. */\n skeleton?: boolean\n /** The column's name in the column picker, for a column whose header is not a string. */\n label?: string\n}\n\n/** The react-table features every `DataTable` registers. */\nexport const DATA_TABLE_FEATURES = tableFeatures({\n rowSortingFeature,\n rowSelectionFeature,\n columnVisibilityFeature,\n columnMeta: metaHelper<DataTableColumnMeta>(),\n})\n\nexport type DataTableFeatures = typeof DATA_TABLE_FEATURES\n\n/**\n * The column helper for a `DataTable` of `TData` rows. A column's `cell` returns one of the\n * table's cell parts (`TableCell`, `TableIdentityCell`, `TableNumberCell`…), which take the\n * column's `align` and `priority` from its meta.\n */\nexport function createDataTableColumnHelper<TData extends RowData>() {\n return createColumnHelper<DataTableFeatures, TData>()\n}\n\nexport interface DataTableSort {\n /** The column's id. */\n id: string\n desc: boolean\n}\n\n/** What a table shows, owned by the app. */\nexport interface DataTableState {\n /** 1-based. Read by a paged table, left out by one that loads as it scrolls. */\n page?: number\n /** Rows on a page. Read by a paged table. */\n perPage?: number\n /** Left out while the server's own order applies, so no column shows an arrow. */\n sort?: DataTableSort\n search?: string\n /** The chosen values of each filtered field, by field. */\n filters?: Record<string, string[]>\n}\n\n/** The page size of a paged table whose state names none. */\nexport const DEFAULT_PER_PAGE = 25\n\nfunction isSameSort(one: DataTableSort | undefined, other: DataTableSort | undefined) {\n return one?.id === other?.id && one?.desc === other?.desc\n}\n\nfunction isSameFilters(one: DataTableState[\"filters\"], other: DataTableState[\"filters\"]) {\n const keys = new Set([...Object.keys(one ?? {}), ...Object.keys(other ?? {})])\n\n return [...keys].every(key => {\n const left = one?.[key] ?? []\n const right = other?.[key] ?? []\n return left.length === right.length && left.every(value => right.includes(value))\n })\n}\n\n/**\n * The state after a change, under the paging rules: a new sort, search, filter or page size\n * starts over on page 1, since the page the reader was on no longer holds the same rows.\n */\nexport function changeDataTableState(\n state: DataTableState,\n change: Partial<DataTableState>,\n): DataTableState {\n const next = { ...state, ...change }\n const restarts =\n (\"sort\" in change && !isSameSort(state.sort, change.sort)) ||\n (\"search\" in change && (state.search ?? \"\") !== (change.search ?? \"\")) ||\n (\"filters\" in change && !isSameFilters(state.filters, change.filters)) ||\n (\"perPage\" in change && state.perPage !== change.perPage)\n\n return restarts ? { ...next, page: 1 } : next\n}\n\n/** How many pages `total` rows fill. An empty table still has its one page. */\nexport function getPageCount(total: number, perPage: number) {\n return Math.max(1, Math.ceil(total / perPage))\n}\n\n/**\n * The page a table shows for the one its state asks for: page 1 for a page before it, the last\n * page for one past the end (a stale link, rows deleted since).\n */\nexport function clampPage(page: number, pageCount: number) {\n return Math.min(Math.max(1, Math.floor(page)), pageCount)\n}\n\n/**\n * A key that changes whenever the rows a table holds stop being the same set: a new page, sort,\n * search or filter. A selection covers one such set.\n */\nexport function formatRowSetKey({ page, perPage, sort, search, filters }: DataTableState) {\n // Sorted, so the same filters in another order make the same key, as `isSameFilters` reads them.\n const filterKey = Object.entries(filters ?? {})\n .filter(([, values]) => values.length > 0)\n .map(([field, values]) => [field, values.toSorted()])\n .toSorted(([one], [other]) => String(one).localeCompare(String(other)))\n\n return JSON.stringify([page, perPage, sort?.id, sort?.desc, search ?? \"\", filterKey])\n}\n\nfunction formatStorageKey(storageKey: string) {\n return `underpin:data-table:${storageKey}:columns`\n}\n\n/** The columns the reader hid on this table, saved in this browser. Empty when none are. */\nexport function readColumnVisibility(storageKey: string): ColumnVisibilityState {\n try {\n const stored: unknown = JSON.parse(localStorage.getItem(formatStorageKey(storageKey)) ?? \"{}\")\n if (typeof stored !== \"object\" || stored === null || Array.isArray(stored)) return {}\n\n return Object.fromEntries(\n Object.entries(stored).filter((entry): entry is [string, boolean] => entry[1] === false),\n )\n } catch {\n return {}\n }\n}\n\n/** Saves the columns the reader hid. A browser that blocks storage keeps them for the visit. */\nexport function writeColumnVisibility(storageKey: string, visibility: ColumnVisibilityState) {\n const hidden = Object.fromEntries(Object.entries(visibility).filter(([, shown]) => !shown))\n\n try {\n localStorage.setItem(formatStorageKey(storageKey), JSON.stringify(hidden))\n } catch {}\n}\n"],"mappings":";;;AAyCA,MAAa,sBAAsB,cAAc;CAC/C;CACA;CACA;CACA,YAAY,WAAgC;AAC9C,CAAC;;;;;;AASD,SAAgB,8BAAqD;CACnE,OAAO,mBAA6C;AACtD;;AAsBA,MAAa,mBAAmB;AAEhC,SAAS,WAAW,KAAgC,OAAkC;CACpF,OAAO,KAAK,OAAO,OAAO,MAAM,KAAK,SAAS,OAAO;AACvD;AAEA,SAAS,cAAc,KAAgC,OAAkC;CAGvF,OAAO,CAAC,mBAAG,IAFM,IAAI,CAAC,GAAG,OAAO,KAAK,OAAO,CAAC,CAAC,GAAG,GAAG,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,CAE9D,CAAC,CAAC,CAAC,OAAM,QAAO;EAC5B,MAAM,OAAO,MAAM,QAAQ,CAAC;EAC5B,MAAM,QAAQ,QAAQ,QAAQ,CAAC;EAC/B,OAAO,KAAK,WAAW,MAAM,UAAU,KAAK,OAAM,UAAS,MAAM,SAA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WAAW,QAAQ,MAAM,QAAQ,MAAM,GAAG,OAAO,CAAC;EAEpF,OAAO,OAAO,YACZ,OAAO,QAAQ,MAAM,CAAC,CAAC,QAAQ,UAAsC,MAAM,OAAO,KAAK,CACzF;CACF,QAAQ;EACN,OAAO,CAAC;CACV;AACF;;AAGA,SAAgB,sBAAsB,YAAoB,YAAmC;CAC3F,MAAM,SAAS,OAAO,YAAY,OAAO,QAAQ,UAAU,CAAC,CAAC,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC;CAE1F,IAAI;EACF,aAAa,QAAQ,iBAAiB,UAAU,GAAG,KAAK,UAAU,MAAM,CAAC;CAC3E,QAAQ,CAAC;AACX"}
|
package/lib/day.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A calendar day as "YYYY-MM-DD" — the shape `<input type="date">` uses, and the one a date
|
|
3
|
+
* without a time of day should travel in. Turning it into a `Date` at *local* midnight keeps
|
|
4
|
+
* the day intact: parsing the string directly reads it as UTC, which lands on the day before
|
|
5
|
+
* anywhere west of Greenwich.
|
|
6
|
+
*/
|
|
7
|
+
export declare function dayToDate(day: string | undefined): Date | undefined;
|
|
8
|
+
/** The inverse of `dayToDate`, reading the date's own calendar fields rather than its instant. */
|
|
9
|
+
export declare function dateToDay(date: Date): string;
|
package/lib/day.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/lib/day.ts
|
|
2
|
+
/**
|
|
3
|
+
* A calendar day as "YYYY-MM-DD" — the shape `<input type="date">` uses, and the one a date
|
|
4
|
+
* without a time of day should travel in. Turning it into a `Date` at *local* midnight keeps
|
|
5
|
+
* the day intact: parsing the string directly reads it as UTC, which lands on the day before
|
|
6
|
+
* anywhere west of Greenwich.
|
|
7
|
+
*/
|
|
8
|
+
function dayToDate(day) {
|
|
9
|
+
const [year, month, date] = (day ?? "").split("-").map(Number);
|
|
10
|
+
return year && month && date ? new Date(year, month - 1, date) : void 0;
|
|
11
|
+
}
|
|
12
|
+
/** The inverse of `dayToDate`, reading the date's own calendar fields rather than its instant. */
|
|
13
|
+
function dateToDay(date) {
|
|
14
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
15
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
16
|
+
return `${date.getFullYear()}-${month}-${day}`;
|
|
17
|
+
}
|
|
18
|
+
export { dateToDay, dayToDate };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=day.js.map
|
package/lib/day.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"day.js","names":[],"sources":["../../src/lib/day.ts"],"sourcesContent":["/**\n * A calendar day as \"YYYY-MM-DD\" — the shape `<input type=\"date\">` uses, and the one a date\n * without a time of day should travel in. Turning it into a `Date` at *local* midnight keeps\n * the day intact: parsing the string directly reads it as UTC, which lands on the day before\n * anywhere west of Greenwich.\n */\nexport function dayToDate(day: string | undefined): Date | undefined {\n const [year, month, date] = (day ?? \"\").split(\"-\").map(Number)\n return year && month && date ? new Date(year, month - 1, date) : undefined\n}\n\n/** The inverse of `dayToDate`, reading the date's own calendar fields rather than its instant. */\nexport function dateToDay(date: Date): string {\n const month = String(date.getMonth() + 1).padStart(2, \"0\")\n const day = String(date.getDate()).padStart(2, \"0\")\n return `${date.getFullYear()}-${month}-${day}`\n}\n"],"mappings":";;;;;;;AAMA,SAAgB,UAAU,KAA2C;CACnE,MAAM,CAAC,MAAM,OAAO,SAAS,OAAO,GAAA,CAAI,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM;CAC7D,OAAO,QAAQ,SAAS,OAAO,IAAI,KAAK,MAAM,QAAQ,GAAG,IAAI,IAAI,KAAA;AACnE;;AAGA,SAAgB,UAAU,MAAoB;CAC5C,MAAM,QAAQ,OAAO,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG;CACzD,MAAM,MAAM,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG;CAClD,OAAO,GAAG,KAAK,YAAY,EAAE,GAAG,MAAM,GAAG;AAC3C"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
interface Paint {
|
|
2
|
+
hex: string;
|
|
3
|
+
alpha: number;
|
|
4
|
+
}
|
|
5
|
+
interface ColorToken {
|
|
6
|
+
name: string;
|
|
7
|
+
css: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
/** The Figma variable this one points at, when the CSS value is a bare `var()` of another. */
|
|
10
|
+
alias?: string;
|
|
11
|
+
light: Paint;
|
|
12
|
+
dark: Paint;
|
|
13
|
+
}
|
|
14
|
+
interface ShadowLayer {
|
|
15
|
+
offsetX: number;
|
|
16
|
+
offsetY: number;
|
|
17
|
+
blur: number;
|
|
18
|
+
spread: number;
|
|
19
|
+
/** Drawn inside the box rather than under it. */
|
|
20
|
+
inset?: boolean;
|
|
21
|
+
/** The colour variable the layer is bound to; `light` and `dark` are its resolved values. */
|
|
22
|
+
variable?: string;
|
|
23
|
+
light: Paint;
|
|
24
|
+
dark: Paint;
|
|
25
|
+
}
|
|
26
|
+
interface ShadowToken {
|
|
27
|
+
name: string;
|
|
28
|
+
css: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
layers: ShadowLayer[];
|
|
31
|
+
}
|
|
32
|
+
interface TextToken {
|
|
33
|
+
name: string;
|
|
34
|
+
css: string;
|
|
35
|
+
size: number;
|
|
36
|
+
lineHeight: number;
|
|
37
|
+
}
|
|
38
|
+
interface NumberToken {
|
|
39
|
+
name: string;
|
|
40
|
+
css: string;
|
|
41
|
+
value: number;
|
|
42
|
+
}
|
|
43
|
+
export interface DesignTokens {
|
|
44
|
+
fonts: {
|
|
45
|
+
sans: string;
|
|
46
|
+
mono: string;
|
|
47
|
+
};
|
|
48
|
+
colors: ColorToken[];
|
|
49
|
+
shadows: ShadowToken[];
|
|
50
|
+
text: TextToken[];
|
|
51
|
+
tracking: NumberToken[];
|
|
52
|
+
radius: NumberToken[];
|
|
53
|
+
spacing: NumberToken[];
|
|
54
|
+
}
|
|
55
|
+
export declare function readDesignTokens(css: string, tailwindTheme: string): DesignTokens;
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { formatHex, readColorNames, readToken, splitTopLevel, toSrgb } from "./color.js";
|
|
2
|
+
// src/lib/design-tokens.ts
|
|
3
|
+
const REM = 16;
|
|
4
|
+
const TAILWIND_TEXT = [
|
|
5
|
+
"xs",
|
|
6
|
+
"sm",
|
|
7
|
+
"base",
|
|
8
|
+
"lg",
|
|
9
|
+
"xl",
|
|
10
|
+
"2xl",
|
|
11
|
+
"3xl",
|
|
12
|
+
"4xl"
|
|
13
|
+
];
|
|
14
|
+
const RADIUS_STEPS = [
|
|
15
|
+
"xs",
|
|
16
|
+
"sm",
|
|
17
|
+
"md",
|
|
18
|
+
"lg",
|
|
19
|
+
"xl"
|
|
20
|
+
];
|
|
21
|
+
const SPACING_STEPS = [
|
|
22
|
+
.5,
|
|
23
|
+
1,
|
|
24
|
+
1.5,
|
|
25
|
+
2,
|
|
26
|
+
2.5,
|
|
27
|
+
3,
|
|
28
|
+
3.5,
|
|
29
|
+
4,
|
|
30
|
+
5,
|
|
31
|
+
6,
|
|
32
|
+
8,
|
|
33
|
+
10,
|
|
34
|
+
12
|
|
35
|
+
];
|
|
36
|
+
/**
|
|
37
|
+
* Every custom property in a stylesheet, in source order, with the comment above its group.
|
|
38
|
+
* A comment applies to the declarations under it until a blank line.
|
|
39
|
+
*/
|
|
40
|
+
function readDeclarations(css) {
|
|
41
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
42
|
+
let comment;
|
|
43
|
+
for (const match of css.matchAll(/(\/\*[\s\S]*?\*\/)|(\n\s*\n)|--([a-z0-9-]+):\s*([^;]+);/g)) {
|
|
44
|
+
const [, block, blank, name, value] = match;
|
|
45
|
+
if (block) comment = block.replace(/^\/\*+|\*+\/$/g, "").split("\n").map((line) => line.replace(/^\s*\*\s?/, "").trim()).filter(Boolean).join(" ");
|
|
46
|
+
else if (blank) comment = void 0;
|
|
47
|
+
else if (name && value && !declarations.has(name)) declarations.set(name, {
|
|
48
|
+
value: value.replace(/\s+/g, " ").trim(),
|
|
49
|
+
description: comment
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return declarations;
|
|
53
|
+
}
|
|
54
|
+
function toPaint(oklch) {
|
|
55
|
+
return {
|
|
56
|
+
hex: formatHex(toSrgb(oklch)),
|
|
57
|
+
alpha: Math.round(oklch.alpha * 1e3) / 1e3
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function toPaints(color) {
|
|
61
|
+
return {
|
|
62
|
+
light: toPaint(color.light),
|
|
63
|
+
dark: toPaint(color.dark)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function toFigmaName(name) {
|
|
67
|
+
return name.replace(/^(color|gray|shadow|text|tracking|radius|spacing)-/, "$1/");
|
|
68
|
+
}
|
|
69
|
+
function readPixels(value) {
|
|
70
|
+
return Number.parseFloat(value) * (value.endsWith("rem") ? REM : 1);
|
|
71
|
+
}
|
|
72
|
+
function readShadowLayers(css, name, declarations) {
|
|
73
|
+
const value = declarations.get(name)?.value ?? "";
|
|
74
|
+
return splitTopLevel(value).flatMap((layer) => {
|
|
75
|
+
const nested = layer.match(/^var\(--(shadow-[a-z0-9-]+)\)$/)?.[1];
|
|
76
|
+
if (nested) return readShadowLayers(css, nested, declarations);
|
|
77
|
+
const inset = layer.startsWith("inset ");
|
|
78
|
+
const [, lengths = "", color = ""] = layer.replace(/^inset /, "").match(/^([-\d.px ]+?)\s+((?:var|rgb)\(.+\))$/) ?? [];
|
|
79
|
+
const [offsetX = 0, offsetY = 0, blur = 0, spread = 0] = lengths.trim().split(/\s+/).map(readPixels);
|
|
80
|
+
const variable = color.match(/^var\(--([a-z0-9-]+)\)$/)?.[1];
|
|
81
|
+
if (variable) return [{
|
|
82
|
+
offsetX,
|
|
83
|
+
offsetY,
|
|
84
|
+
blur,
|
|
85
|
+
spread,
|
|
86
|
+
...inset && { inset },
|
|
87
|
+
variable: toFigmaName(variable),
|
|
88
|
+
...toPaints(readToken(css, variable))
|
|
89
|
+
}];
|
|
90
|
+
const [red = 0, green = 0, blue = 0, alpha = "100%"] = color.match(/^rgb\((\d+) (\d+) (\d+) \/ ([\d.]+%)\)$/)?.slice(1) ?? [];
|
|
91
|
+
const rgb = [
|
|
92
|
+
red,
|
|
93
|
+
green,
|
|
94
|
+
blue
|
|
95
|
+
].map((channel) => Number(channel) / 255);
|
|
96
|
+
const paint = {
|
|
97
|
+
hex: formatHex(rgb),
|
|
98
|
+
alpha: Number.parseFloat(alpha) / 100
|
|
99
|
+
};
|
|
100
|
+
return [{
|
|
101
|
+
offsetX,
|
|
102
|
+
offsetY,
|
|
103
|
+
blur,
|
|
104
|
+
spread,
|
|
105
|
+
...inset && { inset },
|
|
106
|
+
light: paint,
|
|
107
|
+
dark: paint
|
|
108
|
+
}];
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** A line height in pixels: a length, a multiplier, a `calc(a / b)` multiplier or a `var()`. */
|
|
112
|
+
function readLineHeight(value, size, declarations) {
|
|
113
|
+
const alias = value.match(/^var\(--([a-z0-9-]+)\)$/)?.[1];
|
|
114
|
+
if (alias) return readLineHeight(declarations.get(alias)?.value ?? "", size, declarations);
|
|
115
|
+
const ratio = value.match(/^calc\(([\d.]+) \/ ([\d.]+)\)$/);
|
|
116
|
+
if (ratio?.[1] && ratio[2]) return Math.round(size * (Number(ratio[1]) / Number(ratio[2])));
|
|
117
|
+
if (/(px|rem)$/.test(value)) return readPixels(value);
|
|
118
|
+
return Math.round(size * Number(value) * 100) / 100;
|
|
119
|
+
}
|
|
120
|
+
function readText(name, declarations) {
|
|
121
|
+
const size = readPixels(declarations.get(`text-${name}`)?.value ?? "");
|
|
122
|
+
const lineHeight = declarations.get(`text-${name}--line-height`)?.value ?? "";
|
|
123
|
+
return {
|
|
124
|
+
name: `text/${name}`,
|
|
125
|
+
css: `var(--text-${name})`,
|
|
126
|
+
size,
|
|
127
|
+
lineHeight: readLineHeight(lineHeight, size, declarations)
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function readFamily(value = "") {
|
|
131
|
+
return value.split(",")[0]?.replaceAll("\"", "").replace(/ Variable$/, "") ?? "";
|
|
132
|
+
}
|
|
133
|
+
function readDesignTokens(css, tailwindTheme) {
|
|
134
|
+
const declarations = readDeclarations(css);
|
|
135
|
+
const tailwind = readDeclarations(tailwindTheme);
|
|
136
|
+
const names = [...declarations.keys()];
|
|
137
|
+
const colorNames = readColorNames(css);
|
|
138
|
+
const colors = colorNames.map((name) => {
|
|
139
|
+
const { value, description } = declarations.get(name) ?? { value: "" };
|
|
140
|
+
const target = value.match(/^var\(--([a-z0-9-]+)\)$/)?.[1];
|
|
141
|
+
return {
|
|
142
|
+
name: toFigmaName(name),
|
|
143
|
+
css: `var(--${name})`,
|
|
144
|
+
...description && { description },
|
|
145
|
+
...target && colorNames.includes(target) && { alias: toFigmaName(target) },
|
|
146
|
+
...toPaints(readToken(css, name))
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
const shadows = names.filter((name) => name.startsWith("shadow-")).map((name) => {
|
|
150
|
+
const description = declarations.get(name)?.description;
|
|
151
|
+
return {
|
|
152
|
+
name: toFigmaName(name),
|
|
153
|
+
css: `var(--${name})`,
|
|
154
|
+
...description && { description },
|
|
155
|
+
layers: readShadowLayers(css, name, declarations)
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
const customText = names.filter((name) => /^text-[a-z0-9]+$/.test(name)).map((name) => name.replace(/^text-/, ""));
|
|
159
|
+
const allText = new Map([...tailwind, ...declarations]);
|
|
160
|
+
const spacingBase = readPixels(tailwind.get("spacing")?.value ?? "");
|
|
161
|
+
return {
|
|
162
|
+
fonts: {
|
|
163
|
+
sans: readFamily(declarations.get("font-sans")?.value),
|
|
164
|
+
mono: readFamily(declarations.get("font-mono")?.value)
|
|
165
|
+
},
|
|
166
|
+
colors,
|
|
167
|
+
shadows,
|
|
168
|
+
text: [...TAILWIND_TEXT, ...customText].map((name) => readText(name, allText)),
|
|
169
|
+
tracking: names.filter((name) => name.startsWith("tracking-")).map((name) => ({
|
|
170
|
+
name: toFigmaName(name),
|
|
171
|
+
css: `var(--${name})`,
|
|
172
|
+
value: Number.parseFloat(declarations.get(name)?.value ?? "")
|
|
173
|
+
})),
|
|
174
|
+
radius: [...RADIUS_STEPS.map((step) => ({
|
|
175
|
+
name: `radius/${step}`,
|
|
176
|
+
css: `var(--radius-${step})`,
|
|
177
|
+
value: readPixels((declarations.get(`radius-${step}`) ?? tailwind.get(`radius-${step}`))?.value ?? "")
|
|
178
|
+
})), {
|
|
179
|
+
name: "radius/full",
|
|
180
|
+
css: "calc(infinity * 1px)",
|
|
181
|
+
value: 9999
|
|
182
|
+
}],
|
|
183
|
+
spacing: [...SPACING_STEPS.map((step) => ({
|
|
184
|
+
name: `spacing/${String(step).replace(".", "-")}`,
|
|
185
|
+
css: `--spacing(${step})`,
|
|
186
|
+
value: step * spacingBase
|
|
187
|
+
})), ...names.filter((name) => name.startsWith("spacing-")).map((name) => ({
|
|
188
|
+
name: toFigmaName(name),
|
|
189
|
+
css: `var(--${name})`,
|
|
190
|
+
value: readPixels(declarations.get(name)?.value ?? "")
|
|
191
|
+
}))]
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
export { readDesignTokens };
|
|
195
|
+
|
|
196
|
+
//# sourceMappingURL=design-tokens.js.map
|