@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,205 @@
|
|
|
1
|
+
// Switch: a sliding on/off toggle, a track with a round thumb, for a setting that applies at
|
|
2
|
+
// once. `destructive` turns the track red while on (a kill switch); `sm` is a compact track.
|
|
3
|
+
// Given `checked`, it is controlled: a toggle only reports through `onCheckedChange`, and
|
|
4
|
+
// `setChecked` moves it. Behavior, keyboard and ARIA come from Zag's switch machine: a label
|
|
5
|
+
// holding a hidden checkbox.
|
|
6
|
+
import * as zagSwitch from '@zag-js/switch';
|
|
7
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
8
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
9
|
+
import { ownTeardown } from '@luxalgo/vela-ui/dispose';
|
|
10
|
+
import { h } from '@luxalgo/vela-ui';
|
|
11
|
+
import { t } from '@luxalgo/vela-ui';
|
|
12
|
+
import { variants } from '@luxalgo/vela-ui';
|
|
13
|
+
import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
|
|
14
|
+
|
|
15
|
+
export type SwitchSize = 'default' | 'sm';
|
|
16
|
+
export type SwitchVariant = 'default' | 'destructive';
|
|
17
|
+
|
|
18
|
+
export const switchDef = defineComponent({
|
|
19
|
+
name: 'switch',
|
|
20
|
+
parts: ['root', 'control', 'thumb'],
|
|
21
|
+
settings: { width: '32px', height: '18px', 'thumb-size': '14px', 'width-sm': '24px', 'height-sm': '14px', 'thumb-size-sm': '10px' },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const switchVariants = variants({ size: ['default', 'sm'], variant: ['default', 'destructive'] });
|
|
25
|
+
|
|
26
|
+
export interface SwitchProps {
|
|
27
|
+
/** On or off. Given, the switch is controlled: it moves only through `setChecked`. */
|
|
28
|
+
checked?: boolean;
|
|
29
|
+
/** The state it starts in when `checked` is not given. */
|
|
30
|
+
defaultChecked?: boolean;
|
|
31
|
+
/** Called when the user toggles it, with the state asked for. */
|
|
32
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
33
|
+
disabled?: boolean;
|
|
34
|
+
/** Submitted with its form under this name while on. */
|
|
35
|
+
name?: string;
|
|
36
|
+
/** What it submits while on (default `on`). */
|
|
37
|
+
value?: string;
|
|
38
|
+
/** A form does not submit while it is off. */
|
|
39
|
+
required?: boolean;
|
|
40
|
+
/** The id of its focusable control, so a `<label for>` reaches it. */
|
|
41
|
+
id?: string;
|
|
42
|
+
/** Its accessible name, when no `<label>` names it. */
|
|
43
|
+
label?: string;
|
|
44
|
+
size?: SwitchSize;
|
|
45
|
+
variant?: SwitchVariant;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const injectStyles = defineStyles(
|
|
49
|
+
'vela-ui-switch',
|
|
50
|
+
css`
|
|
51
|
+
@layer vela.ui {
|
|
52
|
+
[data-slot='switch'] {
|
|
53
|
+
--_w: ${switchDef.vars.width};
|
|
54
|
+
--_h: ${switchDef.vars.height};
|
|
55
|
+
--_thumb: ${switchDef.vars['thumb-size']};
|
|
56
|
+
/* Holds the hidden native input, which is absolutely positioned: anchored
|
|
57
|
+
anywhere higher, it escapes a scrolling pane and stretches the page. */
|
|
58
|
+
position: relative;
|
|
59
|
+
flex: 0 0 auto;
|
|
60
|
+
align-items: center;
|
|
61
|
+
cursor: pointer;
|
|
62
|
+
line-height: 0;
|
|
63
|
+
|
|
64
|
+
&:where(:not([hidden])) {
|
|
65
|
+
display: inline-flex;
|
|
66
|
+
}
|
|
67
|
+
&[data-size='sm'] {
|
|
68
|
+
--_w: ${switchDef.vars['width-sm']};
|
|
69
|
+
--_h: ${switchDef.vars['height-sm']};
|
|
70
|
+
--_thumb: ${switchDef.vars['thumb-size-sm']};
|
|
71
|
+
}
|
|
72
|
+
&[data-disabled] {
|
|
73
|
+
opacity: 0.5;
|
|
74
|
+
cursor: default;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
[data-slot='switch-control'] {
|
|
78
|
+
position: relative;
|
|
79
|
+
flex: none;
|
|
80
|
+
width: var(--_w);
|
|
81
|
+
height: var(--_h);
|
|
82
|
+
border-radius: 999px;
|
|
83
|
+
background: color-mix(in srgb, ${t.fg.bright} 15%, transparent);
|
|
84
|
+
transition: background ${t.dur.med} ease;
|
|
85
|
+
}
|
|
86
|
+
[data-slot='switch-thumb'] {
|
|
87
|
+
position: absolute;
|
|
88
|
+
top: calc((var(--_h) - var(--_thumb)) / 2);
|
|
89
|
+
inset-inline-start: calc((var(--_h) - var(--_thumb)) / 2);
|
|
90
|
+
width: var(--_thumb);
|
|
91
|
+
height: var(--_thumb);
|
|
92
|
+
border-radius: 50%;
|
|
93
|
+
background: ${t.fg.onFill};
|
|
94
|
+
transition:
|
|
95
|
+
inset-inline-start ${t.dur.med} ease,
|
|
96
|
+
background ${t.dur.med} ease;
|
|
97
|
+
}
|
|
98
|
+
[data-slot='switch'][data-focus-visible] [data-slot='switch-control'] {
|
|
99
|
+
outline: 2px solid ${t.focus};
|
|
100
|
+
outline-offset: 2px;
|
|
101
|
+
}
|
|
102
|
+
[data-slot='switch'][data-state='checked'] [data-slot='switch-control'] {
|
|
103
|
+
background: ${t.selected.bg};
|
|
104
|
+
}
|
|
105
|
+
[data-slot='switch'][data-state='checked'] [data-slot='switch-thumb'] {
|
|
106
|
+
inset-inline-start: calc(var(--_w) - var(--_thumb) - (var(--_h) - var(--_thumb)) / 2);
|
|
107
|
+
background: ${t.selected.fg};
|
|
108
|
+
}
|
|
109
|
+
[data-slot='switch'][data-variant='destructive'][data-state='checked'] [data-slot='switch-control'] {
|
|
110
|
+
background: ${t.danger};
|
|
111
|
+
}
|
|
112
|
+
[data-slot='switch'][data-variant='destructive'][data-state='checked'] [data-slot='switch-thumb'] {
|
|
113
|
+
background: ${t.fg.onFill};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
`,
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
export class Switch {
|
|
120
|
+
readonly el: HTMLLabelElement;
|
|
121
|
+
private readonly input: HTMLInputElement;
|
|
122
|
+
private readonly machine: HandleOf<typeof zagSwitch.machine>;
|
|
123
|
+
private readonly mid: string;
|
|
124
|
+
private readonly release: () => void;
|
|
125
|
+
private readonly control: HTMLElement;
|
|
126
|
+
private readonly thumb: HTMLElement;
|
|
127
|
+
/** The state as the switch's users see it, at once: the machine settles a beat later. */
|
|
128
|
+
private current: boolean;
|
|
129
|
+
private disabled: boolean;
|
|
130
|
+
|
|
131
|
+
constructor(opts: SwitchProps = {}) {
|
|
132
|
+
injectStyles(document);
|
|
133
|
+
this.mid = nextUid('vela-switch');
|
|
134
|
+
this.current = opts.checked ?? opts.defaultChecked ?? false;
|
|
135
|
+
this.disabled = opts.disabled ?? false;
|
|
136
|
+
const controlled = opts.checked !== undefined;
|
|
137
|
+
this.input = h('input', {});
|
|
138
|
+
this.thumb = h('span', { slot: switchDef.slot('thumb') });
|
|
139
|
+
this.control = h('span', { slot: switchDef.slot('control') }, this.thumb);
|
|
140
|
+
this.el = h('label', { className: 'vela-toggle-switch', slot: switchDef.slot('root'), attrs: switchVariants({ size: opts.size ?? 'default', variant: opts.variant ?? 'default' }) }, this.input, this.control);
|
|
141
|
+
this.machine = runMachine(
|
|
142
|
+
zagSwitch.machine,
|
|
143
|
+
() => ({
|
|
144
|
+
id: this.mid,
|
|
145
|
+
...(opts.id ? { ids: { hiddenInput: opts.id } } : {}),
|
|
146
|
+
defaultChecked: this.current,
|
|
147
|
+
disabled: this.disabled,
|
|
148
|
+
...(opts.name !== undefined ? { name: opts.name } : {}),
|
|
149
|
+
...(opts.value !== undefined ? { value: opts.value } : {}),
|
|
150
|
+
...(opts.required !== undefined ? { required: opts.required } : {}),
|
|
151
|
+
}),
|
|
152
|
+
(service) => this.render(service, opts.label),
|
|
153
|
+
);
|
|
154
|
+
this.release = ownTeardown(this.el, () => this.machine.stop());
|
|
155
|
+
this.input.addEventListener('change', () => {
|
|
156
|
+
const asked = this.input.checked;
|
|
157
|
+
if (controlled) this.api().setChecked(this.current);
|
|
158
|
+
else this.current = asked;
|
|
159
|
+
opts.onCheckedChange?.(asked);
|
|
160
|
+
});
|
|
161
|
+
// A press must not focus by scrolling (it may outside-dismiss an open popover).
|
|
162
|
+
this.el.addEventListener('pointerdown', (e) => {
|
|
163
|
+
if (this.disabled) return;
|
|
164
|
+
e.preventDefault();
|
|
165
|
+
this.input.focus({ preventScroll: true });
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get checked(): boolean {
|
|
170
|
+
return this.current;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
setChecked(v: boolean): void {
|
|
174
|
+
if (v === this.current) return;
|
|
175
|
+
this.current = v;
|
|
176
|
+
this.api().setChecked(v);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
setDisabled(disabled: boolean): void {
|
|
180
|
+
if (disabled === this.disabled) return;
|
|
181
|
+
this.disabled = disabled;
|
|
182
|
+
this.machine.flush();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
destroy(): void {
|
|
186
|
+
this.release();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private api(): zagSwitch.Api {
|
|
190
|
+
return zagSwitch.connect(this.machine.service, normalizeProps);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private render(service: zagSwitch.Service, label: string | undefined): void {
|
|
194
|
+
const api = zagSwitch.connect(service, normalizeProps);
|
|
195
|
+
spreadProps(this.el, api.getRootProps(), this.mid);
|
|
196
|
+
spreadProps(this.control, api.getControlProps(), this.mid);
|
|
197
|
+
spreadProps(this.thumb, api.getThumbProps(), this.mid);
|
|
198
|
+
spreadProps(this.input, api.getHiddenInputProps(), this.mid);
|
|
199
|
+
if (label) this.input.setAttribute('aria-label', label);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function createSwitch(props: SwitchProps = {}): Switch {
|
|
204
|
+
return new Switch(props);
|
|
205
|
+
}
|
package/registry/tabs.ts
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// Tabs: a tab list over Zag's tabs machine (WAI-ARIA tabs: roving focus, the arrow keys along
|
|
2
|
+
// the orientation, Home/End, automatic or manual activation). Clicks and the arrow keys
|
|
3
|
+
// select; the caller shows what the selected tab stands for, through panels it attaches or as
|
|
4
|
+
// a filter of one view. `default` puts the tabs on one tinted track, `line` underlines the
|
|
5
|
+
// selected one (the indicator dialog), `filled` is the symbol picker's market strip, `soft`
|
|
6
|
+
// the drawings drawer's category strip, `nav` a vertical settings rail. A tab with a
|
|
7
|
+
// `description` reads on two lines, its label over it (a day strip: the weekday, then the date).
|
|
8
|
+
import * as tabs from '@zag-js/tabs';
|
|
9
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
10
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
11
|
+
import { ownTeardown } from '@luxalgo/vela-ui/dispose';
|
|
12
|
+
import { h } from '@luxalgo/vela-ui';
|
|
13
|
+
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
14
|
+
import { t } from '@luxalgo/vela-ui';
|
|
15
|
+
import { variants, type VariantProps } from '@luxalgo/vela-ui';
|
|
16
|
+
import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
|
|
17
|
+
|
|
18
|
+
export const tabsDef = defineComponent({
|
|
19
|
+
name: 'tabs',
|
|
20
|
+
parts: ['list', 'trigger', 'icon', 'count', 'label', 'description', 'content'],
|
|
21
|
+
settings: { 'track-radius': '8px', 'trigger-radius': t.radius.md, 'filled-radius': '5px', 'soft-radius': '8px', 'count-radius': '8px' },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const tabsVariants = variants({ variant: ['default', 'line', 'filled', 'soft', 'nav'] });
|
|
25
|
+
|
|
26
|
+
export type TabsVariant = (typeof tabsVariants.spec.variant)[number];
|
|
27
|
+
|
|
28
|
+
export interface TabItem {
|
|
29
|
+
value: string;
|
|
30
|
+
label: string;
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
/** Out of the strip for now (its content does not apply); the arrow keys skip it. */
|
|
33
|
+
hidden?: boolean;
|
|
34
|
+
/** Icon id from the kit's registry, before the label. */
|
|
35
|
+
icon?: string;
|
|
36
|
+
/** A count after the label (open orders, trades); null shows none. */
|
|
37
|
+
count?: number | null;
|
|
38
|
+
/** A second line under the label (a day strip's date under its weekday). */
|
|
39
|
+
description?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface TabsProps extends VariantProps<typeof tabsVariants> {
|
|
43
|
+
items: readonly TabItem[];
|
|
44
|
+
/** The selected tab (default: the first available one). */
|
|
45
|
+
defaultValue?: string;
|
|
46
|
+
onValueChange?: (value: string) => void;
|
|
47
|
+
/** Default `horizontal` (`nav` reads best vertical). */
|
|
48
|
+
orientation?: 'horizontal' | 'vertical';
|
|
49
|
+
/** `automatic` (default): moving focus with the arrow keys selects. `manual`: Enter or
|
|
50
|
+
* Space selects the focused tab. */
|
|
51
|
+
activationMode?: 'automatic' | 'manual';
|
|
52
|
+
/** The text direction the arrow keys follow (default `ltr`). */
|
|
53
|
+
dir?: 'ltr' | 'rtl';
|
|
54
|
+
/** Extra class names on the list (the host's layout of the strip). */
|
|
55
|
+
className?: string;
|
|
56
|
+
/** Extra class names on every trigger. */
|
|
57
|
+
triggerClassName?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface TabsHandle {
|
|
61
|
+
readonly el: HTMLElement;
|
|
62
|
+
/** The selected tab. */
|
|
63
|
+
readonly value: string | null;
|
|
64
|
+
/** Show a tab as selected without calling `onValueChange`. */
|
|
65
|
+
setValue(value: string | null): void;
|
|
66
|
+
setItems(items: readonly TabItem[]): void;
|
|
67
|
+
/** Hide or show a tab. Hiding the selected tab selects the first available one (and
|
|
68
|
+
* calls `onValueChange`). */
|
|
69
|
+
setHidden(value: string, hidden: boolean): void;
|
|
70
|
+
/** A tab's count (null: none). */
|
|
71
|
+
setCount(value: string, count: number | null): void;
|
|
72
|
+
/** The trigger of a tab (null: unknown value). */
|
|
73
|
+
trigger(value: string): HTMLButtonElement | null;
|
|
74
|
+
/** Make `el` the panel of tab `value`: it gets the tabpanel role, its link to the trigger,
|
|
75
|
+
* and `hidden` while another tab is selected. */
|
|
76
|
+
panel(value: string, el: HTMLElement): void;
|
|
77
|
+
destroy(): void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A tab a user can reach: neither disabled nor hidden. */
|
|
81
|
+
export const tabAvailable = (item: TabItem): boolean => !item.disabled && !item.hidden;
|
|
82
|
+
|
|
83
|
+
const countEl = (count: number | null | undefined): HTMLElement | null => (count == null ? null : h('span', { slot: tabsDef.slot('count'), text: String(count) }));
|
|
84
|
+
|
|
85
|
+
export const TABS_STYLE_ID = 'vela-ui-tabs';
|
|
86
|
+
|
|
87
|
+
export const TABS_CSS = css`
|
|
88
|
+
@layer vela.ui {
|
|
89
|
+
[data-slot='tabs-icon'] {
|
|
90
|
+
margin-inline-end: 6px;
|
|
91
|
+
}
|
|
92
|
+
[data-slot='tabs-count'] {
|
|
93
|
+
min-width: 16px;
|
|
94
|
+
height: 16px;
|
|
95
|
+
margin-inline-start: 6px;
|
|
96
|
+
padding: 0 5px;
|
|
97
|
+
border-radius: ${tabsDef.vars['count-radius']};
|
|
98
|
+
background: ${t.hover};
|
|
99
|
+
color: ${t.fg.muted};
|
|
100
|
+
font-size: 10px;
|
|
101
|
+
font-weight: 600;
|
|
102
|
+
font-variant-numeric: tabular-nums;
|
|
103
|
+
|
|
104
|
+
&:where(:not([hidden])) {
|
|
105
|
+
display: inline-flex;
|
|
106
|
+
align-items: center;
|
|
107
|
+
justify-content: center;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
[data-slot='tabs-list'] {
|
|
111
|
+
display: flex;
|
|
112
|
+
|
|
113
|
+
&[data-orientation='vertical'] {
|
|
114
|
+
flex-direction: column;
|
|
115
|
+
}
|
|
116
|
+
&[data-variant='default'] {
|
|
117
|
+
display: inline-flex;
|
|
118
|
+
gap: 2px;
|
|
119
|
+
padding: 3px;
|
|
120
|
+
box-sizing: border-box;
|
|
121
|
+
border-radius: ${tabsDef.vars['track-radius']};
|
|
122
|
+
background: color-mix(in srgb, ${t.fg.bright} 6%, transparent);
|
|
123
|
+
}
|
|
124
|
+
&[data-variant='default'][data-orientation='horizontal'] {
|
|
125
|
+
height: 32px;
|
|
126
|
+
align-items: stretch;
|
|
127
|
+
}
|
|
128
|
+
&[data-variant='line'] {
|
|
129
|
+
gap: 12px;
|
|
130
|
+
border-bottom: 1px solid ${t.border};
|
|
131
|
+
}
|
|
132
|
+
&[data-variant='nav'] {
|
|
133
|
+
gap: 2px;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
[data-slot='tabs-trigger']:where(:not([hidden])) {
|
|
137
|
+
all: unset;
|
|
138
|
+
cursor: pointer;
|
|
139
|
+
white-space: nowrap;
|
|
140
|
+
-webkit-tap-highlight-color: transparent;
|
|
141
|
+
|
|
142
|
+
&[data-disabled] {
|
|
143
|
+
cursor: default;
|
|
144
|
+
opacity: 0.45;
|
|
145
|
+
}
|
|
146
|
+
&:focus-visible {
|
|
147
|
+
outline: 2px solid ${t.focus};
|
|
148
|
+
outline-offset: -2px;
|
|
149
|
+
}
|
|
150
|
+
&[data-variant='default'] {
|
|
151
|
+
display: inline-flex;
|
|
152
|
+
align-items: center;
|
|
153
|
+
justify-content: center;
|
|
154
|
+
padding: 0 10px;
|
|
155
|
+
border-radius: ${tabsDef.vars['trigger-radius']};
|
|
156
|
+
color: ${t.fg.muted};
|
|
157
|
+
font-size: 13px;
|
|
158
|
+
font-weight: 500;
|
|
159
|
+
transition:
|
|
160
|
+
background ${t.dur.fast} ease,
|
|
161
|
+
color ${t.dur.fast} ease;
|
|
162
|
+
}
|
|
163
|
+
&[data-variant='default'][data-orientation='vertical'] {
|
|
164
|
+
justify-content: flex-start;
|
|
165
|
+
padding: 6px 10px;
|
|
166
|
+
}
|
|
167
|
+
&[data-variant='default']:not([data-selected], [data-disabled]):hover {
|
|
168
|
+
color: ${t.fg.bright};
|
|
169
|
+
}
|
|
170
|
+
&[data-variant='default'][data-selected] {
|
|
171
|
+
background: color-mix(in srgb, ${t.fg.bright} 12%, transparent);
|
|
172
|
+
color: ${t.fg.bright};
|
|
173
|
+
}
|
|
174
|
+
&[data-variant='line'] {
|
|
175
|
+
padding-bottom: 8px;
|
|
176
|
+
margin-bottom: -1px;
|
|
177
|
+
border-bottom: 2px solid transparent;
|
|
178
|
+
font-size: 13px;
|
|
179
|
+
font-weight: 600;
|
|
180
|
+
line-height: 20px;
|
|
181
|
+
color: ${t.fg.muted};
|
|
182
|
+
transition:
|
|
183
|
+
color ${t.dur.fast} ease,
|
|
184
|
+
border-color ${t.dur.fast} ease;
|
|
185
|
+
}
|
|
186
|
+
&[data-variant='line']:not([data-selected], [data-disabled]):hover {
|
|
187
|
+
color: ${t.fg};
|
|
188
|
+
}
|
|
189
|
+
&[data-variant='line'][data-selected] {
|
|
190
|
+
color: ${t.fg};
|
|
191
|
+
border-bottom-color: ${t.fg.bright};
|
|
192
|
+
}
|
|
193
|
+
&[data-variant='filled'] {
|
|
194
|
+
padding: 3px 10px;
|
|
195
|
+
border-radius: ${tabsDef.vars['filled-radius']};
|
|
196
|
+
color: ${t.fg.muted};
|
|
197
|
+
font-size: 13px;
|
|
198
|
+
font-weight: 600;
|
|
199
|
+
}
|
|
200
|
+
&[data-variant='filled']:not([data-selected], [data-disabled]):hover {
|
|
201
|
+
color: ${t.fg};
|
|
202
|
+
}
|
|
203
|
+
&[data-variant='filled'][data-selected] {
|
|
204
|
+
background: ${t.selected.bg};
|
|
205
|
+
color: ${t.selected.fg};
|
|
206
|
+
}
|
|
207
|
+
&[data-variant='soft'] {
|
|
208
|
+
flex: none;
|
|
209
|
+
min-height: 36px;
|
|
210
|
+
padding: 0 12px;
|
|
211
|
+
display: inline-flex;
|
|
212
|
+
align-items: center;
|
|
213
|
+
border-radius: ${tabsDef.vars['soft-radius']};
|
|
214
|
+
font-size: 13px;
|
|
215
|
+
font-weight: 600;
|
|
216
|
+
color: ${t.fg.muted};
|
|
217
|
+
}
|
|
218
|
+
&[data-variant='soft'][data-selected] {
|
|
219
|
+
color: ${t.fg.bright};
|
|
220
|
+
background: ${t.hover};
|
|
221
|
+
}
|
|
222
|
+
&[data-variant='nav'] {
|
|
223
|
+
padding: 9px 12px;
|
|
224
|
+
border-radius: ${t.radius.md};
|
|
225
|
+
color: ${t.fg.muted};
|
|
226
|
+
font-size: 13px;
|
|
227
|
+
font-weight: 600;
|
|
228
|
+
transition:
|
|
229
|
+
background ${t.dur.fast} ease,
|
|
230
|
+
color ${t.dur.fast} ease;
|
|
231
|
+
}
|
|
232
|
+
&[data-variant='nav']:not([data-selected], [data-disabled]):hover {
|
|
233
|
+
background: ${t.hover};
|
|
234
|
+
color: ${t.fg.bright};
|
|
235
|
+
}
|
|
236
|
+
&[data-variant='nav'][data-selected] {
|
|
237
|
+
background: ${t.active};
|
|
238
|
+
color: ${t.fg.bright};
|
|
239
|
+
}
|
|
240
|
+
&[data-description] {
|
|
241
|
+
display: inline-flex;
|
|
242
|
+
flex-direction: column;
|
|
243
|
+
align-items: center;
|
|
244
|
+
justify-content: center;
|
|
245
|
+
gap: 2px;
|
|
246
|
+
}
|
|
247
|
+
&[data-description]:not([data-variant='line']) {
|
|
248
|
+
padding-block: 6px;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
[data-slot='tabs-list'][data-variant='default']:has(> [data-description]) {
|
|
252
|
+
height: auto;
|
|
253
|
+
}
|
|
254
|
+
[data-slot='tabs-label'] {
|
|
255
|
+
display: inline-flex;
|
|
256
|
+
align-items: center;
|
|
257
|
+
line-height: 16px;
|
|
258
|
+
}
|
|
259
|
+
[data-slot='tabs-description'] {
|
|
260
|
+
font-size: 11px;
|
|
261
|
+
font-weight: 400;
|
|
262
|
+
line-height: 14px;
|
|
263
|
+
opacity: 0.85;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
`;
|
|
267
|
+
|
|
268
|
+
const injectStyles = defineStyles(TABS_STYLE_ID, TABS_CSS);
|
|
269
|
+
|
|
270
|
+
class TabsImpl implements TabsHandle {
|
|
271
|
+
readonly el: HTMLElement;
|
|
272
|
+
private readonly machine: HandleOf<typeof tabs.machine>;
|
|
273
|
+
private readonly mid: string;
|
|
274
|
+
private readonly release: () => void;
|
|
275
|
+
private readonly variant: TabsVariant;
|
|
276
|
+
private readonly triggerClassName: string | undefined;
|
|
277
|
+
private items: TabItem[];
|
|
278
|
+
private readonly triggers = new Map<string, HTMLButtonElement>();
|
|
279
|
+
private readonly panels = new Map<string, HTMLElement>();
|
|
280
|
+
/** The selection as of the last write or report: the machine applies events on a
|
|
281
|
+
* microtask, so its own value lags a write. */
|
|
282
|
+
private current: string | null;
|
|
283
|
+
/** A host write the machine has yet to report: that report is not the user's change. */
|
|
284
|
+
private silentNext: string | undefined;
|
|
285
|
+
|
|
286
|
+
constructor(props: TabsProps) {
|
|
287
|
+
injectStyles(document);
|
|
288
|
+
this.variant = tabsVariants.resolve(props).variant;
|
|
289
|
+
this.triggerClassName = props.triggerClassName;
|
|
290
|
+
this.items = [...props.items];
|
|
291
|
+
this.current = props.defaultValue ?? this.items.find(tabAvailable)?.value ?? null;
|
|
292
|
+
this.mid = nextUid('vela-tabs');
|
|
293
|
+
this.el = h('div', {
|
|
294
|
+
className: props.className ? `vela-tabs ${props.className}` : 'vela-tabs',
|
|
295
|
+
slot: tabsDef.slot('list'),
|
|
296
|
+
data: { variant: this.variant },
|
|
297
|
+
});
|
|
298
|
+
this.build();
|
|
299
|
+
this.machine = runMachine(
|
|
300
|
+
tabs.machine,
|
|
301
|
+
{
|
|
302
|
+
id: this.mid,
|
|
303
|
+
defaultValue: this.current,
|
|
304
|
+
orientation: props.orientation ?? 'horizontal',
|
|
305
|
+
activationMode: props.activationMode ?? 'automatic',
|
|
306
|
+
dir: props.dir ?? 'ltr',
|
|
307
|
+
loopFocus: true,
|
|
308
|
+
onValueChange: ({ value }) => {
|
|
309
|
+
if (value == null) return;
|
|
310
|
+
this.current = value;
|
|
311
|
+
if (this.silentNext === value) {
|
|
312
|
+
this.silentNext = undefined;
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
props.onValueChange?.(value);
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
(service) => this.render(service),
|
|
319
|
+
);
|
|
320
|
+
this.release = ownTeardown(this.el, () => this.machine.stop());
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
get value(): string | null {
|
|
324
|
+
return this.current;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
setValue(value: string | null): void {
|
|
328
|
+
if (value === this.current) return;
|
|
329
|
+
this.current = value;
|
|
330
|
+
if (value === null) {
|
|
331
|
+
this.api().clearValue();
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
this.silentNext = value;
|
|
335
|
+
this.api().setValue(value);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
setItems(items: readonly TabItem[]): void {
|
|
339
|
+
this.items = [...items];
|
|
340
|
+
this.build();
|
|
341
|
+
if (this.current === null || !this.items.some((i) => i.value === this.current)) this.setValue(this.items.find(tabAvailable)?.value ?? null);
|
|
342
|
+
this.machine.flush();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
setHidden(value: string, hidden: boolean): void {
|
|
346
|
+
this.items = this.items.map((i) => (i.value === value ? { ...i, hidden } : i));
|
|
347
|
+
if (hidden && this.current === value) {
|
|
348
|
+
const next = this.items.find(tabAvailable)?.value;
|
|
349
|
+
if (next !== undefined) {
|
|
350
|
+
this.current = next;
|
|
351
|
+
this.api().setValue(next);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
this.machine.flush();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
setCount(value: string, count: number | null): void {
|
|
358
|
+
const b = this.triggers.get(value);
|
|
359
|
+
if (!b) return;
|
|
360
|
+
this.items = this.items.map((i) => (i.value === value ? { ...i, count } : i));
|
|
361
|
+
b.querySelector(`[data-slot="${tabsDef.slot('count')}"]`)?.remove();
|
|
362
|
+
const el = countEl(count);
|
|
363
|
+
if (el) (b.querySelector(`[data-slot="${tabsDef.slot('label')}"]`) ?? b).appendChild(el);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
trigger(value: string): HTMLButtonElement | null {
|
|
367
|
+
return this.triggers.get(value) ?? null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
panel(value: string, el: HTMLElement): void {
|
|
371
|
+
el.dataset.slot = tabsDef.slot('content');
|
|
372
|
+
this.panels.set(value, el);
|
|
373
|
+
this.machine.flush();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
destroy(): void {
|
|
377
|
+
this.release();
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private api(): tabs.Api {
|
|
381
|
+
return tabs.connect(this.machine.service, normalizeProps);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private build(): void {
|
|
385
|
+
this.triggers.clear();
|
|
386
|
+
const className = this.triggerClassName ? `vela-tab ${this.triggerClassName}` : 'vela-tab';
|
|
387
|
+
this.el.replaceChildren(
|
|
388
|
+
...this.items.map((item) => {
|
|
389
|
+
let icon: HTMLElement | null = null;
|
|
390
|
+
if (item.icon) {
|
|
391
|
+
icon = iconEl(item.icon, document);
|
|
392
|
+
icon.dataset.slot = tabsDef.slot('icon');
|
|
393
|
+
}
|
|
394
|
+
const b = item.description
|
|
395
|
+
? h(
|
|
396
|
+
'button',
|
|
397
|
+
{ className, slot: tabsDef.slot('trigger'), data: { variant: this.variant, description: true } },
|
|
398
|
+
h('span', { slot: tabsDef.slot('label') }, icon, item.label, countEl(item.count)),
|
|
399
|
+
h('span', { slot: tabsDef.slot('description'), text: item.description }),
|
|
400
|
+
)
|
|
401
|
+
: h('button', { className, slot: tabsDef.slot('trigger'), data: { variant: this.variant } }, icon, item.label, countEl(item.count));
|
|
402
|
+
this.triggers.set(item.value, b);
|
|
403
|
+
return b;
|
|
404
|
+
}),
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private render(service: tabs.Service): void {
|
|
409
|
+
const api = tabs.connect(service, normalizeProps);
|
|
410
|
+
spreadProps(this.el, api.getListProps(), this.mid);
|
|
411
|
+
for (const item of this.items) {
|
|
412
|
+
const b = this.triggers.get(item.value);
|
|
413
|
+
if (!b) continue;
|
|
414
|
+
// A hidden tab is a disabled trigger to the machine, so the arrow keys skip it.
|
|
415
|
+
const props = api.getTriggerProps({ value: item.value, disabled: !tabAvailable(item) }) as Record<string, unknown>;
|
|
416
|
+
if (!this.panels.has(item.value)) delete props['aria-controls'];
|
|
417
|
+
spreadProps(b, props, this.mid);
|
|
418
|
+
b.hidden = item.hidden === true;
|
|
419
|
+
}
|
|
420
|
+
for (const [value, el] of this.panels) spreadProps(el, api.getContentProps({ value }), this.mid);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function createTabs(props: TabsProps): TabsHandle {
|
|
425
|
+
return new TabsImpl(props);
|
|
426
|
+
}
|