@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 @@
|
|
|
1
|
+
{"version":3,"file":"menu.js","sourceRoot":"","sources":["../../../src/menu/menu.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,IAAI,EAAkD,MAAM,KAAK,CAAC;AAC5F,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,MAAM,OAAO,MAAO,SAAQ,UAAU;aACpB,sBAAiB,GAAmB,EAAE,GAAG,UAAU,CAAC,iBAAiB,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC;aAE9F,eAAU,GAAyB;QACjD,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE;QACtC,KAAK,EAAE,EAAE;QACT,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;QAC1B,SAAS,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;KAC7B,CAAC;IAWF,UAAU,GAAG,KAAK,CAAC;IAEnB;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,cAAc,CAAC;IAClC,CAAC;IAEQ,iBAAiB;QACxB,KAAK,CAAC,iBAAiB,EAAE,CAAC;QA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AO,EAAE,IAAI;YACb,QAAQ,EAAE,IAAI;SACf,CAAC,CACH,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAClB,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC;IACzB,CAAC;IAEQ,MAAM;QACb,OAAO,IAAI,CAAA;;;;;;wBAMS,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO;;iBAEnC,IAAI,CAAC,eAAe;mBAClB,IAAI,CAAC,iBAAiB;;UAE/B,IAAI,CAAC,KAAK;;;;;;;;qBAQC,IAAI,CAAC,KAAK;;kBAEb,IAAI,CAAC,SAAS;mBACb,IAAI,CAAC,cAAc;iBACrB,IAAI,CAAC,YAAY;;4BAEN,IAAI,CAAC,UAAU;;KAEtC,CAAC;IACJ,CAAC;aAEe,WAAM,GAAG;QACvB,SAAS;QACT,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAgGF;KACF,CAAC","sourcesContent":["import { LitElement, css, html, type PropertyDeclarations, type PropertyValues } from 'lit';\nimport { focusRing } from '../core/focus-ring.js';\nimport { checkTokensLoaded, devWarn } from '../core/warn.js';\n\n/**\n * A button that opens a menu of actions, following the WAI-ARIA menu button pattern.\n *\n * @tag va-menu\n * @summary Opens a short list of actions from a button, such as Edit, Duplicate and Delete.\n *\n * @slot - The menu items, as `<button value=\"…\">` elements.\n *\n * @csspart trigger - The button that opens the menu.\n * @csspart menu - The popup list.\n *\n * @cssprop {<color>} --va-menu-bg - Background colour of the menu.\n * @cssprop {<length>} --va-menu-radius - Corner radius of the menu.\n * @cssprop {<color>} --va-menu-item-bg-hover - Background colour of the focused or hovered item.\n *\n * @cssprop-applies --va-menu-bg menu:open background-color\n * @cssprop-applies --va-menu-radius menu:open border-top-left-radius\n * @cssprop-applies --va-menu-item-bg-hover light([role=\"menuitem\"]):focus background-color\n *\n * @keyboard-items [role=\"menuitem\"]\n * @keyboard Enter | opens | Opens the menu and focuses the first item.\n * @keyboard Space | opens | Opens the menu and focuses the first item.\n * @keyboard ArrowDown | opens | Opens the menu and focuses the first item.\n * @keyboard open: ArrowDown | next | Moves to the next item, wrapping round.\n * @keyboard open: ArrowUp | previous | Moves to the previous item, wrapping round.\n * @keyboard open: Home | first | Moves to the first item.\n * @keyboard open: End | last | Moves to the last item.\n * @keyboard open: Enter | selects | Chooses the focused item and closes the menu.\n * @keyboard open: Escape | closes | Closes the menu and returns focus to the button.\n *\n * @a11y-role menu\n * @a11y-name-from label\n * @replaces [role=\"menu\"]\n * @a11y-name The `label`, which is also the button's text.\n * @a11y-note The button has `aria-haspopup=\"menu\"` and `aria-expanded`. Items get `role=\"menuitem\"`.\n * @a11y-note Typing a letter moves to the next item that starts with it.\n *\n * @guidance-use For a few actions on one thing, when there isn't room to show them as buttons.\n * @guidance-avoid For navigation, for choosing a value in a form, or for one or two actions.\n * @guidance-instead Links for navigation; a select or radio group for form values; plain buttons for one or two actions.\n *\n * @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.\n * @event-trigger va-select | press Enter | open\n */\nexport class VaMenu extends LitElement {\n static override shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, delegatesFocus: true };\n\n static override properties: PropertyDeclarations = {\n open: { type: Boolean, reflect: true },\n label: {},\n variant: { reflect: true },\n placement: { reflect: true },\n };\n\n /** Whether the menu is open. */\n declare open: boolean;\n /** The button's text, and the menu's accessible name. */\n declare label: string;\n /** The button's look, as for `va-button`. */\n declare variant: 'secondary' | 'ghost';\n /** Which edge of the button the menu lines up with. */\n declare placement: 'bottom-start' | 'bottom-end';\n\n #focusLast = false;\n\n constructor() {\n super();\n this.open = false;\n this.label = '';\n this.variant = 'secondary';\n this.placement = 'bottom-start';\n }\n\n override connectedCallback(): void {\n super.connectedCallback();\n checkTokensLoaded(this);\n window.addEventListener('resize', this.#position);\n }\n\n override disconnectedCallback(): void {\n super.disconnectedCallback();\n window.removeEventListener('resize', this.#position);\n }\n\n get #items(): HTMLElement[] {\n return [...this.querySelectorAll<HTMLElement>(':scope > button, :scope > [role=\"menuitem\"]')].filter((i) => !i.hasAttribute('disabled'));\n }\n\n get #menu(): HTMLElement | null {\n return this.shadowRoot?.querySelector('[part=\"menu\"]') ?? null;\n }\n\n get #trigger(): HTMLElement | null {\n return this.shadowRoot?.querySelector('[part=\"trigger\"]') ?? null;\n }\n\n override firstUpdated(): void {\n if (!this.label) devWarn('va-menu', 'This menu has no label, so its button is empty.', 'Add label=\"…\" that names the actions (e.g. label=\"Actions\").');\n }\n\n override updated(changed: PropertyValues): void {\n if (!changed.has('open')) return;\n const menu = this.#menu;\n if (!menu) return;\n if (this.open && !menu.matches(':popover-open')) {\n menu.showPopover();\n this.#position();\n const items = this.#items;\n items[this.#focusLast ? items.length - 1 : 0]?.focus();\n this.#focusLast = false;\n } else if (!this.open && menu.matches(':popover-open')) {\n menu.hidePopover();\n }\n }\n\n #syncItems = (): void => {\n for (const item of this.querySelectorAll<HTMLElement>(':scope > button')) {\n item.setAttribute('role', 'menuitem');\n item.tabIndex = -1;\n (item as HTMLButtonElement).type = 'button';\n }\n if ([...this.children].some((c) => c.localName !== 'button' && c.getAttribute('role') !== 'menuitem')) {\n devWarn('va-menu', 'A menu item is not a <button>.', 'Use <button value=\"…\">Label</button> for each item.');\n }\n };\n\n /** Places the menu under the button, flipping above it near the bottom of the window. */\n #position = (): void => {\n const menu = this.#menu;\n const trigger = this.#trigger;\n if (!menu || !trigger || !this.open) return;\n const t = trigger.getBoundingClientRect();\n const m = menu.getBoundingClientRect();\n const gap = 4;\n const below = t.bottom + gap + m.height <= window.innerHeight || t.top < m.height + gap;\n const top = below ? t.bottom + gap : t.top - gap - m.height;\n let left = this.placement === 'bottom-end' ? t.right - m.width : t.left;\n left = Math.max(8, Math.min(left, window.innerWidth - m.width - 8));\n // CSSOM writes, not a style attribute, so a strict CSP allows them.\n menu.style.top = `${Math.round(top)}px`;\n menu.style.left = `${Math.round(left)}px`;\n };\n\n #onToggle(event: ToggleEvent): void {\n // The popover closes itself on Escape or an outside click: keep `open` in step and return focus.\n if (event.newState === 'closed' && this.open) {\n this.open = false;\n if (this.matches(':focus-within') || document.activeElement === document.body) this.#trigger?.focus();\n }\n }\n\n #onTriggerKeydown(event: KeyboardEvent): void {\n if (['ArrowDown', 'ArrowUp'].includes(event.key)) {\n event.preventDefault();\n this.#focusLast = event.key === 'ArrowUp';\n this.open = true;\n }\n }\n\n #onTriggerClick(): void {\n this.open = !this.open;\n }\n\n #onMenuKeydown(event: KeyboardEvent): void {\n const items = this.#items;\n const current = items.indexOf(event.target as HTMLElement);\n const go = (i: number) => items[(i + items.length) % items.length]?.focus();\n switch (event.key) {\n case 'ArrowDown':\n event.preventDefault();\n go(current + 1);\n break;\n case 'ArrowUp':\n event.preventDefault();\n go(current - 1);\n break;\n case 'Home':\n event.preventDefault();\n go(0);\n break;\n case 'End':\n event.preventDefault();\n go(items.length - 1);\n break;\n case 'Escape':\n event.preventDefault();\n this.open = false;\n this.#trigger?.focus();\n break;\n case 'Tab':\n this.open = false;\n break;\n default:\n if (event.key.length === 1 && /\\S/.test(event.key)) {\n // Typeahead: the next item whose text starts with the typed letter.\n const k = event.key.toLowerCase();\n const order = [...items.slice(current + 1), ...items.slice(0, current + 1)];\n order.find((i) => i.textContent?.trim().toLowerCase().startsWith(k))?.focus();\n }\n }\n }\n\n #onMenuClick(event: MouseEvent): void {\n const item = this.#items.find((i) => event.composedPath().includes(i));\n if (!item) return;\n this.dispatchEvent(\n new CustomEvent('va-select', {\n // An item without a value reports its text, so every choice can be told apart.\n detail: { value: (item as HTMLButtonElement).value || item.textContent?.trim() || '', item },\n bubbles: true,\n composed: true,\n })\n );\n this.open = false;\n this.#trigger?.focus();\n }\n\n override render() {\n return html`\n <button\n part=\"trigger\"\n class=\"trigger\"\n type=\"button\"\n aria-haspopup=\"menu\"\n aria-expanded=${this.open ? 'true' : 'false'}\n aria-controls=\"menu\"\n @click=${this.#onTriggerClick}\n @keydown=${this.#onTriggerKeydown}\n >\n ${this.label}\n <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>\n </button>\n <div\n part=\"menu\"\n id=\"menu\"\n class=\"menu\"\n role=\"menu\"\n aria-label=${this.label}\n popover=\"auto\"\n @toggle=${this.#onToggle}\n @keydown=${this.#onMenuKeydown}\n @click=${this.#onMenuClick}\n >\n <slot @slotchange=${this.#syncItems}></slot>\n </div>\n `;\n }\n\n static override styles = [\n focusRing,\n css`\n :host {\n display: inline-block;\n vertical-align: middle;\n font-family: var(--va-font-family-ui);\n }\n :host([hidden]) {\n display: none;\n }\n .trigger {\n display: inline-flex;\n align-items: center;\n gap: var(--va-space-gap-md);\n min-height: var(--va-size-control-md);\n margin: 0;\n padding: 0 var(--va-space-inline-md) 0 var(--va-space-inline-lg);\n border: 1px solid var(--va-color-border-control);\n border-radius: var(--va-radius-control);\n background: transparent;\n color: var(--va-color-text-default);\n font: inherit;\n font-size: var(--va-font-size-md);\n font-weight: var(--va-font-weight-medium);\n cursor: pointer;\n }\n :host([variant='ghost']) .trigger {\n border-color: transparent;\n color: var(--va-color-accent-default);\n }\n .trigger:hover {\n background: var(--va-color-surface-subtle);\n }\n .trigger svg {\n width: 1rem;\n height: 1rem;\n transition: transform var(--va-motion-duration-fast) ease-out;\n }\n :host([open]) .trigger svg {\n transform: rotate(180deg);\n }\n .menu {\n position: fixed;\n inset: auto;\n margin: 0;\n min-width: 10rem;\n max-width: calc(100vw - 16px);\n padding: var(--va-space-gap-sm);\n border: 1px solid var(--va-color-border-subtle);\n border-radius: var(--va-menu-radius, var(--va-radius-surface));\n background-color: var(--va-menu-bg, var(--va-color-surface-default));\n color: var(--va-color-text-default);\n box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);\n }\n .menu:popover-open {\n display: flex;\n flex-direction: column;\n }\n ::slotted(button) {\n display: block;\n width: 100%;\n min-height: var(--va-size-control-sm);\n margin: 0;\n padding: 0 var(--va-space-inline-md);\n border: none;\n border-radius: var(--va-radius-control);\n background: transparent;\n color: inherit;\n font: inherit;\n font-size: var(--va-font-size-md);\n text-align: left;\n cursor: pointer;\n }\n ::slotted(button:hover),\n ::slotted(button:focus) {\n background-color: var(--va-menu-item-bg-hover, var(--va-color-surface-subtle));\n outline: none;\n }\n ::slotted(button:focus-visible) {\n outline: var(--va-size-focus-ring, 2px) solid var(--va-color-focus);\n outline-offset: -2px;\n }\n @media (prefers-reduced-motion: reduce) {\n .trigger svg {\n transition: none;\n }\n }\n @media (forced-colors: active) {\n .menu {\n border: 1px solid CanvasText;\n }\n ::slotted(button:focus) {\n background-color: Highlight;\n color: HighlightText;\n forced-color-adjust: none;\n }\n }\n `,\n ];\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'va-menu': VaMenu;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/switch/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/switch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,QAAQ,EAAE,CAAC;AAEpB,MAAM,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC","sourcesContent":["import { define } from '../core/define.js';\nimport { VaSwitch } from './switch.js';\n\nexport { VaSwitch };\n\ndefine('va-switch', VaSwitch);\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { FormControl } from '../core/form-control.js';
|
|
3
|
+
/**
|
|
4
|
+
* An on/off switch for a setting that takes effect immediately.
|
|
5
|
+
*
|
|
6
|
+
* @tag va-switch
|
|
7
|
+
* @summary Turns a setting on or off, with immediate effect.
|
|
8
|
+
*
|
|
9
|
+
* @slot - The visible label. Clicking it flips the switch.
|
|
10
|
+
* @slot help-text - Optional help text under the label. It becomes the switch's description.
|
|
11
|
+
*
|
|
12
|
+
* @csspart control - The switch's track (a native button with `role="switch"`).
|
|
13
|
+
* @csspart thumb - The round thumb.
|
|
14
|
+
* @csspart label - The label text.
|
|
15
|
+
* @csspart help-text - The help text.
|
|
16
|
+
*
|
|
17
|
+
* @cssprop {<color>} --va-switch-track-bg - Track colour when off.
|
|
18
|
+
* @cssprop {<color>} --va-switch-track-bg-checked - Track colour when on.
|
|
19
|
+
* @cssprop {<color>} --va-switch-thumb-bg - Thumb colour.
|
|
20
|
+
*
|
|
21
|
+
* @cssprop-applies --va-switch-track-bg control:unchecked background-color
|
|
22
|
+
* @cssprop-applies --va-switch-track-bg-checked control:checked background-color
|
|
23
|
+
* @cssprop-applies --va-switch-thumb-bg thumb background-color
|
|
24
|
+
*
|
|
25
|
+
* @keyboard Space | toggles | Turns the switch on or off.
|
|
26
|
+
* @keyboard Enter | toggles | Turns the switch on or off.
|
|
27
|
+
* @keyboard Tab | focus-next | Moves focus to the next control.
|
|
28
|
+
*
|
|
29
|
+
* @a11y-role switch
|
|
30
|
+
* @a11y-name-from text, accessible-label
|
|
31
|
+
* @replaces [role="switch"]
|
|
32
|
+
* @a11y-name The slotted label text. Use `accessible-label` only when there is no visible label.
|
|
33
|
+
* @a11y-note The state is announced as on or off, not checked or unchecked.
|
|
34
|
+
* @a11y-note Style the on state with `va-switch:state(checked)`; the `checked` attribute is the initial state only, as on a native checkbox.
|
|
35
|
+
*
|
|
36
|
+
* @guidance-use For a setting that applies as soon as it changes, such as "Dark theme" or "Email notifications".
|
|
37
|
+
* @guidance-avoid For a choice that is only applied when a form is submitted, or for agreeing to terms.
|
|
38
|
+
* @guidance-instead `va-checkbox` for choices submitted with a form.
|
|
39
|
+
*
|
|
40
|
+
* @fires change - When the user flips the switch. Read `event.target.checked`.
|
|
41
|
+
* @fires input - When the user flips the switch, just before `change`.
|
|
42
|
+
* @event-trigger change | click
|
|
43
|
+
* @event-trigger input | click
|
|
44
|
+
*/
|
|
45
|
+
export declare class VaSwitch extends FormControl {
|
|
46
|
+
#private;
|
|
47
|
+
static shadowRootOptions: ShadowRootInit;
|
|
48
|
+
static properties: PropertyDeclarations;
|
|
49
|
+
/** Whether the switch is on. */
|
|
50
|
+
checked: boolean;
|
|
51
|
+
/** Submitted with `name` while the switch is on. Defaults to `on`, as for a native checkbox. */
|
|
52
|
+
value: string;
|
|
53
|
+
/** Accessible name for a switch with no visible label. */
|
|
54
|
+
accessibleLabel: string;
|
|
55
|
+
constructor();
|
|
56
|
+
connectedCallback(): void;
|
|
57
|
+
firstUpdated(): void;
|
|
58
|
+
updated(): void;
|
|
59
|
+
/** @internal */
|
|
60
|
+
formResetCallback(): void;
|
|
61
|
+
render(): import("lit").TemplateResult<1>;
|
|
62
|
+
static styles: import("lit").CSSResult[];
|
|
63
|
+
}
|
|
64
|
+
declare global {
|
|
65
|
+
interface HTMLElementTagNameMap {
|
|
66
|
+
'va-switch': VaSwitch;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=switch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/switch/switch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,oBAAoB,EAAE,MAAM,KAAK,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAItD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,qBAAa,QAAS,SAAQ,WAAW;;IACvC,OAAgB,iBAAiB,EAAE,cAAc,CAA8D;IAE/G,OAAgB,UAAU,EAAE,oBAAoB,CAI9C;IAEF,gCAAgC;IACxB,OAAO,EAAE,OAAO,CAAC;IACzB,gGAAgG;IACxF,KAAK,EAAE,MAAM,CAAC;IACtB,0DAA0D;IAClD,eAAe,EAAE,MAAM,CAAC;;IAWvB,iBAAiB,IAAI,IAAI;IAOzB,YAAY,IAAI,IAAI;IAoBpB,OAAO,IAAI,IAAI;IAUxB,gBAAgB;IACP,iBAAiB,IAAI,IAAI;IAWzB,MAAM;IAuBf,OAAgB,MAAM,4BAuFpB;CACH;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,WAAW,EAAE,QAAQ,CAAC;KACvB;CACF"}
|