@firstprateek/vastu 0.0.0-stage → 1.1.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 +52 -2
- package/custom-elements.json +3737 -0
- package/dist/dev/all.d.ts +12 -0
- package/dist/dev/all.d.ts.map +1 -0
- package/dist/dev/all.js +13 -0
- package/dist/dev/all.js.map +1 -0
- package/dist/dev/bar-chart/bar-chart.d.ts +63 -0
- package/dist/dev/bar-chart/bar-chart.d.ts.map +1 -0
- package/dist/dev/bar-chart/bar-chart.js +316 -0
- package/dist/dev/bar-chart/bar-chart.js.map +1 -0
- package/dist/dev/bar-chart/index.d.ts +3 -0
- package/dist/dev/bar-chart/index.d.ts.map +1 -0
- package/dist/dev/bar-chart/index.js +5 -0
- package/dist/dev/bar-chart/index.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/cssom.d.ts +15 -0
- package/dist/dev/core/cssom.d.ts.map +1 -0
- package/dist/dev/core/cssom.js +30 -0
- package/dist/dev/core/cssom.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/navbar/index.d.ts +3 -0
- package/dist/dev/navbar/index.d.ts.map +1 -0
- package/dist/dev/navbar/index.js +5 -0
- package/dist/dev/navbar/index.js.map +1 -0
- package/dist/dev/navbar/navbar.d.ts +69 -0
- package/dist/dev/navbar/navbar.d.ts.map +1 -0
- package/dist/dev/navbar/navbar.js +353 -0
- package/dist/dev/navbar/navbar.js.map +1 -0
- package/dist/dev/poll/index.d.ts +3 -0
- package/dist/dev/poll/index.d.ts.map +1 -0
- package/dist/dev/poll/index.js +5 -0
- package/dist/dev/poll/index.js.map +1 -0
- package/dist/dev/poll/poll.d.ts +67 -0
- package/dist/dev/poll/poll.d.ts.map +1 -0
- package/dist/dev/poll/poll.js +301 -0
- package/dist/dev/poll/poll.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 +13 -0
- package/dist/prod/bar-chart/bar-chart.js +316 -0
- package/dist/prod/bar-chart/index.js +5 -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/cssom.js +30 -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/navbar/index.js +5 -0
- package/dist/prod/navbar/navbar.js +353 -0
- package/dist/prod/poll/index.js +5 -0
- package/dist/prod/poll/poll.js +301 -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 +110 -3
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { LitElement, css, html } from 'lit';
|
|
2
|
+
import { cssom } from '../core/cssom.js';
|
|
3
|
+
import { focusRing } from '../core/focus-ring.js';
|
|
4
|
+
import { checkTokensLoaded, devWarn } from '../core/warn.js';
|
|
5
|
+
/**
|
|
6
|
+
* A one-question poll. The user votes with one click, and the poll shows the results.
|
|
7
|
+
*
|
|
8
|
+
* @tag va-poll
|
|
9
|
+
* @summary A poll: asks one question with a few answers. Visitors vote with one click and see everyone's results.
|
|
10
|
+
*
|
|
11
|
+
* @slot - The answers, as `<button value="…" data-votes="N">` elements. `data-votes` is the number of votes so far.
|
|
12
|
+
* @slot help-text - Optional text under the poll, such as when it closes. Overrides the `help-text` attribute.
|
|
13
|
+
*
|
|
14
|
+
* @csspart label - The question.
|
|
15
|
+
* @csspart option - The row around each answer.
|
|
16
|
+
* @csspart bar - The bar behind each answer that shows its share of the votes.
|
|
17
|
+
* @csspart result - The share of the votes, shown after voting.
|
|
18
|
+
* @csspart help-text - The help text and the vote count.
|
|
19
|
+
*
|
|
20
|
+
* @cssprop {<color>} --va-poll-border - Border colour of each answer's row.
|
|
21
|
+
* @cssprop {<length>} --va-poll-radius - Corner radius of each answer's row.
|
|
22
|
+
* @cssprop {<color>} --va-poll-bar-bg - Colour of the result bars.
|
|
23
|
+
*
|
|
24
|
+
* @cssprop-applies --va-poll-border option border-top-color
|
|
25
|
+
* @cssprop-applies --va-poll-radius option border-top-left-radius
|
|
26
|
+
* @cssprop-applies --va-poll-bar-bg bar background-color
|
|
27
|
+
*
|
|
28
|
+
* @keyboard-items button
|
|
29
|
+
* @keyboard items: Enter | activates | Votes for the focused answer.
|
|
30
|
+
* @keyboard items: Space | activates | Votes for the focused answer.
|
|
31
|
+
*
|
|
32
|
+
* @a11y-role group
|
|
33
|
+
* @a11y-name-from label
|
|
34
|
+
* @a11y-name The `label`, which is the question shown above the answers.
|
|
35
|
+
* @a11y-note After a vote the answers stay focusable but get `aria-disabled="true"` and an `aria-description` with their share of the votes. A polite live region announces that the vote was counted.
|
|
36
|
+
*
|
|
37
|
+
* @guidance-use For a quick, one-click opinion poll on a page, with two to six answers.
|
|
38
|
+
* @guidance-avoid For choices that are submitted with a form, for surveys with several questions, or where users need to review their answer before sending it.
|
|
39
|
+
* @guidance-instead A radio group in a form, with a submit `va-button`.
|
|
40
|
+
*
|
|
41
|
+
* @fires {CustomEvent<{ value: string, item: HTMLElement }>} va-select - When the user votes. `event.detail.value` is the answer's `value`, or its text when it has none; `event.detail.item` is the answer. Send the vote to your server here.
|
|
42
|
+
* @event-trigger va-select | click-item 2
|
|
43
|
+
*/
|
|
44
|
+
export class VaPoll extends LitElement {
|
|
45
|
+
static { this.shadowRootOptions = { ...LitElement.shadowRootOptions, slotAssignment: 'manual' }; }
|
|
46
|
+
static { this.properties = {
|
|
47
|
+
label: {},
|
|
48
|
+
value: {},
|
|
49
|
+
showResults: { type: Boolean, reflect: true, attribute: 'show-results' },
|
|
50
|
+
helpText: { attribute: 'help-text' },
|
|
51
|
+
_revision: { state: true },
|
|
52
|
+
}; }
|
|
53
|
+
#announcement = '';
|
|
54
|
+
#observer = new MutationObserver(() => this._revision++);
|
|
55
|
+
constructor() {
|
|
56
|
+
super();
|
|
57
|
+
this.label = '';
|
|
58
|
+
this.value = '';
|
|
59
|
+
this.showResults = false;
|
|
60
|
+
this.helpText = '';
|
|
61
|
+
this._revision = 0;
|
|
62
|
+
this.addEventListener('click', this.#onClick);
|
|
63
|
+
}
|
|
64
|
+
connectedCallback() {
|
|
65
|
+
super.connectedCallback();
|
|
66
|
+
checkTokensLoaded(this);
|
|
67
|
+
this.#observer.observe(this, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['data-votes', 'value', 'slot'] });
|
|
68
|
+
// Children may have changed while the poll was detached and unobserved.
|
|
69
|
+
if (this.hasUpdated)
|
|
70
|
+
this._revision++;
|
|
71
|
+
}
|
|
72
|
+
disconnectedCallback() {
|
|
73
|
+
super.disconnectedCallback();
|
|
74
|
+
this.#observer.disconnect();
|
|
75
|
+
}
|
|
76
|
+
get #items() {
|
|
77
|
+
return [...this.children].filter((c) => !c.slot);
|
|
78
|
+
}
|
|
79
|
+
get #showing() {
|
|
80
|
+
return this.showResults || this.value !== '';
|
|
81
|
+
}
|
|
82
|
+
#valueOf(item) {
|
|
83
|
+
return item.value || item.textContent?.trim() || '';
|
|
84
|
+
}
|
|
85
|
+
#votesOf(item) {
|
|
86
|
+
const votes = Number(item.dataset.votes ?? 0);
|
|
87
|
+
return Number.isFinite(votes) && votes > 0 ? votes : 0;
|
|
88
|
+
}
|
|
89
|
+
firstUpdated() {
|
|
90
|
+
if (!this.label)
|
|
91
|
+
devWarn('va-poll', 'This poll has no label, so its question is missing.', 'Add label="…" with the question (e.g. label="Tabs or spaces?").');
|
|
92
|
+
}
|
|
93
|
+
updated(changed) {
|
|
94
|
+
const items = this.#items;
|
|
95
|
+
const slots = [...this.shadowRoot.querySelectorAll('slot:not([name])')];
|
|
96
|
+
items.forEach((item, i) => slots[i]?.assign(item));
|
|
97
|
+
this.shadowRoot.querySelector('slot[name="help-text"]').assign(...this.querySelectorAll(':scope > [slot="help-text"]'));
|
|
98
|
+
const showing = this.#showing;
|
|
99
|
+
const total = items.reduce((sum, i) => sum + this.#votesOf(i), 0);
|
|
100
|
+
for (const item of items) {
|
|
101
|
+
if (item.localName === 'button')
|
|
102
|
+
item.type = 'button';
|
|
103
|
+
if (showing) {
|
|
104
|
+
// The share is shadow text after the button, which aria-describedby can't reach: describe it
|
|
105
|
+
// on the button itself, so tabbing through the answers reads the results.
|
|
106
|
+
const votes = this.#votesOf(item);
|
|
107
|
+
const share = total ? Math.round((votes / total) * 100) : 0;
|
|
108
|
+
const chosen = this.value !== '' && this.#valueOf(item) === this.value;
|
|
109
|
+
item.setAttribute('aria-disabled', 'true');
|
|
110
|
+
item.setAttribute('aria-description', `${share}%, ${votes} ${votes === 1 ? 'vote' : 'votes'}${chosen ? ', your vote' : ''}`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
item.removeAttribute('aria-disabled');
|
|
114
|
+
item.removeAttribute('aria-description');
|
|
115
|
+
}
|
|
116
|
+
if (item.dataset.votes !== undefined && !(Number(item.dataset.votes) >= 0)) {
|
|
117
|
+
devWarn('va-poll', `data-votes="${item.dataset.votes}" on "${item.textContent?.trim()}" is not a number of votes.`, 'Set data-votes to a whole number of 0 or more (e.g. data-votes="12").');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (changed.has('_revision') && items.some((c) => c.localName !== 'button')) {
|
|
121
|
+
devWarn('va-poll', 'An answer is not a <button>, so it may not be keyboard accessible.', 'Use <button value="…" data-votes="N">Answer</button> for each answer.');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
#onClick = (event) => {
|
|
125
|
+
const item = this.#items.find((i) => event.composedPath().includes(i));
|
|
126
|
+
if (!item || this.#showing)
|
|
127
|
+
return;
|
|
128
|
+
// Count the vote here, so the results include it before the server answers.
|
|
129
|
+
item.dataset.votes = String(this.#votesOf(item) + 1);
|
|
130
|
+
this.value = this.#valueOf(item);
|
|
131
|
+
const total = this.#items.reduce((sum, i) => sum + this.#votesOf(i), 0);
|
|
132
|
+
this.#announcement = `Vote counted. ${total} ${total === 1 ? 'vote' : 'votes'} in total.`;
|
|
133
|
+
this.dispatchEvent(new CustomEvent('va-select', { detail: { value: this.value, item }, bubbles: true, composed: true }));
|
|
134
|
+
};
|
|
135
|
+
render() {
|
|
136
|
+
const items = this.#items;
|
|
137
|
+
const showing = this.#showing;
|
|
138
|
+
const total = items.reduce((sum, i) => sum + this.#votesOf(i), 0);
|
|
139
|
+
return html `
|
|
140
|
+
<div class=${showing ? 'poll showing' : 'poll'} role="group" aria-labelledby="label">
|
|
141
|
+
<div part="label" id="label" class="label">${this.label}</div>
|
|
142
|
+
<ul class="options" role="list">
|
|
143
|
+
${items.map((item) => {
|
|
144
|
+
const votes = this.#votesOf(item);
|
|
145
|
+
const share = total ? Math.round((votes / total) * 100) : 0;
|
|
146
|
+
const chosen = showing && this.value !== '' && this.#valueOf(item) === this.value;
|
|
147
|
+
return html `
|
|
148
|
+
<li part="option" class=${chosen ? 'option chosen' : 'option'}>
|
|
149
|
+
<span part="bar" class="bar" ${cssom({ width: showing ? `${share}%` : '0%' })}></span>
|
|
150
|
+
<slot></slot>
|
|
151
|
+
<span part="result" class="result" ?hidden=${!showing}>
|
|
152
|
+
${chosen ? html `<svg viewBox="0 0 16 16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 8.5l3 3 7-7" /></svg>` : ''}
|
|
153
|
+
${share}%<span class="visually-hidden">, ${votes} ${votes === 1 ? 'vote' : 'votes'}${chosen ? ', your vote' : ''}</span>
|
|
154
|
+
</span>
|
|
155
|
+
</li>
|
|
156
|
+
`;
|
|
157
|
+
})}
|
|
158
|
+
</ul>
|
|
159
|
+
<div part="help-text" class="footer">
|
|
160
|
+
<span ?hidden=${!showing}>${total} ${total === 1 ? 'vote' : 'votes'}</span>
|
|
161
|
+
<slot name="help-text">${this.helpText}</slot>
|
|
162
|
+
</div>
|
|
163
|
+
<div class="visually-hidden" role="status">${this.#announcement}</div>
|
|
164
|
+
</div>
|
|
165
|
+
`;
|
|
166
|
+
}
|
|
167
|
+
static { this.styles = [
|
|
168
|
+
focusRing,
|
|
169
|
+
css `
|
|
170
|
+
:host {
|
|
171
|
+
display: block;
|
|
172
|
+
box-sizing: border-box;
|
|
173
|
+
width: 100%;
|
|
174
|
+
max-width: 32rem;
|
|
175
|
+
font-family: var(--va-font-family-ui);
|
|
176
|
+
font-size: var(--va-font-size-md);
|
|
177
|
+
color: var(--va-color-text-default);
|
|
178
|
+
}
|
|
179
|
+
:host([hidden]) {
|
|
180
|
+
display: none;
|
|
181
|
+
}
|
|
182
|
+
.label {
|
|
183
|
+
margin-bottom: var(--va-space-gap-md);
|
|
184
|
+
font-size: var(--va-font-size-lg);
|
|
185
|
+
font-weight: var(--va-font-weight-bold);
|
|
186
|
+
line-height: 1.3;
|
|
187
|
+
}
|
|
188
|
+
.options {
|
|
189
|
+
display: flex;
|
|
190
|
+
flex-direction: column;
|
|
191
|
+
gap: var(--va-space-gap-sm);
|
|
192
|
+
margin: 0;
|
|
193
|
+
padding: 0;
|
|
194
|
+
list-style: none;
|
|
195
|
+
}
|
|
196
|
+
.option {
|
|
197
|
+
position: relative;
|
|
198
|
+
display: grid;
|
|
199
|
+
grid-template-columns: 1fr auto;
|
|
200
|
+
align-items: center;
|
|
201
|
+
overflow: hidden;
|
|
202
|
+
border: 1px solid var(--va-poll-border, var(--va-color-border-control));
|
|
203
|
+
border-radius: var(--va-poll-radius, var(--va-radius-control));
|
|
204
|
+
background: var(--va-color-surface-default);
|
|
205
|
+
}
|
|
206
|
+
.poll:not(.showing) .option:hover {
|
|
207
|
+
background: var(--va-color-surface-subtle);
|
|
208
|
+
}
|
|
209
|
+
.bar {
|
|
210
|
+
position: absolute;
|
|
211
|
+
inset: 0 auto 0 0;
|
|
212
|
+
background-color: var(--va-poll-bar-bg, color-mix(in srgb, var(--va-color-accent-default) 16%, transparent));
|
|
213
|
+
transition: width var(--va-motion-duration-slow) ease-out;
|
|
214
|
+
}
|
|
215
|
+
.chosen .bar {
|
|
216
|
+
background-color: var(--va-poll-bar-bg, color-mix(in srgb, var(--va-color-accent-default) 28%, transparent));
|
|
217
|
+
}
|
|
218
|
+
slot:not([name]) {
|
|
219
|
+
display: contents;
|
|
220
|
+
}
|
|
221
|
+
::slotted(button) {
|
|
222
|
+
position: relative;
|
|
223
|
+
box-sizing: border-box;
|
|
224
|
+
width: 100%;
|
|
225
|
+
min-height: var(--va-size-control-lg);
|
|
226
|
+
margin: 0;
|
|
227
|
+
padding: var(--va-space-gap-sm) var(--va-space-inline-md);
|
|
228
|
+
border: none;
|
|
229
|
+
border-radius: var(--va-poll-radius, var(--va-radius-control));
|
|
230
|
+
background: transparent;
|
|
231
|
+
color: inherit;
|
|
232
|
+
font: inherit;
|
|
233
|
+
text-align: start;
|
|
234
|
+
cursor: pointer;
|
|
235
|
+
}
|
|
236
|
+
::slotted(button:focus-visible) {
|
|
237
|
+
outline: var(--va-size-focus-ring, 2px) solid var(--va-color-focus);
|
|
238
|
+
outline-offset: -2px;
|
|
239
|
+
}
|
|
240
|
+
::slotted([aria-disabled='true']) {
|
|
241
|
+
cursor: default;
|
|
242
|
+
}
|
|
243
|
+
.chosen ::slotted(button) {
|
|
244
|
+
font-weight: var(--va-font-weight-bold);
|
|
245
|
+
}
|
|
246
|
+
.result {
|
|
247
|
+
position: relative;
|
|
248
|
+
display: inline-flex;
|
|
249
|
+
align-items: center;
|
|
250
|
+
gap: var(--va-space-gap-sm);
|
|
251
|
+
padding-inline-end: var(--va-space-inline-md);
|
|
252
|
+
font-variant-numeric: tabular-nums;
|
|
253
|
+
font-weight: var(--va-font-weight-medium);
|
|
254
|
+
pointer-events: none;
|
|
255
|
+
}
|
|
256
|
+
.result[hidden] {
|
|
257
|
+
display: none;
|
|
258
|
+
}
|
|
259
|
+
.result svg {
|
|
260
|
+
width: 1rem;
|
|
261
|
+
height: 1rem;
|
|
262
|
+
color: var(--va-color-accent-default);
|
|
263
|
+
}
|
|
264
|
+
.footer {
|
|
265
|
+
display: flex;
|
|
266
|
+
flex-wrap: wrap;
|
|
267
|
+
gap: 0 var(--va-space-gap-md);
|
|
268
|
+
margin-top: var(--va-space-gap-md);
|
|
269
|
+
color: var(--va-color-text-muted);
|
|
270
|
+
font-size: var(--va-font-size-sm);
|
|
271
|
+
}
|
|
272
|
+
.footer [hidden] {
|
|
273
|
+
display: none;
|
|
274
|
+
}
|
|
275
|
+
.visually-hidden {
|
|
276
|
+
position: absolute;
|
|
277
|
+
width: 1px;
|
|
278
|
+
height: 1px;
|
|
279
|
+
overflow: hidden;
|
|
280
|
+
clip-path: inset(50%);
|
|
281
|
+
white-space: nowrap;
|
|
282
|
+
}
|
|
283
|
+
@media (prefers-reduced-motion: reduce) {
|
|
284
|
+
.bar {
|
|
285
|
+
transition: none;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
@media (forced-colors: active) {
|
|
289
|
+
.option {
|
|
290
|
+
border-color: CanvasText;
|
|
291
|
+
}
|
|
292
|
+
.bar {
|
|
293
|
+
background-color: Highlight;
|
|
294
|
+
opacity: 0.35;
|
|
295
|
+
forced-color-adjust: none;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
`,
|
|
299
|
+
]; }
|
|
300
|
+
}
|
|
301
|
+
//# sourceMappingURL=poll.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poll.js","sourceRoot":"","sources":["../../../src/poll/poll.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,IAAI,EAAkD,MAAM,KAAK,CA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CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC5D,MAAM,MAAM,GAAG,OAAO,IAAI,IAAI,CAAC,KAAK,KAAK,EAAE,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC;YAClF,OAAO,IAAI,CAAA;wCACiB,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,QAAQ;+CAC5B,KAAK,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;;6DAEhC,CAAC,OAAO;oBACjD,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA,kIAAkI,CAAC,CAAC,CAAC,EAAE;oBACpJ,KAAK,oCAAoC,KAAK,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE;;;aAGrH,CAAC;QACJ,CAAC,CAAC;;;0BAGc,CAAC,OAAO,IAAI,KAAK,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO;mCAC1C,IAAI,CAAC,QAAQ;;qDAEK,IAAI,CAAC,aAAa;;KAElE,CAAC;IACJ,CAAC;aAEe,WAAM,GAAG;QACvB,SAAS;QACT,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAiIF;KACF,CAAC","sourcesContent":["import { LitElement, css, html, type PropertyDeclarations, type PropertyValues } from 'lit';\nimport { cssom } from '../core/cssom.js';\nimport { focusRing } from '../core/focus-ring.js';\nimport { checkTokensLoaded, devWarn } from '../core/warn.js';\n\n/**\n * A one-question poll. The user votes with one click, and the poll shows the results.\n *\n * @tag va-poll\n * @summary A poll: asks one question with a few answers. Visitors vote with one click and see everyone's results.\n *\n * @slot - The answers, as `<button value=\"…\" data-votes=\"N\">` elements. `data-votes` is the number of votes so far.\n * @slot help-text - Optional text under the poll, such as when it closes. Overrides the `help-text` attribute.\n *\n * @csspart label - The question.\n * @csspart option - The row around each answer.\n * @csspart bar - The bar behind each answer that shows its share of the votes.\n * @csspart result - The share of the votes, shown after voting.\n * @csspart help-text - The help text and the vote count.\n *\n * @cssprop {<color>} --va-poll-border - Border colour of each answer's row.\n * @cssprop {<length>} --va-poll-radius - Corner radius of each answer's row.\n * @cssprop {<color>} --va-poll-bar-bg - Colour of the result bars.\n *\n * @cssprop-applies --va-poll-border option border-top-color\n * @cssprop-applies --va-poll-radius option border-top-left-radius\n * @cssprop-applies --va-poll-bar-bg bar background-color\n *\n * @keyboard-items button\n * @keyboard items: Enter | activates | Votes for the focused answer.\n * @keyboard items: Space | activates | Votes for the focused answer.\n *\n * @a11y-role group\n * @a11y-name-from label\n * @a11y-name The `label`, which is the question shown above the answers.\n * @a11y-note After a vote the answers stay focusable but get `aria-disabled=\"true\"` and an `aria-description` with their share of the votes. A polite live region announces that the vote was counted.\n *\n * @guidance-use For a quick, one-click opinion poll on a page, with two to six answers.\n * @guidance-avoid For choices that are submitted with a form, for surveys with several questions, or where users need to review their answer before sending it.\n * @guidance-instead A radio group in a form, with a submit `va-button`.\n *\n * @fires {CustomEvent<{ value: string, item: HTMLElement }>} va-select - When the user votes. `event.detail.value` is the answer's `value`, or its text when it has none; `event.detail.item` is the answer. Send the vote to your server here.\n * @event-trigger va-select | click-item 2\n */\nexport class VaPoll extends LitElement {\n static override shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, slotAssignment: 'manual' };\n\n static override properties: PropertyDeclarations = {\n label: {},\n value: {},\n showResults: { type: Boolean, reflect: true, attribute: 'show-results' },\n helpText: { attribute: 'help-text' },\n _revision: { state: true },\n };\n\n /** The question, shown above the answers. */\n declare label: string;\n /** The value of the answer the user voted for, or an empty string before a vote. Setting it shows the results with that answer marked, for a user who has already voted. */\n declare value: string;\n /** Shows the results without a vote, such as for a closed poll. */\n declare showResults: boolean;\n /** Text under the poll, such as when it closes. */\n declare helpText: string;\n\n declare private _revision: number;\n #announcement = '';\n #observer = new MutationObserver(() => this._revision++);\n\n constructor() {\n super();\n this.label = '';\n this.value = '';\n this.showResults = false;\n this.helpText = '';\n this._revision = 0;\n this.addEventListener('click', this.#onClick);\n }\n\n override connectedCallback(): void {\n super.connectedCallback();\n checkTokensLoaded(this);\n this.#observer.observe(this, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['data-votes', 'value', 'slot'] });\n // Children may have changed while the poll was detached and unobserved.\n if (this.hasUpdated) this._revision++;\n }\n\n override disconnectedCallback(): void {\n super.disconnectedCallback();\n this.#observer.disconnect();\n }\n\n get #items(): HTMLElement[] {\n return [...this.children].filter((c): c is HTMLElement => !c.slot) as HTMLElement[];\n }\n\n get #showing(): boolean {\n return this.showResults || this.value !== '';\n }\n\n #valueOf(item: HTMLElement): string {\n return (item as HTMLButtonElement).value || item.textContent?.trim() || '';\n }\n\n #votesOf(item: HTMLElement): number {\n const votes = Number(item.dataset.votes ?? 0);\n return Number.isFinite(votes) && votes > 0 ? votes : 0;\n }\n\n override firstUpdated(): void {\n if (!this.label) devWarn('va-poll', 'This poll has no label, so its question is missing.', 'Add label=\"…\" with the question (e.g. label=\"Tabs or spaces?\").');\n }\n\n override updated(changed: PropertyValues): void {\n const items = this.#items;\n const slots = [...this.shadowRoot!.querySelectorAll<HTMLSlotElement>('slot:not([name])')];\n items.forEach((item, i) => slots[i]?.assign(item));\n this.shadowRoot!.querySelector<HTMLSlotElement>('slot[name=\"help-text\"]')!.assign(...this.querySelectorAll(':scope > [slot=\"help-text\"]'));\n\n const showing = this.#showing;\n const total = items.reduce((sum, i) => sum + this.#votesOf(i), 0);\n for (const item of items) {\n if (item.localName === 'button') (item as HTMLButtonElement).type = 'button';\n if (showing) {\n // The share is shadow text after the button, which aria-describedby can't reach: describe it\n // on the button itself, so tabbing through the answers reads the results.\n const votes = this.#votesOf(item);\n const share = total ? Math.round((votes / total) * 100) : 0;\n const chosen = this.value !== '' && this.#valueOf(item) === this.value;\n item.setAttribute('aria-disabled', 'true');\n item.setAttribute('aria-description', `${share}%, ${votes} ${votes === 1 ? 'vote' : 'votes'}${chosen ? ', your vote' : ''}`);\n } else {\n item.removeAttribute('aria-disabled');\n item.removeAttribute('aria-description');\n }\n if (item.dataset.votes !== undefined && !(Number(item.dataset.votes) >= 0)) {\n devWarn('va-poll', `data-votes=\"${item.dataset.votes}\" on \"${item.textContent?.trim()}\" is not a number of votes.`, 'Set data-votes to a whole number of 0 or more (e.g. data-votes=\"12\").');\n }\n }\n if (changed.has('_revision') && items.some((c) => c.localName !== 'button')) {\n devWarn('va-poll', 'An answer is not a <button>, so it may not be keyboard accessible.', 'Use <button value=\"…\" data-votes=\"N\">Answer</button> for each answer.');\n }\n }\n\n #onClick = (event: MouseEvent): void => {\n const item = this.#items.find((i) => event.composedPath().includes(i));\n if (!item || this.#showing) return;\n // Count the vote here, so the results include it before the server answers.\n item.dataset.votes = String(this.#votesOf(item) + 1);\n this.value = this.#valueOf(item);\n const total = this.#items.reduce((sum, i) => sum + this.#votesOf(i), 0);\n this.#announcement = `Vote counted. ${total} ${total === 1 ? 'vote' : 'votes'} in total.`;\n this.dispatchEvent(new CustomEvent('va-select', { detail: { value: this.value, item }, bubbles: true, composed: true }));\n };\n\n override render() {\n const items = this.#items;\n const showing = this.#showing;\n const total = items.reduce((sum, i) => sum + this.#votesOf(i), 0);\n return html`\n <div class=${showing ? 'poll showing' : 'poll'} role=\"group\" aria-labelledby=\"label\">\n <div part=\"label\" id=\"label\" class=\"label\">${this.label}</div>\n <ul class=\"options\" role=\"list\">\n ${items.map((item) => {\n const votes = this.#votesOf(item);\n const share = total ? Math.round((votes / total) * 100) : 0;\n const chosen = showing && this.value !== '' && this.#valueOf(item) === this.value;\n return html`\n <li part=\"option\" class=${chosen ? 'option chosen' : 'option'}>\n <span part=\"bar\" class=\"bar\" ${cssom({ width: showing ? `${share}%` : '0%' })}></span>\n <slot></slot>\n <span part=\"result\" class=\"result\" ?hidden=${!showing}>\n ${chosen ? html`<svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><path d=\"M3 8.5l3 3 7-7\" /></svg>` : ''}\n ${share}%<span class=\"visually-hidden\">, ${votes} ${votes === 1 ? 'vote' : 'votes'}${chosen ? ', your vote' : ''}</span>\n </span>\n </li>\n `;\n })}\n </ul>\n <div part=\"help-text\" class=\"footer\">\n <span ?hidden=${!showing}>${total} ${total === 1 ? 'vote' : 'votes'}</span>\n <slot name=\"help-text\">${this.helpText}</slot>\n </div>\n <div class=\"visually-hidden\" role=\"status\">${this.#announcement}</div>\n </div>\n `;\n }\n\n static override styles = [\n focusRing,\n css`\n :host {\n display: block;\n box-sizing: border-box;\n width: 100%;\n max-width: 32rem;\n font-family: var(--va-font-family-ui);\n font-size: var(--va-font-size-md);\n color: var(--va-color-text-default);\n }\n :host([hidden]) {\n display: none;\n }\n .label {\n margin-bottom: var(--va-space-gap-md);\n font-size: var(--va-font-size-lg);\n font-weight: var(--va-font-weight-bold);\n line-height: 1.3;\n }\n .options {\n display: flex;\n flex-direction: column;\n gap: var(--va-space-gap-sm);\n margin: 0;\n padding: 0;\n list-style: none;\n }\n .option {\n position: relative;\n display: grid;\n grid-template-columns: 1fr auto;\n align-items: center;\n overflow: hidden;\n border: 1px solid var(--va-poll-border, var(--va-color-border-control));\n border-radius: var(--va-poll-radius, var(--va-radius-control));\n background: var(--va-color-surface-default);\n }\n .poll:not(.showing) .option:hover {\n background: var(--va-color-surface-subtle);\n }\n .bar {\n position: absolute;\n inset: 0 auto 0 0;\n background-color: var(--va-poll-bar-bg, color-mix(in srgb, var(--va-color-accent-default) 16%, transparent));\n transition: width var(--va-motion-duration-slow) ease-out;\n }\n .chosen .bar {\n background-color: var(--va-poll-bar-bg, color-mix(in srgb, var(--va-color-accent-default) 28%, transparent));\n }\n slot:not([name]) {\n display: contents;\n }\n ::slotted(button) {\n position: relative;\n box-sizing: border-box;\n width: 100%;\n min-height: var(--va-size-control-lg);\n margin: 0;\n padding: var(--va-space-gap-sm) var(--va-space-inline-md);\n border: none;\n border-radius: var(--va-poll-radius, var(--va-radius-control));\n background: transparent;\n color: inherit;\n font: inherit;\n text-align: start;\n cursor: pointer;\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 ::slotted([aria-disabled='true']) {\n cursor: default;\n }\n .chosen ::slotted(button) {\n font-weight: var(--va-font-weight-bold);\n }\n .result {\n position: relative;\n display: inline-flex;\n align-items: center;\n gap: var(--va-space-gap-sm);\n padding-inline-end: var(--va-space-inline-md);\n font-variant-numeric: tabular-nums;\n font-weight: var(--va-font-weight-medium);\n pointer-events: none;\n }\n .result[hidden] {\n display: none;\n }\n .result svg {\n width: 1rem;\n height: 1rem;\n color: var(--va-color-accent-default);\n }\n .footer {\n display: flex;\n flex-wrap: wrap;\n gap: 0 var(--va-space-gap-md);\n margin-top: var(--va-space-gap-md);\n color: var(--va-color-text-muted);\n font-size: var(--va-font-size-sm);\n }\n .footer [hidden] {\n display: none;\n }\n .visually-hidden {\n position: absolute;\n width: 1px;\n height: 1px;\n overflow: hidden;\n clip-path: inset(50%);\n white-space: nowrap;\n }\n @media (prefers-reduced-motion: reduce) {\n .bar {\n transition: none;\n }\n }\n @media (forced-colors: active) {\n .option {\n border-color: CanvasText;\n }\n .bar {\n background-color: Highlight;\n opacity: 0.35;\n forced-color-adjust: none;\n }\n }\n `,\n ];\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'va-poll': VaPoll;\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"}
|
|
@@ -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
|