@luxalgo/vela-ui 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +91 -0
- package/LICENSE +201 -0
- package/NOTICE +28 -0
- package/README.md +86 -3
- package/cli/engine.mjs +282 -0
- package/cli/index.mjs +154 -0
- package/dist/badge.d.ts +26 -0
- package/dist/badge.js +44 -0
- package/dist/bar-chart.d.ts +40 -0
- package/dist/bar-chart.js +105 -0
- package/dist/button.d.ts +55 -0
- package/dist/button.js +11 -0
- package/dist/calendar.d.ts +24 -0
- package/dist/calendar.js +1 -0
- package/dist/callout-bubble.d.ts +103 -0
- package/dist/callout-bubble.js +177 -0
- package/dist/chart.d.ts +30 -0
- package/dist/chart.js +1 -0
- package/dist/checkbox.d.ts +60 -0
- package/dist/checkbox.js +12 -0
- package/dist/chunk-2GQIZM5Z.js +28 -0
- package/dist/chunk-3Q32K4PE.js +85 -0
- package/dist/chunk-6QCILIT2.js +52 -0
- package/dist/chunk-7VMN7NKJ.js +133 -0
- package/dist/chunk-A2FJ7Y7D.js +55 -0
- package/dist/chunk-A72NWYNP.js +26 -0
- package/dist/chunk-B5ZSRUDQ.js +227 -0
- package/dist/chunk-BXMWQIBO.js +66 -0
- package/dist/chunk-C3ICYMI3.js +25 -0
- package/dist/chunk-CC3DDNFC.js +117 -0
- package/dist/chunk-E2FE6V2N.js +81 -0
- package/dist/chunk-FN2BUFNM.js +10 -0
- package/dist/chunk-FVH6L7FM.js +18 -0
- package/dist/chunk-G7IQLY3A.js +183 -0
- package/dist/chunk-GVRX3UAV.js +82 -0
- package/dist/chunk-J2QJATCD.js +12 -0
- package/dist/chunk-LZWZV2ZS.js +18 -0
- package/dist/chunk-MJ4RUGX7.js +403 -0
- package/dist/chunk-MMTMOV6R.js +31 -0
- package/dist/chunk-P4MTEMT7.js +23 -0
- package/dist/chunk-Q6WTDVH6.js +31 -0
- package/dist/chunk-S7USVSYP.js +50 -0
- package/dist/chunk-TAKSIV57.js +90 -0
- package/dist/chunk-U3EH42YY.js +72 -0
- package/dist/chunk-V2Q4MRMS.js +573 -0
- package/dist/chunk-VMCR3S2A.js +22 -0
- package/dist/chunk-WBGSYCV2.js +101 -0
- package/dist/chunk-WGULXRWD.js +351 -0
- package/dist/chunk-WXH3EFV5.js +219 -0
- package/dist/chunk-X66RJAIS.js +14 -0
- package/dist/chunk-XBEL4K33.js +142 -0
- package/dist/chunk-Z5PINFQO.js +280 -0
- package/dist/collapsible.d.ts +49 -0
- package/dist/collapsible.js +106 -0
- package/dist/color-picker.d.ts +103 -0
- package/dist/color-picker.js +19 -0
- package/dist/color.d.ts +24 -0
- package/dist/color.js +1 -0
- package/dist/context-menu.d.ts +72 -0
- package/dist/context-menu.js +161 -0
- package/dist/css.d.ts +17 -0
- package/dist/css.js +4 -0
- package/dist/data-table.d.ts +40 -0
- package/dist/data-table.js +88 -0
- package/dist/date-picker.d.ts +90 -0
- package/dist/date-picker.js +257 -0
- package/dist/define-D5u3F5Dj.d.ts +21 -0
- package/dist/dialog.d.ts +89 -0
- package/dist/dialog.js +258 -0
- package/dist/dispose.d.ts +9 -0
- package/dist/dispose.js +2 -0
- package/dist/donut.d.ts +27 -0
- package/dist/donut.js +67 -0
- package/dist/drawer.d.ts +119 -0
- package/dist/drawer.js +326 -0
- package/dist/dropdown-menu.d.ts +114 -0
- package/dist/dropdown-menu.js +18 -0
- package/dist/empty.d.ts +21 -0
- package/dist/empty.js +38 -0
- package/dist/field.d.ts +147 -0
- package/dist/field.js +328 -0
- package/dist/glyph-select.d.ts +57 -0
- package/dist/glyph-select.js +17 -0
- package/dist/h-Bl37qUis.d.ts +24 -0
- package/dist/headless.d.ts +6 -0
- package/dist/headless.js +9 -0
- package/dist/hover-card.d.ts +43 -0
- package/dist/hover-card.js +97 -0
- package/dist/i18n.d.ts +20 -0
- package/dist/i18n.js +1 -0
- package/dist/icons.d.ts +20 -0
- package/dist/icons.js +2 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +13 -0
- package/dist/input-group.d.ts +57 -0
- package/dist/input-group.js +87 -0
- package/dist/input.d.ts +54 -0
- package/dist/input.js +9 -0
- package/dist/item.d.ts +72 -0
- package/dist/item.js +131 -0
- package/dist/kbd.d.ts +19 -0
- package/dist/kbd.js +8 -0
- package/dist/kit-text.d.ts +8 -0
- package/dist/kit-text.js +2 -0
- package/dist/line-chart.d.ts +32 -0
- package/dist/line-chart.js +120 -0
- package/dist/manifest.json +4508 -0
- package/dist/month-grid.d.ts +45 -0
- package/dist/month-grid.js +157 -0
- package/dist/number-input.d.ts +99 -0
- package/dist/number-input.js +14 -0
- package/dist/page.d.ts +7 -0
- package/dist/page.js +1 -0
- package/dist/palette.d.ts +55 -0
- package/dist/palette.js +1 -0
- package/dist/placement.d.ts +98 -0
- package/dist/placement.js +1 -0
- package/dist/popover.d.ts +107 -0
- package/dist/popover.js +15 -0
- package/dist/scatter-chart.d.ts +32 -0
- package/dist/scatter-chart.js +121 -0
- package/dist/select.d.ts +112 -0
- package/dist/select.js +19 -0
- package/dist/slider.d.ts +46 -0
- package/dist/slider.js +111 -0
- package/dist/sort-button.d.ts +41 -0
- package/dist/sort-button.js +88 -0
- package/dist/sparkline.d.ts +22 -0
- package/dist/sparkline.js +72 -0
- package/dist/stat.d.ts +29 -0
- package/dist/stat.js +41 -0
- package/dist/styles.d.ts +22 -0
- package/dist/styles.js +2 -0
- package/dist/surface-events.d.ts +17 -0
- package/dist/surface-events.js +1 -0
- package/dist/surface-exit.d.ts +25 -0
- package/dist/surface-exit.js +1 -0
- package/dist/switch.d.ts +54 -0
- package/dist/switch.js +92 -0
- package/dist/t.d.ts +89 -0
- package/dist/t.js +1 -0
- package/dist/tabs.d.ts +65 -0
- package/dist/tabs.js +155 -0
- package/dist/text-area.d.ts +54 -0
- package/dist/text-area.js +9 -0
- package/dist/theme.d.ts +199 -0
- package/dist/theme.js +8 -0
- package/dist/theme.schema.json +1994 -0
- package/dist/themes/dark.css +3 -0
- package/dist/themes/light.css +3 -0
- package/dist/toggle-group.d.ts +56 -0
- package/dist/toggle-group.js +130 -0
- package/dist/tokens.d.ts +16 -0
- package/dist/tokens.js +3 -0
- package/dist/tooltip.d.ts +47 -0
- package/dist/tooltip.js +74 -0
- package/dist/variants-B4stUBnY.d.ts +16 -0
- package/dist/zag.d.ts +19 -0
- package/dist/zag.js +2 -0
- package/docs/theming.md +217 -0
- package/docs/what-to-change.md +20 -0
- package/docs/writing-a-component.md +250 -0
- package/llms.txt +71 -0
- package/package.json +90 -6
- package/registry/badge.ts +119 -0
- package/registry/bar-chart.ts +215 -0
- package/registry/button.ts +379 -0
- package/registry/callout-bubble.ts +347 -0
- package/registry/checkbox.ts +234 -0
- package/registry/collapsible.ts +271 -0
- package/registry/color-picker.ts +582 -0
- package/registry/context-menu.ts +208 -0
- package/registry/data-table.ts +215 -0
- package/registry/date-picker.ts +483 -0
- package/registry/dialog.ts +535 -0
- package/registry/donut.ts +145 -0
- package/registry/drawer.ts +636 -0
- package/registry/dropdown-menu.ts +762 -0
- package/registry/empty.ts +91 -0
- package/registry/field.ts +585 -0
- package/registry/glyph-select.ts +268 -0
- package/registry/hover-card.ts +222 -0
- package/registry/input-group.ts +229 -0
- package/registry/input.ts +181 -0
- package/registry/item.ts +376 -0
- package/registry/kbd.ts +64 -0
- package/registry/line-chart.ts +218 -0
- package/registry/month-grid.ts +328 -0
- package/registry/number-input.ts +443 -0
- package/registry/popover.ts +368 -0
- package/registry/scatter-chart.ts +221 -0
- package/registry/select.ts +620 -0
- package/registry/slider.ts +259 -0
- package/registry/sort-button.ts +189 -0
- package/registry/sparkline.ts +114 -0
- package/registry/stat.ts +96 -0
- package/registry/switch.ts +205 -0
- package/registry/tabs.ts +426 -0
- package/registry/text-area.ts +186 -0
- package/registry/toggle-group.ts +295 -0
- package/registry/tooltip.ts +156 -0
- package/skills/theming-vela-ui/SKILL.md +137 -0
- package/skills/using-vela-ui-components/SKILL.md +137 -0
- package/skills/writing-vela-ui-components/SKILL.md +163 -0
- package/src/components/badge.ts +119 -0
- package/src/components/bar-chart.ts +215 -0
- package/src/components/button.ts +379 -0
- package/src/components/callout-bubble.ts +347 -0
- package/src/components/checkbox.ts +234 -0
- package/src/components/collapsible.ts +271 -0
- package/src/components/color-picker.ts +582 -0
- package/src/components/context-menu.ts +208 -0
- package/src/components/data-table.ts +215 -0
- package/src/components/date-picker.ts +483 -0
- package/src/components/dialog.ts +535 -0
- package/src/components/donut.ts +145 -0
- package/src/components/drawer.ts +636 -0
- package/src/components/dropdown-menu.ts +762 -0
- package/src/components/empty.ts +91 -0
- package/src/components/field.ts +585 -0
- package/src/components/glyph-select.ts +268 -0
- package/src/components/hover-card.ts +222 -0
- package/src/components/input-group.ts +229 -0
- package/src/components/input.ts +181 -0
- package/src/components/item.ts +376 -0
- package/src/components/kbd.ts +64 -0
- package/src/components/line-chart.ts +218 -0
- package/src/components/month-grid.ts +328 -0
- package/src/components/number-input.ts +443 -0
- package/src/components/popover.ts +368 -0
- package/src/components/scatter-chart.ts +221 -0
- package/src/components/select.ts +620 -0
- package/src/components/slider.ts +259 -0
- package/src/components/sort-button.ts +189 -0
- package/src/components/sparkline.ts +114 -0
- package/src/components/stat.ts +96 -0
- package/src/components/switch.ts +205 -0
- package/src/components/tabs.ts +426 -0
- package/src/components/text-area.ts +186 -0
- package/src/components/toggle-group.ts +295 -0
- package/src/components/tooltip.ts +156 -0
- package/src/index.ts +24 -0
- package/src/lib/calendar.ts +117 -0
- package/src/lib/chart.ts +72 -0
- package/src/lib/color.ts +89 -0
- package/src/lib/component-index.ts +151 -0
- package/src/lib/css.ts +48 -0
- package/src/lib/define.ts +45 -0
- package/src/lib/dispose.ts +36 -0
- package/src/lib/h.ts +55 -0
- package/src/lib/headless.ts +16 -0
- package/src/lib/i18n.ts +30 -0
- package/src/lib/icons.ts +237 -0
- package/src/lib/kit-text.ts +26 -0
- package/src/lib/page.ts +24 -0
- package/src/lib/palette.ts +81 -0
- package/src/lib/placement.ts +188 -0
- package/src/lib/scope.ts +135 -0
- package/src/lib/styles.ts +53 -0
- package/src/lib/surface-events.ts +31 -0
- package/src/lib/surface-exit.ts +87 -0
- package/src/lib/t.ts +68 -0
- package/src/lib/theme.ts +664 -0
- package/src/lib/tokens.ts +111 -0
- package/src/lib/variants.ts +32 -0
- package/src/lib/zag.ts +46 -0
- package/stylelint/index.mjs +60 -0
- package/stylelint/lint.mjs +81 -0
- package/stylelint/plugin.mjs +115 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { variants } from './chunk-LZWZV2ZS.js';
|
|
2
|
+
import { iconEl } from './chunk-G7IQLY3A.js';
|
|
3
|
+
import { ownTeardown } from './chunk-A72NWYNP.js';
|
|
4
|
+
import { nextUid, runMachine, normalizeProps, spreadProps } from './chunk-P4MTEMT7.js';
|
|
5
|
+
import { h } from './chunk-Q6WTDVH6.js';
|
|
6
|
+
import { defineComponent } from './chunk-VMCR3S2A.js';
|
|
7
|
+
import { defineStyles, css } from './chunk-XBEL4K33.js';
|
|
8
|
+
import { t } from './chunk-6QCILIT2.js';
|
|
9
|
+
import * as zagSwitch from '@zag-js/switch';
|
|
10
|
+
|
|
11
|
+
function checkboxController(opts = {}) {
|
|
12
|
+
let checked = opts.checked ?? opts.defaultChecked ?? false;
|
|
13
|
+
const disabled = opts.disabled ?? false;
|
|
14
|
+
return {
|
|
15
|
+
get checked() {
|
|
16
|
+
return checked;
|
|
17
|
+
},
|
|
18
|
+
disabled,
|
|
19
|
+
size: opts.size ?? "md",
|
|
20
|
+
tone: opts.tone ?? "bright",
|
|
21
|
+
setChecked(v) {
|
|
22
|
+
checked = v;
|
|
23
|
+
},
|
|
24
|
+
toggle() {
|
|
25
|
+
if (disabled) return checked;
|
|
26
|
+
checked = !checked;
|
|
27
|
+
opts.onCheckedChange?.(checked);
|
|
28
|
+
opts.onChange?.(checked);
|
|
29
|
+
return checked;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
var checkboxDef = defineComponent({
|
|
34
|
+
name: "checkbox",
|
|
35
|
+
parts: ["root", "icon"],
|
|
36
|
+
settings: { radius: "5px" }
|
|
37
|
+
});
|
|
38
|
+
var checkboxVariants = variants({ size: ["md", "sm"], tone: ["bright", "selected"] });
|
|
39
|
+
var injectStyles = defineStyles(
|
|
40
|
+
"vela-ui-checkbox",
|
|
41
|
+
css`@layer vela.ui{[data-slot=checkbox]{border:1px solid ${t.border};border-radius:${checkboxDef.vars.radius};color:#0000;cursor:pointer;width:20px;height:20px;transition:background ${t.dur.fast} ease, border-color ${t.dur.fast} ease, color ${t.dur.fast} ease;background:0 0;flex:none;justify-content:center;align-items:center;padding:0;line-height:0;position:relative}[data-slot=checkbox]:where(:not([hidden])){display:inline-flex}[data-slot=checkbox] [data-slot=checkbox-icon],[data-slot=checkbox] svg{width:12px;height:12px;display:block}[data-slot=checkbox][data-size=sm] [data-slot=checkbox-icon],[data-slot=checkbox][data-size=sm] svg{width:11px;height:11px}[data-slot=checkbox]:hover{border-color:${t.fg.muted}}[data-slot=checkbox][data-focus-visible]{outline:2px solid ${t.focus};outline-offset:1px}[data-slot=checkbox][data-state=checked]{background:${t.fg.bright};border-color:${t.fg.bright};color:${t.bg}}[data-slot=checkbox][data-size=sm]{border-color:${t.border.strong};width:18px;height:18px}[data-slot=checkbox][data-size=sm]:hover{border-color:${t.fg.muted}}[data-slot=checkbox][data-size=sm][data-state=checked]{background:${t.selected.bg};border-color:${t.selected.bg};color:${t.selected.fg}}[data-slot=checkbox][data-tone=selected][data-state=checked]{background:${t.selected.bg};border-color:${t.selected.bg};color:${t.selected.fg}}[data-slot=checkbox][data-disabled]{opacity:.4;cursor:default}}`
|
|
42
|
+
);
|
|
43
|
+
var Checkbox = class {
|
|
44
|
+
constructor(opts = {}) {
|
|
45
|
+
injectStyles(document);
|
|
46
|
+
this.mid = nextUid("vela-checkbox");
|
|
47
|
+
this.current = opts.checked ?? opts.defaultChecked ?? false;
|
|
48
|
+
const icon = iconEl("check", document);
|
|
49
|
+
icon.dataset.slot = checkboxDef.slot("icon");
|
|
50
|
+
this.input = h("input", {});
|
|
51
|
+
this.el = h("label", { className: "vela-switch", slot: checkboxDef.slot("root"), attrs: checkboxVariants({ size: opts.size ?? "md", tone: opts.tone ?? "bright" }) }, this.input, icon);
|
|
52
|
+
this.machine = runMachine(
|
|
53
|
+
zagSwitch.machine,
|
|
54
|
+
{
|
|
55
|
+
id: this.mid,
|
|
56
|
+
// The host's id names the focusable control, so its own `<label for>` reaches it.
|
|
57
|
+
...opts.id ? { ids: { hiddenInput: opts.id } } : {},
|
|
58
|
+
defaultChecked: this.current,
|
|
59
|
+
disabled: opts.disabled ?? false,
|
|
60
|
+
...opts.name !== void 0 ? { name: opts.name } : {},
|
|
61
|
+
...opts.required !== void 0 ? { required: opts.required } : {}
|
|
62
|
+
},
|
|
63
|
+
(service) => this.render(service)
|
|
64
|
+
);
|
|
65
|
+
this.release = ownTeardown(this.el, () => this.machine.stop());
|
|
66
|
+
this.input.addEventListener("change", () => {
|
|
67
|
+
this.current = this.input.checked;
|
|
68
|
+
opts.onCheckedChange?.(this.current);
|
|
69
|
+
opts.onChange?.(this.current);
|
|
70
|
+
});
|
|
71
|
+
this.el.addEventListener("pointerdown", (e) => {
|
|
72
|
+
if (opts.disabled) return;
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
this.input.focus({ preventScroll: true });
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
get checked() {
|
|
78
|
+
return this.current;
|
|
79
|
+
}
|
|
80
|
+
setChecked(v) {
|
|
81
|
+
if (v === this.current) return;
|
|
82
|
+
this.current = v;
|
|
83
|
+
this.api().setChecked(v);
|
|
84
|
+
}
|
|
85
|
+
destroy() {
|
|
86
|
+
this.release();
|
|
87
|
+
}
|
|
88
|
+
api() {
|
|
89
|
+
return zagSwitch.connect(this.machine.service, normalizeProps);
|
|
90
|
+
}
|
|
91
|
+
render(service) {
|
|
92
|
+
const api = zagSwitch.connect(service, normalizeProps);
|
|
93
|
+
spreadProps(this.el, api.getRootProps(), this.mid);
|
|
94
|
+
spreadProps(this.input, api.getHiddenInputProps(), this.mid);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
function createCheckbox(props = {}) {
|
|
98
|
+
return new Checkbox(props);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { Checkbox, checkboxController, checkboxDef, checkboxVariants, createCheckbox };
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { iconEl } from './chunk-G7IQLY3A.js';
|
|
2
|
+
import { kitTranslator } from './chunk-FVH6L7FM.js';
|
|
3
|
+
import { msg, textOf } from './chunk-FN2BUFNM.js';
|
|
4
|
+
import { announceSurface } from './chunk-J2QJATCD.js';
|
|
5
|
+
import { holdForExit } from './chunk-S7USVSYP.js';
|
|
6
|
+
import { floatingLayerHost } from './chunk-V2Q4MRMS.js';
|
|
7
|
+
import { zagPositioning, splitPlacement } from './chunk-3Q32K4PE.js';
|
|
8
|
+
import { normalizeProps, nextUid, runMachine, spreadProps } from './chunk-P4MTEMT7.js';
|
|
9
|
+
import { addSlot } from './chunk-Q6WTDVH6.js';
|
|
10
|
+
import { defineComponent } from './chunk-VMCR3S2A.js';
|
|
11
|
+
import { defineStyles, css } from './chunk-XBEL4K33.js';
|
|
12
|
+
import { t } from './chunk-6QCILIT2.js';
|
|
13
|
+
import * as zagMenu from '@zag-js/menu';
|
|
14
|
+
|
|
15
|
+
function dropdownMenuController(opts) {
|
|
16
|
+
return {
|
|
17
|
+
machine: zagMenu.machine,
|
|
18
|
+
props: {
|
|
19
|
+
id: opts.id ?? nextUid("vela-menu"),
|
|
20
|
+
ids: opts.triggerId ? { trigger: opts.triggerId } : void 0,
|
|
21
|
+
positioning: {
|
|
22
|
+
...zagPositioning(opts, opts.placement ? splitPlacement(opts.placement) : { side: "bottom", align: "start" }),
|
|
23
|
+
...opts.getAnchorRect ? { getAnchorRect: () => opts.getAnchorRect() } : {}
|
|
24
|
+
},
|
|
25
|
+
onSelect: (d) => opts.onSelect?.(d.value),
|
|
26
|
+
onOpenChange: (d) => opts.onOpenChange?.(d.open)
|
|
27
|
+
},
|
|
28
|
+
connect: (service) => zagMenu.connect(service, normalizeProps)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
var dropdownMenuDef = defineComponent({
|
|
32
|
+
name: "dropdown-menu",
|
|
33
|
+
parts: ["content", "item", "icon", "label", "hint", "mark", "badge", "star", "switch", "arrow", "separator"],
|
|
34
|
+
settings: { radius: t.radius.md, "switch-radius": "9px" }
|
|
35
|
+
});
|
|
36
|
+
var injectStyles = defineStyles(
|
|
37
|
+
"vela-ui-menu",
|
|
38
|
+
css`@layer vela.ui{[data-slot~=dropdown-menu-content]{background:${t.surface};color:${t.fg};border:1px solid ${t.border.strong};border-radius:${dropdownMenuDef.vars.radius};box-shadow:${t.shadow};min-width:180px;max-height:min(60vh, var(--available-height,60vh));z-index:${t.z.menu};outline:2px solid #0000;margin:0;padding:6px;font-size:13px;list-style:none;overflow-y:auto}[data-slot~=dropdown-menu-content][data-state=open]{animation:vela-menu-in ${t.dur.fast} ${t.ease}}@keyframes vela-menu-in{0%{opacity:0;transform:translateY(-3px)}to{opacity:1;transform:translateY(0)}}[data-slot~=dropdown-menu-item]{border-radius:${t.radius.sm};cursor:pointer;-webkit-user-select:none;user-select:none;white-space:nowrap;align-items:center;gap:10px;padding:7px 10px;display:flex}[data-slot~=dropdown-menu-item][data-highlighted]{background:${t.hover}}[data-slot~=dropdown-menu-item][data-disabled]{opacity:.4;cursor:default}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-icon]{width:16px;height:16px;color:${t.fg.muted};justify-content:center;font-size:16px}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-label]{flex:auto}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-hint]{color:${t.fg.faint};font-size:${t.fontSize.sm}}[data-slot~=dropdown-menu-item][data-checked]{background:${t.hover.strong};color:${t.fg.bright}}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-mark]{flex:none;justify-content:center;align-items:center;width:14px;display:inline-flex}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-mark] [data-slot~=dropdown-menu-icon]{width:14px;height:14px;color:${t.fg.bright};font-size:14px}[data-slot~=dropdown-menu-item][data-checkmark]{color:${t.fg.bright}}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-badge]{box-sizing:border-box;border-radius:${t.radius.md};background:${t.hover};border:1px solid ${t.border};width:24px;height:24px;color:${t.fg.muted};transition:background ${t.dur.fast} ${t.ease}, border-color ${t.dur.fast} ${t.ease}, color ${t.dur.fast} ${t.ease};flex:none;justify-content:center;align-items:center;padding:3px;display:inline-flex}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-badge]:empty{background:0 0;border-color:#0000}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-badge] [data-slot~=dropdown-menu-icon]{color:inherit}[data-slot~=dropdown-menu-item][data-highlighted] [data-slot~=dropdown-menu-badge]:not(:empty){color:${t.fg.bright}}[data-slot~=dropdown-menu-item][data-checked] [data-slot~=dropdown-menu-badge]:not(:empty),[data-slot~=dropdown-menu-item][data-checkmark] [data-slot~=dropdown-menu-badge]:not(:empty){background:color-mix(in srgb, ${t.fg.bright} 83%, ${t.surface});border-color:color-mix(in srgb, ${t.fg.bright} 40%, transparent);color:${t.surface}}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-star]{border-radius:${t.radius.sm};opacity:0;width:26px;height:22px;transition:opacity ${t.dur.fast} ease, background ${t.dur.fast} ease;order:98;margin-block:-3px;margin-inline:0 -5px;padding:3px 5px}[data-slot~=dropdown-menu-item][data-highlighted] [data-slot~=dropdown-menu-star]{opacity:.55}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-star]:hover{opacity:1;background:${t.hover.strong}}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-star][data-fav]{opacity:.95;color:${t.highlight}}[data-slot~=dropdown-menu-item] [data-slot~=dropdown-menu-arrow]{width:12px;color:${t.fg.faint};flex:none;order:99;margin-inline-start:auto;font-size:11px}[data-slot~=dropdown-menu-item][data-state=open]{background:${t.hover}}[data-slot~=dropdown-menu-content][data-badges]>[data-slot~=dropdown-menu-item]{padding-block:5px;padding-inline:6px 10px}[data-slot~=dropdown-menu-switch]{border-radius:${dropdownMenuDef.vars["switch-radius"]};background:${t.surface.overlay};border:1px solid ${t.border.soft};width:34px;height:18px;transition:background ${t.dur.med} ease, border-color ${t.dur.med} ease;flex:none;order:99;margin-inline-start:auto;position:relative}[data-slot~=dropdown-menu-switch]:after{content:"";top:2px;background:${t.fg.muted};width:12px;height:12px;transition:transform ${t.dur.med} ease, background ${t.dur.med} ease;border-radius:50%;position:absolute;inset-inline-start:2px}[data-slot~=dropdown-menu-switch][data-on]{background:${t.selected.bg};border-color:${t.selected.bg}}[data-slot~=dropdown-menu-switch][data-on]:after{background:${t.selected.fg};transform:translate(16px)}[data-slot~=dropdown-menu-separator]{background:${t.border};height:1px;margin:4px 6px}}`
|
|
39
|
+
);
|
|
40
|
+
function menuIcon(id, doc) {
|
|
41
|
+
const el = iconEl(id, doc);
|
|
42
|
+
el.dataset.slot = dropdownMenuDef.slot("icon");
|
|
43
|
+
return el;
|
|
44
|
+
}
|
|
45
|
+
var TEXT = {
|
|
46
|
+
addFavorite: msg("ui.menu.addFavorite", "Add to favorites"),
|
|
47
|
+
removeFavorite: msg("ui.menu.removeFavorite", "Remove from favorites")
|
|
48
|
+
};
|
|
49
|
+
var ROW_INSET_PX = 7;
|
|
50
|
+
var SUBMENU_GAP_PX = 4;
|
|
51
|
+
var Surface = class _Surface {
|
|
52
|
+
constructor(doc, opts) {
|
|
53
|
+
this.items = [];
|
|
54
|
+
/** Branch item id → the surface it opens. */
|
|
55
|
+
this.subs = /* @__PURE__ */ new Map();
|
|
56
|
+
/** Trigger rect captured when this level opened — the list stays put if the
|
|
57
|
+
* trigger then moves (favorite chips shifting the caret). */
|
|
58
|
+
this.pinnedAnchor = null;
|
|
59
|
+
/** The open state last announced — see {@link announce}. */
|
|
60
|
+
this.shown = false;
|
|
61
|
+
/** The list's exit animation after a close; a reopen cancels it. */
|
|
62
|
+
this.exit = null;
|
|
63
|
+
this.doc = doc;
|
|
64
|
+
this.host = opts.host;
|
|
65
|
+
this.onSelect = opts.onSelect;
|
|
66
|
+
this.onFavorite = opts.onFavorite;
|
|
67
|
+
this.i18n = opts.i18n;
|
|
68
|
+
this.checkmarks = opts.checkmarks === true;
|
|
69
|
+
this.iconBadges = opts.iconBadges === true;
|
|
70
|
+
this.positioner = doc.createElement("div");
|
|
71
|
+
this.positioner.className = "vela-ui-layer";
|
|
72
|
+
this.list = doc.createElement("ul");
|
|
73
|
+
this.list.className = "vela-menu";
|
|
74
|
+
this.list.dataset.slot = dropdownMenuDef.slot("content");
|
|
75
|
+
if (opts.minWidth) this.list.style.minWidth = opts.minWidth;
|
|
76
|
+
this.positioner.appendChild(this.list);
|
|
77
|
+
this.host.appendChild(this.positioner);
|
|
78
|
+
const trigger = opts.trigger;
|
|
79
|
+
this.opener = trigger ?? opts.opener ?? null;
|
|
80
|
+
this.ctrl = dropdownMenuController({
|
|
81
|
+
items: [],
|
|
82
|
+
id: opts.id,
|
|
83
|
+
...opts.placement,
|
|
84
|
+
onSelect: (id) => this.onSelect(id),
|
|
85
|
+
getAnchorRect: () => this.pinnedAnchor,
|
|
86
|
+
onOpenChange: (open) => {
|
|
87
|
+
if (open && trigger) {
|
|
88
|
+
const r = trigger.getBoundingClientRect();
|
|
89
|
+
this.pinnedAnchor = { x: r.x, y: r.y, width: r.width, height: r.height };
|
|
90
|
+
} else {
|
|
91
|
+
this.pinnedAnchor = null;
|
|
92
|
+
}
|
|
93
|
+
opts.onOpenChange?.(open);
|
|
94
|
+
},
|
|
95
|
+
triggerId: opts.triggerId
|
|
96
|
+
});
|
|
97
|
+
if (opts.submenu) {
|
|
98
|
+
this.ctrl.props.positioning = {
|
|
99
|
+
...this.ctrl.props.positioning,
|
|
100
|
+
offset: { mainAxis: ROW_INSET_PX + SUBMENU_GAP_PX },
|
|
101
|
+
shift: -ROW_INSET_PX
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
this.mid = String(this.ctrl.props.id);
|
|
105
|
+
if (opts.triggerId && trigger) trigger.id = opts.triggerId;
|
|
106
|
+
this.handle = runMachine(this.ctrl.machine, this.ctrl.props, (service) => {
|
|
107
|
+
const api = this.ctrl.connect(service);
|
|
108
|
+
const closing = this.shown && !api.open;
|
|
109
|
+
if (closing) this.announce(false);
|
|
110
|
+
if (api.open && this.exit) {
|
|
111
|
+
this.exit.cancel();
|
|
112
|
+
this.exit = null;
|
|
113
|
+
}
|
|
114
|
+
if (trigger) spreadProps(trigger, api.getTriggerProps(), this.mid);
|
|
115
|
+
spreadProps(this.positioner, api.getPositionerProps(), this.mid);
|
|
116
|
+
spreadProps(this.list, api.getContentProps(), this.mid);
|
|
117
|
+
this.project(api);
|
|
118
|
+
if (closing) this.holdExit();
|
|
119
|
+
if (api.open && !this.shown) this.announce(true);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/** Keep the just-closed list up through its exit animation. `spreadProps` re-applies
|
|
123
|
+
* `hidden` only when the machine's value for it changes, so the list stays shown until
|
|
124
|
+
* the exit ends — or until a reopen, which projects `hidden: false` itself. */
|
|
125
|
+
holdExit() {
|
|
126
|
+
this.list.hidden = false;
|
|
127
|
+
this.exit = holdForExit(this.list, () => {
|
|
128
|
+
this.exit = null;
|
|
129
|
+
if (!this.shown) this.list.hidden = true;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
get api() {
|
|
133
|
+
return this.ctrl.connect(this.handle.service);
|
|
134
|
+
}
|
|
135
|
+
announce(open) {
|
|
136
|
+
this.shown = open;
|
|
137
|
+
announceSurface(this.list, open, "menu", this.opener);
|
|
138
|
+
}
|
|
139
|
+
setItems(items) {
|
|
140
|
+
this.items = items;
|
|
141
|
+
for (const sub of this.subs.values()) sub.destroy();
|
|
142
|
+
this.subs.clear();
|
|
143
|
+
this.build();
|
|
144
|
+
const api = this.api;
|
|
145
|
+
for (const sub of this.subs.values()) {
|
|
146
|
+
api.setChild(sub.handle.service);
|
|
147
|
+
sub.api.setParent(this.handle.service);
|
|
148
|
+
}
|
|
149
|
+
this.handle.flush();
|
|
150
|
+
if (this.subs.size > 0) queueMicrotask(() => this.handle.flush());
|
|
151
|
+
}
|
|
152
|
+
destroy() {
|
|
153
|
+
for (const sub of this.subs.values()) sub.destroy();
|
|
154
|
+
this.subs.clear();
|
|
155
|
+
this.exit?.finish();
|
|
156
|
+
this.handle.stop();
|
|
157
|
+
if (this.shown) this.announce(false);
|
|
158
|
+
this.positioner.remove();
|
|
159
|
+
}
|
|
160
|
+
byId(id) {
|
|
161
|
+
return this.items.find((i) => i.id === id);
|
|
162
|
+
}
|
|
163
|
+
/** Push the machine's item props onto the rendered rows. A branch takes the submenu
|
|
164
|
+
* trigger props, which carry the child's ids and hover handlers; a leaf takes plain ones. */
|
|
165
|
+
project(api) {
|
|
166
|
+
for (const li of this.list.children) {
|
|
167
|
+
const id = li.dataset.veiId;
|
|
168
|
+
if (!id) continue;
|
|
169
|
+
const sub = this.subs.get(id);
|
|
170
|
+
if (sub) {
|
|
171
|
+
spreadProps(li, api.getTriggerItemProps(sub.api), this.mid);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const item = this.byId(id);
|
|
175
|
+
spreadProps(li, api.getItemProps({ value: id, disabled: item?.disabled, closeOnSelect: item?.toggle ? false : void 0 }), this.mid);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
build() {
|
|
179
|
+
const doc = this.doc;
|
|
180
|
+
this.list.replaceChildren();
|
|
181
|
+
const markable = this.checkmarks && this.items.some((i) => !(i.submenu && i.submenu.length > 0) && !i.toggle && i.checked !== void 0);
|
|
182
|
+
const iconic = this.items.some((i) => !!i.icon);
|
|
183
|
+
const badged = this.iconBadges && iconic;
|
|
184
|
+
if (badged) this.list.dataset.badges = "1";
|
|
185
|
+
else delete this.list.dataset.badges;
|
|
186
|
+
for (const item of this.items) {
|
|
187
|
+
if (item.separatorBefore) {
|
|
188
|
+
const sep = doc.createElement("li");
|
|
189
|
+
sep.className = "vela-menu-sep";
|
|
190
|
+
sep.dataset.slot = dropdownMenuDef.slot("separator");
|
|
191
|
+
sep.setAttribute("role", "separator");
|
|
192
|
+
this.list.appendChild(sep);
|
|
193
|
+
}
|
|
194
|
+
const branch = item.submenu !== void 0 && item.submenu.length > 0;
|
|
195
|
+
const li = doc.createElement("li");
|
|
196
|
+
li.className = "vela-menu-item";
|
|
197
|
+
li.dataset.slot = dropdownMenuDef.slot("item");
|
|
198
|
+
li.dataset.veiId = item.id;
|
|
199
|
+
if (item.toggle) {
|
|
200
|
+
li.dataset.toggle = "1";
|
|
201
|
+
}
|
|
202
|
+
if (markable) {
|
|
203
|
+
const mark = doc.createElement("span");
|
|
204
|
+
mark.className = "vela-menu-mark";
|
|
205
|
+
mark.dataset.slot = dropdownMenuDef.slot("mark");
|
|
206
|
+
if (!branch && !item.toggle && item.checked) {
|
|
207
|
+
mark.appendChild(menuIcon("check", doc));
|
|
208
|
+
li.dataset.checkmark = "1";
|
|
209
|
+
}
|
|
210
|
+
li.appendChild(mark);
|
|
211
|
+
} else if (!branch && !item.toggle && item.checked) {
|
|
212
|
+
li.dataset.checked = "1";
|
|
213
|
+
}
|
|
214
|
+
if (badged) {
|
|
215
|
+
const badge = doc.createElement("span");
|
|
216
|
+
badge.className = "vela-menu-badge";
|
|
217
|
+
badge.dataset.slot = dropdownMenuDef.slot("badge");
|
|
218
|
+
if (item.icon) badge.appendChild(menuIcon(item.icon, doc));
|
|
219
|
+
li.appendChild(badge);
|
|
220
|
+
} else if (item.icon) {
|
|
221
|
+
li.appendChild(menuIcon(item.icon, doc));
|
|
222
|
+
} else if (iconic) {
|
|
223
|
+
li.appendChild(menuIcon("", doc));
|
|
224
|
+
}
|
|
225
|
+
const label = doc.createElement("span");
|
|
226
|
+
label.className = "vela-menu-label";
|
|
227
|
+
label.dataset.slot = dropdownMenuDef.slot("label");
|
|
228
|
+
label.textContent = item.label;
|
|
229
|
+
li.appendChild(label);
|
|
230
|
+
if (item.hint) {
|
|
231
|
+
const hint = doc.createElement("span");
|
|
232
|
+
hint.className = "vela-menu-hint";
|
|
233
|
+
hint.dataset.slot = dropdownMenuDef.slot("hint");
|
|
234
|
+
hint.textContent = item.hint;
|
|
235
|
+
li.appendChild(hint);
|
|
236
|
+
}
|
|
237
|
+
if (item.favorite !== void 0 && this.onFavorite) {
|
|
238
|
+
const on = item.favorite;
|
|
239
|
+
const star = menuIcon(on ? "star-filled" : "star", doc);
|
|
240
|
+
star.classList.add("vela-menu-star");
|
|
241
|
+
addSlot(star, dropdownMenuDef.slot("star"));
|
|
242
|
+
if (on) {
|
|
243
|
+
star.classList.add("vela-fav");
|
|
244
|
+
star.dataset.fav = "";
|
|
245
|
+
}
|
|
246
|
+
star.removeAttribute("aria-hidden");
|
|
247
|
+
star.setAttribute("role", "button");
|
|
248
|
+
star.setAttribute("aria-label", textOf(this.i18n, on ? TEXT.removeFavorite : TEXT.addFavorite));
|
|
249
|
+
star.setAttribute("aria-pressed", on ? "true" : "false");
|
|
250
|
+
for (const ev of ["pointerdown", "pointerup", "mousedown", "mouseup"]) {
|
|
251
|
+
star.addEventListener(ev, (e) => e.stopPropagation());
|
|
252
|
+
}
|
|
253
|
+
star.addEventListener("click", (e) => {
|
|
254
|
+
e.stopPropagation();
|
|
255
|
+
e.preventDefault();
|
|
256
|
+
this.onFavorite?.(item.id, !on);
|
|
257
|
+
});
|
|
258
|
+
li.appendChild(star);
|
|
259
|
+
}
|
|
260
|
+
if (item.toggle) {
|
|
261
|
+
const sw = doc.createElement("span");
|
|
262
|
+
sw.className = "vela-menu-switch" + (item.checked ? " on" : "");
|
|
263
|
+
sw.dataset.slot = dropdownMenuDef.slot("switch");
|
|
264
|
+
if (item.checked) sw.dataset.on = "";
|
|
265
|
+
sw.setAttribute("aria-hidden", "true");
|
|
266
|
+
li.appendChild(sw);
|
|
267
|
+
}
|
|
268
|
+
if (branch) {
|
|
269
|
+
li.dataset.branch = "1";
|
|
270
|
+
const arrow = menuIcon("chevron-right", doc);
|
|
271
|
+
arrow.classList.add("vela-menu-arrow");
|
|
272
|
+
addSlot(arrow, dropdownMenuDef.slot("arrow"));
|
|
273
|
+
li.appendChild(arrow);
|
|
274
|
+
const sub = new _Surface(doc, {
|
|
275
|
+
host: this.host,
|
|
276
|
+
placement: { placement: "right-start" },
|
|
277
|
+
submenu: true,
|
|
278
|
+
opener: li,
|
|
279
|
+
onSelect: this.onSelect,
|
|
280
|
+
onFavorite: this.onFavorite,
|
|
281
|
+
i18n: this.i18n,
|
|
282
|
+
checkmarks: this.checkmarks,
|
|
283
|
+
iconBadges: this.iconBadges,
|
|
284
|
+
id: `${this.mid}--${item.id}`
|
|
285
|
+
});
|
|
286
|
+
sub.setItems(item.submenu ?? []);
|
|
287
|
+
this.subs.set(item.id, sub);
|
|
288
|
+
}
|
|
289
|
+
this.list.appendChild(li);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
var DropdownMenu = class {
|
|
294
|
+
constructor(opts) {
|
|
295
|
+
const anchor = opts.trigger ?? opts.host ?? document.body;
|
|
296
|
+
const doc = anchor.ownerDocument;
|
|
297
|
+
injectStyles(doc);
|
|
298
|
+
const host = floatingLayerHost(opts.host ?? anchor, doc.body);
|
|
299
|
+
this.root = new Surface(doc, {
|
|
300
|
+
host,
|
|
301
|
+
placement: {
|
|
302
|
+
placement: opts.placement,
|
|
303
|
+
side: opts.side,
|
|
304
|
+
align: opts.align,
|
|
305
|
+
sideOffset: opts.sideOffset,
|
|
306
|
+
alignOffset: opts.alignOffset,
|
|
307
|
+
avoidCollisions: opts.avoidCollisions,
|
|
308
|
+
collisionPadding: opts.collisionPadding,
|
|
309
|
+
collisionBoundary: opts.collisionBoundary
|
|
310
|
+
},
|
|
311
|
+
onSelect: (id) => opts.onSelect?.(id),
|
|
312
|
+
onOpenChange: opts.onOpenChange,
|
|
313
|
+
trigger: opts.trigger,
|
|
314
|
+
triggerId: opts.triggerId,
|
|
315
|
+
id: opts.id,
|
|
316
|
+
minWidth: opts.minWidth,
|
|
317
|
+
onFavorite: opts.onFavorite,
|
|
318
|
+
i18n: opts.i18n ?? kitTranslator(),
|
|
319
|
+
checkmarks: opts.checkmarks,
|
|
320
|
+
iconBadges: opts.iconBadges
|
|
321
|
+
});
|
|
322
|
+
this.root.setItems(opts.items);
|
|
323
|
+
}
|
|
324
|
+
get api() {
|
|
325
|
+
return this.root.api;
|
|
326
|
+
}
|
|
327
|
+
open() {
|
|
328
|
+
this.api.setOpen(true);
|
|
329
|
+
}
|
|
330
|
+
/** Open anchored to a viewport point (context menus). */
|
|
331
|
+
openAt(clientX, clientY) {
|
|
332
|
+
const api = this.api;
|
|
333
|
+
api.setOpen(true);
|
|
334
|
+
api.reposition({ getAnchorRect: () => ({ x: clientX, y: clientY, width: 0, height: 0 }) });
|
|
335
|
+
}
|
|
336
|
+
close() {
|
|
337
|
+
this.api.setOpen(false);
|
|
338
|
+
}
|
|
339
|
+
/** Swap the item descriptors (e.g. checked states) and re-project. */
|
|
340
|
+
setItems(items) {
|
|
341
|
+
this.root.setItems(items);
|
|
342
|
+
}
|
|
343
|
+
destroy() {
|
|
344
|
+
this.root.destroy();
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
function createDropdownMenu(props) {
|
|
348
|
+
return new DropdownMenu(props);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export { DropdownMenu, createDropdownMenu, dropdownMenuController, dropdownMenuDef };
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { variants } from './chunk-LZWZV2ZS.js';
|
|
2
|
+
import { iconEl } from './chunk-G7IQLY3A.js';
|
|
3
|
+
import { kitTranslator } from './chunk-FVH6L7FM.js';
|
|
4
|
+
import { msg, textOf } from './chunk-FN2BUFNM.js';
|
|
5
|
+
import { ownTeardown } from './chunk-A72NWYNP.js';
|
|
6
|
+
import { nextUid, runMachine, normalizeProps, spreadProps } from './chunk-P4MTEMT7.js';
|
|
7
|
+
import { h } from './chunk-Q6WTDVH6.js';
|
|
8
|
+
import { defineComponent } from './chunk-VMCR3S2A.js';
|
|
9
|
+
import { css, defineStyles } from './chunk-XBEL4K33.js';
|
|
10
|
+
import { t } from './chunk-6QCILIT2.js';
|
|
11
|
+
import * as zagNumber from '@zag-js/number-input';
|
|
12
|
+
|
|
13
|
+
function clampNumber(n, opts) {
|
|
14
|
+
let v = n;
|
|
15
|
+
if (opts.min !== void 0) v = Math.max(opts.min, v);
|
|
16
|
+
if (opts.max !== void 0) v = Math.min(opts.max, v);
|
|
17
|
+
if (opts.integer) v = Math.round(v);
|
|
18
|
+
return v;
|
|
19
|
+
}
|
|
20
|
+
function decimalsOf(n) {
|
|
21
|
+
const s = String(n);
|
|
22
|
+
const e = s.search(/[eE]/);
|
|
23
|
+
if (e >= 0) {
|
|
24
|
+
const frac = s.slice(0, e).split(".")[1]?.length ?? 0;
|
|
25
|
+
return Math.max(0, Math.min(20, frac - Number(s.slice(e + 1))));
|
|
26
|
+
}
|
|
27
|
+
return s.split(".")[1]?.length ?? 0;
|
|
28
|
+
}
|
|
29
|
+
function snapToStep(n, base, step) {
|
|
30
|
+
const d = Math.min(20, Math.max(decimalsOf(base), decimalsOf(step)));
|
|
31
|
+
return Number(n.toFixed(d));
|
|
32
|
+
}
|
|
33
|
+
function numberInputController(opts) {
|
|
34
|
+
let value = opts.value;
|
|
35
|
+
const integer = opts.integer ?? false;
|
|
36
|
+
const commit = opts.commit ?? "blur";
|
|
37
|
+
const clamp = opts.clamp ?? commit === "blur";
|
|
38
|
+
const step = opts.step ?? (integer ? 1 : 0.1);
|
|
39
|
+
const onChange = opts.onChange;
|
|
40
|
+
const normalize = (raw) => clamp ? clampNumber(raw, { min: opts.min, max: opts.max, integer }) : raw;
|
|
41
|
+
const apply = (raw) => {
|
|
42
|
+
if (!Number.isFinite(raw)) return null;
|
|
43
|
+
const n = normalize(raw);
|
|
44
|
+
if (n !== value) {
|
|
45
|
+
value = n;
|
|
46
|
+
onChange?.(n);
|
|
47
|
+
}
|
|
48
|
+
return n;
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
get value() {
|
|
52
|
+
return value;
|
|
53
|
+
},
|
|
54
|
+
min: opts.min,
|
|
55
|
+
max: opts.max,
|
|
56
|
+
step,
|
|
57
|
+
integer,
|
|
58
|
+
size: opts.size ?? "md",
|
|
59
|
+
commit,
|
|
60
|
+
steppers: opts.steppers ?? commit === "blur",
|
|
61
|
+
clamp,
|
|
62
|
+
disabled: opts.disabled ?? false,
|
|
63
|
+
apply,
|
|
64
|
+
sync(raw) {
|
|
65
|
+
const n = Number.isFinite(raw) ? normalize(raw) : value;
|
|
66
|
+
value = n;
|
|
67
|
+
return n;
|
|
68
|
+
},
|
|
69
|
+
nudge(dir) {
|
|
70
|
+
return apply(snapToStep(value + dir * step, value, step));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
var numberInputDef = defineComponent({
|
|
75
|
+
name: "number-input",
|
|
76
|
+
parts: ["root", "input", "steps", "step"],
|
|
77
|
+
settings: { radius: t.radius.md, "step-radius": "3px", "focus-ring": `0 0 0 3px ${t.focus.soft}` }
|
|
78
|
+
});
|
|
79
|
+
var numberInputVariants = variants({ size: ["md", "sm"] });
|
|
80
|
+
var TEXT = {
|
|
81
|
+
increase: msg("ui.numberInput.increase", "Increase"),
|
|
82
|
+
decrease: msg("ui.numberInput.decrease", "Decrease")
|
|
83
|
+
};
|
|
84
|
+
var NUMBER_STYLE_ID = "vela-ui-number-3";
|
|
85
|
+
var PLAIN = new Intl.NumberFormat("en-US", { useGrouping: false, maximumFractionDigits: 20 });
|
|
86
|
+
var NUMBER_CSS = css`@layer vela.ui{[data-slot=number-input-input]{box-sizing:border-box;border:1px solid ${t.border.strong};border-radius:${numberInputDef.vars.radius};width:100%;height:34px;color:${t.fg.bright};transition:border-color ${t.dur.fast} ease, box-shadow ${t.dur.fast} ease;appearance:textfield;-webkit-user-select:text;user-select:text;background:0 0;outline:2px solid #0000;padding:0 8px;font-family:inherit;font-size:14px}[data-slot=number-input-input]::-webkit-inner-spin-button{-webkit-appearance:none;margin:0}[data-slot=number-input-input]::-webkit-outer-spin-button{-webkit-appearance:none;margin:0}[data-slot=number-input-input]:hover{border-color:${t.fg.muted}}[data-slot=number-input-input]:focus{border-color:${t.focus};box-shadow:${numberInputDef.vars["focus-ring"]}}[data-slot=number-input-steps]{inset-inline-end:0;box-sizing:border-box;flex-direction:column;justify-content:center;width:22px;padding-block:2px;padding-inline:0 6px;line-height:0;display:none;position:absolute;top:0;bottom:0}[data-slot=number-input-step]{width:100%;min-height:0;color:${t.fg.muted};border-radius:${numberInputDef.vars["step-radius"]};cursor:pointer;background:0 0;border:none;flex:1;justify-content:center;align-items:center;padding:0;display:flex}[data-slot=number-input-step]:hover{background:${t.hover};color:${t.fg.bright}}[data-slot=number-input-step]>span,[data-slot=number-input-step] svg{width:12px;height:12px;display:block}[data-slot=number-input]{min-width:0;position:relative}[data-slot=number-input]:where(:not([hidden])){display:inline-block}[data-slot=number-input][data-fill]{width:100%}[data-slot=number-input]:not([data-fill]){flex:none;justify-self:start;width:100px}[data-slot=number-input]:not([data-fill])[data-compact]{width:80px}[data-slot=number-input][data-size=sm]{flex:none;width:64px}[data-slot=number-input][data-size=sm][data-compact]{width:56px}[data-slot=number-input][data-size=sm] [data-slot=number-input-input]{background:${t.surface.elev};border-radius:${t.radius.sm};height:28px;color:${t.fg};box-shadow:none;font-size:13px}[data-slot=number-input][data-size=sm] [data-slot=number-input-input]:focus{box-shadow:none}[data-slot=number-input][data-steppers]:hover [data-slot=number-input-input]{padding-inline-end:26px}[data-slot=number-input][data-steppers]:hover [data-slot=number-input-steps]{display:flex}}`;
|
|
87
|
+
var injectStyles = defineStyles(NUMBER_STYLE_ID, NUMBER_CSS);
|
|
88
|
+
var NumberInput = class {
|
|
89
|
+
constructor(opts) {
|
|
90
|
+
/** The user is typing a value the field commits on blur: the machine's reports of it
|
|
91
|
+
* are not commits. */
|
|
92
|
+
this.typing = false;
|
|
93
|
+
this.up = null;
|
|
94
|
+
this.down = null;
|
|
95
|
+
injectStyles(document);
|
|
96
|
+
this.i18n = opts.i18n ?? kitTranslator();
|
|
97
|
+
this.ctrl = numberInputController(opts);
|
|
98
|
+
this.placeholderMode = opts.placeholder !== void 0;
|
|
99
|
+
this.emptyValue = opts.emptyValue ?? opts.value;
|
|
100
|
+
this.mid = nextUid("vela-number-input");
|
|
101
|
+
const ni = h("input", { slot: numberInputDef.slot("input"), attrs: { title: opts.title } });
|
|
102
|
+
if (this.placeholderMode) ni.placeholder = opts.placeholder ?? "";
|
|
103
|
+
this.input = ni;
|
|
104
|
+
const commitRaw = (raw) => {
|
|
105
|
+
this.typing = false;
|
|
106
|
+
const n = this.ctrl.apply(raw);
|
|
107
|
+
this.show(n === null ? this.ctrl.value : n);
|
|
108
|
+
};
|
|
109
|
+
ni.addEventListener("input", () => {
|
|
110
|
+
if (this.ctrl.commit === "live" && !this.placeholderMode) {
|
|
111
|
+
const v = Number(ni.value);
|
|
112
|
+
if (ni.value.trim() !== "" && Number.isFinite(v)) this.ctrl.apply(v);
|
|
113
|
+
} else this.typing = true;
|
|
114
|
+
});
|
|
115
|
+
if (this.placeholderMode) {
|
|
116
|
+
ni.addEventListener("change", () => {
|
|
117
|
+
const raw = ni.value.trim() === "" ? this.emptyValue : Number(ni.value);
|
|
118
|
+
commitRaw(Number.isFinite(raw) ? raw : this.emptyValue);
|
|
119
|
+
});
|
|
120
|
+
} else if (this.ctrl.commit !== "live") {
|
|
121
|
+
ni.addEventListener("blur", () => commitRaw(Number(ni.value)));
|
|
122
|
+
ni.addEventListener("keydown", (e) => {
|
|
123
|
+
if (e.key === "Enter") {
|
|
124
|
+
e.preventDefault();
|
|
125
|
+
ni.blur();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
ni.addEventListener("keydown", (e) => {
|
|
130
|
+
if (e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "PageUp" || e.key === "PageDown") this.typing = false;
|
|
131
|
+
});
|
|
132
|
+
const steppers = this.ctrl.steppers ? this.buildSteppers(document) : null;
|
|
133
|
+
this.el = h(
|
|
134
|
+
"div",
|
|
135
|
+
{
|
|
136
|
+
className: "vela-num",
|
|
137
|
+
slot: numberInputDef.slot("root"),
|
|
138
|
+
attrs: numberInputVariants({ size: this.ctrl.size }),
|
|
139
|
+
data: { fill: opts.fill ?? this.ctrl.size !== "sm", compact: opts.compact === true, steppers: this.ctrl.steppers }
|
|
140
|
+
},
|
|
141
|
+
ni,
|
|
142
|
+
steppers
|
|
143
|
+
);
|
|
144
|
+
const clamp = this.ctrl.clamp;
|
|
145
|
+
this.machine = runMachine(
|
|
146
|
+
zagNumber.machine,
|
|
147
|
+
{
|
|
148
|
+
id: this.mid,
|
|
149
|
+
...opts.id ? { ids: { input: opts.id } } : {},
|
|
150
|
+
defaultValue: this.placeholderMode && opts.value === this.emptyValue ? "" : String(opts.value),
|
|
151
|
+
step: this.ctrl.step,
|
|
152
|
+
...clamp && this.ctrl.min !== void 0 ? { min: this.ctrl.min } : {},
|
|
153
|
+
...clamp && this.ctrl.max !== void 0 ? { max: this.ctrl.max } : {},
|
|
154
|
+
clampValueOnBlur: clamp,
|
|
155
|
+
allowOverflow: !clamp,
|
|
156
|
+
disabled: this.ctrl.disabled,
|
|
157
|
+
...opts.name !== void 0 ? { name: opts.name } : {},
|
|
158
|
+
...opts.required !== void 0 ? { required: opts.required } : {},
|
|
159
|
+
// Plain numbers, as the field has always shown them: no grouping, every decimal.
|
|
160
|
+
locale: "en-US",
|
|
161
|
+
formatOptions: { useGrouping: false, maximumFractionDigits: 20 },
|
|
162
|
+
// A press on a stepper keeps the focus where it is, and repeats while held.
|
|
163
|
+
focusInputOnChange: false,
|
|
164
|
+
spinOnPress: true,
|
|
165
|
+
translations: { incrementLabel: textOf(this.i18n, TEXT.increase), decrementLabel: textOf(this.i18n, TEXT.decrease) },
|
|
166
|
+
// The steppers' and the arrow keys' steps arrive here, a beat after the press.
|
|
167
|
+
onValueChange: ({ valueAsNumber }) => {
|
|
168
|
+
if (this.typing || !Number.isFinite(valueAsNumber) || valueAsNumber === this.ctrl.value) return;
|
|
169
|
+
commitRaw(valueAsNumber);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
(service) => this.render(service)
|
|
173
|
+
);
|
|
174
|
+
this.release = ownTeardown(this.el, () => this.machine.stop());
|
|
175
|
+
}
|
|
176
|
+
get value() {
|
|
177
|
+
return this.ctrl.value;
|
|
178
|
+
}
|
|
179
|
+
setValue(v) {
|
|
180
|
+
this.typing = false;
|
|
181
|
+
this.show(this.ctrl.sync(v));
|
|
182
|
+
}
|
|
183
|
+
destroy() {
|
|
184
|
+
this.release();
|
|
185
|
+
}
|
|
186
|
+
/** Show `n` in the field: through the machine, so its next render keeps it. */
|
|
187
|
+
show(n) {
|
|
188
|
+
const api = zagNumber.connect(this.machine.service, normalizeProps);
|
|
189
|
+
if (this.placeholderMode && n === this.emptyValue) {
|
|
190
|
+
if (!api.empty) api.clearValue();
|
|
191
|
+
this.input.value = "";
|
|
192
|
+
} else {
|
|
193
|
+
if (api.valueAsNumber !== n) api.setValue(n);
|
|
194
|
+
this.input.value = PLAIN.format(n);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
render(service) {
|
|
198
|
+
const api = zagNumber.connect(service, normalizeProps);
|
|
199
|
+
spreadProps(this.el, api.getRootProps(), this.mid);
|
|
200
|
+
spreadProps(this.input, api.getInputProps(), this.mid);
|
|
201
|
+
if (this.up) spreadProps(this.up, api.getIncrementTriggerProps(), this.mid);
|
|
202
|
+
if (this.down) spreadProps(this.down, api.getDecrementTriggerProps(), this.mid);
|
|
203
|
+
}
|
|
204
|
+
buildSteppers(doc) {
|
|
205
|
+
const mk = (icon) => {
|
|
206
|
+
const b = h("button", { slot: numberInputDef.slot("step"), attrs: { type: "button", tabindex: -1 } }, iconEl(icon, doc));
|
|
207
|
+
b.addEventListener("pointerdown", (e) => e.stopPropagation());
|
|
208
|
+
return b;
|
|
209
|
+
};
|
|
210
|
+
this.up = mk("chevron-up");
|
|
211
|
+
this.down = mk("chevron-down");
|
|
212
|
+
return h("div", { className: "vela-num-step", slot: numberInputDef.slot("steps") }, this.up, this.down);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
function createNumberInput(props) {
|
|
216
|
+
return new NumberInput(props);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export { NUMBER_CSS, NUMBER_STYLE_ID, NumberInput, clampNumber, createNumberInput, numberInputController, numberInputDef, numberInputVariants, snapToStep };
|