@firstprateek/vastu 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +51 -2
- package/custom-elements.json +3046 -0
- package/dist/dev/all.d.ts +9 -0
- package/dist/dev/all.d.ts.map +1 -0
- package/dist/dev/all.js +10 -0
- package/dist/dev/all.js.map +1 -0
- package/dist/dev/button/button.d.ts +98 -0
- package/dist/dev/button/button.d.ts.map +1 -0
- package/dist/dev/button/button.js +292 -0
- package/dist/dev/button/button.js.map +1 -0
- package/dist/dev/button/index.d.ts +4 -0
- package/dist/dev/button/index.d.ts.map +1 -0
- package/dist/dev/button/index.js +5 -0
- package/dist/dev/button/index.js.map +1 -0
- package/dist/dev/checkbox/checkbox.d.ts +71 -0
- package/dist/dev/checkbox/checkbox.d.ts.map +1 -0
- package/dist/dev/checkbox/checkbox.js +232 -0
- package/dist/dev/checkbox/checkbox.js.map +1 -0
- package/dist/dev/checkbox/index.d.ts +3 -0
- package/dist/dev/checkbox/index.d.ts.map +1 -0
- package/dist/dev/checkbox/index.js +5 -0
- package/dist/dev/checkbox/index.js.map +1 -0
- package/dist/dev/core/define.d.ts +3 -0
- package/dist/dev/core/define.d.ts.map +1 -0
- package/dist/dev/core/define.js +6 -0
- package/dist/dev/core/define.js.map +1 -0
- package/dist/dev/core/dev-mode.d.ts +6 -0
- package/dist/dev/core/dev-mode.d.ts.map +1 -0
- package/dist/dev/core/dev-mode.js +6 -0
- package/dist/dev/core/dev-mode.js.map +1 -0
- package/dist/dev/core/focus-ring.d.ts +3 -0
- package/dist/dev/core/focus-ring.d.ts.map +1 -0
- package/dist/dev/core/focus-ring.js +14 -0
- package/dist/dev/core/focus-ring.js.map +1 -0
- package/dist/dev/core/form-control.d.ts +47 -0
- package/dist/dev/core/form-control.d.ts.map +1 -0
- package/dist/dev/core/form-control.js +75 -0
- package/dist/dev/core/form-control.js.map +1 -0
- package/dist/dev/core/warn.d.ts +12 -0
- package/dist/dev/core/warn.d.ts.map +1 -0
- package/dist/dev/core/warn.js +35 -0
- package/dist/dev/core/warn.js.map +1 -0
- package/dist/dev/dialog/dialog.d.ts +77 -0
- package/dist/dev/dialog/dialog.d.ts.map +1 -0
- package/dist/dev/dialog/dialog.js +253 -0
- package/dist/dev/dialog/dialog.js.map +1 -0
- package/dist/dev/dialog/index.d.ts +4 -0
- package/dist/dev/dialog/index.d.ts.map +1 -0
- package/dist/dev/dialog/index.js +5 -0
- package/dist/dev/dialog/index.js.map +1 -0
- package/dist/dev/menu/index.d.ts +3 -0
- package/dist/dev/menu/index.d.ts.map +1 -0
- package/dist/dev/menu/index.js +5 -0
- package/dist/dev/menu/index.js.map +1 -0
- package/dist/dev/menu/menu.d.ts +71 -0
- package/dist/dev/menu/menu.d.ts.map +1 -0
- package/dist/dev/menu/menu.js +330 -0
- package/dist/dev/menu/menu.js.map +1 -0
- package/dist/dev/switch/index.d.ts +3 -0
- package/dist/dev/switch/index.d.ts.map +1 -0
- package/dist/dev/switch/index.js +5 -0
- package/dist/dev/switch/index.js.map +1 -0
- package/dist/dev/switch/switch.d.ts +69 -0
- package/dist/dev/switch/switch.d.ts.map +1 -0
- package/dist/dev/switch/switch.js +213 -0
- package/dist/dev/switch/switch.js.map +1 -0
- package/dist/dev/tabs/index.d.ts +3 -0
- package/dist/dev/tabs/index.d.ts.map +1 -0
- package/dist/dev/tabs/index.js +5 -0
- package/dist/dev/tabs/index.js.map +1 -0
- package/dist/dev/tabs/tabs.d.ts +64 -0
- package/dist/dev/tabs/tabs.d.ts.map +1 -0
- package/dist/dev/tabs/tabs.js +212 -0
- package/dist/dev/tabs/tabs.js.map +1 -0
- package/dist/dev/text-field/index.d.ts +3 -0
- package/dist/dev/text-field/index.d.ts.map +1 -0
- package/dist/dev/text-field/index.js +5 -0
- package/dist/dev/text-field/index.js.map +1 -0
- package/dist/dev/text-field/text-field.d.ts +91 -0
- package/dist/dev/text-field/text-field.d.ts.map +1 -0
- package/dist/dev/text-field/text-field.js +267 -0
- package/dist/dev/text-field/text-field.js.map +1 -0
- package/dist/dev/toast/index.d.ts +4 -0
- package/dist/dev/toast/index.d.ts.map +1 -0
- package/dist/dev/toast/index.js +5 -0
- package/dist/dev/toast/index.js.map +1 -0
- package/dist/dev/toast/toast.d.ts +71 -0
- package/dist/dev/toast/toast.d.ts.map +1 -0
- package/dist/dev/toast/toast.js +258 -0
- package/dist/dev/toast/toast.js.map +1 -0
- package/dist/prod/all.js +10 -0
- package/dist/prod/button/button.js +292 -0
- package/dist/prod/button/index.js +5 -0
- package/dist/prod/checkbox/checkbox.js +232 -0
- package/dist/prod/checkbox/index.js +5 -0
- package/dist/prod/core/define.js +6 -0
- package/dist/prod/core/dev-mode.js +1 -0
- package/dist/prod/core/focus-ring.js +14 -0
- package/dist/prod/core/form-control.js +75 -0
- package/dist/prod/core/warn.js +2 -0
- package/dist/prod/dialog/dialog.js +253 -0
- package/dist/prod/dialog/index.js +5 -0
- package/dist/prod/menu/index.js +5 -0
- package/dist/prod/menu/menu.js +330 -0
- package/dist/prod/switch/index.js +5 -0
- package/dist/prod/switch/switch.js +213 -0
- package/dist/prod/tabs/index.js +5 -0
- package/dist/prod/tabs/tabs.js +212 -0
- package/dist/prod/text-field/index.js +5 -0
- package/dist/prod/text-field/text-field.js +267 -0
- package/dist/prod/toast/index.js +5 -0
- package/dist/prod/toast/toast.js +258 -0
- package/package.json +95 -3
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { LitElement, css, html } from 'lit';
|
|
2
|
+
import { focusRing } from '../core/focus-ring.js';
|
|
3
|
+
import { checkTokensLoaded, devWarn } from '../core/warn.js';
|
|
4
|
+
/**
|
|
5
|
+
* A button that opens a menu of actions, following the WAI-ARIA menu button pattern.
|
|
6
|
+
*
|
|
7
|
+
* @tag va-menu
|
|
8
|
+
* @summary Opens a short list of actions from a button, such as Edit, Duplicate and Delete.
|
|
9
|
+
*
|
|
10
|
+
* @slot - The menu items, as `<button value="…">` elements.
|
|
11
|
+
*
|
|
12
|
+
* @csspart trigger - The button that opens the menu.
|
|
13
|
+
* @csspart menu - The popup list.
|
|
14
|
+
*
|
|
15
|
+
* @cssprop {<color>} --va-menu-bg - Background colour of the menu.
|
|
16
|
+
* @cssprop {<length>} --va-menu-radius - Corner radius of the menu.
|
|
17
|
+
* @cssprop {<color>} --va-menu-item-bg-hover - Background colour of the focused or hovered item.
|
|
18
|
+
*
|
|
19
|
+
* @cssprop-applies --va-menu-bg menu:open background-color
|
|
20
|
+
* @cssprop-applies --va-menu-radius menu:open border-top-left-radius
|
|
21
|
+
* @cssprop-applies --va-menu-item-bg-hover light([role="menuitem"]):focus background-color
|
|
22
|
+
*
|
|
23
|
+
* @keyboard-items [role="menuitem"]
|
|
24
|
+
* @keyboard Enter | opens | Opens the menu and focuses the first item.
|
|
25
|
+
* @keyboard Space | opens | Opens the menu and focuses the first item.
|
|
26
|
+
* @keyboard ArrowDown | opens | Opens the menu and focuses the first item.
|
|
27
|
+
* @keyboard open: ArrowDown | next | Moves to the next item, wrapping round.
|
|
28
|
+
* @keyboard open: ArrowUp | previous | Moves to the previous item, wrapping round.
|
|
29
|
+
* @keyboard open: Home | first | Moves to the first item.
|
|
30
|
+
* @keyboard open: End | last | Moves to the last item.
|
|
31
|
+
* @keyboard open: Enter | selects | Chooses the focused item and closes the menu.
|
|
32
|
+
* @keyboard open: Escape | closes | Closes the menu and returns focus to the button.
|
|
33
|
+
*
|
|
34
|
+
* @a11y-role menu
|
|
35
|
+
* @a11y-name-from label
|
|
36
|
+
* @replaces [role="menu"]
|
|
37
|
+
* @a11y-name The `label`, which is also the button's text.
|
|
38
|
+
* @a11y-note The button has `aria-haspopup="menu"` and `aria-expanded`. Items get `role="menuitem"`.
|
|
39
|
+
* @a11y-note Typing a letter moves to the next item that starts with it.
|
|
40
|
+
*
|
|
41
|
+
* @guidance-use For a few actions on one thing, when there isn't room to show them as buttons.
|
|
42
|
+
* @guidance-avoid For navigation, for choosing a value in a form, or for one or two actions.
|
|
43
|
+
* @guidance-instead Links for navigation; a select or radio group for form values; plain buttons for one or two actions.
|
|
44
|
+
*
|
|
45
|
+
* @fires {CustomEvent<{ value: string, item: HTMLElement }>} va-select - When the user chooses an item. `event.detail.value` is the item's `value`, or its text when it has none; `event.detail.item` is the item.
|
|
46
|
+
* @event-trigger va-select | press Enter | open
|
|
47
|
+
*/
|
|
48
|
+
export class VaMenu extends LitElement {
|
|
49
|
+
static { this.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }; }
|
|
50
|
+
static { this.properties = {
|
|
51
|
+
open: { type: Boolean, reflect: true },
|
|
52
|
+
label: {},
|
|
53
|
+
variant: { reflect: true },
|
|
54
|
+
placement: { reflect: true },
|
|
55
|
+
}; }
|
|
56
|
+
#focusLast = false;
|
|
57
|
+
constructor() {
|
|
58
|
+
super();
|
|
59
|
+
this.open = false;
|
|
60
|
+
this.label = '';
|
|
61
|
+
this.variant = 'secondary';
|
|
62
|
+
this.placement = 'bottom-start';
|
|
63
|
+
}
|
|
64
|
+
connectedCallback() {
|
|
65
|
+
super.connectedCallback();
|
|
66
|
+
checkTokensLoaded(this);
|
|
67
|
+
window.addEventListener('resize', this.#position);
|
|
68
|
+
}
|
|
69
|
+
disconnectedCallback() {
|
|
70
|
+
super.disconnectedCallback();
|
|
71
|
+
window.removeEventListener('resize', this.#position);
|
|
72
|
+
}
|
|
73
|
+
get #items() {
|
|
74
|
+
return [...this.querySelectorAll(':scope > button, :scope > [role="menuitem"]')].filter((i) => !i.hasAttribute('disabled'));
|
|
75
|
+
}
|
|
76
|
+
get #menu() {
|
|
77
|
+
return this.shadowRoot?.querySelector('[part="menu"]') ?? null;
|
|
78
|
+
}
|
|
79
|
+
get #trigger() {
|
|
80
|
+
return this.shadowRoot?.querySelector('[part="trigger"]') ?? null;
|
|
81
|
+
}
|
|
82
|
+
firstUpdated() {
|
|
83
|
+
if (!this.label)
|
|
84
|
+
devWarn('va-menu', 'This menu has no label, so its button is empty.', 'Add label="…" that names the actions (e.g. label="Actions").');
|
|
85
|
+
}
|
|
86
|
+
updated(changed) {
|
|
87
|
+
if (!changed.has('open'))
|
|
88
|
+
return;
|
|
89
|
+
const menu = this.#menu;
|
|
90
|
+
if (!menu)
|
|
91
|
+
return;
|
|
92
|
+
if (this.open && !menu.matches(':popover-open')) {
|
|
93
|
+
menu.showPopover();
|
|
94
|
+
this.#position();
|
|
95
|
+
const items = this.#items;
|
|
96
|
+
items[this.#focusLast ? items.length - 1 : 0]?.focus();
|
|
97
|
+
this.#focusLast = false;
|
|
98
|
+
}
|
|
99
|
+
else if (!this.open && menu.matches(':popover-open')) {
|
|
100
|
+
menu.hidePopover();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
#syncItems = () => {
|
|
104
|
+
for (const item of this.querySelectorAll(':scope > button')) {
|
|
105
|
+
item.setAttribute('role', 'menuitem');
|
|
106
|
+
item.tabIndex = -1;
|
|
107
|
+
item.type = 'button';
|
|
108
|
+
}
|
|
109
|
+
if ([...this.children].some((c) => c.localName !== 'button' && c.getAttribute('role') !== 'menuitem')) {
|
|
110
|
+
devWarn('va-menu', 'A menu item is not a <button>.', 'Use <button value="…">Label</button> for each item.');
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
/** Places the menu under the button, flipping above it near the bottom of the window. */
|
|
114
|
+
#position = () => {
|
|
115
|
+
const menu = this.#menu;
|
|
116
|
+
const trigger = this.#trigger;
|
|
117
|
+
if (!menu || !trigger || !this.open)
|
|
118
|
+
return;
|
|
119
|
+
const t = trigger.getBoundingClientRect();
|
|
120
|
+
const m = menu.getBoundingClientRect();
|
|
121
|
+
const gap = 4;
|
|
122
|
+
const below = t.bottom + gap + m.height <= window.innerHeight || t.top < m.height + gap;
|
|
123
|
+
const top = below ? t.bottom + gap : t.top - gap - m.height;
|
|
124
|
+
let left = this.placement === 'bottom-end' ? t.right - m.width : t.left;
|
|
125
|
+
left = Math.max(8, Math.min(left, window.innerWidth - m.width - 8));
|
|
126
|
+
// CSSOM writes, not a style attribute, so a strict CSP allows them.
|
|
127
|
+
menu.style.top = `${Math.round(top)}px`;
|
|
128
|
+
menu.style.left = `${Math.round(left)}px`;
|
|
129
|
+
};
|
|
130
|
+
#onToggle(event) {
|
|
131
|
+
// The popover closes itself on Escape or an outside click: keep `open` in step and return focus.
|
|
132
|
+
if (event.newState === 'closed' && this.open) {
|
|
133
|
+
this.open = false;
|
|
134
|
+
if (this.matches(':focus-within') || document.activeElement === document.body)
|
|
135
|
+
this.#trigger?.focus();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
#onTriggerKeydown(event) {
|
|
139
|
+
if (['ArrowDown', 'ArrowUp'].includes(event.key)) {
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
this.#focusLast = event.key === 'ArrowUp';
|
|
142
|
+
this.open = true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
#onTriggerClick() {
|
|
146
|
+
this.open = !this.open;
|
|
147
|
+
}
|
|
148
|
+
#onMenuKeydown(event) {
|
|
149
|
+
const items = this.#items;
|
|
150
|
+
const current = items.indexOf(event.target);
|
|
151
|
+
const go = (i) => items[(i + items.length) % items.length]?.focus();
|
|
152
|
+
switch (event.key) {
|
|
153
|
+
case 'ArrowDown':
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
go(current + 1);
|
|
156
|
+
break;
|
|
157
|
+
case 'ArrowUp':
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
go(current - 1);
|
|
160
|
+
break;
|
|
161
|
+
case 'Home':
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
go(0);
|
|
164
|
+
break;
|
|
165
|
+
case 'End':
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
go(items.length - 1);
|
|
168
|
+
break;
|
|
169
|
+
case 'Escape':
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
this.open = false;
|
|
172
|
+
this.#trigger?.focus();
|
|
173
|
+
break;
|
|
174
|
+
case 'Tab':
|
|
175
|
+
this.open = false;
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
if (event.key.length === 1 && /\S/.test(event.key)) {
|
|
179
|
+
// Typeahead: the next item whose text starts with the typed letter.
|
|
180
|
+
const k = event.key.toLowerCase();
|
|
181
|
+
const order = [...items.slice(current + 1), ...items.slice(0, current + 1)];
|
|
182
|
+
order.find((i) => i.textContent?.trim().toLowerCase().startsWith(k))?.focus();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
#onMenuClick(event) {
|
|
187
|
+
const item = this.#items.find((i) => event.composedPath().includes(i));
|
|
188
|
+
if (!item)
|
|
189
|
+
return;
|
|
190
|
+
this.dispatchEvent(new CustomEvent('va-select', {
|
|
191
|
+
// An item without a value reports its text, so every choice can be told apart.
|
|
192
|
+
detail: { value: item.value || item.textContent?.trim() || '', item },
|
|
193
|
+
bubbles: true,
|
|
194
|
+
composed: true,
|
|
195
|
+
}));
|
|
196
|
+
this.open = false;
|
|
197
|
+
this.#trigger?.focus();
|
|
198
|
+
}
|
|
199
|
+
render() {
|
|
200
|
+
return html `
|
|
201
|
+
<button
|
|
202
|
+
part="trigger"
|
|
203
|
+
class="trigger"
|
|
204
|
+
type="button"
|
|
205
|
+
aria-haspopup="menu"
|
|
206
|
+
aria-expanded=${this.open ? 'true' : 'false'}
|
|
207
|
+
aria-controls="menu"
|
|
208
|
+
@click=${this.#onTriggerClick}
|
|
209
|
+
@keydown=${this.#onTriggerKeydown}
|
|
210
|
+
>
|
|
211
|
+
${this.label}
|
|
212
|
+
<svg viewBox="0 0 16 16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.75"><path d="M4 6l4 4 4-4" /></svg>
|
|
213
|
+
</button>
|
|
214
|
+
<div
|
|
215
|
+
part="menu"
|
|
216
|
+
id="menu"
|
|
217
|
+
class="menu"
|
|
218
|
+
role="menu"
|
|
219
|
+
aria-label=${this.label}
|
|
220
|
+
popover="auto"
|
|
221
|
+
@toggle=${this.#onToggle}
|
|
222
|
+
@keydown=${this.#onMenuKeydown}
|
|
223
|
+
@click=${this.#onMenuClick}
|
|
224
|
+
>
|
|
225
|
+
<slot @slotchange=${this.#syncItems}></slot>
|
|
226
|
+
</div>
|
|
227
|
+
`;
|
|
228
|
+
}
|
|
229
|
+
static { this.styles = [
|
|
230
|
+
focusRing,
|
|
231
|
+
css `
|
|
232
|
+
:host {
|
|
233
|
+
display: inline-block;
|
|
234
|
+
vertical-align: middle;
|
|
235
|
+
font-family: var(--va-font-family-ui);
|
|
236
|
+
}
|
|
237
|
+
:host([hidden]) {
|
|
238
|
+
display: none;
|
|
239
|
+
}
|
|
240
|
+
.trigger {
|
|
241
|
+
display: inline-flex;
|
|
242
|
+
align-items: center;
|
|
243
|
+
gap: var(--va-space-gap-md);
|
|
244
|
+
min-height: var(--va-size-control-md);
|
|
245
|
+
margin: 0;
|
|
246
|
+
padding: 0 var(--va-space-inline-md) 0 var(--va-space-inline-lg);
|
|
247
|
+
border: 1px solid var(--va-color-border-control);
|
|
248
|
+
border-radius: var(--va-radius-control);
|
|
249
|
+
background: transparent;
|
|
250
|
+
color: var(--va-color-text-default);
|
|
251
|
+
font: inherit;
|
|
252
|
+
font-size: var(--va-font-size-md);
|
|
253
|
+
font-weight: var(--va-font-weight-medium);
|
|
254
|
+
cursor: pointer;
|
|
255
|
+
}
|
|
256
|
+
:host([variant='ghost']) .trigger {
|
|
257
|
+
border-color: transparent;
|
|
258
|
+
color: var(--va-color-accent-default);
|
|
259
|
+
}
|
|
260
|
+
.trigger:hover {
|
|
261
|
+
background: var(--va-color-surface-subtle);
|
|
262
|
+
}
|
|
263
|
+
.trigger svg {
|
|
264
|
+
width: 1rem;
|
|
265
|
+
height: 1rem;
|
|
266
|
+
transition: transform var(--va-motion-duration-fast) ease-out;
|
|
267
|
+
}
|
|
268
|
+
:host([open]) .trigger svg {
|
|
269
|
+
transform: rotate(180deg);
|
|
270
|
+
}
|
|
271
|
+
.menu {
|
|
272
|
+
position: fixed;
|
|
273
|
+
inset: auto;
|
|
274
|
+
margin: 0;
|
|
275
|
+
min-width: 10rem;
|
|
276
|
+
max-width: calc(100vw - 16px);
|
|
277
|
+
padding: var(--va-space-gap-sm);
|
|
278
|
+
border: 1px solid var(--va-color-border-subtle);
|
|
279
|
+
border-radius: var(--va-menu-radius, var(--va-radius-surface));
|
|
280
|
+
background-color: var(--va-menu-bg, var(--va-color-surface-default));
|
|
281
|
+
color: var(--va-color-text-default);
|
|
282
|
+
box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
|
|
283
|
+
}
|
|
284
|
+
.menu:popover-open {
|
|
285
|
+
display: flex;
|
|
286
|
+
flex-direction: column;
|
|
287
|
+
}
|
|
288
|
+
::slotted(button) {
|
|
289
|
+
display: block;
|
|
290
|
+
width: 100%;
|
|
291
|
+
min-height: var(--va-size-control-sm);
|
|
292
|
+
margin: 0;
|
|
293
|
+
padding: 0 var(--va-space-inline-md);
|
|
294
|
+
border: none;
|
|
295
|
+
border-radius: var(--va-radius-control);
|
|
296
|
+
background: transparent;
|
|
297
|
+
color: inherit;
|
|
298
|
+
font: inherit;
|
|
299
|
+
font-size: var(--va-font-size-md);
|
|
300
|
+
text-align: left;
|
|
301
|
+
cursor: pointer;
|
|
302
|
+
}
|
|
303
|
+
::slotted(button:hover),
|
|
304
|
+
::slotted(button:focus) {
|
|
305
|
+
background-color: var(--va-menu-item-bg-hover, var(--va-color-surface-subtle));
|
|
306
|
+
outline: none;
|
|
307
|
+
}
|
|
308
|
+
::slotted(button:focus-visible) {
|
|
309
|
+
outline: var(--va-size-focus-ring, 2px) solid var(--va-color-focus);
|
|
310
|
+
outline-offset: -2px;
|
|
311
|
+
}
|
|
312
|
+
@media (prefers-reduced-motion: reduce) {
|
|
313
|
+
.trigger svg {
|
|
314
|
+
transition: none;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
@media (forced-colors: active) {
|
|
318
|
+
.menu {
|
|
319
|
+
border: 1px solid CanvasText;
|
|
320
|
+
}
|
|
321
|
+
::slotted(button:focus) {
|
|
322
|
+
background-color: Highlight;
|
|
323
|
+
color: HighlightText;
|
|
324
|
+
forced-color-adjust: none;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
`,
|
|
328
|
+
]; }
|
|
329
|
+
}
|
|
330
|
+
//# sourceMappingURL=menu.js.map
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { FormControl } from '../core/form-control.js';
|
|
3
|
+
import { focusRing } from '../core/focus-ring.js';
|
|
4
|
+
import { checkTokensLoaded, devWarn } from '../core/warn.js';
|
|
5
|
+
/**
|
|
6
|
+
* An on/off switch for a setting that takes effect immediately.
|
|
7
|
+
*
|
|
8
|
+
* @tag va-switch
|
|
9
|
+
* @summary Turns a setting on or off, with immediate effect.
|
|
10
|
+
*
|
|
11
|
+
* @slot - The visible label. Clicking it flips the switch.
|
|
12
|
+
* @slot help-text - Optional help text under the label. It becomes the switch's description.
|
|
13
|
+
*
|
|
14
|
+
* @csspart control - The switch's track (a native button with `role="switch"`).
|
|
15
|
+
* @csspart thumb - The round thumb.
|
|
16
|
+
* @csspart label - The label text.
|
|
17
|
+
* @csspart help-text - The help text.
|
|
18
|
+
*
|
|
19
|
+
* @cssprop {<color>} --va-switch-track-bg - Track colour when off.
|
|
20
|
+
* @cssprop {<color>} --va-switch-track-bg-checked - Track colour when on.
|
|
21
|
+
* @cssprop {<color>} --va-switch-thumb-bg - Thumb colour.
|
|
22
|
+
*
|
|
23
|
+
* @cssprop-applies --va-switch-track-bg control:unchecked background-color
|
|
24
|
+
* @cssprop-applies --va-switch-track-bg-checked control:checked background-color
|
|
25
|
+
* @cssprop-applies --va-switch-thumb-bg thumb background-color
|
|
26
|
+
*
|
|
27
|
+
* @keyboard Space | toggles | Turns the switch on or off.
|
|
28
|
+
* @keyboard Enter | toggles | Turns the switch on or off.
|
|
29
|
+
* @keyboard Tab | focus-next | Moves focus to the next control.
|
|
30
|
+
*
|
|
31
|
+
* @a11y-role switch
|
|
32
|
+
* @a11y-name-from text, accessible-label
|
|
33
|
+
* @replaces [role="switch"]
|
|
34
|
+
* @a11y-name The slotted label text. Use `accessible-label` only when there is no visible label.
|
|
35
|
+
* @a11y-note The state is announced as on or off, not checked or unchecked.
|
|
36
|
+
* @a11y-note Style the on state with `va-switch:state(checked)`; the `checked` attribute is the initial state only, as on a native checkbox.
|
|
37
|
+
*
|
|
38
|
+
* @guidance-use For a setting that applies as soon as it changes, such as "Dark theme" or "Email notifications".
|
|
39
|
+
* @guidance-avoid For a choice that is only applied when a form is submitted, or for agreeing to terms.
|
|
40
|
+
* @guidance-instead `va-checkbox` for choices submitted with a form.
|
|
41
|
+
*
|
|
42
|
+
* @fires change - When the user flips the switch. Read `event.target.checked`.
|
|
43
|
+
* @fires input - When the user flips the switch, just before `change`.
|
|
44
|
+
* @event-trigger change | click
|
|
45
|
+
* @event-trigger input | click
|
|
46
|
+
*/
|
|
47
|
+
export class VaSwitch extends FormControl {
|
|
48
|
+
static { this.shadowRootOptions = { ...FormControl.shadowRootOptions, delegatesFocus: true }; }
|
|
49
|
+
static { this.properties = {
|
|
50
|
+
checked: { type: Boolean },
|
|
51
|
+
value: {},
|
|
52
|
+
accessibleLabel: { attribute: 'accessible-label' },
|
|
53
|
+
}; }
|
|
54
|
+
#defaultChecked;
|
|
55
|
+
constructor() {
|
|
56
|
+
super();
|
|
57
|
+
this.checked = false;
|
|
58
|
+
this.value = 'on';
|
|
59
|
+
this.accessibleLabel = '';
|
|
60
|
+
}
|
|
61
|
+
connectedCallback() {
|
|
62
|
+
super.connectedCallback();
|
|
63
|
+
// The default is the initial attribute, read once: moving the element must not change it.
|
|
64
|
+
this.#defaultChecked ??= this.hasAttribute('checked');
|
|
65
|
+
checkTokensLoaded(this);
|
|
66
|
+
}
|
|
67
|
+
firstUpdated() {
|
|
68
|
+
requestAnimationFrame(() => {
|
|
69
|
+
const text = this.#labelText();
|
|
70
|
+
if (!text && !this.accessibleLabel) {
|
|
71
|
+
devWarn('va-switch', 'This switch has no accessible name.', 'Put label text inside it (e.g. Dark theme), or add accessible-label="…" when there is no room for one.');
|
|
72
|
+
}
|
|
73
|
+
else if (text && this.accessibleLabel && !this.accessibleLabel.toLowerCase().includes(text.toLowerCase())) {
|
|
74
|
+
devWarn('va-switch', `accessible-label="${this.accessibleLabel}" replaces the visible label "${text}" for screen readers (WCAG 2.5.3).`, 'Remove accessible-label: the visible text is the name.');
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/** The visible label: text not in the help-text slot. */
|
|
79
|
+
#labelText() {
|
|
80
|
+
return [...this.childNodes]
|
|
81
|
+
.filter((n) => !(n instanceof Element && n.slot))
|
|
82
|
+
.map((n) => n.textContent ?? '')
|
|
83
|
+
.join('')
|
|
84
|
+
.trim();
|
|
85
|
+
}
|
|
86
|
+
updated() {
|
|
87
|
+
this._setState('checked', this.checked);
|
|
88
|
+
this._internals.setFormValue(this.checked ? this.value : null);
|
|
89
|
+
this._internals.setValidity(this.required && !this.checked ? { valueMissing: true } : {}, this.required && !this.checked ? 'Turn this on to continue.' : '', this.shadowRoot?.querySelector('button') ?? undefined);
|
|
90
|
+
}
|
|
91
|
+
/** @internal */
|
|
92
|
+
formResetCallback() {
|
|
93
|
+
this.checked = this.#defaultChecked ?? false;
|
|
94
|
+
}
|
|
95
|
+
#toggle() {
|
|
96
|
+
if (this._isDisabled)
|
|
97
|
+
return;
|
|
98
|
+
this.checked = !this.checked;
|
|
99
|
+
this._emit('input');
|
|
100
|
+
this._emit('change');
|
|
101
|
+
}
|
|
102
|
+
render() {
|
|
103
|
+
return html `
|
|
104
|
+
<button
|
|
105
|
+
part="control"
|
|
106
|
+
class="control ${this.checked ? 'on' : ''}"
|
|
107
|
+
type="button"
|
|
108
|
+
role="switch"
|
|
109
|
+
aria-checked=${this.checked ? 'true' : 'false'}
|
|
110
|
+
aria-labelledby=${this.accessibleLabel ? nothing : 'label'}
|
|
111
|
+
aria-label=${this.accessibleLabel || nothing}
|
|
112
|
+
aria-describedby="help-text"
|
|
113
|
+
?disabled=${this._isDisabled}
|
|
114
|
+
@click=${this.#toggle}
|
|
115
|
+
>
|
|
116
|
+
<span part="thumb" class="thumb"></span>
|
|
117
|
+
</button>
|
|
118
|
+
<span class="text">
|
|
119
|
+
<span part="label" id="label" class="label ${this._isDisabled ? 'disabled' : ''}" @click=${this.#toggle}><slot></slot></span>
|
|
120
|
+
<span part="help-text" id="help-text" class="help-text"><slot name="help-text"></slot></span>
|
|
121
|
+
</span>
|
|
122
|
+
`;
|
|
123
|
+
}
|
|
124
|
+
static { this.styles = [
|
|
125
|
+
focusRing,
|
|
126
|
+
css `
|
|
127
|
+
:host {
|
|
128
|
+
--_track: var(--va-switch-track-bg, var(--va-color-border-control));
|
|
129
|
+
--_track-on: var(--va-switch-track-bg-checked, var(--va-color-accent-default));
|
|
130
|
+
--_thumb: var(--va-switch-thumb-bg, var(--va-color-surface-default));
|
|
131
|
+
display: inline-flex;
|
|
132
|
+
align-items: flex-start;
|
|
133
|
+
gap: var(--va-space-inline-sm);
|
|
134
|
+
font-family: var(--va-font-family-ui);
|
|
135
|
+
font-size: var(--va-font-size-md);
|
|
136
|
+
color: var(--va-color-text-default);
|
|
137
|
+
vertical-align: middle;
|
|
138
|
+
}
|
|
139
|
+
:host([hidden]) {
|
|
140
|
+
display: none;
|
|
141
|
+
}
|
|
142
|
+
.control {
|
|
143
|
+
flex: none;
|
|
144
|
+
position: relative;
|
|
145
|
+
width: 2.75rem;
|
|
146
|
+
height: 1.5rem;
|
|
147
|
+
margin: 0;
|
|
148
|
+
padding: 0;
|
|
149
|
+
border: 1px solid transparent;
|
|
150
|
+
border-radius: var(--va-radius-round);
|
|
151
|
+
background-color: var(--_track);
|
|
152
|
+
cursor: pointer;
|
|
153
|
+
transition: background-color var(--va-motion-duration-fast) ease-out;
|
|
154
|
+
}
|
|
155
|
+
.control.on {
|
|
156
|
+
background-color: var(--_track-on);
|
|
157
|
+
}
|
|
158
|
+
.thumb {
|
|
159
|
+
position: absolute;
|
|
160
|
+
top: 2px;
|
|
161
|
+
left: 2px;
|
|
162
|
+
width: calc(1.5rem - 6px);
|
|
163
|
+
height: calc(1.5rem - 6px);
|
|
164
|
+
border-radius: 50%;
|
|
165
|
+
background-color: var(--_thumb);
|
|
166
|
+
box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
|
|
167
|
+
transition: transform var(--va-motion-duration-fast) ease-out;
|
|
168
|
+
}
|
|
169
|
+
.control.on .thumb {
|
|
170
|
+
transform: translateX(1.25rem);
|
|
171
|
+
}
|
|
172
|
+
.control:disabled,
|
|
173
|
+
.label.disabled {
|
|
174
|
+
opacity: 0.5;
|
|
175
|
+
cursor: not-allowed;
|
|
176
|
+
}
|
|
177
|
+
.text {
|
|
178
|
+
display: flex;
|
|
179
|
+
flex-direction: column;
|
|
180
|
+
padding-top: 0.1rem;
|
|
181
|
+
}
|
|
182
|
+
.label {
|
|
183
|
+
cursor: pointer;
|
|
184
|
+
user-select: none;
|
|
185
|
+
}
|
|
186
|
+
.help-text {
|
|
187
|
+
color: var(--va-color-text-muted);
|
|
188
|
+
font-size: var(--va-font-size-sm);
|
|
189
|
+
}
|
|
190
|
+
@media (prefers-reduced-motion: reduce) {
|
|
191
|
+
.control,
|
|
192
|
+
.thumb {
|
|
193
|
+
transition: none;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
@media (forced-colors: active) {
|
|
197
|
+
.control {
|
|
198
|
+
border-color: ButtonText;
|
|
199
|
+
}
|
|
200
|
+
.control.on {
|
|
201
|
+
background-color: Highlight;
|
|
202
|
+
}
|
|
203
|
+
.thumb {
|
|
204
|
+
background-color: ButtonText;
|
|
205
|
+
}
|
|
206
|
+
.control.on .thumb {
|
|
207
|
+
background-color: HighlightText;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
`,
|
|
211
|
+
]; }
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=switch.js.map
|