@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,258 @@
|
|
|
1
|
+
import { LitElement, css, html, nothing } from 'lit';
|
|
2
|
+
import { repeat } from 'lit/directives/repeat.js';
|
|
3
|
+
import { focusRing } from '../core/focus-ring.js';
|
|
4
|
+
import { checkTokensLoaded, devWarn } from '../core/warn.js';
|
|
5
|
+
const VARIANTS = ['info', 'success', 'error'];
|
|
6
|
+
let nextId = 1;
|
|
7
|
+
/**
|
|
8
|
+
* A region for short, non-blocking status messages ("toasts"). Place one per page and call `show()`,
|
|
9
|
+
* or point a `va-button` at it with `command="--show"`; the button's `value` is the message.
|
|
10
|
+
*
|
|
11
|
+
* @tag va-toast
|
|
12
|
+
* @summary Shows short status messages, such as "Changes saved", without interrupting the user.
|
|
13
|
+
*
|
|
14
|
+
* @csspart region - The fixed area the toasts appear in.
|
|
15
|
+
* @csspart toast - Each message.
|
|
16
|
+
* @csspart close-button - Each message's close button.
|
|
17
|
+
*
|
|
18
|
+
* @cssprop {<color>} --va-toast-bg - Background colour of a message.
|
|
19
|
+
* @cssprop {<color>} --va-toast-fg - Text colour of a message.
|
|
20
|
+
* @cssprop {<length>} --va-toast-radius - Corner radius of a message.
|
|
21
|
+
*
|
|
22
|
+
* @cssprop-applies --va-toast-bg toast:open background-color
|
|
23
|
+
* @cssprop-applies --va-toast-fg toast:open color
|
|
24
|
+
* @cssprop-applies --va-toast-radius toast:open border-top-left-radius
|
|
25
|
+
*
|
|
26
|
+
* @keyboard-items [part~="close-button"]
|
|
27
|
+
* @keyboard open: Escape | closes | Dismisses the message that has focus.
|
|
28
|
+
*
|
|
29
|
+
* @a11y-role status
|
|
30
|
+
* @a11y-name The `accessible-label` attribute, "Notifications" by default (not shown).
|
|
31
|
+
* @a11y-note Info and success messages are announced politely; errors use `role="alert"` and are announced at once.
|
|
32
|
+
* @a11y-note Messages pause their timer while hovered or focused, and errors stay until dismissed (WCAG 2.2.1).
|
|
33
|
+
* @a11y-note While a modal dialog is open, the element moves itself into that dialog (a light-DOM move, so its messages stay clickable and announced) and back to its place when the dialog closes. Don't render it from a framework that would fight the move; place it once, outside framework-managed DOM.
|
|
34
|
+
*
|
|
35
|
+
* @guidance-use To confirm that something happened, such as "Changes saved" or "Link copied", or to report a background failure.
|
|
36
|
+
* @guidance-avoid For anything the user must act on or read in full, or for form validation errors.
|
|
37
|
+
* @guidance-instead `va-dialog` for decisions; `va-text-field`'s `error` for field errors.
|
|
38
|
+
*
|
|
39
|
+
* @command --show | Shows the sending button's `value` as a message. Its `data-toast-variant` sets the look: `info` (default), `success` or `error`.
|
|
40
|
+
* @command-data --show | toast-variant | info success error
|
|
41
|
+
* @fires {CustomEvent<{ id: number, reason: 'timeout' | 'close-button' | 'escape' | 'method' }>} va-dismiss - After a message has closed. `event.detail.id` is the id `show()` returned.
|
|
42
|
+
* @event-trigger va-dismiss | press Escape | open
|
|
43
|
+
*/
|
|
44
|
+
export class VaToast extends LitElement {
|
|
45
|
+
static { this.properties = {
|
|
46
|
+
duration: { type: Number },
|
|
47
|
+
accessibleLabel: { attribute: 'accessible-label' },
|
|
48
|
+
_toasts: { state: true },
|
|
49
|
+
}; }
|
|
50
|
+
constructor() {
|
|
51
|
+
super();
|
|
52
|
+
this.duration = 5000;
|
|
53
|
+
this.accessibleLabel = 'Notifications';
|
|
54
|
+
this._toasts = [];
|
|
55
|
+
this.addEventListener('command', this.#onCommand);
|
|
56
|
+
}
|
|
57
|
+
connectedCallback() {
|
|
58
|
+
super.connectedCallback();
|
|
59
|
+
checkTokensLoaded(this);
|
|
60
|
+
}
|
|
61
|
+
disconnectedCallback() {
|
|
62
|
+
super.disconnectedCallback();
|
|
63
|
+
for (const t of this._toasts)
|
|
64
|
+
clearTimeout(t.timer);
|
|
65
|
+
}
|
|
66
|
+
/** True while at least one message is showing. */
|
|
67
|
+
get hasMessages() {
|
|
68
|
+
return this._toasts.length > 0;
|
|
69
|
+
}
|
|
70
|
+
#home = null;
|
|
71
|
+
#dialogWatch = null;
|
|
72
|
+
/**
|
|
73
|
+
* A modal dialog makes the rest of the page inert, so a toast outside it could be seen but not
|
|
74
|
+
* clicked or read by screen readers. While a modal is open, move into it; move back when it closes.
|
|
75
|
+
*/
|
|
76
|
+
#followModal() {
|
|
77
|
+
const modal = document.querySelector('va-dialog[open], dialog:modal');
|
|
78
|
+
if (!modal || modal.contains(this))
|
|
79
|
+
return;
|
|
80
|
+
if (!this.#home)
|
|
81
|
+
this.#home = { parent: this.parentNode, next: this.nextSibling };
|
|
82
|
+
modal.append(this);
|
|
83
|
+
this.#dialogWatch?.disconnect();
|
|
84
|
+
this.#dialogWatch = new MutationObserver(() => {
|
|
85
|
+
if (modal.hasAttribute('open'))
|
|
86
|
+
return;
|
|
87
|
+
this.#dialogWatch?.disconnect();
|
|
88
|
+
this.#dialogWatch = null;
|
|
89
|
+
const home = this.#home;
|
|
90
|
+
this.#home = null;
|
|
91
|
+
if (home?.parent.isConnected)
|
|
92
|
+
home.parent.insertBefore(this, home.next?.parentNode === home.parent ? home.next : null);
|
|
93
|
+
});
|
|
94
|
+
this.#dialogWatch.observe(modal, { attributes: true, attributeFilter: ['open'] });
|
|
95
|
+
}
|
|
96
|
+
/** Shows a message and returns its id. `variant` defaults to `info`; errors stay until dismissed. */
|
|
97
|
+
show(message, options = {}) {
|
|
98
|
+
this.#followModal();
|
|
99
|
+
const variant = options.variant ?? 'info';
|
|
100
|
+
const duration = variant === 'error' ? 0 : (options.duration ?? this.duration);
|
|
101
|
+
const toast = { id: nextId++, message, variant, duration, remaining: duration, started: 0 };
|
|
102
|
+
this._toasts = [...this._toasts, toast];
|
|
103
|
+
this.#start(toast);
|
|
104
|
+
return toast.id;
|
|
105
|
+
}
|
|
106
|
+
/** Closes one message by id, or every message when no id is given. */
|
|
107
|
+
dismiss(id) {
|
|
108
|
+
for (const t of [...this._toasts])
|
|
109
|
+
if (id === undefined || t.id === id)
|
|
110
|
+
this.#close(t, 'method');
|
|
111
|
+
}
|
|
112
|
+
#onCommand = (event) => {
|
|
113
|
+
if (event.command !== '--show')
|
|
114
|
+
return;
|
|
115
|
+
const source = event.source;
|
|
116
|
+
const message = source?.value || source?.textContent?.trim() || '';
|
|
117
|
+
const asked = source?.getAttribute('data-toast-variant');
|
|
118
|
+
let variant;
|
|
119
|
+
if (asked && VARIANTS.includes(asked))
|
|
120
|
+
variant = asked;
|
|
121
|
+
else if (asked) {
|
|
122
|
+
devWarn('va-toast', `data-toast-variant="${asked}" is not a toast variant, so the message shows as info.`, `Use one of: ${VARIANTS.join(', ')}${asked === 'danger' ? ' (error is the toast for failures)' : ''}.`);
|
|
123
|
+
}
|
|
124
|
+
if (message)
|
|
125
|
+
this.show(message, { variant });
|
|
126
|
+
};
|
|
127
|
+
#start(toast) {
|
|
128
|
+
if (!toast.remaining)
|
|
129
|
+
return;
|
|
130
|
+
toast.started = Date.now();
|
|
131
|
+
toast.timer = setTimeout(() => this.#close(toast, 'timeout'), toast.remaining);
|
|
132
|
+
}
|
|
133
|
+
#pause(toast) {
|
|
134
|
+
if (!toast.timer)
|
|
135
|
+
return;
|
|
136
|
+
clearTimeout(toast.timer);
|
|
137
|
+
toast.timer = undefined;
|
|
138
|
+
toast.remaining = Math.max(1000, toast.remaining - (Date.now() - toast.started));
|
|
139
|
+
}
|
|
140
|
+
#resume(toast, item) {
|
|
141
|
+
// Stay paused while the pointer or focus is still on the message.
|
|
142
|
+
if (item.matches(':hover') || item.matches(':focus-within'))
|
|
143
|
+
return;
|
|
144
|
+
if (!toast.timer)
|
|
145
|
+
this.#start(toast);
|
|
146
|
+
}
|
|
147
|
+
#close(toast, reason) {
|
|
148
|
+
if (!this._toasts.includes(toast))
|
|
149
|
+
return;
|
|
150
|
+
clearTimeout(toast.timer);
|
|
151
|
+
const hadFocus = !!this.shadowRoot?.activeElement;
|
|
152
|
+
this._toasts = this._toasts.filter((t) => t !== toast);
|
|
153
|
+
this.dispatchEvent(new CustomEvent('va-dismiss', { detail: { id: toast.id, reason }, bubbles: true, composed: true }));
|
|
154
|
+
if (hadFocus) {
|
|
155
|
+
// Don't strand focus: move it to the next message, or let it go back to the page.
|
|
156
|
+
this.updateComplete.then(() => this.shadowRoot?.querySelector('.dismiss')?.focus());
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
render() {
|
|
160
|
+
return html `
|
|
161
|
+
<section part="region" class="region" aria-label=${this.accessibleLabel}>
|
|
162
|
+
<ol class="list" role="status" aria-live="polite">
|
|
163
|
+
${repeat(this._toasts, (t) => t.id, (t) => html `
|
|
164
|
+
<li
|
|
165
|
+
part="toast"
|
|
166
|
+
class="toast ${t.variant}"
|
|
167
|
+
role=${t.variant === 'error' ? 'alert' : nothing}
|
|
168
|
+
@mouseenter=${() => this.#pause(t)}
|
|
169
|
+
@mouseleave=${(e) => this.#resume(t, e.currentTarget)}
|
|
170
|
+
@focusin=${() => this.#pause(t)}
|
|
171
|
+
@focusout=${(e) => this.#resume(t, e.currentTarget)}
|
|
172
|
+
@keydown=${(e) => e.key === 'Escape' && this.#close(t, 'escape')}
|
|
173
|
+
>
|
|
174
|
+
<span class="message">${t.message}</span>
|
|
175
|
+
<button part="close-button" class="dismiss" type="button" aria-label="Dismiss" @click=${() => this.#close(t, 'close-button')}>
|
|
176
|
+
<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 6l12 12M18 6L6 18" /></svg>
|
|
177
|
+
</button>
|
|
178
|
+
</li>
|
|
179
|
+
`)}
|
|
180
|
+
</ol>
|
|
181
|
+
</section>
|
|
182
|
+
`;
|
|
183
|
+
}
|
|
184
|
+
static { this.styles = [
|
|
185
|
+
focusRing,
|
|
186
|
+
css `
|
|
187
|
+
:host {
|
|
188
|
+
position: fixed;
|
|
189
|
+
inset: auto 16px 16px auto;
|
|
190
|
+
z-index: 1000;
|
|
191
|
+
font-family: var(--va-font-family-ui);
|
|
192
|
+
}
|
|
193
|
+
.list {
|
|
194
|
+
display: flex;
|
|
195
|
+
flex-direction: column;
|
|
196
|
+
gap: var(--va-space-gap-md);
|
|
197
|
+
margin: 0;
|
|
198
|
+
padding: 0;
|
|
199
|
+
list-style: none;
|
|
200
|
+
}
|
|
201
|
+
.toast {
|
|
202
|
+
display: flex;
|
|
203
|
+
align-items: center;
|
|
204
|
+
gap: var(--va-space-inline-sm);
|
|
205
|
+
min-width: 16rem;
|
|
206
|
+
max-width: min(24rem, calc(100vw - 32px));
|
|
207
|
+
padding: var(--va-space-inline-sm) var(--va-space-inline-sm) var(--va-space-inline-sm) var(--va-space-inline-md);
|
|
208
|
+
border-radius: var(--va-toast-radius, var(--va-radius-surface));
|
|
209
|
+
background-color: var(--va-toast-bg, var(--va-color-text-default));
|
|
210
|
+
color: var(--va-toast-fg, var(--va-color-surface-default));
|
|
211
|
+
box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
|
|
212
|
+
font-size: var(--va-font-size-md);
|
|
213
|
+
}
|
|
214
|
+
.toast.success {
|
|
215
|
+
border-left: 4px solid var(--va-color-accent-default);
|
|
216
|
+
}
|
|
217
|
+
.toast.error {
|
|
218
|
+
background-color: var(--va-toast-bg, var(--va-color-danger-default));
|
|
219
|
+
color: var(--va-toast-fg, var(--va-color-text-on-danger));
|
|
220
|
+
}
|
|
221
|
+
.message {
|
|
222
|
+
flex: 1;
|
|
223
|
+
}
|
|
224
|
+
.dismiss {
|
|
225
|
+
flex: none;
|
|
226
|
+
display: grid;
|
|
227
|
+
place-items: center;
|
|
228
|
+
width: 2rem;
|
|
229
|
+
height: 2rem;
|
|
230
|
+
padding: 0;
|
|
231
|
+
border: none;
|
|
232
|
+
border-radius: var(--va-radius-control);
|
|
233
|
+
background: transparent;
|
|
234
|
+
color: inherit;
|
|
235
|
+
cursor: pointer;
|
|
236
|
+
}
|
|
237
|
+
.dismiss svg {
|
|
238
|
+
width: 1.125rem;
|
|
239
|
+
height: 1.125rem;
|
|
240
|
+
}
|
|
241
|
+
@media (max-width: 480px) {
|
|
242
|
+
:host {
|
|
243
|
+
inset: auto 16px 16px 16px;
|
|
244
|
+
}
|
|
245
|
+
.toast {
|
|
246
|
+
min-width: 0;
|
|
247
|
+
max-width: none;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
@media (forced-colors: active) {
|
|
251
|
+
.toast {
|
|
252
|
+
border: 2px solid CanvasText;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
`,
|
|
256
|
+
]; }
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=toast.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toast.js","sourceRoot":"","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AC,CAAC;8BACpB,CAAC,CAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,aAA4B,CAAC;2BAChE,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;4BACnB,CAAC,CAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,aAA4B,CAAC;2BAC9D,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC;;wCAEvD,CAAC,CAAC,OAAO;wGACuD,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,cAAc,CAAC;;;;aAI/H,CACF;;;KAGN,CAAC;IACJ,CAAC;aAEe,WAAM,GAAG;QACvB,SAAS;QACT,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAqEF;KACF,CAAC","sourcesContent":["import { LitElement, css, html, nothing, type PropertyDeclarations } from 'lit';\nimport { repeat } from 'lit/directives/repeat.js';\nimport { focusRing } from '../core/focus-ring.js';\nimport { checkTokensLoaded, devWarn } from '../core/warn.js';\n\n/** How a toast looks and how urgently it is announced. */\nexport type ToastVariant = 'info' | 'success' | 'error';\n/** Why a message closed, in the `va-dismiss` event's `detail.reason`. */\nexport type ToastDismissReason = 'timeout' | 'close-button' | 'escape' | 'method';\nconst VARIANTS: readonly ToastVariant[] = ['info', 'success', 'error'];\n\ninterface Toast {\n id: number;\n message: string;\n variant: ToastVariant;\n duration: number;\n remaining: number;\n started: number;\n timer?: ReturnType<typeof setTimeout>;\n}\n\nlet nextId = 1;\n\n/**\n * A region for short, non-blocking status messages (\"toasts\"). Place one per page and call `show()`,\n * or point a `va-button` at it with `command=\"--show\"`; the button's `value` is the message.\n *\n * @tag va-toast\n * @summary Shows short status messages, such as \"Changes saved\", without interrupting the user.\n *\n * @csspart region - The fixed area the toasts appear in.\n * @csspart toast - Each message.\n * @csspart close-button - Each message's close button.\n *\n * @cssprop {<color>} --va-toast-bg - Background colour of a message.\n * @cssprop {<color>} --va-toast-fg - Text colour of a message.\n * @cssprop {<length>} --va-toast-radius - Corner radius of a message.\n *\n * @cssprop-applies --va-toast-bg toast:open background-color\n * @cssprop-applies --va-toast-fg toast:open color\n * @cssprop-applies --va-toast-radius toast:open border-top-left-radius\n *\n * @keyboard-items [part~=\"close-button\"]\n * @keyboard open: Escape | closes | Dismisses the message that has focus.\n *\n * @a11y-role status\n * @a11y-name The `accessible-label` attribute, \"Notifications\" by default (not shown).\n * @a11y-note Info and success messages are announced politely; errors use `role=\"alert\"` and are announced at once.\n * @a11y-note Messages pause their timer while hovered or focused, and errors stay until dismissed (WCAG 2.2.1).\n * @a11y-note While a modal dialog is open, the element moves itself into that dialog (a light-DOM move, so its messages stay clickable and announced) and back to its place when the dialog closes. Don't render it from a framework that would fight the move; place it once, outside framework-managed DOM.\n *\n * @guidance-use To confirm that something happened, such as \"Changes saved\" or \"Link copied\", or to report a background failure.\n * @guidance-avoid For anything the user must act on or read in full, or for form validation errors.\n * @guidance-instead `va-dialog` for decisions; `va-text-field`'s `error` for field errors.\n *\n * @command --show | Shows the sending button's `value` as a message. Its `data-toast-variant` sets the look: `info` (default), `success` or `error`.\n * @command-data --show | toast-variant | info success error\n * @fires {CustomEvent<{ id: number, reason: 'timeout' | 'close-button' | 'escape' | 'method' }>} va-dismiss - After a message has closed. `event.detail.id` is the id `show()` returned.\n * @event-trigger va-dismiss | press Escape | open\n */\nexport class VaToast extends LitElement {\n static override properties: PropertyDeclarations = {\n duration: { type: Number },\n accessibleLabel: { attribute: 'accessible-label' },\n _toasts: { state: true },\n };\n\n /** Milliseconds before an info or success message closes by itself. Errors stay until dismissed. */\n declare duration: number;\n /** Accessible name of the region. Not shown. */\n declare accessibleLabel: string;\n\n declare private _toasts: Toast[];\n\n constructor() {\n super();\n this.duration = 5000;\n this.accessibleLabel = 'Notifications';\n this._toasts = [];\n this.addEventListener('command', this.#onCommand as EventListener);\n }\n\n override connectedCallback(): void {\n super.connectedCallback();\n checkTokensLoaded(this);\n }\n\n override disconnectedCallback(): void {\n super.disconnectedCallback();\n for (const t of this._toasts) clearTimeout(t.timer);\n }\n\n /** True while at least one message is showing. */\n get hasMessages(): boolean {\n return this._toasts.length > 0;\n }\n\n #home: { parent: Node; next: Node | null } | null = null;\n #dialogWatch: MutationObserver | null = null;\n\n /**\n * A modal dialog makes the rest of the page inert, so a toast outside it could be seen but not\n * clicked or read by screen readers. While a modal is open, move into it; move back when it closes.\n */\n #followModal(): void {\n const modal = document.querySelector<HTMLElement>('va-dialog[open], dialog:modal');\n if (!modal || modal.contains(this)) return;\n if (!this.#home) this.#home = { parent: this.parentNode!, next: this.nextSibling };\n modal.append(this);\n this.#dialogWatch?.disconnect();\n this.#dialogWatch = new MutationObserver(() => {\n if (modal.hasAttribute('open')) return;\n this.#dialogWatch?.disconnect();\n this.#dialogWatch = null;\n const home = this.#home;\n this.#home = null;\n if (home?.parent.isConnected) home.parent.insertBefore(this, home.next?.parentNode === home.parent ? home.next : null);\n });\n this.#dialogWatch.observe(modal, { attributes: true, attributeFilter: ['open'] });\n }\n\n /** Shows a message and returns its id. `variant` defaults to `info`; errors stay until dismissed. */\n show(message: string, options: { variant?: 'info' | 'success' | 'error'; duration?: number } = {}): number {\n this.#followModal();\n const variant = options.variant ?? 'info';\n const duration = variant === 'error' ? 0 : (options.duration ?? this.duration);\n const toast: Toast = { id: nextId++, message, variant, duration, remaining: duration, started: 0 };\n this._toasts = [...this._toasts, toast];\n this.#start(toast);\n return toast.id;\n }\n\n /** Closes one message by id, or every message when no id is given. */\n dismiss(id?: number): void {\n for (const t of [...this._toasts]) if (id === undefined || t.id === id) this.#close(t, 'method');\n }\n\n #onCommand = (event: Event & { command?: string; source?: Element | null }): void => {\n if (event.command !== '--show') return;\n const source = event.source as (Element & { value?: string }) | null;\n const message = source?.value || source?.textContent?.trim() || '';\n const asked = source?.getAttribute('data-toast-variant');\n let variant: ToastVariant | undefined;\n if (asked && VARIANTS.includes(asked as ToastVariant)) variant = asked as ToastVariant;\n else if (asked) {\n devWarn('va-toast', `data-toast-variant=\"${asked}\" is not a toast variant, so the message shows as info.`, `Use one of: ${VARIANTS.join(', ')}${asked === 'danger' ? ' (error is the toast for failures)' : ''}.`);\n }\n if (message) this.show(message, { variant });\n };\n\n #start(toast: Toast): void {\n if (!toast.remaining) return;\n toast.started = Date.now();\n toast.timer = setTimeout(() => this.#close(toast, 'timeout'), toast.remaining);\n }\n\n #pause(toast: Toast): void {\n if (!toast.timer) return;\n clearTimeout(toast.timer);\n toast.timer = undefined;\n toast.remaining = Math.max(1000, toast.remaining - (Date.now() - toast.started));\n }\n\n #resume(toast: Toast, item: HTMLElement): void {\n // Stay paused while the pointer or focus is still on the message.\n if (item.matches(':hover') || item.matches(':focus-within')) return;\n if (!toast.timer) this.#start(toast);\n }\n\n #close(toast: Toast, reason: ToastDismissReason): void {\n if (!this._toasts.includes(toast)) return;\n clearTimeout(toast.timer);\n const hadFocus = !!this.shadowRoot?.activeElement;\n this._toasts = this._toasts.filter((t) => t !== toast);\n this.dispatchEvent(new CustomEvent('va-dismiss', { detail: { id: toast.id, reason }, bubbles: true, composed: true }));\n if (hadFocus) {\n // Don't strand focus: move it to the next message, or let it go back to the page.\n this.updateComplete.then(() => this.shadowRoot?.querySelector<HTMLElement>('.dismiss')?.focus());\n }\n }\n\n override render() {\n return html`\n <section part=\"region\" class=\"region\" aria-label=${this.accessibleLabel}>\n <ol class=\"list\" role=\"status\" aria-live=\"polite\">\n ${repeat(\n this._toasts,\n (t) => t.id,\n (t) => html`\n <li\n part=\"toast\"\n class=\"toast ${t.variant}\"\n role=${t.variant === 'error' ? 'alert' : nothing}\n @mouseenter=${() => this.#pause(t)}\n @mouseleave=${(e: Event) => this.#resume(t, e.currentTarget as HTMLElement)}\n @focusin=${() => this.#pause(t)}\n @focusout=${(e: Event) => this.#resume(t, e.currentTarget as HTMLElement)}\n @keydown=${(e: KeyboardEvent) => e.key === 'Escape' && this.#close(t, 'escape')}\n >\n <span class=\"message\">${t.message}</span>\n <button part=\"close-button\" class=\"dismiss\" type=\"button\" aria-label=\"Dismiss\" @click=${() => this.#close(t, 'close-button')}>\n <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><path d=\"M6 6l12 12M18 6L6 18\" /></svg>\n </button>\n </li>\n `\n )}\n </ol>\n </section>\n `;\n }\n\n static override styles = [\n focusRing,\n css`\n :host {\n position: fixed;\n inset: auto 16px 16px auto;\n z-index: 1000;\n font-family: var(--va-font-family-ui);\n }\n .list {\n display: flex;\n flex-direction: column;\n gap: var(--va-space-gap-md);\n margin: 0;\n padding: 0;\n list-style: none;\n }\n .toast {\n display: flex;\n align-items: center;\n gap: var(--va-space-inline-sm);\n min-width: 16rem;\n max-width: min(24rem, calc(100vw - 32px));\n padding: var(--va-space-inline-sm) var(--va-space-inline-sm) var(--va-space-inline-sm) var(--va-space-inline-md);\n border-radius: var(--va-toast-radius, var(--va-radius-surface));\n background-color: var(--va-toast-bg, var(--va-color-text-default));\n color: var(--va-toast-fg, var(--va-color-surface-default));\n box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);\n font-size: var(--va-font-size-md);\n }\n .toast.success {\n border-left: 4px solid var(--va-color-accent-default);\n }\n .toast.error {\n background-color: var(--va-toast-bg, var(--va-color-danger-default));\n color: var(--va-toast-fg, var(--va-color-text-on-danger));\n }\n .message {\n flex: 1;\n }\n .dismiss {\n flex: none;\n display: grid;\n place-items: center;\n width: 2rem;\n height: 2rem;\n padding: 0;\n border: none;\n border-radius: var(--va-radius-control);\n background: transparent;\n color: inherit;\n cursor: pointer;\n }\n .dismiss svg {\n width: 1.125rem;\n height: 1.125rem;\n }\n @media (max-width: 480px) {\n :host {\n inset: auto 16px 16px 16px;\n }\n .toast {\n min-width: 0;\n max-width: none;\n }\n }\n @media (forced-colors: active) {\n .toast {\n border: 2px solid CanvasText;\n }\n }\n `,\n ];\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'va-toast': VaToast;\n }\n}\n"]}
|
package/dist/prod/all.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Generated by build.js: registers every Vastu component.
|
|
2
|
+
export * from './button/index.js';
|
|
3
|
+
export * from './checkbox/index.js';
|
|
4
|
+
export * from './dialog/index.js';
|
|
5
|
+
export * from './menu/index.js';
|
|
6
|
+
export * from './switch/index.js';
|
|
7
|
+
export * from './tabs/index.js';
|
|
8
|
+
export * from './text-field/index.js';
|
|
9
|
+
export * from './toast/index.js';
|
|
10
|
+
//# sourceMappingURL=all.js.map
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { LitElement, css, html, nothing } from 'lit';
|
|
2
|
+
import { focusRing } from '../core/focus-ring.js';
|
|
3
|
+
import { checkTokensLoaded, devWarn } from '../core/warn.js';
|
|
4
|
+
/**
|
|
5
|
+
* A button that starts an action or submits a form.
|
|
6
|
+
*
|
|
7
|
+
* @tag va-button
|
|
8
|
+
* @summary Starts an action, or submits or resets the form it is in.
|
|
9
|
+
*
|
|
10
|
+
* @slot - The button's text. This is its accessible name.
|
|
11
|
+
* @slot start - An icon before the text. Mark decorative icons `aria-hidden="true"`.
|
|
12
|
+
* @slot end - An icon after the text.
|
|
13
|
+
*
|
|
14
|
+
* @csspart control - The native button inside.
|
|
15
|
+
* @csspart label - The wrapper around the text.
|
|
16
|
+
*
|
|
17
|
+
* @cssprop {<color>} --va-button-bg - Background colour.
|
|
18
|
+
* @cssprop {<color>} --va-button-bg-hover - Background colour on hover.
|
|
19
|
+
* @cssprop {<color>} --va-button-fg - Text and icon colour.
|
|
20
|
+
* @cssprop {<color>} --va-button-border - Border colour.
|
|
21
|
+
* @cssprop {<length>} --va-button-radius - Corner radius.
|
|
22
|
+
* @cssprop {<length>} --va-button-height - Minimum height.
|
|
23
|
+
* @cssprop {<length>} --va-button-padding-inline - Space before and after the text.
|
|
24
|
+
* @cssprop {<length>} --va-button-font-size - Text size.
|
|
25
|
+
*
|
|
26
|
+
* @cssprop-applies --va-button-bg control background-color
|
|
27
|
+
* @cssprop-applies --va-button-bg-hover control:hover background-color
|
|
28
|
+
* @cssprop-applies --va-button-fg control color
|
|
29
|
+
* @cssprop-applies --va-button-border control border-top-color
|
|
30
|
+
* @cssprop-applies --va-button-radius control border-top-left-radius
|
|
31
|
+
* @cssprop-applies --va-button-height control min-height
|
|
32
|
+
* @cssprop-applies --va-button-padding-inline control padding-left
|
|
33
|
+
* @cssprop-applies --va-button-font-size control font-size
|
|
34
|
+
*
|
|
35
|
+
* @keyboard Enter | activates | Activates the button.
|
|
36
|
+
* @keyboard Space | activates | Activates the button.
|
|
37
|
+
* @keyboard Tab | focus-next | Moves focus to the next control. A disabled button is skipped.
|
|
38
|
+
*
|
|
39
|
+
* @a11y-role button
|
|
40
|
+
* @a11y-name-from text, accessible-label
|
|
41
|
+
* @replaces button
|
|
42
|
+
* @a11y-name The slotted text. A button with only an icon must have `accessible-label`.
|
|
43
|
+
* @a11y-note `loading` keeps the button focusable, sets `aria-disabled` and `aria-busy`, and adds "Loading" to its name.
|
|
44
|
+
*
|
|
45
|
+
* @guidance-use For an action on the current page (Save, Delete, Open dialog) or to submit a form.
|
|
46
|
+
* @guidance-avoid For navigation to another page or URL.
|
|
47
|
+
* @guidance-instead A link (`<a href>`) for navigation.
|
|
48
|
+
*
|
|
49
|
+
* @fires click - The native click event. Not fired while `disabled` or `loading`.
|
|
50
|
+
* @event-trigger click | click
|
|
51
|
+
*/
|
|
52
|
+
export class VaButton extends LitElement {
|
|
53
|
+
static { this.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }; }
|
|
54
|
+
/** @internal */
|
|
55
|
+
static { this.formAssociated = true; }
|
|
56
|
+
static { this.properties = {
|
|
57
|
+
variant: { reflect: true },
|
|
58
|
+
size: { reflect: true },
|
|
59
|
+
type: { reflect: true },
|
|
60
|
+
disabled: { type: Boolean, reflect: true },
|
|
61
|
+
loading: { type: Boolean, reflect: true },
|
|
62
|
+
name: {},
|
|
63
|
+
value: {},
|
|
64
|
+
accessibleLabel: { attribute: 'accessible-label' },
|
|
65
|
+
commandfor: {},
|
|
66
|
+
command: {},
|
|
67
|
+
}; }
|
|
68
|
+
#internals;
|
|
69
|
+
constructor() {
|
|
70
|
+
super();
|
|
71
|
+
this.variant = 'primary';
|
|
72
|
+
this.size = 'md';
|
|
73
|
+
this.type = 'button';
|
|
74
|
+
this.disabled = false;
|
|
75
|
+
this.loading = false;
|
|
76
|
+
this.name = '';
|
|
77
|
+
this.value = '';
|
|
78
|
+
this.accessibleLabel = '';
|
|
79
|
+
this.commandfor = '';
|
|
80
|
+
this.command = '';
|
|
81
|
+
this.#internals = this.attachInternals();
|
|
82
|
+
this.addEventListener('click', this.#onClick);
|
|
83
|
+
}
|
|
84
|
+
/** The form this button belongs to, if any. */
|
|
85
|
+
get form() {
|
|
86
|
+
return this.#internals.form;
|
|
87
|
+
}
|
|
88
|
+
connectedCallback() {
|
|
89
|
+
super.connectedCallback();
|
|
90
|
+
checkTokensLoaded(this);
|
|
91
|
+
}
|
|
92
|
+
#onClick = (event) => {
|
|
93
|
+
if (this.disabled || this.loading) {
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
event.stopImmediatePropagation();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (this.commandfor && this.command) {
|
|
99
|
+
// Same shape as the native CommandEvent, so components handle both.
|
|
100
|
+
const target = this.getRootNode().getElementById(this.commandfor);
|
|
101
|
+
if (target)
|
|
102
|
+
target.dispatchEvent(Object.assign(new Event('command', { cancelable: true }), { command: this.command, source: this }));
|
|
103
|
+
else
|
|
104
|
+
devWarn('va-button', `commandfor="${this.commandfor}" doesn't match any element id.`, 'Set commandfor to the id of the element to control, in the same document or shadow root.');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const form = this.#internals.form;
|
|
108
|
+
if (!form)
|
|
109
|
+
return;
|
|
110
|
+
if (this.type === 'submit') {
|
|
111
|
+
// requestSubmit() needs a real submit button as the submitter, so name=value is sent.
|
|
112
|
+
const submitter = document.createElement('button');
|
|
113
|
+
submitter.type = 'submit';
|
|
114
|
+
submitter.hidden = true;
|
|
115
|
+
if (this.name) {
|
|
116
|
+
submitter.name = this.name;
|
|
117
|
+
submitter.value = this.value;
|
|
118
|
+
}
|
|
119
|
+
form.append(submitter);
|
|
120
|
+
form.requestSubmit(submitter);
|
|
121
|
+
submitter.remove();
|
|
122
|
+
}
|
|
123
|
+
else if (this.type === 'reset') {
|
|
124
|
+
form.reset();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
firstUpdated() {
|
|
128
|
+
// Wait a frame so children parsed after the upgrade are in place before checking the name.
|
|
129
|
+
requestAnimationFrame(() => this.#checkName());
|
|
130
|
+
}
|
|
131
|
+
#checkName = () => {
|
|
132
|
+
const text = this.textContent?.trim() ?? '';
|
|
133
|
+
if (!text && !this.accessibleLabel) {
|
|
134
|
+
devWarn('va-button', 'This button has no accessible name: it has no text and no accessible-label.', 'Put text inside it, or add accessible-label="…" that says what it does (e.g. accessible-label="Close").');
|
|
135
|
+
}
|
|
136
|
+
else if (text && this.accessibleLabel && !this.accessibleLabel.toLowerCase().includes(text.toLowerCase())) {
|
|
137
|
+
devWarn('va-button', `accessible-label="${this.accessibleLabel}" replaces the visible text "${text}" for screen readers, so voice users can't say what they see (WCAG 2.5.3).`, 'Remove accessible-label, or make it start with the visible text.');
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
/** While loading, the name gains ", loading"; otherwise only accessible-label overrides the text. */
|
|
141
|
+
#ariaLabel() {
|
|
142
|
+
const name = this.accessibleLabel || this.textContent?.trim() || '';
|
|
143
|
+
if (this.loading)
|
|
144
|
+
return name ? `${name}, loading` : 'Loading';
|
|
145
|
+
return this.accessibleLabel || undefined;
|
|
146
|
+
}
|
|
147
|
+
render() {
|
|
148
|
+
return html `
|
|
149
|
+
<button
|
|
150
|
+
part="control"
|
|
151
|
+
class="control"
|
|
152
|
+
type="button"
|
|
153
|
+
?disabled=${this.disabled}
|
|
154
|
+
aria-disabled=${this.loading ? 'true' : nothing}
|
|
155
|
+
aria-busy=${this.loading ? 'true' : nothing}
|
|
156
|
+
aria-label=${this.#ariaLabel() ?? nothing}
|
|
157
|
+
>
|
|
158
|
+
<slot name="start"></slot>
|
|
159
|
+
<span part="label" class="label"><slot @slotchange=${this.#checkName}></slot></span>
|
|
160
|
+
${this.loading ? html `<span class="spinner" aria-hidden="true"></span>` : nothing}
|
|
161
|
+
<slot name="end"></slot>
|
|
162
|
+
</button>
|
|
163
|
+
`;
|
|
164
|
+
}
|
|
165
|
+
static { this.styles = [
|
|
166
|
+
focusRing,
|
|
167
|
+
css `
|
|
168
|
+
:host {
|
|
169
|
+
--_bg: var(--va-button-bg, var(--va-color-accent-default));
|
|
170
|
+
--_bg-hover: var(--va-button-bg-hover, var(--va-color-accent-hover));
|
|
171
|
+
--_fg: var(--va-button-fg, var(--va-color-text-on-accent));
|
|
172
|
+
--_border: var(--va-button-border, transparent);
|
|
173
|
+
--_height: var(--va-button-height, var(--va-size-control-md));
|
|
174
|
+
--_padding: var(--va-button-padding-inline, var(--va-space-inline-lg));
|
|
175
|
+
--_font-size: var(--va-button-font-size, var(--va-font-size-md));
|
|
176
|
+
display: inline-block;
|
|
177
|
+
vertical-align: middle;
|
|
178
|
+
}
|
|
179
|
+
:host([hidden]) {
|
|
180
|
+
display: none;
|
|
181
|
+
}
|
|
182
|
+
:host([variant='secondary']) {
|
|
183
|
+
--_bg: var(--va-button-bg, transparent);
|
|
184
|
+
--_bg-hover: var(--va-button-bg-hover, var(--va-color-surface-subtle));
|
|
185
|
+
--_fg: var(--va-button-fg, var(--va-color-text-default));
|
|
186
|
+
--_border: var(--va-button-border, var(--va-color-border-control));
|
|
187
|
+
}
|
|
188
|
+
:host([variant='ghost']) {
|
|
189
|
+
--_bg: var(--va-button-bg, transparent);
|
|
190
|
+
--_bg-hover: var(--va-button-bg-hover, var(--va-color-surface-subtle));
|
|
191
|
+
--_fg: var(--va-button-fg, var(--va-color-accent-default));
|
|
192
|
+
}
|
|
193
|
+
:host([variant='danger']) {
|
|
194
|
+
--_bg: var(--va-button-bg, var(--va-color-danger-default));
|
|
195
|
+
--_bg-hover: var(--va-button-bg-hover, var(--va-color-danger-hover));
|
|
196
|
+
--_fg: var(--va-button-fg, var(--va-color-text-on-danger));
|
|
197
|
+
}
|
|
198
|
+
:host([size='sm']) {
|
|
199
|
+
--_height: var(--va-button-height, var(--va-size-control-sm));
|
|
200
|
+
--_padding: var(--va-button-padding-inline, var(--va-space-inline-md));
|
|
201
|
+
--_font-size: var(--va-button-font-size, var(--va-font-size-sm));
|
|
202
|
+
}
|
|
203
|
+
:host([size='lg']) {
|
|
204
|
+
--_height: var(--va-button-height, var(--va-size-control-lg));
|
|
205
|
+
--_font-size: var(--va-button-font-size, var(--va-font-size-lg));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.control {
|
|
209
|
+
box-sizing: border-box;
|
|
210
|
+
display: inline-flex;
|
|
211
|
+
align-items: center;
|
|
212
|
+
justify-content: center;
|
|
213
|
+
gap: var(--va-space-gap-md);
|
|
214
|
+
width: 100%;
|
|
215
|
+
min-height: var(--_height);
|
|
216
|
+
margin: 0;
|
|
217
|
+
padding: 0 var(--_padding);
|
|
218
|
+
border: 1px solid var(--_border);
|
|
219
|
+
border-radius: var(--va-button-radius, var(--va-radius-control));
|
|
220
|
+
background-color: var(--_bg);
|
|
221
|
+
color: var(--_fg);
|
|
222
|
+
font-family: var(--va-font-family-ui);
|
|
223
|
+
font-size: var(--_font-size);
|
|
224
|
+
font-weight: var(--va-font-weight-medium);
|
|
225
|
+
line-height: 1.2;
|
|
226
|
+
cursor: pointer;
|
|
227
|
+
transition: background-color var(--va-motion-duration-fast) ease-out;
|
|
228
|
+
-webkit-tap-highlight-color: transparent;
|
|
229
|
+
}
|
|
230
|
+
.control:hover:not(:disabled):not([aria-disabled='true']) {
|
|
231
|
+
background-color: var(--_bg-hover);
|
|
232
|
+
}
|
|
233
|
+
.control:disabled {
|
|
234
|
+
opacity: 0.5;
|
|
235
|
+
cursor: not-allowed;
|
|
236
|
+
}
|
|
237
|
+
.control[aria-disabled='true'] {
|
|
238
|
+
cursor: progress;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
::slotted(svg),
|
|
242
|
+
::slotted([slot='start']),
|
|
243
|
+
::slotted([slot='end']) {
|
|
244
|
+
flex: none;
|
|
245
|
+
width: 1.25em;
|
|
246
|
+
height: 1.25em;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.spinner {
|
|
250
|
+
flex: none;
|
|
251
|
+
width: 1em;
|
|
252
|
+
height: 1em;
|
|
253
|
+
border: 2px solid currentColor;
|
|
254
|
+
border-right-color: transparent;
|
|
255
|
+
border-radius: 50%;
|
|
256
|
+
animation: spin 0.8s linear infinite;
|
|
257
|
+
}
|
|
258
|
+
@keyframes spin {
|
|
259
|
+
to {
|
|
260
|
+
transform: rotate(1turn);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
@media (prefers-reduced-motion: reduce) {
|
|
265
|
+
.control {
|
|
266
|
+
transition: none;
|
|
267
|
+
}
|
|
268
|
+
.spinner {
|
|
269
|
+
animation-duration: 2.4s;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
@media (forced-colors: active) {
|
|
274
|
+
.control {
|
|
275
|
+
border-color: ButtonText;
|
|
276
|
+
}
|
|
277
|
+
:host(:not([variant])) .control,
|
|
278
|
+
:host([variant='primary']) .control,
|
|
279
|
+
:host([variant='danger']) .control {
|
|
280
|
+
background-color: ButtonText;
|
|
281
|
+
color: ButtonFace;
|
|
282
|
+
forced-color-adjust: none;
|
|
283
|
+
}
|
|
284
|
+
.control:disabled {
|
|
285
|
+
border-color: GrayText;
|
|
286
|
+
color: GrayText;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
`,
|
|
290
|
+
]; }
|
|
291
|
+
}
|
|
292
|
+
//# sourceMappingURL=button.js.map
|