@luxalgo/vela-ui 0.3.0 → 0.4.1
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 +165 -132
- package/LICENSE +201 -201
- package/NOTICE +28 -28
- package/README.md +86 -86
- package/cli/engine.mjs +282 -282
- package/cli/index.mjs +154 -154
- package/dist/button.js +2 -2
- package/dist/callout-bubble.js +3 -3
- package/dist/checkbox.js +3 -3
- package/dist/{chunk-OJ2SEMN6.js → chunk-5A54GODF.js} +2 -2
- package/dist/{chunk-UEO6W37Z.js → chunk-7LYWI32I.js} +2 -2
- package/dist/{chunk-IOOYDSSK.js → chunk-BIU66EHS.js} +2 -2
- package/dist/{chunk-KKS6NEGI.js → chunk-BWSOMG3G.js} +1 -1
- package/dist/{chunk-DXDCKZ2M.js → chunk-CZRHHVSB.js} +1 -1
- package/dist/{chunk-C3PXY2SW.js → chunk-EDYD5XKJ.js} +9 -0
- package/dist/chunk-GUIYFQ27.js +30 -0
- package/dist/{chunk-WWHNUKSZ.js → chunk-JEGB72M4.js} +8 -8
- package/dist/{chunk-B2EZB5ZG.js → chunk-ODPGOUQT.js} +4 -3
- package/dist/{chunk-TBXUF5YX.js → chunk-RH53NQGE.js} +18 -4
- package/dist/{chunk-OQXFQMZM.js → chunk-TGJNHUYV.js} +2 -2
- package/dist/collapsible.js +2 -2
- package/dist/color-picker.js +4 -4
- package/dist/context-menu.js +4 -4
- package/dist/date-picker.js +1 -1
- package/dist/dialog.js +4 -4
- package/dist/donut.d.ts +1 -1
- package/dist/drawer.js +1 -1
- package/dist/dropdown-menu.d.ts +2 -0
- package/dist/dropdown-menu.js +4 -4
- package/dist/empty.js +1 -1
- package/dist/field.js +9 -9
- package/dist/{stat.d.ts → figure.d.ts} +9 -9
- package/dist/figure.js +41 -0
- package/dist/glyph-select.js +4 -4
- package/dist/hover-card.js +1 -1
- package/dist/icons.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/input-group.js +2 -2
- package/dist/item.js +1 -1
- package/dist/manifest.json +75 -74
- package/dist/number-input.js +3 -3
- package/dist/popover.js +2 -2
- package/dist/select.d.ts +5 -1
- package/dist/select.js +5 -5
- package/dist/slider.js +1 -1
- package/dist/sort-button.js +2 -2
- package/dist/switch.js +1 -1
- package/dist/tabs.js +2 -2
- package/dist/{theme-Ct-q58xz.d.ts → theme-TefB8vtl.d.ts} +2 -2
- package/dist/theme.d.ts +2 -2
- package/dist/theme.js +1 -1
- package/dist/theme.schema.json +30 -30
- package/dist/toast.d.ts +2 -2
- package/dist/toast.js +2 -2
- package/dist/toggle-group.js +2 -2
- package/dist/tooltip.js +1 -1
- package/dist/zag.d.ts +11 -2
- package/dist/zag.js +1 -1
- package/docs/theming.md +217 -217
- package/docs/what-to-change.md +20 -20
- package/docs/writing-a-component.md +250 -250
- package/llms.txt +3 -3
- package/package.json +90 -90
- package/registry/badge.ts +119 -119
- package/registry/bar-chart.ts +215 -215
- package/registry/button.ts +387 -387
- package/registry/callout-bubble.ts +347 -347
- package/registry/card.ts +379 -379
- package/registry/checkbox.ts +234 -234
- package/registry/collapsible.ts +271 -271
- package/registry/color-picker.ts +582 -582
- package/registry/context-menu.ts +208 -208
- package/registry/data-table.ts +215 -215
- package/registry/date-picker.ts +483 -483
- package/registry/dialog.ts +551 -551
- package/registry/donut.ts +145 -145
- package/registry/drawer.ts +636 -636
- package/registry/dropdown-menu.ts +765 -762
- package/registry/empty.ts +91 -91
- package/registry/field.ts +584 -584
- package/registry/{stat.ts → figure.ts} +96 -96
- package/registry/glyph-select.ts +268 -268
- package/registry/hover-card.ts +222 -222
- package/registry/input-group.ts +229 -229
- package/registry/input.ts +181 -181
- package/registry/item.ts +376 -376
- package/registry/kbd.ts +64 -64
- package/registry/line-chart.ts +218 -218
- package/registry/month-grid.ts +328 -328
- package/registry/number-input.ts +443 -443
- package/registry/popover.ts +368 -368
- package/registry/scatter-chart.ts +221 -221
- package/registry/select.ts +629 -620
- package/registry/slider.ts +259 -259
- package/registry/sort-button.ts +189 -189
- package/registry/sparkline.ts +114 -114
- package/registry/switch.ts +205 -205
- package/registry/tabs.ts +426 -426
- package/registry/text-area.ts +186 -186
- package/registry/toast.ts +965 -965
- package/registry/toggle-group.ts +295 -295
- package/registry/tooltip.ts +156 -156
- package/registry/treemap.ts +847 -847
- package/skills/theming-vela-ui/SKILL.md +137 -137
- package/skills/using-vela-ui-components/SKILL.md +141 -139
- package/skills/writing-vela-ui-components/SKILL.md +163 -163
- package/src/components/badge.ts +119 -119
- package/src/components/bar-chart.ts +215 -215
- package/src/components/button.ts +387 -387
- package/src/components/callout-bubble.ts +347 -347
- package/src/components/card.ts +379 -379
- package/src/components/checkbox.ts +234 -234
- package/src/components/collapsible.ts +271 -271
- package/src/components/color-picker.ts +582 -582
- package/src/components/context-menu.ts +208 -208
- package/src/components/data-table.ts +215 -215
- package/src/components/date-picker.ts +483 -483
- package/src/components/dialog.ts +551 -551
- package/src/components/donut.ts +145 -145
- package/src/components/drawer.ts +636 -636
- package/src/components/dropdown-menu.ts +765 -762
- package/src/components/empty.ts +91 -91
- package/src/components/field.ts +584 -584
- package/src/components/{stat.ts → figure.ts} +96 -96
- package/src/components/glyph-select.ts +268 -268
- package/src/components/hover-card.ts +222 -222
- package/src/components/input-group.ts +229 -229
- package/src/components/input.ts +181 -181
- package/src/components/item.ts +376 -376
- package/src/components/kbd.ts +64 -64
- package/src/components/line-chart.ts +218 -218
- package/src/components/month-grid.ts +328 -328
- package/src/components/number-input.ts +443 -443
- package/src/components/popover.ts +368 -368
- package/src/components/scatter-chart.ts +221 -221
- package/src/components/select.ts +629 -620
- package/src/components/slider.ts +259 -259
- package/src/components/sort-button.ts +189 -189
- package/src/components/sparkline.ts +114 -114
- package/src/components/switch.ts +205 -205
- package/src/components/tabs.ts +426 -426
- package/src/components/text-area.ts +186 -186
- package/src/components/toast.ts +965 -965
- package/src/components/toggle-group.ts +295 -295
- package/src/components/tooltip.ts +156 -156
- package/src/components/treemap.ts +847 -847
- package/src/index.ts +24 -24
- package/src/lib/calendar.ts +117 -117
- package/src/lib/chart.ts +72 -72
- package/src/lib/color.ts +89 -89
- package/src/lib/component-index.ts +163 -163
- package/src/lib/css.ts +48 -48
- package/src/lib/define.ts +45 -45
- package/src/lib/dispose.ts +36 -36
- package/src/lib/h.ts +55 -55
- package/src/lib/headless.ts +16 -16
- package/src/lib/i18n.ts +30 -30
- package/src/lib/icons.ts +253 -244
- package/src/lib/kit-text.ts +26 -26
- package/src/lib/page.ts +24 -24
- package/src/lib/palette.ts +81 -81
- package/src/lib/placement.ts +188 -188
- package/src/lib/scope.ts +135 -135
- package/src/lib/styles.ts +53 -53
- package/src/lib/surface-events.ts +31 -31
- package/src/lib/surface-exit.ts +87 -87
- package/src/lib/t.ts +68 -68
- package/src/lib/theme.ts +684 -670
- package/src/lib/tokens.ts +110 -110
- package/src/lib/variants.ts +32 -32
- package/src/lib/zag.ts +61 -46
- package/stylelint/index.mjs +60 -60
- package/stylelint/lint.mjs +81 -81
- package/stylelint/plugin.mjs +115 -115
- package/dist/chunk-P4MTEMT7.js +0 -23
- package/dist/stat.js +0 -41
package/registry/color-picker.ts
CHANGED
|
@@ -1,582 +1,582 @@
|
|
|
1
|
-
// ColorPicker: a swatch grid (grays, then hue × shade), a recents row with a custom "+"
|
|
2
|
-
// chooser, and an opacity slider over a transparency checker; and ColorField, the swatch
|
|
3
|
-
// button that opens it on a popover. Colors travel as `#RRGGBB` / `#RRGGBBAA`.
|
|
4
|
-
import { ACCENT, ACCENT_BRIGHT, BEARISH, BULLISH, NEUTRAL, WARNING } from '@luxalgo/vela-ui/palette';
|
|
5
|
-
import { msg } from '@luxalgo/vela-ui/i18n';
|
|
6
|
-
import { textOf } from '@luxalgo/vela-ui/i18n';
|
|
7
|
-
import type { Translator } from '@luxalgo/vela-ui/i18n';
|
|
8
|
-
import type { ChartTheme } from '@luxalgo/vela-ui/tokens';
|
|
9
|
-
import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
|
|
10
|
-
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
11
|
-
import { defineComponent } from '@luxalgo/vela-ui';
|
|
12
|
-
import { disposeWithin, ownTeardown } from '@luxalgo/vela-ui/dispose';
|
|
13
|
-
import { h } from '@luxalgo/vela-ui';
|
|
14
|
-
import { t } from '@luxalgo/vela-ui';
|
|
15
|
-
import { nextUid, normalizeProps, runMachine, spreadProps } from '@luxalgo/vela-ui/zag';
|
|
16
|
-
import { Popover, closeOpenPopovers, openPopoverTrigger, type PlacementProps, type PopoverBoundary, type PopoverOptions } from '@luxalgo/vela-ui/popover';
|
|
17
|
-
import * as zagSlider from '@zag-js/slider';
|
|
18
|
-
|
|
19
|
-
/** Split a color into its `#RRGGBB` part + alpha 0..1 (handles `#RGB`, `#RRGGBB(AA)`, `rgba()`). */
|
|
20
|
-
export function splitColor(color: string): { hex6: string; alpha: number } {
|
|
21
|
-
const s = String(color ?? '').trim();
|
|
22
|
-
let m = /^#([0-9a-f]{6})([0-9a-f]{2})?$/i.exec(s);
|
|
23
|
-
if (m) return { hex6: `#${m[1]!.toLowerCase()}`, alpha: m[2] ? parseInt(m[2], 16) / 255 : 1 };
|
|
24
|
-
m = /^#([0-9a-f]{3})$/i.exec(s);
|
|
25
|
-
if (m) {
|
|
26
|
-
const x = m[1]!.toLowerCase();
|
|
27
|
-
return { hex6: `#${x[0]}${x[0]}${x[1]}${x[1]}${x[2]}${x[2]}`, alpha: 1 };
|
|
28
|
-
}
|
|
29
|
-
const rgba = /^rgba?\(([^)]+)\)/i.exec(s);
|
|
30
|
-
if (rgba) {
|
|
31
|
-
const p = rgba[1]!.split(',').map((v) => v.trim());
|
|
32
|
-
const hx = (n: number): string => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, '0');
|
|
33
|
-
const a = p[3] != null ? Math.max(0, Math.min(1, parseFloat(p[3]))) : 1;
|
|
34
|
-
return { hex6: `#${hx(parseInt(p[0] ?? '0', 10))}${hx(parseInt(p[1] ?? '0', 10))}${hx(parseInt(p[2] ?? '0', 10))}`, alpha: a };
|
|
35
|
-
}
|
|
36
|
-
return { hex6: ACCENT_BRIGHT, alpha: 1 };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Combine a `#RRGGBB` + alpha into `#RRGGBB` (opaque) or `#RRGGBBAA`. */
|
|
40
|
-
export function combineColor(hex6: string, alpha: number): string {
|
|
41
|
-
const a = Math.max(0, Math.min(1, alpha));
|
|
42
|
-
if (a >= 0.999) return hex6;
|
|
43
|
-
return `${hex6}${Math.round(a * 255)
|
|
44
|
-
.toString(16)
|
|
45
|
-
.padStart(2, '0')}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Composite `fg` over `bg` at `alpha`, returning an opaque `#rrggbb`. */
|
|
49
|
-
export function blendOver(fg: string, bg: string, alpha: number): string {
|
|
50
|
-
const rgb = (c: string): [number, number, number] => {
|
|
51
|
-
const n = parseInt(splitColor(c).hex6.slice(1), 16);
|
|
52
|
-
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
53
|
-
};
|
|
54
|
-
const [fr, fgc, fb] = rgb(fg);
|
|
55
|
-
const [br, bgc, bb] = rgb(bg);
|
|
56
|
-
const a = Math.max(0, Math.min(1, alpha));
|
|
57
|
-
const hx = (n: number): string => Math.round(n).toString(16).padStart(2, '0');
|
|
58
|
-
return `#${hx(fr * a + br * (1 - a))}${hx(fgc * a + bgc * (1 - a))}${hx(fb * a + bb * (1 - a))}`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** HSL (h 0-360, s/l 0-100) → `#rrggbb`. */
|
|
62
|
-
export function hslHex(hue: number, s: number, l: number): string {
|
|
63
|
-
const sn = s / 100;
|
|
64
|
-
const ln = l / 100;
|
|
65
|
-
const k = (n: number): number => (n + hue / 30) % 12;
|
|
66
|
-
const a = sn * Math.min(ln, 1 - ln);
|
|
67
|
-
const f = (n: number): number => ln - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
|
|
68
|
-
const to = (x: number): string => Math.round(x * 255).toString(16).padStart(2, '0');
|
|
69
|
-
return `#${to(f(0))}${to(f(8))}${to(f(4))}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** A swatch palette: a grayscale row, then hue columns × shade rows. */
|
|
73
|
-
export function buildPalette(): string[][] {
|
|
74
|
-
const grays = [100, 90, 80, 68, 56, 45, 35, 25, 14, 0].map((l) => hslHex(0, 0, l));
|
|
75
|
-
const hues = [4, 28, 50, 96, 140, 174, 200, 224, 270, 322];
|
|
76
|
-
const levels: Array<[number, number]> = [
|
|
77
|
-
[82, 56],
|
|
78
|
-
[58, 84],
|
|
79
|
-
[62, 74],
|
|
80
|
-
[66, 62],
|
|
81
|
-
[70, 50],
|
|
82
|
-
[64, 39],
|
|
83
|
-
[54, 29],
|
|
84
|
-
];
|
|
85
|
-
return [grays, ...levels.map(([s, l]) => hues.map((hue) => hslHex(hue, s, l)))];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** The alpha checkerboard laid under translucent colors. Its grays are fixed on purpose:
|
|
89
|
-
* it stands for "nothing here", so it must not shift with the theme. */
|
|
90
|
-
export function transparencyChecker(size: number): string {
|
|
91
|
-
return `repeating-conic-gradient(#9aa0a6 0% 25%, #d3d6da 0% 50%) 0 0 / ${size}px ${size}px`; // palette-exempt: these grays stand for transparency itself
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export type ColorFieldShape = 'square' | 'circle';
|
|
95
|
-
|
|
96
|
-
export interface ColorPickerControllerOptions {
|
|
97
|
-
color?: string;
|
|
98
|
-
onChange?: (value: string) => void;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface ColorPickerController {
|
|
102
|
-
hex6: string;
|
|
103
|
-
alpha: number;
|
|
104
|
-
combined(): string;
|
|
105
|
-
setHex(hex: string): void;
|
|
106
|
-
setAlpha(a: number): void;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** The picked color as a hex part and an opacity, without DOM. */
|
|
110
|
-
export function colorPickerController(opts: ColorPickerControllerOptions = {}): ColorPickerController {
|
|
111
|
-
const parsed = splitColor(opts.color ?? ACCENT_BRIGHT);
|
|
112
|
-
let hex6 = parsed.hex6;
|
|
113
|
-
let alpha = parsed.alpha;
|
|
114
|
-
return {
|
|
115
|
-
get hex6() {
|
|
116
|
-
return hex6;
|
|
117
|
-
},
|
|
118
|
-
get alpha() {
|
|
119
|
-
return alpha;
|
|
120
|
-
},
|
|
121
|
-
combined() {
|
|
122
|
-
return combineColor(hex6, alpha);
|
|
123
|
-
},
|
|
124
|
-
setHex(hex: string) {
|
|
125
|
-
hex6 = splitColor(hex).hex6;
|
|
126
|
-
},
|
|
127
|
-
setAlpha(a: number) {
|
|
128
|
-
alpha = Math.max(0, Math.min(1, a));
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export const colorPickerDef = defineComponent({
|
|
134
|
-
name: 'color-picker',
|
|
135
|
-
parts: ['root', 'trigger', 'swatch', 'content'],
|
|
136
|
-
settings: {
|
|
137
|
-
'swatch-ring': 'inset 0 0 0 1px rgba(0, 0, 0, 0.25)',
|
|
138
|
-
'circle-swatch-radius': '2px',
|
|
139
|
-
'content-radius': t.radius.lg,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const TEXT = {
|
|
144
|
-
opacity: msg('ui.colorPicker.opacity', 'Opacity'),
|
|
145
|
-
opacityPercent: msg('ui.colorPicker.opacityPercent', 'Opacity (%)'),
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const PALETTE = buildPalette();
|
|
149
|
-
const CHECKER = transparencyChecker(10);
|
|
150
|
-
const FIELD_CHECKER = transparencyChecker(8);
|
|
151
|
-
|
|
152
|
-
/** Session-shared recently-picked colors (most-recent first). */
|
|
153
|
-
const recents: string[] = [ACCENT, BULLISH, BEARISH, WARNING, NEUTRAL];
|
|
154
|
-
|
|
155
|
-
function addRecent(hex6: string): void {
|
|
156
|
-
const i = recents.findIndex((c) => c.toLowerCase() === hex6.toLowerCase());
|
|
157
|
-
if (i >= 0) recents.splice(i, 1);
|
|
158
|
-
recents.unshift(hex6);
|
|
159
|
-
if (recents.length > 10) recents.length = 10;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* When the opacity slider hands its value to `onChange` during a drag: `'live'` on every
|
|
164
|
-
* pointer move (the consumer repaints cheaply, so the chart previews the drag), `'release'`
|
|
165
|
-
* once on pointerup/pointercancel (the consumer recomputes — a script re-runs over its whole
|
|
166
|
-
* history, so a per-move commit would queue seconds of work behind the pointer). The knob,
|
|
167
|
-
* gradient and percentage follow the pointer either way; swatch picks always emit at once.
|
|
168
|
-
*/
|
|
169
|
-
export type ColorCommit = 'live' | 'release';
|
|
170
|
-
|
|
171
|
-
export interface ColorPickerOptions {
|
|
172
|
-
commit?: ColorCommit;
|
|
173
|
-
/** The translator of the picker's labels (default: the kit build's, else English). */
|
|
174
|
-
i18n?: Translator | null;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const injectStyles = defineStyles(
|
|
178
|
-
'vela-ui-color',
|
|
179
|
-
css`
|
|
180
|
-
@layer vela.ui {
|
|
181
|
-
[data-slot='color-picker-swatch'] {
|
|
182
|
-
display: block;
|
|
183
|
-
width: 100%;
|
|
184
|
-
height: 100%;
|
|
185
|
-
border-radius: 0;
|
|
186
|
-
box-shadow: ${colorPickerDef.vars['swatch-ring']};
|
|
187
|
-
}
|
|
188
|
-
[data-slot='color-picker-trigger'] {
|
|
189
|
-
width: 24px;
|
|
190
|
-
height: 24px;
|
|
191
|
-
padding: 2px;
|
|
192
|
-
border: 1px solid ${t.border};
|
|
193
|
-
border-radius: 0;
|
|
194
|
-
background: ${t.surface.sunken};
|
|
195
|
-
cursor: pointer;
|
|
196
|
-
flex: none;
|
|
197
|
-
|
|
198
|
-
&:where(:not([hidden])) {
|
|
199
|
-
display: inline-flex;
|
|
200
|
-
}
|
|
201
|
-
&:where([data-shape='circle']) {
|
|
202
|
-
width: 26px;
|
|
203
|
-
height: 26px;
|
|
204
|
-
padding: 3px;
|
|
205
|
-
border: 1px solid ${t.border.strong};
|
|
206
|
-
border-radius: ${t.radius.sm};
|
|
207
|
-
background: transparent;
|
|
208
|
-
overflow: hidden;
|
|
209
|
-
}
|
|
210
|
-
&:hover {
|
|
211
|
-
border-color: ${t.fg.muted};
|
|
212
|
-
}
|
|
213
|
-
&:where([data-shape='circle']) [data-slot='color-picker-swatch'] {
|
|
214
|
-
border-radius: ${colorPickerDef.vars['circle-swatch-radius']};
|
|
215
|
-
box-shadow: none;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
[data-slot~='color-picker-content'] {
|
|
219
|
-
background: ${t.surface.overlay};
|
|
220
|
-
border: 1px solid ${t.border};
|
|
221
|
-
border-radius: ${colorPickerDef.vars['content-radius']};
|
|
222
|
-
box-shadow: ${t.shadow};
|
|
223
|
-
padding: 10px;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
`,
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* A self-contained color picker: a swatch grid (grays + hue × shade), a
|
|
231
|
-
* recents row with a custom "+" picker, and an opacity slider over a transparency checker.
|
|
232
|
-
* Emits `#RRGGBB` / `#RRGGBBAA` through `onChange`.
|
|
233
|
-
*/
|
|
234
|
-
export function buildColorPicker(color: string, theme: ChartTheme, onChange: (v: string) => void, options: ColorPickerOptions = {}): HTMLElement {
|
|
235
|
-
const commit: ColorCommit = options.commit ?? 'live';
|
|
236
|
-
const i18n = options.i18n ?? kitTranslator();
|
|
237
|
-
const parsed = splitColor(color);
|
|
238
|
-
let curHex = parsed.hex6;
|
|
239
|
-
let curAlpha = parsed.alpha;
|
|
240
|
-
|
|
241
|
-
const root = document.createElement('div');
|
|
242
|
-
root.dataset.slot = colorPickerDef.slot('root');
|
|
243
|
-
root.style.cssText = 'display:flex;flex-direction:column;gap:9px;width:236px;';
|
|
244
|
-
|
|
245
|
-
const grid = document.createElement('div');
|
|
246
|
-
grid.style.cssText = 'display:grid;grid-template-columns:repeat(10,1fr);gap:4px;';
|
|
247
|
-
const swatches: HTMLButtonElement[] = [];
|
|
248
|
-
for (const row of PALETTE) {
|
|
249
|
-
for (const c of row) {
|
|
250
|
-
const sw = document.createElement('button');
|
|
251
|
-
sw.type = 'button';
|
|
252
|
-
sw.dataset.c = c;
|
|
253
|
-
sw.setAttribute('aria-label', c);
|
|
254
|
-
sw.style.cssText = `width:100%;aspect-ratio:1;border-radius:4px;border:none;cursor:pointer;background:${c};padding:0;`;
|
|
255
|
-
sw.addEventListener('click', (e) => {
|
|
256
|
-
e.stopPropagation();
|
|
257
|
-
pickHex(c);
|
|
258
|
-
});
|
|
259
|
-
grid.appendChild(sw);
|
|
260
|
-
swatches.push(sw);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
const paintSelection = (): void => {
|
|
264
|
-
for (const sw of swatches) {
|
|
265
|
-
const on = (sw.dataset.c ?? '').toLowerCase() === curHex.toLowerCase();
|
|
266
|
-
sw.setAttribute('aria-pressed', String(on));
|
|
267
|
-
sw.style.outline = on ? `2px solid ${theme.textColor}` : 'none';
|
|
268
|
-
sw.style.outlineOffset = '2px';
|
|
269
|
-
sw.style.zIndex = on ? '1' : '';
|
|
270
|
-
sw.style.position = on ? 'relative' : '';
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const recentRow = document.createElement('div');
|
|
275
|
-
recentRow.style.cssText = 'display:flex;align-items:center;gap:5px;flex-wrap:wrap;border-top:1px solid var(--vela-border);padding-top:9px;';
|
|
276
|
-
// Swatches get their own wrapper so re-rendering them never detaches the native
|
|
277
|
-
// color input — the browser closes its chooser the moment its input leaves the DOM.
|
|
278
|
-
const recentSwatches = document.createElement('div');
|
|
279
|
-
recentSwatches.style.cssText = 'display:contents;';
|
|
280
|
-
const add = document.createElement('label');
|
|
281
|
-
add.style.cssText = `width:17px;height:17px;border-radius:var(--vela-radius-sm);border:1px dashed var(--vela-border-strong);cursor:pointer;display:flex;align-items:center;justify-content:center;color:${theme.textColor};font:14px ${theme.fontFamily};position:relative;`;
|
|
282
|
-
add.textContent = '+';
|
|
283
|
-
const customInput = document.createElement('input');
|
|
284
|
-
customInput.type = 'color';
|
|
285
|
-
customInput.value = curHex;
|
|
286
|
-
customInput.style.cssText = 'position:absolute;inset:0;opacity:0;cursor:pointer;';
|
|
287
|
-
// `input` streams while the chooser is open (preview); `change` is the committed pick.
|
|
288
|
-
customInput.addEventListener('input', () => previewHex(customInput.value));
|
|
289
|
-
customInput.addEventListener('change', () => pickHex(customInput.value, true));
|
|
290
|
-
add.appendChild(customInput);
|
|
291
|
-
recentRow.append(recentSwatches, add);
|
|
292
|
-
const renderRecents = (): void => {
|
|
293
|
-
recentSwatches.replaceChildren();
|
|
294
|
-
for (const c of recents) {
|
|
295
|
-
const sw = document.createElement('button');
|
|
296
|
-
sw.type = 'button';
|
|
297
|
-
sw.setAttribute('aria-label', c);
|
|
298
|
-
sw.style.cssText = `width:17px;height:17px;border-radius:var(--vela-radius-sm);border:1px solid var(--vela-border);cursor:pointer;background:${c};padding:0;`;
|
|
299
|
-
sw.addEventListener('click', (e) => {
|
|
300
|
-
e.stopPropagation();
|
|
301
|
-
pickHex(c);
|
|
302
|
-
});
|
|
303
|
-
recentSwatches.appendChild(sw);
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
const opLabel = document.createElement('div');
|
|
308
|
-
opLabel.textContent = textOf(i18n, TEXT.opacity);
|
|
309
|
-
opLabel.style.cssText = `font:11px ${theme.fontFamily};color:var(--vela-fg-muted);`;
|
|
310
|
-
const opRow = document.createElement('div');
|
|
311
|
-
opRow.style.cssText = 'display:flex;align-items:center;gap:10px;';
|
|
312
|
-
const track = document.createElement('div');
|
|
313
|
-
track.style.cssText = 'flex:1;position:relative;height:13px;border-radius:7px;cursor:pointer;';
|
|
314
|
-
const knob = document.createElement('div');
|
|
315
|
-
knob.style.cssText = 'position:absolute;top:50%;width:15px;height:15px;border-radius:50%;background:var(--vela-selected-bg);box-shadow:0 1px 3px rgba(0,0,0,0.55);transform:translate(-50%,-50%);pointer-events:none;';
|
|
316
|
-
track.appendChild(knob);
|
|
317
|
-
const pctBox = document.createElement('input');
|
|
318
|
-
pctBox.type = 'text';
|
|
319
|
-
pctBox.inputMode = 'numeric';
|
|
320
|
-
pctBox.title = textOf(i18n, TEXT.opacityPercent);
|
|
321
|
-
pctBox.style.cssText = `width:46px;box-sizing:border-box;text-align:center;font:var(--vela-font-size-md) ${theme.fontFamily};color:var(--vela-fg);background:transparent;border:1px solid var(--vela-border);border-radius:5px;padding:3px 4px;outline:none;`;
|
|
322
|
-
pctBox.addEventListener('focus', () => {
|
|
323
|
-
pctBox.style.borderColor = 'var(--vela-border-strong)';
|
|
324
|
-
pctBox.select();
|
|
325
|
-
});
|
|
326
|
-
opRow.append(track, pctBox);
|
|
327
|
-
const paintOpacity = (): void => {
|
|
328
|
-
track.style.background = `linear-gradient(to right, ${curHex}00, ${curHex}ff), ${CHECKER}`;
|
|
329
|
-
knob.style.left = `${curAlpha * 100}%`;
|
|
330
|
-
pctBox.value = `${Math.round(curAlpha * 100)}%`;
|
|
331
|
-
};
|
|
332
|
-
// A typed value is one discrete edit, so it commits once under either `commit` policy.
|
|
333
|
-
// Unparseable input snaps back to the current opacity.
|
|
334
|
-
const commitTyped = (): void => {
|
|
335
|
-
pctBox.style.borderColor = 'var(--vela-border)';
|
|
336
|
-
const n = parseFloat(pctBox.value.replace('%', ''));
|
|
337
|
-
if (Number.isFinite(n)) {
|
|
338
|
-
const next = Math.max(0, Math.min(100, n)) / 100;
|
|
339
|
-
if (Math.round(next * 100) !== Math.round(curAlpha * 100)) {
|
|
340
|
-
curAlpha = next;
|
|
341
|
-
paintOpacity();
|
|
342
|
-
emit();
|
|
343
|
-
zagSlider.connect(slider.service, normalizeProps).setValue([next]);
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
paintOpacity();
|
|
348
|
-
};
|
|
349
|
-
pctBox.addEventListener('change', commitTyped);
|
|
350
|
-
pctBox.addEventListener('blur', () => {
|
|
351
|
-
// `change` does not fire when the text is unchanged; still restore the "%" readout.
|
|
352
|
-
pctBox.style.borderColor = 'var(--vela-border)';
|
|
353
|
-
paintOpacity();
|
|
354
|
-
});
|
|
355
|
-
pctBox.addEventListener('keydown', (e) => {
|
|
356
|
-
// Typing must not reach chart/dialog shortcuts; Escape stays with the popover.
|
|
357
|
-
if (e.key === 'Escape') return;
|
|
358
|
-
e.stopPropagation();
|
|
359
|
-
if (e.key === 'Enter') {
|
|
360
|
-
e.preventDefault();
|
|
361
|
-
commitTyped();
|
|
362
|
-
pctBox.blur();
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
// The opacity is Zag's slider machine: the track is its control, the knob its thumb (the
|
|
366
|
-
// slider role, the arrow keys, Page Up/Down, Home/End). A move always previews (knob,
|
|
367
|
-
// readout, gradient); whether each move also COMMITS is the `commit` option — see
|
|
368
|
-
// {@link ColorCommit}. The knob and the track keep their own paint: the machine's inline
|
|
369
|
-
// styles are left out.
|
|
370
|
-
const pct = (a: number): number => Math.round(a * 100);
|
|
371
|
-
const sliderId = nextUid('vela-color-opacity');
|
|
372
|
-
// A press or a key on the slider is pending its end; the opacity typed in the readout
|
|
373
|
-
// (synced into the machine) is not, it has committed already.
|
|
374
|
-
let pressed = false;
|
|
375
|
-
const slider = runMachine(
|
|
376
|
-
zagSlider.machine,
|
|
377
|
-
{
|
|
378
|
-
id: sliderId,
|
|
379
|
-
min: 0,
|
|
380
|
-
max: 1,
|
|
381
|
-
step: 0.01,
|
|
382
|
-
defaultValue: [curAlpha],
|
|
383
|
-
thumbAlignment: 'center',
|
|
384
|
-
'aria-label': [textOf(i18n, TEXT.opacity)],
|
|
385
|
-
getAriaValueText: ({ value }) => `${pct(value)}%`,
|
|
386
|
-
onValueChange: ({ value: [a] }) => {
|
|
387
|
-
if (a === undefined || pct(a) === pct(curAlpha)) return;
|
|
388
|
-
curAlpha = a;
|
|
389
|
-
paintOpacity();
|
|
390
|
-
if (commit === 'live') emit();
|
|
391
|
-
},
|
|
392
|
-
onValueChangeEnd: ({ value: [a] }) => {
|
|
393
|
-
if (commit !== 'release' || !pressed) return;
|
|
394
|
-
pressed = false;
|
|
395
|
-
if (a !== undefined && pct(a) !== pct(curAlpha)) {
|
|
396
|
-
curAlpha = a;
|
|
397
|
-
paintOpacity();
|
|
398
|
-
}
|
|
399
|
-
emit();
|
|
400
|
-
},
|
|
401
|
-
},
|
|
402
|
-
(service) => {
|
|
403
|
-
const api = zagSlider.connect(service, normalizeProps);
|
|
404
|
-
const own = <T extends object>(props: T): T => {
|
|
405
|
-
const { style: _style, ...rest } = props as T & { style?: unknown };
|
|
406
|
-
return rest as T;
|
|
407
|
-
};
|
|
408
|
-
spreadProps(opRow, own(api.getRootProps()), sliderId);
|
|
409
|
-
spreadProps(track, own(api.getControlProps()), sliderId);
|
|
410
|
-
spreadProps(knob, own(api.getThumbProps({ index: 0 })), sliderId);
|
|
411
|
-
},
|
|
412
|
-
);
|
|
413
|
-
ownTeardown(root, () => slider.stop());
|
|
414
|
-
// The press belongs to the picker: an open popover or dialog must not read it as its own.
|
|
415
|
-
track.addEventListener('pointerdown', (e) => {
|
|
416
|
-
e.stopPropagation();
|
|
417
|
-
pressed = true;
|
|
418
|
-
});
|
|
419
|
-
knob.addEventListener('keydown', () => {
|
|
420
|
-
pressed = true;
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
function emit(): void {
|
|
424
|
-
onChange(combineColor(curHex, curAlpha));
|
|
425
|
-
}
|
|
426
|
-
function previewHex(hex: string): void {
|
|
427
|
-
curHex = splitColor(hex).hex6;
|
|
428
|
-
paintSelection();
|
|
429
|
-
paintOpacity();
|
|
430
|
-
emit();
|
|
431
|
-
}
|
|
432
|
-
function pickHex(hex: string, custom = false): void {
|
|
433
|
-
previewHex(hex);
|
|
434
|
-
addRecent(curHex);
|
|
435
|
-
customInput.value = curHex;
|
|
436
|
-
if (custom) renderRecents();
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
renderRecents();
|
|
440
|
-
paintSelection();
|
|
441
|
-
paintOpacity();
|
|
442
|
-
root.append(grid, recentRow, opLabel, opRow);
|
|
443
|
-
return root;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/** Where the picker opens beside the swatch (shadcn's placement props; default below and
|
|
447
|
-
* end-aligned, 6px away). */
|
|
448
|
-
export interface ColorFieldOpts extends PlacementProps {
|
|
449
|
-
shape?: ColorFieldShape;
|
|
450
|
-
theme: ChartTheme;
|
|
451
|
-
getVal: () => string;
|
|
452
|
-
onVal: (v: string) => void;
|
|
453
|
-
id?: string;
|
|
454
|
-
/** The picker's host, layer and positioning strategy. `gap`, `align` and `boundary` here
|
|
455
|
-
* are deprecated: the field's placement props replace them. */
|
|
456
|
-
popover?: Pick<PopoverOptions, 'host' | 'position' | 'boundary' | 'zIndex' | 'gap' | 'align'>;
|
|
457
|
-
/** Opacity-drag commit policy of the popover picker (default `'live'`) — see {@link ColorCommit}. */
|
|
458
|
-
commit?: ColorCommit;
|
|
459
|
-
/** The translator of the picker's labels (default: the kit build's, else English). */
|
|
460
|
-
i18n?: Translator | null;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
export type ColorFieldProps = ColorFieldOpts;
|
|
464
|
-
|
|
465
|
-
/** Closed-state swatch shape. `circle` is the settings-dialog preview (a square chip
|
|
466
|
-
* inset from a matching field border); `square` is the compact drawing-chrome swatch. */
|
|
467
|
-
export function colorField(
|
|
468
|
-
theme: ChartTheme,
|
|
469
|
-
getVal: () => string,
|
|
470
|
-
onVal: (v: string) => void,
|
|
471
|
-
opts?: { shape?: ColorFieldShape; id?: string; popover?: ColorFieldOpts['popover']; commit?: ColorCommit } & PlacementProps,
|
|
472
|
-
): HTMLElement {
|
|
473
|
-
return new ColorField({ ...opts, theme, getVal, onVal }).el;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
export class ColorField {
|
|
477
|
-
readonly el: HTMLButtonElement;
|
|
478
|
-
private readonly swatch: HTMLElement;
|
|
479
|
-
private readonly getVal: () => string;
|
|
480
|
-
private readonly onVal: (v: string) => void;
|
|
481
|
-
private readonly theme: ChartTheme;
|
|
482
|
-
private readonly popoverOpts: ColorFieldOpts['popover'];
|
|
483
|
-
private readonly placement: PlacementProps;
|
|
484
|
-
private readonly commit: ColorCommit | undefined;
|
|
485
|
-
private readonly i18n: Translator | null;
|
|
486
|
-
private readonly release: () => void;
|
|
487
|
-
|
|
488
|
-
constructor(opts: ColorFieldOpts) {
|
|
489
|
-
injectStyles(document);
|
|
490
|
-
this.theme = opts.theme;
|
|
491
|
-
this.getVal = opts.getVal;
|
|
492
|
-
this.onVal = opts.onVal;
|
|
493
|
-
this.popoverOpts = opts.popover;
|
|
494
|
-
this.placement = opts;
|
|
495
|
-
this.commit = opts.commit;
|
|
496
|
-
this.i18n = opts.i18n ?? kitTranslator();
|
|
497
|
-
|
|
498
|
-
const circle = opts.shape === 'circle';
|
|
499
|
-
const swatch = h('span', { className: 'vela-color-field-swatch', slot: colorPickerDef.slot('swatch') });
|
|
500
|
-
const trigger = h(
|
|
501
|
-
'button',
|
|
502
|
-
{
|
|
503
|
-
className: circle ? 'vela-color-field vela-color-field-circle' : 'vela-color-field',
|
|
504
|
-
slot: colorPickerDef.slot('trigger'),
|
|
505
|
-
attrs: { type: 'button', id: opts.id },
|
|
506
|
-
data: { shape: opts.shape ?? 'square' },
|
|
507
|
-
},
|
|
508
|
-
swatch,
|
|
509
|
-
);
|
|
510
|
-
this.el = trigger;
|
|
511
|
-
this.swatch = swatch;
|
|
512
|
-
this.paint();
|
|
513
|
-
trigger.addEventListener('vela-sync', () => this.paint());
|
|
514
|
-
// Suppress the default mousedown focus: when this press outside-dismisses an open
|
|
515
|
-
// popover, Chrome's focus action can scroll the dialog body under the pointer.
|
|
516
|
-
trigger.addEventListener('pointerdown', (e) => {
|
|
517
|
-
e.preventDefault();
|
|
518
|
-
trigger.focus({ preventScroll: true });
|
|
519
|
-
});
|
|
520
|
-
trigger.addEventListener('click', (e) => {
|
|
521
|
-
e.stopPropagation();
|
|
522
|
-
this.toggle();
|
|
523
|
-
});
|
|
524
|
-
this.release = ownTeardown(trigger, () => {
|
|
525
|
-
if (openPopoverTrigger() === trigger) closeOpenPopovers();
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
destroy(): void {
|
|
530
|
-
this.release();
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
private paint(): void {
|
|
534
|
-
const v = this.getVal();
|
|
535
|
-
this.swatch.style.background = `linear-gradient(${v}, ${v}), ${FIELD_CHECKER}`;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
private toggle(): void {
|
|
539
|
-
if (openPopoverTrigger() === this.el) {
|
|
540
|
-
closeOpenPopovers();
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
const content = buildColorPicker(
|
|
544
|
-
this.getVal(),
|
|
545
|
-
this.theme,
|
|
546
|
-
(val) => {
|
|
547
|
-
this.onVal(val);
|
|
548
|
-
this.paint();
|
|
549
|
-
},
|
|
550
|
-
{ commit: this.commit, i18n: this.i18n },
|
|
551
|
-
);
|
|
552
|
-
const pop = new Popover({
|
|
553
|
-
trigger: this.el,
|
|
554
|
-
theme: this.theme,
|
|
555
|
-
side: this.placement.side,
|
|
556
|
-
align: this.placement.align ?? this.popoverOpts?.align ?? 'end',
|
|
557
|
-
sideOffset: this.placement.sideOffset ?? this.popoverOpts?.gap ?? 6,
|
|
558
|
-
alignOffset: this.placement.alignOffset,
|
|
559
|
-
avoidCollisions: this.placement.avoidCollisions,
|
|
560
|
-
collisionPadding: this.placement.collisionPadding,
|
|
561
|
-
host: this.popoverOpts?.host,
|
|
562
|
-
position: this.popoverOpts?.position,
|
|
563
|
-
collisionBoundary: this.placement.collisionBoundary ?? (this.popoverOpts?.boundary as PopoverBoundary | undefined),
|
|
564
|
-
zIndex: this.popoverOpts?.zIndex,
|
|
565
|
-
className: 'vela-color-field-pop',
|
|
566
|
-
slot: colorPickerDef.slot('content'),
|
|
567
|
-
content,
|
|
568
|
-
// A fresh popover per open: closing discards it.
|
|
569
|
-
onClose: () => disposeWithin(content),
|
|
570
|
-
});
|
|
571
|
-
pop.show();
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
export function createColorField(props: ColorFieldProps): ColorField {
|
|
576
|
-
return new ColorField(props);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
/** Close any open color (or other kit) popover — dialog teardown. */
|
|
580
|
-
export function closeColorPopover(): void {
|
|
581
|
-
closeOpenPopovers();
|
|
582
|
-
}
|
|
1
|
+
// ColorPicker: a swatch grid (grays, then hue × shade), a recents row with a custom "+"
|
|
2
|
+
// chooser, and an opacity slider over a transparency checker; and ColorField, the swatch
|
|
3
|
+
// button that opens it on a popover. Colors travel as `#RRGGBB` / `#RRGGBBAA`.
|
|
4
|
+
import { ACCENT, ACCENT_BRIGHT, BEARISH, BULLISH, NEUTRAL, WARNING } from '@luxalgo/vela-ui/palette';
|
|
5
|
+
import { msg } from '@luxalgo/vela-ui/i18n';
|
|
6
|
+
import { textOf } from '@luxalgo/vela-ui/i18n';
|
|
7
|
+
import type { Translator } from '@luxalgo/vela-ui/i18n';
|
|
8
|
+
import type { ChartTheme } from '@luxalgo/vela-ui/tokens';
|
|
9
|
+
import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
|
|
10
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
11
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
12
|
+
import { disposeWithin, ownTeardown } from '@luxalgo/vela-ui/dispose';
|
|
13
|
+
import { h } from '@luxalgo/vela-ui';
|
|
14
|
+
import { t } from '@luxalgo/vela-ui';
|
|
15
|
+
import { nextUid, normalizeProps, runMachine, spreadProps } from '@luxalgo/vela-ui/zag';
|
|
16
|
+
import { Popover, closeOpenPopovers, openPopoverTrigger, type PlacementProps, type PopoverBoundary, type PopoverOptions } from '@luxalgo/vela-ui/popover';
|
|
17
|
+
import * as zagSlider from '@zag-js/slider';
|
|
18
|
+
|
|
19
|
+
/** Split a color into its `#RRGGBB` part + alpha 0..1 (handles `#RGB`, `#RRGGBB(AA)`, `rgba()`). */
|
|
20
|
+
export function splitColor(color: string): { hex6: string; alpha: number } {
|
|
21
|
+
const s = String(color ?? '').trim();
|
|
22
|
+
let m = /^#([0-9a-f]{6})([0-9a-f]{2})?$/i.exec(s);
|
|
23
|
+
if (m) return { hex6: `#${m[1]!.toLowerCase()}`, alpha: m[2] ? parseInt(m[2], 16) / 255 : 1 };
|
|
24
|
+
m = /^#([0-9a-f]{3})$/i.exec(s);
|
|
25
|
+
if (m) {
|
|
26
|
+
const x = m[1]!.toLowerCase();
|
|
27
|
+
return { hex6: `#${x[0]}${x[0]}${x[1]}${x[1]}${x[2]}${x[2]}`, alpha: 1 };
|
|
28
|
+
}
|
|
29
|
+
const rgba = /^rgba?\(([^)]+)\)/i.exec(s);
|
|
30
|
+
if (rgba) {
|
|
31
|
+
const p = rgba[1]!.split(',').map((v) => v.trim());
|
|
32
|
+
const hx = (n: number): string => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, '0');
|
|
33
|
+
const a = p[3] != null ? Math.max(0, Math.min(1, parseFloat(p[3]))) : 1;
|
|
34
|
+
return { hex6: `#${hx(parseInt(p[0] ?? '0', 10))}${hx(parseInt(p[1] ?? '0', 10))}${hx(parseInt(p[2] ?? '0', 10))}`, alpha: a };
|
|
35
|
+
}
|
|
36
|
+
return { hex6: ACCENT_BRIGHT, alpha: 1 };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Combine a `#RRGGBB` + alpha into `#RRGGBB` (opaque) or `#RRGGBBAA`. */
|
|
40
|
+
export function combineColor(hex6: string, alpha: number): string {
|
|
41
|
+
const a = Math.max(0, Math.min(1, alpha));
|
|
42
|
+
if (a >= 0.999) return hex6;
|
|
43
|
+
return `${hex6}${Math.round(a * 255)
|
|
44
|
+
.toString(16)
|
|
45
|
+
.padStart(2, '0')}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Composite `fg` over `bg` at `alpha`, returning an opaque `#rrggbb`. */
|
|
49
|
+
export function blendOver(fg: string, bg: string, alpha: number): string {
|
|
50
|
+
const rgb = (c: string): [number, number, number] => {
|
|
51
|
+
const n = parseInt(splitColor(c).hex6.slice(1), 16);
|
|
52
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
53
|
+
};
|
|
54
|
+
const [fr, fgc, fb] = rgb(fg);
|
|
55
|
+
const [br, bgc, bb] = rgb(bg);
|
|
56
|
+
const a = Math.max(0, Math.min(1, alpha));
|
|
57
|
+
const hx = (n: number): string => Math.round(n).toString(16).padStart(2, '0');
|
|
58
|
+
return `#${hx(fr * a + br * (1 - a))}${hx(fgc * a + bgc * (1 - a))}${hx(fb * a + bb * (1 - a))}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** HSL (h 0-360, s/l 0-100) → `#rrggbb`. */
|
|
62
|
+
export function hslHex(hue: number, s: number, l: number): string {
|
|
63
|
+
const sn = s / 100;
|
|
64
|
+
const ln = l / 100;
|
|
65
|
+
const k = (n: number): number => (n + hue / 30) % 12;
|
|
66
|
+
const a = sn * Math.min(ln, 1 - ln);
|
|
67
|
+
const f = (n: number): number => ln - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
|
|
68
|
+
const to = (x: number): string => Math.round(x * 255).toString(16).padStart(2, '0');
|
|
69
|
+
return `#${to(f(0))}${to(f(8))}${to(f(4))}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A swatch palette: a grayscale row, then hue columns × shade rows. */
|
|
73
|
+
export function buildPalette(): string[][] {
|
|
74
|
+
const grays = [100, 90, 80, 68, 56, 45, 35, 25, 14, 0].map((l) => hslHex(0, 0, l));
|
|
75
|
+
const hues = [4, 28, 50, 96, 140, 174, 200, 224, 270, 322];
|
|
76
|
+
const levels: Array<[number, number]> = [
|
|
77
|
+
[82, 56],
|
|
78
|
+
[58, 84],
|
|
79
|
+
[62, 74],
|
|
80
|
+
[66, 62],
|
|
81
|
+
[70, 50],
|
|
82
|
+
[64, 39],
|
|
83
|
+
[54, 29],
|
|
84
|
+
];
|
|
85
|
+
return [grays, ...levels.map(([s, l]) => hues.map((hue) => hslHex(hue, s, l)))];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The alpha checkerboard laid under translucent colors. Its grays are fixed on purpose:
|
|
89
|
+
* it stands for "nothing here", so it must not shift with the theme. */
|
|
90
|
+
export function transparencyChecker(size: number): string {
|
|
91
|
+
return `repeating-conic-gradient(#9aa0a6 0% 25%, #d3d6da 0% 50%) 0 0 / ${size}px ${size}px`; // palette-exempt: these grays stand for transparency itself
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type ColorFieldShape = 'square' | 'circle';
|
|
95
|
+
|
|
96
|
+
export interface ColorPickerControllerOptions {
|
|
97
|
+
color?: string;
|
|
98
|
+
onChange?: (value: string) => void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ColorPickerController {
|
|
102
|
+
hex6: string;
|
|
103
|
+
alpha: number;
|
|
104
|
+
combined(): string;
|
|
105
|
+
setHex(hex: string): void;
|
|
106
|
+
setAlpha(a: number): void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The picked color as a hex part and an opacity, without DOM. */
|
|
110
|
+
export function colorPickerController(opts: ColorPickerControllerOptions = {}): ColorPickerController {
|
|
111
|
+
const parsed = splitColor(opts.color ?? ACCENT_BRIGHT);
|
|
112
|
+
let hex6 = parsed.hex6;
|
|
113
|
+
let alpha = parsed.alpha;
|
|
114
|
+
return {
|
|
115
|
+
get hex6() {
|
|
116
|
+
return hex6;
|
|
117
|
+
},
|
|
118
|
+
get alpha() {
|
|
119
|
+
return alpha;
|
|
120
|
+
},
|
|
121
|
+
combined() {
|
|
122
|
+
return combineColor(hex6, alpha);
|
|
123
|
+
},
|
|
124
|
+
setHex(hex: string) {
|
|
125
|
+
hex6 = splitColor(hex).hex6;
|
|
126
|
+
},
|
|
127
|
+
setAlpha(a: number) {
|
|
128
|
+
alpha = Math.max(0, Math.min(1, a));
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const colorPickerDef = defineComponent({
|
|
134
|
+
name: 'color-picker',
|
|
135
|
+
parts: ['root', 'trigger', 'swatch', 'content'],
|
|
136
|
+
settings: {
|
|
137
|
+
'swatch-ring': 'inset 0 0 0 1px rgba(0, 0, 0, 0.25)',
|
|
138
|
+
'circle-swatch-radius': '2px',
|
|
139
|
+
'content-radius': t.radius.lg,
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const TEXT = {
|
|
144
|
+
opacity: msg('ui.colorPicker.opacity', 'Opacity'),
|
|
145
|
+
opacityPercent: msg('ui.colorPicker.opacityPercent', 'Opacity (%)'),
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const PALETTE = buildPalette();
|
|
149
|
+
const CHECKER = transparencyChecker(10);
|
|
150
|
+
const FIELD_CHECKER = transparencyChecker(8);
|
|
151
|
+
|
|
152
|
+
/** Session-shared recently-picked colors (most-recent first). */
|
|
153
|
+
const recents: string[] = [ACCENT, BULLISH, BEARISH, WARNING, NEUTRAL];
|
|
154
|
+
|
|
155
|
+
function addRecent(hex6: string): void {
|
|
156
|
+
const i = recents.findIndex((c) => c.toLowerCase() === hex6.toLowerCase());
|
|
157
|
+
if (i >= 0) recents.splice(i, 1);
|
|
158
|
+
recents.unshift(hex6);
|
|
159
|
+
if (recents.length > 10) recents.length = 10;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* When the opacity slider hands its value to `onChange` during a drag: `'live'` on every
|
|
164
|
+
* pointer move (the consumer repaints cheaply, so the chart previews the drag), `'release'`
|
|
165
|
+
* once on pointerup/pointercancel (the consumer recomputes — a script re-runs over its whole
|
|
166
|
+
* history, so a per-move commit would queue seconds of work behind the pointer). The knob,
|
|
167
|
+
* gradient and percentage follow the pointer either way; swatch picks always emit at once.
|
|
168
|
+
*/
|
|
169
|
+
export type ColorCommit = 'live' | 'release';
|
|
170
|
+
|
|
171
|
+
export interface ColorPickerOptions {
|
|
172
|
+
commit?: ColorCommit;
|
|
173
|
+
/** The translator of the picker's labels (default: the kit build's, else English). */
|
|
174
|
+
i18n?: Translator | null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const injectStyles = defineStyles(
|
|
178
|
+
'vela-ui-color',
|
|
179
|
+
css`
|
|
180
|
+
@layer vela.ui {
|
|
181
|
+
[data-slot='color-picker-swatch'] {
|
|
182
|
+
display: block;
|
|
183
|
+
width: 100%;
|
|
184
|
+
height: 100%;
|
|
185
|
+
border-radius: 0;
|
|
186
|
+
box-shadow: ${colorPickerDef.vars['swatch-ring']};
|
|
187
|
+
}
|
|
188
|
+
[data-slot='color-picker-trigger'] {
|
|
189
|
+
width: 24px;
|
|
190
|
+
height: 24px;
|
|
191
|
+
padding: 2px;
|
|
192
|
+
border: 1px solid ${t.border};
|
|
193
|
+
border-radius: 0;
|
|
194
|
+
background: ${t.surface.sunken};
|
|
195
|
+
cursor: pointer;
|
|
196
|
+
flex: none;
|
|
197
|
+
|
|
198
|
+
&:where(:not([hidden])) {
|
|
199
|
+
display: inline-flex;
|
|
200
|
+
}
|
|
201
|
+
&:where([data-shape='circle']) {
|
|
202
|
+
width: 26px;
|
|
203
|
+
height: 26px;
|
|
204
|
+
padding: 3px;
|
|
205
|
+
border: 1px solid ${t.border.strong};
|
|
206
|
+
border-radius: ${t.radius.sm};
|
|
207
|
+
background: transparent;
|
|
208
|
+
overflow: hidden;
|
|
209
|
+
}
|
|
210
|
+
&:hover {
|
|
211
|
+
border-color: ${t.fg.muted};
|
|
212
|
+
}
|
|
213
|
+
&:where([data-shape='circle']) [data-slot='color-picker-swatch'] {
|
|
214
|
+
border-radius: ${colorPickerDef.vars['circle-swatch-radius']};
|
|
215
|
+
box-shadow: none;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
[data-slot~='color-picker-content'] {
|
|
219
|
+
background: ${t.surface.overlay};
|
|
220
|
+
border: 1px solid ${t.border};
|
|
221
|
+
border-radius: ${colorPickerDef.vars['content-radius']};
|
|
222
|
+
box-shadow: ${t.shadow};
|
|
223
|
+
padding: 10px;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
`,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A self-contained color picker: a swatch grid (grays + hue × shade), a
|
|
231
|
+
* recents row with a custom "+" picker, and an opacity slider over a transparency checker.
|
|
232
|
+
* Emits `#RRGGBB` / `#RRGGBBAA` through `onChange`.
|
|
233
|
+
*/
|
|
234
|
+
export function buildColorPicker(color: string, theme: ChartTheme, onChange: (v: string) => void, options: ColorPickerOptions = {}): HTMLElement {
|
|
235
|
+
const commit: ColorCommit = options.commit ?? 'live';
|
|
236
|
+
const i18n = options.i18n ?? kitTranslator();
|
|
237
|
+
const parsed = splitColor(color);
|
|
238
|
+
let curHex = parsed.hex6;
|
|
239
|
+
let curAlpha = parsed.alpha;
|
|
240
|
+
|
|
241
|
+
const root = document.createElement('div');
|
|
242
|
+
root.dataset.slot = colorPickerDef.slot('root');
|
|
243
|
+
root.style.cssText = 'display:flex;flex-direction:column;gap:9px;width:236px;';
|
|
244
|
+
|
|
245
|
+
const grid = document.createElement('div');
|
|
246
|
+
grid.style.cssText = 'display:grid;grid-template-columns:repeat(10,1fr);gap:4px;';
|
|
247
|
+
const swatches: HTMLButtonElement[] = [];
|
|
248
|
+
for (const row of PALETTE) {
|
|
249
|
+
for (const c of row) {
|
|
250
|
+
const sw = document.createElement('button');
|
|
251
|
+
sw.type = 'button';
|
|
252
|
+
sw.dataset.c = c;
|
|
253
|
+
sw.setAttribute('aria-label', c);
|
|
254
|
+
sw.style.cssText = `width:100%;aspect-ratio:1;border-radius:4px;border:none;cursor:pointer;background:${c};padding:0;`;
|
|
255
|
+
sw.addEventListener('click', (e) => {
|
|
256
|
+
e.stopPropagation();
|
|
257
|
+
pickHex(c);
|
|
258
|
+
});
|
|
259
|
+
grid.appendChild(sw);
|
|
260
|
+
swatches.push(sw);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const paintSelection = (): void => {
|
|
264
|
+
for (const sw of swatches) {
|
|
265
|
+
const on = (sw.dataset.c ?? '').toLowerCase() === curHex.toLowerCase();
|
|
266
|
+
sw.setAttribute('aria-pressed', String(on));
|
|
267
|
+
sw.style.outline = on ? `2px solid ${theme.textColor}` : 'none';
|
|
268
|
+
sw.style.outlineOffset = '2px';
|
|
269
|
+
sw.style.zIndex = on ? '1' : '';
|
|
270
|
+
sw.style.position = on ? 'relative' : '';
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const recentRow = document.createElement('div');
|
|
275
|
+
recentRow.style.cssText = 'display:flex;align-items:center;gap:5px;flex-wrap:wrap;border-top:1px solid var(--vela-border);padding-top:9px;';
|
|
276
|
+
// Swatches get their own wrapper so re-rendering them never detaches the native
|
|
277
|
+
// color input — the browser closes its chooser the moment its input leaves the DOM.
|
|
278
|
+
const recentSwatches = document.createElement('div');
|
|
279
|
+
recentSwatches.style.cssText = 'display:contents;';
|
|
280
|
+
const add = document.createElement('label');
|
|
281
|
+
add.style.cssText = `width:17px;height:17px;border-radius:var(--vela-radius-sm);border:1px dashed var(--vela-border-strong);cursor:pointer;display:flex;align-items:center;justify-content:center;color:${theme.textColor};font:14px ${theme.fontFamily};position:relative;`;
|
|
282
|
+
add.textContent = '+';
|
|
283
|
+
const customInput = document.createElement('input');
|
|
284
|
+
customInput.type = 'color';
|
|
285
|
+
customInput.value = curHex;
|
|
286
|
+
customInput.style.cssText = 'position:absolute;inset:0;opacity:0;cursor:pointer;';
|
|
287
|
+
// `input` streams while the chooser is open (preview); `change` is the committed pick.
|
|
288
|
+
customInput.addEventListener('input', () => previewHex(customInput.value));
|
|
289
|
+
customInput.addEventListener('change', () => pickHex(customInput.value, true));
|
|
290
|
+
add.appendChild(customInput);
|
|
291
|
+
recentRow.append(recentSwatches, add);
|
|
292
|
+
const renderRecents = (): void => {
|
|
293
|
+
recentSwatches.replaceChildren();
|
|
294
|
+
for (const c of recents) {
|
|
295
|
+
const sw = document.createElement('button');
|
|
296
|
+
sw.type = 'button';
|
|
297
|
+
sw.setAttribute('aria-label', c);
|
|
298
|
+
sw.style.cssText = `width:17px;height:17px;border-radius:var(--vela-radius-sm);border:1px solid var(--vela-border);cursor:pointer;background:${c};padding:0;`;
|
|
299
|
+
sw.addEventListener('click', (e) => {
|
|
300
|
+
e.stopPropagation();
|
|
301
|
+
pickHex(c);
|
|
302
|
+
});
|
|
303
|
+
recentSwatches.appendChild(sw);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const opLabel = document.createElement('div');
|
|
308
|
+
opLabel.textContent = textOf(i18n, TEXT.opacity);
|
|
309
|
+
opLabel.style.cssText = `font:11px ${theme.fontFamily};color:var(--vela-fg-muted);`;
|
|
310
|
+
const opRow = document.createElement('div');
|
|
311
|
+
opRow.style.cssText = 'display:flex;align-items:center;gap:10px;';
|
|
312
|
+
const track = document.createElement('div');
|
|
313
|
+
track.style.cssText = 'flex:1;position:relative;height:13px;border-radius:7px;cursor:pointer;';
|
|
314
|
+
const knob = document.createElement('div');
|
|
315
|
+
knob.style.cssText = 'position:absolute;top:50%;width:15px;height:15px;border-radius:50%;background:var(--vela-selected-bg);box-shadow:0 1px 3px rgba(0,0,0,0.55);transform:translate(-50%,-50%);pointer-events:none;';
|
|
316
|
+
track.appendChild(knob);
|
|
317
|
+
const pctBox = document.createElement('input');
|
|
318
|
+
pctBox.type = 'text';
|
|
319
|
+
pctBox.inputMode = 'numeric';
|
|
320
|
+
pctBox.title = textOf(i18n, TEXT.opacityPercent);
|
|
321
|
+
pctBox.style.cssText = `width:46px;box-sizing:border-box;text-align:center;font:var(--vela-font-size-md) ${theme.fontFamily};color:var(--vela-fg);background:transparent;border:1px solid var(--vela-border);border-radius:5px;padding:3px 4px;outline:none;`;
|
|
322
|
+
pctBox.addEventListener('focus', () => {
|
|
323
|
+
pctBox.style.borderColor = 'var(--vela-border-strong)';
|
|
324
|
+
pctBox.select();
|
|
325
|
+
});
|
|
326
|
+
opRow.append(track, pctBox);
|
|
327
|
+
const paintOpacity = (): void => {
|
|
328
|
+
track.style.background = `linear-gradient(to right, ${curHex}00, ${curHex}ff), ${CHECKER}`;
|
|
329
|
+
knob.style.left = `${curAlpha * 100}%`;
|
|
330
|
+
pctBox.value = `${Math.round(curAlpha * 100)}%`;
|
|
331
|
+
};
|
|
332
|
+
// A typed value is one discrete edit, so it commits once under either `commit` policy.
|
|
333
|
+
// Unparseable input snaps back to the current opacity.
|
|
334
|
+
const commitTyped = (): void => {
|
|
335
|
+
pctBox.style.borderColor = 'var(--vela-border)';
|
|
336
|
+
const n = parseFloat(pctBox.value.replace('%', ''));
|
|
337
|
+
if (Number.isFinite(n)) {
|
|
338
|
+
const next = Math.max(0, Math.min(100, n)) / 100;
|
|
339
|
+
if (Math.round(next * 100) !== Math.round(curAlpha * 100)) {
|
|
340
|
+
curAlpha = next;
|
|
341
|
+
paintOpacity();
|
|
342
|
+
emit();
|
|
343
|
+
zagSlider.connect(slider.service, normalizeProps).setValue([next]);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
paintOpacity();
|
|
348
|
+
};
|
|
349
|
+
pctBox.addEventListener('change', commitTyped);
|
|
350
|
+
pctBox.addEventListener('blur', () => {
|
|
351
|
+
// `change` does not fire when the text is unchanged; still restore the "%" readout.
|
|
352
|
+
pctBox.style.borderColor = 'var(--vela-border)';
|
|
353
|
+
paintOpacity();
|
|
354
|
+
});
|
|
355
|
+
pctBox.addEventListener('keydown', (e) => {
|
|
356
|
+
// Typing must not reach chart/dialog shortcuts; Escape stays with the popover.
|
|
357
|
+
if (e.key === 'Escape') return;
|
|
358
|
+
e.stopPropagation();
|
|
359
|
+
if (e.key === 'Enter') {
|
|
360
|
+
e.preventDefault();
|
|
361
|
+
commitTyped();
|
|
362
|
+
pctBox.blur();
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
// The opacity is Zag's slider machine: the track is its control, the knob its thumb (the
|
|
366
|
+
// slider role, the arrow keys, Page Up/Down, Home/End). A move always previews (knob,
|
|
367
|
+
// readout, gradient); whether each move also COMMITS is the `commit` option — see
|
|
368
|
+
// {@link ColorCommit}. The knob and the track keep their own paint: the machine's inline
|
|
369
|
+
// styles are left out.
|
|
370
|
+
const pct = (a: number): number => Math.round(a * 100);
|
|
371
|
+
const sliderId = nextUid('vela-color-opacity');
|
|
372
|
+
// A press or a key on the slider is pending its end; the opacity typed in the readout
|
|
373
|
+
// (synced into the machine) is not, it has committed already.
|
|
374
|
+
let pressed = false;
|
|
375
|
+
const slider = runMachine(
|
|
376
|
+
zagSlider.machine,
|
|
377
|
+
{
|
|
378
|
+
id: sliderId,
|
|
379
|
+
min: 0,
|
|
380
|
+
max: 1,
|
|
381
|
+
step: 0.01,
|
|
382
|
+
defaultValue: [curAlpha],
|
|
383
|
+
thumbAlignment: 'center',
|
|
384
|
+
'aria-label': [textOf(i18n, TEXT.opacity)],
|
|
385
|
+
getAriaValueText: ({ value }) => `${pct(value)}%`,
|
|
386
|
+
onValueChange: ({ value: [a] }) => {
|
|
387
|
+
if (a === undefined || pct(a) === pct(curAlpha)) return;
|
|
388
|
+
curAlpha = a;
|
|
389
|
+
paintOpacity();
|
|
390
|
+
if (commit === 'live') emit();
|
|
391
|
+
},
|
|
392
|
+
onValueChangeEnd: ({ value: [a] }) => {
|
|
393
|
+
if (commit !== 'release' || !pressed) return;
|
|
394
|
+
pressed = false;
|
|
395
|
+
if (a !== undefined && pct(a) !== pct(curAlpha)) {
|
|
396
|
+
curAlpha = a;
|
|
397
|
+
paintOpacity();
|
|
398
|
+
}
|
|
399
|
+
emit();
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
(service) => {
|
|
403
|
+
const api = zagSlider.connect(service, normalizeProps);
|
|
404
|
+
const own = <T extends object>(props: T): T => {
|
|
405
|
+
const { style: _style, ...rest } = props as T & { style?: unknown };
|
|
406
|
+
return rest as T;
|
|
407
|
+
};
|
|
408
|
+
spreadProps(opRow, own(api.getRootProps()), sliderId);
|
|
409
|
+
spreadProps(track, own(api.getControlProps()), sliderId);
|
|
410
|
+
spreadProps(knob, own(api.getThumbProps({ index: 0 })), sliderId);
|
|
411
|
+
},
|
|
412
|
+
);
|
|
413
|
+
ownTeardown(root, () => slider.stop());
|
|
414
|
+
// The press belongs to the picker: an open popover or dialog must not read it as its own.
|
|
415
|
+
track.addEventListener('pointerdown', (e) => {
|
|
416
|
+
e.stopPropagation();
|
|
417
|
+
pressed = true;
|
|
418
|
+
});
|
|
419
|
+
knob.addEventListener('keydown', () => {
|
|
420
|
+
pressed = true;
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
function emit(): void {
|
|
424
|
+
onChange(combineColor(curHex, curAlpha));
|
|
425
|
+
}
|
|
426
|
+
function previewHex(hex: string): void {
|
|
427
|
+
curHex = splitColor(hex).hex6;
|
|
428
|
+
paintSelection();
|
|
429
|
+
paintOpacity();
|
|
430
|
+
emit();
|
|
431
|
+
}
|
|
432
|
+
function pickHex(hex: string, custom = false): void {
|
|
433
|
+
previewHex(hex);
|
|
434
|
+
addRecent(curHex);
|
|
435
|
+
customInput.value = curHex;
|
|
436
|
+
if (custom) renderRecents();
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
renderRecents();
|
|
440
|
+
paintSelection();
|
|
441
|
+
paintOpacity();
|
|
442
|
+
root.append(grid, recentRow, opLabel, opRow);
|
|
443
|
+
return root;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** Where the picker opens beside the swatch (shadcn's placement props; default below and
|
|
447
|
+
* end-aligned, 6px away). */
|
|
448
|
+
export interface ColorFieldOpts extends PlacementProps {
|
|
449
|
+
shape?: ColorFieldShape;
|
|
450
|
+
theme: ChartTheme;
|
|
451
|
+
getVal: () => string;
|
|
452
|
+
onVal: (v: string) => void;
|
|
453
|
+
id?: string;
|
|
454
|
+
/** The picker's host, layer and positioning strategy. `gap`, `align` and `boundary` here
|
|
455
|
+
* are deprecated: the field's placement props replace them. */
|
|
456
|
+
popover?: Pick<PopoverOptions, 'host' | 'position' | 'boundary' | 'zIndex' | 'gap' | 'align'>;
|
|
457
|
+
/** Opacity-drag commit policy of the popover picker (default `'live'`) — see {@link ColorCommit}. */
|
|
458
|
+
commit?: ColorCommit;
|
|
459
|
+
/** The translator of the picker's labels (default: the kit build's, else English). */
|
|
460
|
+
i18n?: Translator | null;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export type ColorFieldProps = ColorFieldOpts;
|
|
464
|
+
|
|
465
|
+
/** Closed-state swatch shape. `circle` is the settings-dialog preview (a square chip
|
|
466
|
+
* inset from a matching field border); `square` is the compact drawing-chrome swatch. */
|
|
467
|
+
export function colorField(
|
|
468
|
+
theme: ChartTheme,
|
|
469
|
+
getVal: () => string,
|
|
470
|
+
onVal: (v: string) => void,
|
|
471
|
+
opts?: { shape?: ColorFieldShape; id?: string; popover?: ColorFieldOpts['popover']; commit?: ColorCommit } & PlacementProps,
|
|
472
|
+
): HTMLElement {
|
|
473
|
+
return new ColorField({ ...opts, theme, getVal, onVal }).el;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export class ColorField {
|
|
477
|
+
readonly el: HTMLButtonElement;
|
|
478
|
+
private readonly swatch: HTMLElement;
|
|
479
|
+
private readonly getVal: () => string;
|
|
480
|
+
private readonly onVal: (v: string) => void;
|
|
481
|
+
private readonly theme: ChartTheme;
|
|
482
|
+
private readonly popoverOpts: ColorFieldOpts['popover'];
|
|
483
|
+
private readonly placement: PlacementProps;
|
|
484
|
+
private readonly commit: ColorCommit | undefined;
|
|
485
|
+
private readonly i18n: Translator | null;
|
|
486
|
+
private readonly release: () => void;
|
|
487
|
+
|
|
488
|
+
constructor(opts: ColorFieldOpts) {
|
|
489
|
+
injectStyles(document);
|
|
490
|
+
this.theme = opts.theme;
|
|
491
|
+
this.getVal = opts.getVal;
|
|
492
|
+
this.onVal = opts.onVal;
|
|
493
|
+
this.popoverOpts = opts.popover;
|
|
494
|
+
this.placement = opts;
|
|
495
|
+
this.commit = opts.commit;
|
|
496
|
+
this.i18n = opts.i18n ?? kitTranslator();
|
|
497
|
+
|
|
498
|
+
const circle = opts.shape === 'circle';
|
|
499
|
+
const swatch = h('span', { className: 'vela-color-field-swatch', slot: colorPickerDef.slot('swatch') });
|
|
500
|
+
const trigger = h(
|
|
501
|
+
'button',
|
|
502
|
+
{
|
|
503
|
+
className: circle ? 'vela-color-field vela-color-field-circle' : 'vela-color-field',
|
|
504
|
+
slot: colorPickerDef.slot('trigger'),
|
|
505
|
+
attrs: { type: 'button', id: opts.id },
|
|
506
|
+
data: { shape: opts.shape ?? 'square' },
|
|
507
|
+
},
|
|
508
|
+
swatch,
|
|
509
|
+
);
|
|
510
|
+
this.el = trigger;
|
|
511
|
+
this.swatch = swatch;
|
|
512
|
+
this.paint();
|
|
513
|
+
trigger.addEventListener('vela-sync', () => this.paint());
|
|
514
|
+
// Suppress the default mousedown focus: when this press outside-dismisses an open
|
|
515
|
+
// popover, Chrome's focus action can scroll the dialog body under the pointer.
|
|
516
|
+
trigger.addEventListener('pointerdown', (e) => {
|
|
517
|
+
e.preventDefault();
|
|
518
|
+
trigger.focus({ preventScroll: true });
|
|
519
|
+
});
|
|
520
|
+
trigger.addEventListener('click', (e) => {
|
|
521
|
+
e.stopPropagation();
|
|
522
|
+
this.toggle();
|
|
523
|
+
});
|
|
524
|
+
this.release = ownTeardown(trigger, () => {
|
|
525
|
+
if (openPopoverTrigger() === trigger) closeOpenPopovers();
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
destroy(): void {
|
|
530
|
+
this.release();
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
private paint(): void {
|
|
534
|
+
const v = this.getVal();
|
|
535
|
+
this.swatch.style.background = `linear-gradient(${v}, ${v}), ${FIELD_CHECKER}`;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
private toggle(): void {
|
|
539
|
+
if (openPopoverTrigger() === this.el) {
|
|
540
|
+
closeOpenPopovers();
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
const content = buildColorPicker(
|
|
544
|
+
this.getVal(),
|
|
545
|
+
this.theme,
|
|
546
|
+
(val) => {
|
|
547
|
+
this.onVal(val);
|
|
548
|
+
this.paint();
|
|
549
|
+
},
|
|
550
|
+
{ commit: this.commit, i18n: this.i18n },
|
|
551
|
+
);
|
|
552
|
+
const pop = new Popover({
|
|
553
|
+
trigger: this.el,
|
|
554
|
+
theme: this.theme,
|
|
555
|
+
side: this.placement.side,
|
|
556
|
+
align: this.placement.align ?? this.popoverOpts?.align ?? 'end',
|
|
557
|
+
sideOffset: this.placement.sideOffset ?? this.popoverOpts?.gap ?? 6,
|
|
558
|
+
alignOffset: this.placement.alignOffset,
|
|
559
|
+
avoidCollisions: this.placement.avoidCollisions,
|
|
560
|
+
collisionPadding: this.placement.collisionPadding,
|
|
561
|
+
host: this.popoverOpts?.host,
|
|
562
|
+
position: this.popoverOpts?.position,
|
|
563
|
+
collisionBoundary: this.placement.collisionBoundary ?? (this.popoverOpts?.boundary as PopoverBoundary | undefined),
|
|
564
|
+
zIndex: this.popoverOpts?.zIndex,
|
|
565
|
+
className: 'vela-color-field-pop',
|
|
566
|
+
slot: colorPickerDef.slot('content'),
|
|
567
|
+
content,
|
|
568
|
+
// A fresh popover per open: closing discards it.
|
|
569
|
+
onClose: () => disposeWithin(content),
|
|
570
|
+
});
|
|
571
|
+
pop.show();
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
export function createColorField(props: ColorFieldProps): ColorField {
|
|
576
|
+
return new ColorField(props);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** Close any open color (or other kit) popover — dialog teardown. */
|
|
580
|
+
export function closeColorPopover(): void {
|
|
581
|
+
closeOpenPopovers();
|
|
582
|
+
}
|