@luxalgo/vela-ui 0.2.0 → 0.4.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 +153 -91
- 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/badge.js +1 -1
- package/dist/bar-chart.d.ts +1 -1
- package/dist/bar-chart.js +1 -1
- package/dist/button.d.ts +1 -1
- package/dist/button.js +4 -4
- package/dist/callout-bubble.d.ts +1 -1
- package/dist/callout-bubble.js +4 -4
- package/dist/card.d.ts +52 -0
- package/dist/card.js +179 -0
- package/dist/checkbox.js +5 -5
- package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
- package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
- package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
- package/dist/chunk-GUIYFQ27.js +30 -0
- package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
- package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
- package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
- package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
- package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
- package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
- package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
- package/dist/collapsible.d.ts +1 -1
- package/dist/collapsible.js +3 -3
- package/dist/color-picker.js +5 -5
- package/dist/context-menu.js +5 -5
- package/dist/data-table.d.ts +1 -1
- package/dist/data-table.js +1 -1
- package/dist/date-picker.d.ts +1 -1
- package/dist/date-picker.js +2 -2
- package/dist/dialog.d.ts +9 -1
- package/dist/dialog.js +15 -5
- package/dist/donut.d.ts +1 -1
- package/dist/donut.js +1 -1
- package/dist/drawer.d.ts +1 -1
- package/dist/drawer.js +2 -2
- package/dist/dropdown-menu.d.ts +1 -1
- package/dist/dropdown-menu.js +5 -5
- package/dist/empty.d.ts +1 -1
- package/dist/empty.js +2 -2
- package/dist/field.js +13 -13
- package/dist/{stat.d.ts → figure.d.ts} +9 -9
- package/dist/figure.js +41 -0
- package/dist/glyph-select.js +5 -5
- package/dist/hover-card.d.ts +1 -1
- package/dist/hover-card.js +3 -3
- package/dist/icons.js +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -3
- package/dist/input-group.d.ts +1 -1
- package/dist/input-group.js +4 -4
- package/dist/input.js +2 -2
- package/dist/item.d.ts +1 -1
- package/dist/item.js +2 -2
- package/dist/kbd.d.ts +1 -1
- package/dist/kbd.js +2 -2
- package/dist/line-chart.d.ts +1 -1
- package/dist/line-chart.js +1 -1
- package/dist/manifest.json +691 -74
- package/dist/month-grid.js +1 -1
- package/dist/number-input.d.ts +1 -1
- package/dist/number-input.js +5 -5
- package/dist/popover.js +3 -3
- package/dist/scatter-chart.d.ts +1 -1
- package/dist/scatter-chart.js +1 -1
- package/dist/select.d.ts +1 -1
- package/dist/select.js +7 -7
- package/dist/slider.d.ts +1 -1
- package/dist/slider.js +2 -2
- package/dist/sort-button.js +4 -4
- package/dist/sparkline.d.ts +1 -1
- package/dist/sparkline.js +1 -1
- package/dist/switch.d.ts +1 -1
- package/dist/switch.js +3 -3
- package/dist/tabs.d.ts +1 -1
- package/dist/tabs.js +4 -4
- package/dist/text-area.d.ts +1 -1
- package/dist/text-area.js +2 -2
- package/dist/theme-TefB8vtl.d.ts +341 -0
- package/dist/theme.d.ts +42 -192
- package/dist/theme.js +1 -1
- package/dist/theme.schema.json +321 -30
- package/dist/toast.d.ts +49 -0
- package/dist/toast.js +463 -0
- package/dist/toggle-group.d.ts +1 -1
- package/dist/toggle-group.js +4 -4
- package/dist/tooltip.js +2 -2
- package/dist/treemap.d.ts +80 -0
- package/dist/treemap.js +470 -0
- 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 +6 -3
- package/package.json +90 -90
- package/registry/badge.ts +119 -119
- package/registry/bar-chart.ts +215 -215
- package/registry/button.ts +387 -379
- package/registry/callout-bubble.ts +347 -347
- package/registry/card.ts +379 -0
- 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 -535
- package/registry/donut.ts +145 -145
- package/registry/drawer.ts +636 -636
- package/registry/dropdown-menu.ts +762 -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 +620 -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 -0
- package/registry/toggle-group.ts +295 -295
- package/registry/tooltip.ts +156 -156
- package/registry/treemap.ts +847 -0
- package/skills/theming-vela-ui/SKILL.md +137 -137
- package/skills/using-vela-ui-components/SKILL.md +139 -137
- 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 -379
- package/src/components/callout-bubble.ts +347 -347
- package/src/components/card.ts +379 -0
- 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 -535
- package/src/components/donut.ts +145 -145
- package/src/components/drawer.ts +636 -636
- package/src/components/dropdown-menu.ts +762 -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 +620 -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 -0
- package/src/components/toggle-group.ts +295 -295
- package/src/components/tooltip.ts +156 -156
- package/src/components/treemap.ts +847 -0
- 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 -151
- 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 +244 -237
- 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 -664
- 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/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
- package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
- package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
package/src/components/switch.ts
CHANGED
|
@@ -1,205 +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 '../lib/css';
|
|
8
|
-
import { defineComponent } from '../lib/define';
|
|
9
|
-
import { ownTeardown } from '../lib/dispose';
|
|
10
|
-
import { h } from '../lib/h';
|
|
11
|
-
import { t } from '../lib/t';
|
|
12
|
-
import { variants } from '../lib/variants';
|
|
13
|
-
import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '../lib/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
|
-
}
|
|
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 '../lib/css';
|
|
8
|
+
import { defineComponent } from '../lib/define';
|
|
9
|
+
import { ownTeardown } from '../lib/dispose';
|
|
10
|
+
import { h } from '../lib/h';
|
|
11
|
+
import { t } from '../lib/t';
|
|
12
|
+
import { variants } from '../lib/variants';
|
|
13
|
+
import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '../lib/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
|
+
}
|