@volvel/ui 0.0.0-stage → 0.7.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 +202 -0
- package/THIRD_PARTY_NOTICES.md +23036 -0
- package/dist/Badge.d.ts +20 -0
- package/dist/Badge.js +35 -0
- package/dist/Button.d.ts +56 -0
- package/dist/Button.js +52 -0
- package/dist/Chart.d.ts +23 -0
- package/dist/Chart.js +98 -0
- package/dist/Combobox.d.ts +45 -0
- package/dist/Combobox.js +42 -0
- package/dist/ContextMenu.d.ts +16 -0
- package/dist/ContextMenu.js +38 -0
- package/dist/DateInput.d.ts +31 -0
- package/dist/DateInput.js +329 -0
- package/dist/Details.d.ts +12 -0
- package/dist/Details.js +9 -0
- package/dist/Dialog.d.ts +44 -0
- package/dist/Dialog.js +95 -0
- package/dist/Dialogs.d.ts +56 -0
- package/dist/Dialogs.js +95 -0
- package/dist/Doc.d.ts +25 -0
- package/dist/Doc.js +118 -0
- package/dist/DocServed.d.ts +6 -0
- package/dist/DocServed.js +154 -0
- package/dist/DocZoom.d.ts +11 -0
- package/dist/DocZoom.js +133 -0
- package/dist/Facts.d.ts +12 -0
- package/dist/Facts.js +10 -0
- package/dist/Fields.d.ts +184 -0
- package/dist/Fields.js +249 -0
- package/dist/Form.d.ts +47 -0
- package/dist/Form.js +189 -0
- package/dist/Grid.d.ts +118 -0
- package/dist/Grid.js +475 -0
- package/dist/HeadingSlot.d.ts +13 -0
- package/dist/HeadingSlot.js +19 -0
- package/dist/JsonView.d.ts +11 -0
- package/dist/JsonView.js +68 -0
- package/dist/LogViewer.d.ts +45 -0
- package/dist/LogViewer.js +299 -0
- package/dist/Markdown.d.ts +21 -0
- package/dist/Markdown.js +27 -0
- package/dist/MarkdownBody.d.ts +6 -0
- package/dist/MarkdownBody.js +14 -0
- package/dist/Menu.d.ts +29 -0
- package/dist/Menu.js +8 -0
- package/dist/MoveButtons.d.ts +19 -0
- package/dist/MoveButtons.js +22 -0
- package/dist/Notice.d.ts +47 -0
- package/dist/Notice.js +50 -0
- package/dist/PageShell.d.ts +42 -0
- package/dist/PageShell.js +14 -0
- package/dist/Pagination.d.ts +13 -0
- package/dist/Pagination.js +9 -0
- package/dist/Panel.d.ts +31 -0
- package/dist/Panel.js +33 -0
- package/dist/Pickers.d.ts +37 -0
- package/dist/Pickers.js +44 -0
- package/dist/Popover.d.ts +24 -0
- package/dist/Popover.js +12 -0
- package/dist/ResultView.d.ts +28 -0
- package/dist/ResultView.js +140 -0
- package/dist/SchemaForm.d.ts +45 -0
- package/dist/SchemaForm.js +626 -0
- package/dist/SideSheet.d.ts +31 -0
- package/dist/SideSheet.js +136 -0
- package/dist/Sparkline.d.ts +14 -0
- package/dist/Sparkline.js +23 -0
- package/dist/Surfaces.d.ts +30 -0
- package/dist/Surfaces.js +34 -0
- package/dist/Switch.d.ts +18 -0
- package/dist/Switch.js +11 -0
- package/dist/Table.d.ts +15 -0
- package/dist/Table.js +6 -0
- package/dist/Tabs.d.ts +32 -0
- package/dist/Tabs.js +11 -0
- package/dist/Term.d.ts +82 -0
- package/dist/Term.js +153 -0
- package/dist/Timeline.d.ts +29 -0
- package/dist/Timeline.js +39 -0
- package/dist/Toast.d.ts +43 -0
- package/dist/Toast.js +140 -0
- package/dist/Tooltip.d.ts +34 -0
- package/dist/Tooltip.js +70 -0
- package/dist/Tree.d.ts +51 -0
- package/dist/Tree.js +233 -0
- package/dist/chartEngine.d.ts +32 -0
- package/dist/chartEngine.js +72 -0
- package/dist/chartTheme.d.ts +41 -0
- package/dist/chartTheme.js +131 -0
- package/dist/contrast.d.ts +27 -0
- package/dist/contrast.js +110 -0
- package/dist/dates.d.ts +61 -0
- package/dist/dates.js +294 -0
- package/dist/drawer/Chips.d.ts +37 -0
- package/dist/drawer/Chips.js +32 -0
- package/dist/drawer/Choices.d.ts +47 -0
- package/dist/drawer/Choices.js +81 -0
- package/dist/drawer/Drawer.d.ts +125 -0
- package/dist/drawer/Drawer.js +150 -0
- package/dist/drawer/EditorBodies.d.ts +95 -0
- package/dist/drawer/EditorBodies.js +169 -0
- package/dist/drawer/NumberFormat.d.ts +36 -0
- package/dist/drawer/NumberFormat.js +98 -0
- package/dist/drawer/SortableRows.d.ts +35 -0
- package/dist/drawer/SortableRows.js +23 -0
- package/dist/drawer/edgeShadows.d.ts +7 -0
- package/dist/drawer/edgeShadows.js +17 -0
- package/dist/drawer/index.d.ts +8 -0
- package/dist/drawer/index.js +8 -0
- package/dist/drawer/useSortable.d.ts +53 -0
- package/dist/drawer/useSortable.js +323 -0
- package/dist/format.d.ts +6 -0
- package/dist/format.js +17 -0
- package/dist/icons.d.ts +20 -0
- package/dist/icons.js +16 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +48 -0
- package/dist/markdownKnowledge.d.ts +45 -0
- package/dist/markdownKnowledge.js +37 -0
- package/dist/overlay.d.ts +7 -0
- package/dist/overlay.js +10 -0
- package/dist/pageIcons.d.ts +12 -0
- package/dist/pageIcons.js +32 -0
- package/dist/presence.d.ts +41 -0
- package/dist/presence.js +108 -0
- package/dist/returnFocus.d.ts +9 -0
- package/dist/returnFocus.js +44 -0
- package/dist/tokens.css +437 -0
- package/dist/type-scale.css +34 -0
- package/dist/useFlip.d.ts +32 -0
- package/dist/useFlip.js +118 -0
- package/fonts/OFL.txt +193 -0
- package/fonts/geist-latin.woff2 +0 -0
- package/fonts/geist-mono-latin.woff2 +0 -0
- package/package.json +52 -3
- package/README.md +0 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { localeOf } from "./dates.js";
|
|
2
|
+
let loading;
|
|
3
|
+
/**
|
|
4
|
+
* Fetches the chart engine the first time a chart needs it. It is large, so
|
|
5
|
+
* it stays out of the page's main script: a page with no chart never loads it.
|
|
6
|
+
*/
|
|
7
|
+
export function loadChartEngine() {
|
|
8
|
+
loading ??= Promise.all([import("vega"), import("vega-lite"), import("vega-interpreter")]).then(([vega, lite, interpreter]) => ({ vega, lite, interpreter }), (failure) => {
|
|
9
|
+
// A failed download is tried again by the next chart.
|
|
10
|
+
loading = undefined;
|
|
11
|
+
throw failure;
|
|
12
|
+
});
|
|
13
|
+
return loading;
|
|
14
|
+
}
|
|
15
|
+
/** Views that take their size from several charts cannot follow the container's width. */
|
|
16
|
+
const COMPOSED = ["facet", "concat", "hconcat", "vconcat", "repeat"];
|
|
17
|
+
/**
|
|
18
|
+
* The spec as it is drawn: with the rows put in as its data when there are
|
|
19
|
+
* any, as wide as its container and of the given height unless it says otherwise.
|
|
20
|
+
*/
|
|
21
|
+
export function chartSpec(spec, data, height) {
|
|
22
|
+
const given = spec;
|
|
23
|
+
const single = !COMPOSED.some((key) => key in given);
|
|
24
|
+
return {
|
|
25
|
+
...(single ? { width: "container", height } : {}),
|
|
26
|
+
...given,
|
|
27
|
+
...(data ? { data: { values: data } } : {}),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** What d3 needs to write numbers the way a locale does, read from Intl so no table is kept. */
|
|
31
|
+
export function numberLocale(locale) {
|
|
32
|
+
let use = "en-US";
|
|
33
|
+
try {
|
|
34
|
+
use = Intl.NumberFormat.supportedLocalesOf(locale)[0] ?? "en-US";
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
use = "en-US";
|
|
38
|
+
}
|
|
39
|
+
const parts = new Intl.NumberFormat(use, { minimumFractionDigits: 2 }).formatToParts(1234567.5);
|
|
40
|
+
const decimal = parts.find((part) => part.type === "decimal")?.value ?? ".";
|
|
41
|
+
const thousands = parts.find((part) => part.type === "group")?.value ?? ",";
|
|
42
|
+
const integers = parts.filter((part) => part.type === "integer");
|
|
43
|
+
const last = integers[integers.length - 1]?.value.length ?? 3;
|
|
44
|
+
const money = new Intl.NumberFormat(use, { style: "currency", currency: "USD" }).formatToParts(1);
|
|
45
|
+
const symbolAt = money.findIndex((part) => part.type === "currency");
|
|
46
|
+
const integerAt = money.findIndex((part) => part.type === "integer");
|
|
47
|
+
const symbol = money[symbolAt]?.value ?? "$";
|
|
48
|
+
const space = money.find((part) => part.type === "literal")?.value ?? "";
|
|
49
|
+
return {
|
|
50
|
+
decimal,
|
|
51
|
+
thousands,
|
|
52
|
+
grouping: [last],
|
|
53
|
+
currency: symbolAt < integerAt ? [symbol, ""] : ["", `${space}${symbol}`],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Compiles a Vega-Lite spec and builds its view. Expressions are parsed to a
|
|
58
|
+
* syntax tree and run by the interpreter, so nothing is evaluated from a
|
|
59
|
+
* string and the page needs no `unsafe-eval` in its content security policy.
|
|
60
|
+
* Throws when the spec does not compile.
|
|
61
|
+
*/
|
|
62
|
+
export function createChartView(engine, spec, config, container, locale) {
|
|
63
|
+
// Axes and labels write numbers as the reader does (D38).
|
|
64
|
+
engine.vega.formatLocale(numberLocale(locale ?? localeOf()));
|
|
65
|
+
const compiled = engine.lite.compile(spec, { config }).spec;
|
|
66
|
+
const runtime = engine.vega.parse(compiled, undefined, { ast: true });
|
|
67
|
+
return new engine.vega.View(runtime, {
|
|
68
|
+
expr: engine.interpreter.expressionInterpreter,
|
|
69
|
+
renderer: "svg",
|
|
70
|
+
...(container ? { container, hover: true } : {}),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Config } from "vega-lite";
|
|
2
|
+
/** The theme tokens a chart is drawn with, as the values they have where the chart sits. */
|
|
3
|
+
export interface ChartTokens {
|
|
4
|
+
ink: string;
|
|
5
|
+
inkMuted: string;
|
|
6
|
+
line: string;
|
|
7
|
+
action: string;
|
|
8
|
+
actionSoft: string;
|
|
9
|
+
attention: string;
|
|
10
|
+
success: string;
|
|
11
|
+
danger: string;
|
|
12
|
+
font: string;
|
|
13
|
+
/** What the chart sits on: the fill of a ring that marks a point, and the edge between slices. */
|
|
14
|
+
surface?: string;
|
|
15
|
+
/** The console's type scale where the chart sits (body size over 14 px): 1 where nothing sets it. */
|
|
16
|
+
typeScale?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Stands in a spec for the color of what the chart sits on. A spec is JSON and a chart's SVG cannot
|
|
20
|
+
* hold `var()`, so the chart puts the theme's value where this is written when it draws.
|
|
21
|
+
*/
|
|
22
|
+
export declare const SURFACE = "chart-surface";
|
|
23
|
+
/**
|
|
24
|
+
* Reads the tokens as they apply to one element, so that a chart inside a
|
|
25
|
+
* themed part of the page takes that theme. A chart is drawn as SVG
|
|
26
|
+
* attributes, which cannot hold `var()`: the values have to be read.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readChartTokens(element: Element): ChartTokens;
|
|
29
|
+
/**
|
|
30
|
+
* The colors of a chart's series, in the order they are given out. Neighbours differ in hue as
|
|
31
|
+
* well as in lightness (teal, amber, violet, green, coral, blue, gray), so that two series drawn
|
|
32
|
+
* side by side are never two greens, in either theme. The first is the theme's action color.
|
|
33
|
+
*/
|
|
34
|
+
export declare function seriesColors(tokens: ChartTokens): string[];
|
|
35
|
+
/**
|
|
36
|
+
* The Vega-Lite configuration that makes a chart look like the rest of the
|
|
37
|
+
* kit, and as the boards draw one: a few quiet rules and short labels, no box, no
|
|
38
|
+
* axis lines, rounded bars with room between them, a line with rings on its points.
|
|
39
|
+
* A token with no value is left out, so the engine's default applies.
|
|
40
|
+
*/
|
|
41
|
+
export declare function chartConfig(tokens: ChartTokens): Config;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const variables = {
|
|
2
|
+
ink: "--vx-ink",
|
|
3
|
+
inkMuted: "--vx-ink-muted",
|
|
4
|
+
line: "--vx-line",
|
|
5
|
+
action: "--vx-action",
|
|
6
|
+
actionSoft: "--vx-action-soft",
|
|
7
|
+
attention: "--vx-attention",
|
|
8
|
+
success: "--vx-success",
|
|
9
|
+
danger: "--vx-danger",
|
|
10
|
+
font: "--vx-font",
|
|
11
|
+
surface: "--vx-surface",
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Stands in a spec for the color of what the chart sits on. A spec is JSON and a chart's SVG cannot
|
|
15
|
+
* hold `var()`, so the chart puts the theme's value where this is written when it draws.
|
|
16
|
+
*/
|
|
17
|
+
export const SURFACE = "chart-surface";
|
|
18
|
+
/**
|
|
19
|
+
* Reads the tokens as they apply to one element, so that a chart inside a
|
|
20
|
+
* themed part of the page takes that theme. A chart is drawn as SVG
|
|
21
|
+
* attributes, which cannot hold `var()`: the values have to be read.
|
|
22
|
+
*/
|
|
23
|
+
export function readChartTokens(element) {
|
|
24
|
+
const style = getComputedStyle(element);
|
|
25
|
+
const tokens = {};
|
|
26
|
+
for (const [token, variable] of Object.entries(variables)) {
|
|
27
|
+
tokens[token] = style.getPropertyValue(variable).trim();
|
|
28
|
+
}
|
|
29
|
+
tokens.typeScale = readTypeScale(style.getPropertyValue("--vx-type-scale"));
|
|
30
|
+
return tokens;
|
|
31
|
+
}
|
|
32
|
+
/** `calc(18 / 14)` or `1.2857`, as the stylesheet gives the type scale: a number, 1 when there is none. */
|
|
33
|
+
function readTypeScale(value) {
|
|
34
|
+
const ratio = /calc\(\s*([\d.]+)\s*\/\s*([\d.]+)\s*\)/.exec(value);
|
|
35
|
+
const scale = ratio ? Number(ratio[1]) / Number(ratio[2]) : Number.parseFloat(value);
|
|
36
|
+
return Number.isFinite(scale) && scale > 0 ? scale : 1;
|
|
37
|
+
}
|
|
38
|
+
/** Whether a color is a light one: the ink of a dark theme is. */
|
|
39
|
+
function isLight(color) {
|
|
40
|
+
const found = /^#([0-9a-f]{6})$/i.exec(color.trim());
|
|
41
|
+
if (!found)
|
|
42
|
+
return false;
|
|
43
|
+
const rgb = Number.parseInt(found[1], 16);
|
|
44
|
+
const [r, g, b] = [(rgb >> 16) & 255, (rgb >> 8) & 255, rgb & 255];
|
|
45
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b > 140;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The colors of a chart's series, in the order they are given out. Neighbours differ in hue as
|
|
49
|
+
* well as in lightness (teal, amber, violet, green, coral, blue, gray), so that two series drawn
|
|
50
|
+
* side by side are never two greens, in either theme. The first is the theme's action color.
|
|
51
|
+
*/
|
|
52
|
+
export function seriesColors(tokens) {
|
|
53
|
+
const dark = isLight(tokens.ink);
|
|
54
|
+
const fixed = dark
|
|
55
|
+
? { violet: "#a78bfa", blue: "#7aa7ff" }
|
|
56
|
+
: { violet: "#6741d9", blue: "#2f5fd0" };
|
|
57
|
+
return [
|
|
58
|
+
tokens.action,
|
|
59
|
+
tokens.attention,
|
|
60
|
+
fixed.violet,
|
|
61
|
+
tokens.success,
|
|
62
|
+
tokens.danger,
|
|
63
|
+
fixed.blue,
|
|
64
|
+
tokens.inkMuted,
|
|
65
|
+
].filter((color) => color !== "");
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The Vega-Lite configuration that makes a chart look like the rest of the
|
|
69
|
+
* kit, and as the boards draw one: a few quiet rules and short labels, no box, no
|
|
70
|
+
* axis lines, rounded bars with room between them, a line with rings on its points.
|
|
71
|
+
* A token with no value is left out, so the engine's default applies.
|
|
72
|
+
*/
|
|
73
|
+
export function chartConfig(tokens) {
|
|
74
|
+
const grow = (size) => Math.round(size * (tokens.typeScale ?? 1));
|
|
75
|
+
const set = (key, value) => (value === "" ? {} : { [key]: value });
|
|
76
|
+
const labels = { ...set("labelColor", tokens.inkMuted), ...set("titleColor", tokens.ink) };
|
|
77
|
+
const category = seriesColors(tokens);
|
|
78
|
+
const ramp = [tokens.actionSoft, tokens.action].filter((color) => color !== "");
|
|
79
|
+
const ground = tokens.surface ?? "";
|
|
80
|
+
const quiet = {
|
|
81
|
+
...set("labelColor", tokens.inkMuted),
|
|
82
|
+
...set("titleColor", tokens.inkMuted),
|
|
83
|
+
labelFontSize: grow(10),
|
|
84
|
+
labelPadding: 6,
|
|
85
|
+
labelOverlap: "greedy",
|
|
86
|
+
labelSeparation: 8,
|
|
87
|
+
titleFontSize: grow(11),
|
|
88
|
+
titleFontWeight: "normal",
|
|
89
|
+
titlePadding: 10,
|
|
90
|
+
domain: false,
|
|
91
|
+
ticks: false,
|
|
92
|
+
...set("gridColor", tokens.line),
|
|
93
|
+
gridWidth: 1,
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
background: "transparent",
|
|
97
|
+
...set("font", tokens.font),
|
|
98
|
+
mark: set("color", tokens.action),
|
|
99
|
+
text: { ...set("color", tokens.ink), fontSize: grow(11) },
|
|
100
|
+
rule: set("color", tokens.inkMuted),
|
|
101
|
+
view: { stroke: null },
|
|
102
|
+
axis: quiet,
|
|
103
|
+
axisY: { tickCount: 4, grid: true },
|
|
104
|
+
axisX: { grid: false, labelPadding: 8 },
|
|
105
|
+
bar: { cornerRadiusTopLeft: 4, cornerRadiusTopRight: 4, fillOpacity: 0.9 },
|
|
106
|
+
line: { strokeWidth: 2.2, strokeJoin: "round", strokeCap: "round" },
|
|
107
|
+
area: { fillOpacity: 0.08, line: { strokeWidth: 2.2 } },
|
|
108
|
+
rect: { cornerRadius: 3, ...(ground === "" ? {} : { stroke: ground }), strokeWidth: 2 },
|
|
109
|
+
arc: { ...(ground === "" ? {} : { stroke: ground }), strokeWidth: 2 },
|
|
110
|
+
scale: { bandPaddingInner: 0.35, bandPaddingOuter: 0.15 },
|
|
111
|
+
legend: {
|
|
112
|
+
...labels,
|
|
113
|
+
...set("titleColor", tokens.inkMuted),
|
|
114
|
+
labelFontSize: grow(11),
|
|
115
|
+
titleFontSize: grow(11),
|
|
116
|
+
titleFontWeight: "normal",
|
|
117
|
+
symbolType: "circle",
|
|
118
|
+
symbolSize: 70,
|
|
119
|
+
orient: "top",
|
|
120
|
+
direction: "horizontal",
|
|
121
|
+
offset: 8,
|
|
122
|
+
padding: 4,
|
|
123
|
+
},
|
|
124
|
+
header: labels,
|
|
125
|
+
title: { ...set("color", tokens.ink), ...set("subtitleColor", tokens.inkMuted) },
|
|
126
|
+
range: {
|
|
127
|
+
...(category.length > 0 ? { category } : {}),
|
|
128
|
+
...(ramp.length === 2 ? { ramp, heatmap: ramp } : {}),
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contrast between two colors, as WCAG 2 measures it: from 1 (the same) to 21
|
|
3
|
+
* (black on white). Text needs 4.5 or more, large text and the edges of
|
|
4
|
+
* controls 3 or more. A color with some transparency is laid over the ground it
|
|
5
|
+
* is drawn on first (and that over the page, `white` unless said), so a faint ink
|
|
6
|
+
* is judged as the faint ink it is.
|
|
7
|
+
*/
|
|
8
|
+
export declare function contrastRatio(a: string, b: string, page?: string): number | undefined;
|
|
9
|
+
/** The least each kind of pair needs. */
|
|
10
|
+
export declare const CONTRAST_NEEDED: {
|
|
11
|
+
readonly text: 4.5;
|
|
12
|
+
readonly edge: 3;
|
|
13
|
+
};
|
|
14
|
+
/** A color's relative luminance when it is solid. Anything that is not a color gives nothing. */
|
|
15
|
+
export declare function luminance(color: string): number | undefined;
|
|
16
|
+
export interface Rgba {
|
|
17
|
+
r: number;
|
|
18
|
+
g: number;
|
|
19
|
+
b: number;
|
|
20
|
+
a: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Reads a CSS color written as a name, `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()`, `rgba()`,
|
|
24
|
+
* `hsl()` or `hsla()` (commas or spaces, `/` before an alpha). Anything else, a `var()` or a color
|
|
25
|
+
* function this does not know, gives nothing: it is not checked, and is said not to be.
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseColor(input: string): Rgba | undefined;
|
package/dist/contrast.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contrast between two colors, as WCAG 2 measures it: from 1 (the same) to 21
|
|
3
|
+
* (black on white). Text needs 4.5 or more, large text and the edges of
|
|
4
|
+
* controls 3 or more. A color with some transparency is laid over the ground it
|
|
5
|
+
* is drawn on first (and that over the page, `white` unless said), so a faint ink
|
|
6
|
+
* is judged as the faint ink it is.
|
|
7
|
+
*/
|
|
8
|
+
export function contrastRatio(a, b, page = "#ffffff") {
|
|
9
|
+
const ink = parseColor(a);
|
|
10
|
+
const ground = parseColor(b);
|
|
11
|
+
const behind = parseColor(page) ?? { r: 255, g: 255, b: 255, a: 1 };
|
|
12
|
+
if (!ink || !ground)
|
|
13
|
+
return undefined;
|
|
14
|
+
const solidGround = over(ground, { ...behind, a: 1 });
|
|
15
|
+
const one = relative(over(ink, solidGround));
|
|
16
|
+
const two = relative(solidGround);
|
|
17
|
+
const [light, dark] = one > two ? [one, two] : [two, one];
|
|
18
|
+
return (light + 0.05) / (dark + 0.05);
|
|
19
|
+
}
|
|
20
|
+
/** The least each kind of pair needs. */
|
|
21
|
+
export const CONTRAST_NEEDED = { text: 4.5, edge: 3 };
|
|
22
|
+
/** A color's relative luminance when it is solid. Anything that is not a color gives nothing. */
|
|
23
|
+
export function luminance(color) {
|
|
24
|
+
const parsed = parseColor(color);
|
|
25
|
+
return parsed ? relative(over(parsed, { r: 255, g: 255, b: 255, a: 1 })) : undefined;
|
|
26
|
+
}
|
|
27
|
+
const relative = (color) => {
|
|
28
|
+
const [r, g, b] = [color.r, color.g, color.b].map((value) => {
|
|
29
|
+
const c = value / 255;
|
|
30
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
31
|
+
});
|
|
32
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
33
|
+
};
|
|
34
|
+
/** `top` laid over a solid `ground`. */
|
|
35
|
+
const over = (top, ground) => ({
|
|
36
|
+
r: top.r * top.a + ground.r * (1 - top.a),
|
|
37
|
+
g: top.g * top.a + ground.g * (1 - top.a),
|
|
38
|
+
b: top.b * top.a + ground.b * (1 - top.a),
|
|
39
|
+
a: 1,
|
|
40
|
+
});
|
|
41
|
+
const NAMED = new Map(
|
|
42
|
+
// british-ok: the CSS color names are spelled as CSS spells them.
|
|
43
|
+
"aliceblue:f0f8ff antiquewhite:faebd7 aqua:00ffff aquamarine:7fffd4 azure:f0ffff beige:f5f5dc bisque:ffe4c4 black:000000 blanchedalmond:ffebcd blue:0000ff blueviolet:8a2be2 brown:a52a2a burlywood:deb887 cadetblue:5f9ea0 chartreuse:7fff00 chocolate:d2691e coral:ff7f50 cornflowerblue:6495ed cornsilk:fff8dc crimson:dc143c cyan:00ffff darkblue:00008b darkcyan:008b8b darkgoldenrod:b8860b darkgray:a9a9a9 darkgreen:006400 darkgrey:a9a9a9 darkkhaki:bdb76b darkmagenta:8b008b darkolivegreen:556b2f darkorange:ff8c00 darkorchid:9932cc darkred:8b0000 darksalmon:e9967a darkseagreen:8fbc8f darkslateblue:483d8b darkslategray:2f4f4f darkslategrey:2f4f4f darkturquoise:00ced1 darkviolet:9400d3 deeppink:ff1493 deepskyblue:00bfff dimgray:696969 dimgrey:696969 dodgerblue:1e90ff firebrick:b22222 floralwhite:fffaf0 forestgreen:228b22 fuchsia:ff00ff gainsboro:dcdcdc ghostwhite:f8f8ff gold:ffd700 goldenrod:daa520 gray:808080 green:008000 greenyellow:adff2f grey:808080 honeydew:f0fff0 hotpink:ff69b4 indianred:cd5c5c indigo:4b0082 ivory:fffff0 khaki:f0e68c lavender:e6e6fa lavenderblush:fff0f5 lawngreen:7cfc00 lemonchiffon:fffacd lightblue:add8e6 lightcoral:f08080 lightcyan:e0ffff lightgoldenrodyellow:fafad2 lightgray:d3d3d3 lightgreen:90ee90 lightgrey:d3d3d3 lightpink:ffb6c1 lightsalmon:ffa07a lightseagreen:20b2aa lightskyblue:87cefa lightslategray:778899 lightslategrey:778899 lightsteelblue:b0c4de lightyellow:ffffe0 lime:00ff00 limegreen:32cd32 linen:faf0e6 magenta:ff00ff maroon:800000 mediumaquamarine:66cdaa mediumblue:0000cd mediumorchid:ba55d3 mediumpurple:9370db mediumseagreen:3cb371 mediumslateblue:7b68ee mediumspringgreen:00fa9a mediumturquoise:48d1cc mediumvioletred:c71585 midnightblue:191970 mintcream:f5fffa mistyrose:ffe4e1 moccasin:ffe4b5 navajowhite:ffdead navy:000080 oldlace:fdf5e6 olive:808000 olivedrab:6b8e23 orange:ffa500 orangered:ff4500 orchid:da70d6 palegoldenrod:eee8aa palegreen:98fb98 paleturquoise:afeeee palevioletred:db7093 papayawhip:ffefd5 peachpuff:ffdab9 peru:cd853f pink:ffc0cb plum:dda0dd powderblue:b0e0e6 purple:800080 rebeccapurple:663399 red:ff0000 rosybrown:bc8f8f royalblue:4169e1 saddlebrown:8b4513 salmon:fa8072 sandybrown:f4a460 seagreen:2e8b57 seashell:fff5ee sienna:a0522d silver:c0c0c0 skyblue:87ceeb slateblue:6a5acd slategray:708090 slategrey:708090 snow:fffafa springgreen:00ff7f steelblue:4682b4 tan:d2b48c teal:008080 thistle:d8bfd8 tomato:ff6347 turquoise:40e0d0 violet:ee82ee wheat:f5deb3 white:ffffff whitesmoke:f5f5f5 yellow:ffff00 yellowgreen:9acd32"
|
|
44
|
+
.split(" ")
|
|
45
|
+
.map((pair) => pair.split(":")));
|
|
46
|
+
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
|
|
47
|
+
/**
|
|
48
|
+
* Reads a CSS color written as a name, `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()`, `rgba()`,
|
|
49
|
+
* `hsl()` or `hsla()` (commas or spaces, `/` before an alpha). Anything else, a `var()` or a color
|
|
50
|
+
* function this does not know, gives nothing: it is not checked, and is said not to be.
|
|
51
|
+
*/
|
|
52
|
+
export function parseColor(input) {
|
|
53
|
+
const color = input.trim().toLowerCase();
|
|
54
|
+
if (color === "transparent")
|
|
55
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
56
|
+
const named = NAMED.get(color);
|
|
57
|
+
if (named)
|
|
58
|
+
return fromHex(named);
|
|
59
|
+
if (color.startsWith("#"))
|
|
60
|
+
return fromHex(color.slice(1));
|
|
61
|
+
const fn = /^(rgba?|hsla?)\(\s*([^)]*)\)$/.exec(color);
|
|
62
|
+
if (!fn)
|
|
63
|
+
return undefined;
|
|
64
|
+
const parts = fn[2].split(/\s*[,/]\s*|\s+/).filter(Boolean);
|
|
65
|
+
if (parts.length < 3 || parts.length > 4)
|
|
66
|
+
return undefined;
|
|
67
|
+
const alpha = parts[3] === undefined ? 1 : amount(parts[3], 1);
|
|
68
|
+
if (alpha === undefined)
|
|
69
|
+
return undefined;
|
|
70
|
+
if (fn[1]?.startsWith("rgb")) {
|
|
71
|
+
const channels = parts.slice(0, 3).map((part) => amount(part, 255));
|
|
72
|
+
if (channels.some((value) => value === undefined))
|
|
73
|
+
return undefined;
|
|
74
|
+
const [r, g, b] = channels;
|
|
75
|
+
return { r: clamp(r, 0, 255), g: clamp(g, 0, 255), b: clamp(b, 0, 255), a: clamp(alpha, 0, 1) };
|
|
76
|
+
}
|
|
77
|
+
const hue = /^(-?[\d.]+)(deg|turn)?$/.exec(parts[0]);
|
|
78
|
+
const saturation = percent(parts[1]);
|
|
79
|
+
const lightness = percent(parts[2]);
|
|
80
|
+
if (!hue || saturation === undefined || lightness === undefined)
|
|
81
|
+
return undefined;
|
|
82
|
+
const h = ((((Number(hue[1]) * (hue[2] === "turn" ? 360 : 1)) % 360) + 360) % 360) / 30;
|
|
83
|
+
const s = clamp(saturation, 0, 1);
|
|
84
|
+
const l = clamp(lightness, 0, 1);
|
|
85
|
+
const k = (n) => (n + h) % 12;
|
|
86
|
+
const f = (n) => (l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1))) * 255;
|
|
87
|
+
return { r: Math.round(f(0)), g: Math.round(f(8)), b: Math.round(f(4)), a: clamp(alpha, 0, 1) };
|
|
88
|
+
}
|
|
89
|
+
/** A number, or a percent of `whole`. */
|
|
90
|
+
function amount(part, whole) {
|
|
91
|
+
const found = /^(-?[\d.]+)(%?)$/.exec(part);
|
|
92
|
+
if (!found || Number.isNaN(Number(found[1])))
|
|
93
|
+
return undefined;
|
|
94
|
+
return found[2] ? (Number(found[1]) / 100) * whole : Number(found[1]);
|
|
95
|
+
}
|
|
96
|
+
function percent(part) {
|
|
97
|
+
const found = /^(-?[\d.]+)%$/.exec(part);
|
|
98
|
+
return found ? Number(found[1]) / 100 : undefined;
|
|
99
|
+
}
|
|
100
|
+
function fromHex(hex) {
|
|
101
|
+
if (!/^[0-9a-f]+$/.test(hex))
|
|
102
|
+
return undefined;
|
|
103
|
+
const full = hex.length === 3 || hex.length === 4
|
|
104
|
+
? [...hex].map((digit) => `${digit}${digit}`).join("")
|
|
105
|
+
: hex;
|
|
106
|
+
if (full.length !== 6 && full.length !== 8)
|
|
107
|
+
return undefined;
|
|
108
|
+
const byte = (at) => Number.parseInt(full.slice(at, at + 2), 16);
|
|
109
|
+
return { r: byte(0), g: byte(2), b: byte(4), a: full.length === 8 ? byte(6) / 255 : 1 };
|
|
110
|
+
}
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dates as the kit's date input reads, writes and says them. A day is `{ y, m, d }` with the
|
|
3
|
+
* month from 1; a time is `{ h, mi }`. The text a field holds is a "wall" string, `2026-11-21` or
|
|
4
|
+
* `2026-11-21T14:30`, with no zone: which clock it is on is the field's business.
|
|
5
|
+
*/
|
|
6
|
+
export interface Ymd {
|
|
7
|
+
y: number;
|
|
8
|
+
m: number;
|
|
9
|
+
d: number;
|
|
10
|
+
}
|
|
11
|
+
export interface Hm {
|
|
12
|
+
h: number;
|
|
13
|
+
mi: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const ymdText: ({ y, m, d }: Ymd) => string;
|
|
16
|
+
export declare const hmText: ({ h, mi }: Hm) => string;
|
|
17
|
+
export declare function validYmd({ y, m, d }: Ymd): boolean;
|
|
18
|
+
export declare function daysInMonth(y: number, m: number): number;
|
|
19
|
+
export declare function addDays(day: Ymd, n: number): Ymd;
|
|
20
|
+
/** A month on, clamped to the length of the month it lands in (31 Jan + 1 month is 28 Feb). */
|
|
21
|
+
export declare function addMonths(day: Ymd, n: number): Ymd;
|
|
22
|
+
/** 0 for Sunday. */
|
|
23
|
+
export declare const weekdayOf: (day: Ymd) => number;
|
|
24
|
+
export declare const compareYmd: (a: Ymd, b: Ymd) => number;
|
|
25
|
+
export declare const sameDay: (a: Ymd | undefined, b: Ymd | undefined) => boolean;
|
|
26
|
+
export declare function todayYmd(now?: Date): Ymd;
|
|
27
|
+
/** What a wall string holds. Seconds and a zone mark after the minute are ignored. */
|
|
28
|
+
export declare function readWall(text: string | undefined): {
|
|
29
|
+
day: Ymd;
|
|
30
|
+
time: Hm | undefined;
|
|
31
|
+
} | undefined;
|
|
32
|
+
export declare const localeOf: () => string;
|
|
33
|
+
/** In which order the locale writes day, month and year in numbers: `["m", "d", "y"]` in the US. */
|
|
34
|
+
export declare function dateOrder(locale: string): ("d" | "m" | "y")[];
|
|
35
|
+
/**
|
|
36
|
+
* What an empty date box says, as a hint and never as a value: the shape the locale writes
|
|
37
|
+
* ("mm/dd/yyyy", "dd.mm.yyyy"), with " hh:mm" for a date and time. "Pick a date" where the
|
|
38
|
+
* locale cannot say it.
|
|
39
|
+
*/
|
|
40
|
+
export declare function datePattern(locale: string, withTime: boolean): string;
|
|
41
|
+
/** Which day a week starts on in the locale, 0 for Sunday. */
|
|
42
|
+
export declare function firstDayOf(locale: string): number;
|
|
43
|
+
export declare function monthNames(locale: string, style: "long" | "short"): string[];
|
|
44
|
+
/** Names of the days from Sunday: 2021-08-01 was one. */
|
|
45
|
+
export declare function weekdayNames(locale: string, style: "long" | "short" | "narrow"): string[];
|
|
46
|
+
export declare function formatDay(day: Ymd, locale: string, style?: "medium" | "full"): string;
|
|
47
|
+
export declare function formatTime(time: Hm, locale: string): string;
|
|
48
|
+
export declare function formatWall(text: string | undefined, locale: string, withTime: boolean): string;
|
|
49
|
+
/** A day from what a person typed, or nothing if it can't be read. */
|
|
50
|
+
export declare function parseDay(raw: string, locale: string, now?: Date): Ymd | undefined;
|
|
51
|
+
/** A time of day from "14:30", "2:30 PM", "9am", "noon". */
|
|
52
|
+
export declare function parseTime(raw: string): Hm | undefined;
|
|
53
|
+
export type Entry = {
|
|
54
|
+
day: Ymd;
|
|
55
|
+
time: Hm | undefined;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* What a box holds, read: a day, and with a time asked for, a time too. `2026-11-21T14:30`,
|
|
59
|
+
* "21 nov 2026 9am", "tomorrow 14:30". A day alone keeps the time the field had.
|
|
60
|
+
*/
|
|
61
|
+
export declare function parseEntry(raw: string, withTime: boolean, locale: string, now?: Date): Entry | undefined;
|