@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.
Files changed (149) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +52 -2
  3. package/custom-elements.json +3737 -0
  4. package/dist/dev/all.d.ts +12 -0
  5. package/dist/dev/all.d.ts.map +1 -0
  6. package/dist/dev/all.js +13 -0
  7. package/dist/dev/all.js.map +1 -0
  8. package/dist/dev/bar-chart/bar-chart.d.ts +63 -0
  9. package/dist/dev/bar-chart/bar-chart.d.ts.map +1 -0
  10. package/dist/dev/bar-chart/bar-chart.js +316 -0
  11. package/dist/dev/bar-chart/bar-chart.js.map +1 -0
  12. package/dist/dev/bar-chart/index.d.ts +3 -0
  13. package/dist/dev/bar-chart/index.d.ts.map +1 -0
  14. package/dist/dev/bar-chart/index.js +5 -0
  15. package/dist/dev/bar-chart/index.js.map +1 -0
  16. package/dist/dev/button/button.d.ts +98 -0
  17. package/dist/dev/button/button.d.ts.map +1 -0
  18. package/dist/dev/button/button.js +292 -0
  19. package/dist/dev/button/button.js.map +1 -0
  20. package/dist/dev/button/index.d.ts +4 -0
  21. package/dist/dev/button/index.d.ts.map +1 -0
  22. package/dist/dev/button/index.js +5 -0
  23. package/dist/dev/button/index.js.map +1 -0
  24. package/dist/dev/checkbox/checkbox.d.ts +71 -0
  25. package/dist/dev/checkbox/checkbox.d.ts.map +1 -0
  26. package/dist/dev/checkbox/checkbox.js +232 -0
  27. package/dist/dev/checkbox/checkbox.js.map +1 -0
  28. package/dist/dev/checkbox/index.d.ts +3 -0
  29. package/dist/dev/checkbox/index.d.ts.map +1 -0
  30. package/dist/dev/checkbox/index.js +5 -0
  31. package/dist/dev/checkbox/index.js.map +1 -0
  32. package/dist/dev/core/cssom.d.ts +15 -0
  33. package/dist/dev/core/cssom.d.ts.map +1 -0
  34. package/dist/dev/core/cssom.js +30 -0
  35. package/dist/dev/core/cssom.js.map +1 -0
  36. package/dist/dev/core/define.d.ts +3 -0
  37. package/dist/dev/core/define.d.ts.map +1 -0
  38. package/dist/dev/core/define.js +6 -0
  39. package/dist/dev/core/define.js.map +1 -0
  40. package/dist/dev/core/dev-mode.d.ts +6 -0
  41. package/dist/dev/core/dev-mode.d.ts.map +1 -0
  42. package/dist/dev/core/dev-mode.js +6 -0
  43. package/dist/dev/core/dev-mode.js.map +1 -0
  44. package/dist/dev/core/focus-ring.d.ts +3 -0
  45. package/dist/dev/core/focus-ring.d.ts.map +1 -0
  46. package/dist/dev/core/focus-ring.js +14 -0
  47. package/dist/dev/core/focus-ring.js.map +1 -0
  48. package/dist/dev/core/form-control.d.ts +47 -0
  49. package/dist/dev/core/form-control.d.ts.map +1 -0
  50. package/dist/dev/core/form-control.js +75 -0
  51. package/dist/dev/core/form-control.js.map +1 -0
  52. package/dist/dev/core/warn.d.ts +12 -0
  53. package/dist/dev/core/warn.d.ts.map +1 -0
  54. package/dist/dev/core/warn.js +35 -0
  55. package/dist/dev/core/warn.js.map +1 -0
  56. package/dist/dev/dialog/dialog.d.ts +77 -0
  57. package/dist/dev/dialog/dialog.d.ts.map +1 -0
  58. package/dist/dev/dialog/dialog.js +253 -0
  59. package/dist/dev/dialog/dialog.js.map +1 -0
  60. package/dist/dev/dialog/index.d.ts +4 -0
  61. package/dist/dev/dialog/index.d.ts.map +1 -0
  62. package/dist/dev/dialog/index.js +5 -0
  63. package/dist/dev/dialog/index.js.map +1 -0
  64. package/dist/dev/menu/index.d.ts +3 -0
  65. package/dist/dev/menu/index.d.ts.map +1 -0
  66. package/dist/dev/menu/index.js +5 -0
  67. package/dist/dev/menu/index.js.map +1 -0
  68. package/dist/dev/menu/menu.d.ts +71 -0
  69. package/dist/dev/menu/menu.d.ts.map +1 -0
  70. package/dist/dev/menu/menu.js +330 -0
  71. package/dist/dev/menu/menu.js.map +1 -0
  72. package/dist/dev/navbar/index.d.ts +3 -0
  73. package/dist/dev/navbar/index.d.ts.map +1 -0
  74. package/dist/dev/navbar/index.js +5 -0
  75. package/dist/dev/navbar/index.js.map +1 -0
  76. package/dist/dev/navbar/navbar.d.ts +69 -0
  77. package/dist/dev/navbar/navbar.d.ts.map +1 -0
  78. package/dist/dev/navbar/navbar.js +353 -0
  79. package/dist/dev/navbar/navbar.js.map +1 -0
  80. package/dist/dev/poll/index.d.ts +3 -0
  81. package/dist/dev/poll/index.d.ts.map +1 -0
  82. package/dist/dev/poll/index.js +5 -0
  83. package/dist/dev/poll/index.js.map +1 -0
  84. package/dist/dev/poll/poll.d.ts +67 -0
  85. package/dist/dev/poll/poll.d.ts.map +1 -0
  86. package/dist/dev/poll/poll.js +301 -0
  87. package/dist/dev/poll/poll.js.map +1 -0
  88. package/dist/dev/switch/index.d.ts +3 -0
  89. package/dist/dev/switch/index.d.ts.map +1 -0
  90. package/dist/dev/switch/index.js +5 -0
  91. package/dist/dev/switch/index.js.map +1 -0
  92. package/dist/dev/switch/switch.d.ts +69 -0
  93. package/dist/dev/switch/switch.d.ts.map +1 -0
  94. package/dist/dev/switch/switch.js +213 -0
  95. package/dist/dev/switch/switch.js.map +1 -0
  96. package/dist/dev/tabs/index.d.ts +3 -0
  97. package/dist/dev/tabs/index.d.ts.map +1 -0
  98. package/dist/dev/tabs/index.js +5 -0
  99. package/dist/dev/tabs/index.js.map +1 -0
  100. package/dist/dev/tabs/tabs.d.ts +64 -0
  101. package/dist/dev/tabs/tabs.d.ts.map +1 -0
  102. package/dist/dev/tabs/tabs.js +212 -0
  103. package/dist/dev/tabs/tabs.js.map +1 -0
  104. package/dist/dev/text-field/index.d.ts +3 -0
  105. package/dist/dev/text-field/index.d.ts.map +1 -0
  106. package/dist/dev/text-field/index.js +5 -0
  107. package/dist/dev/text-field/index.js.map +1 -0
  108. package/dist/dev/text-field/text-field.d.ts +91 -0
  109. package/dist/dev/text-field/text-field.d.ts.map +1 -0
  110. package/dist/dev/text-field/text-field.js +267 -0
  111. package/dist/dev/text-field/text-field.js.map +1 -0
  112. package/dist/dev/toast/index.d.ts +4 -0
  113. package/dist/dev/toast/index.d.ts.map +1 -0
  114. package/dist/dev/toast/index.js +5 -0
  115. package/dist/dev/toast/index.js.map +1 -0
  116. package/dist/dev/toast/toast.d.ts +71 -0
  117. package/dist/dev/toast/toast.d.ts.map +1 -0
  118. package/dist/dev/toast/toast.js +258 -0
  119. package/dist/dev/toast/toast.js.map +1 -0
  120. package/dist/prod/all.js +13 -0
  121. package/dist/prod/bar-chart/bar-chart.js +316 -0
  122. package/dist/prod/bar-chart/index.js +5 -0
  123. package/dist/prod/button/button.js +292 -0
  124. package/dist/prod/button/index.js +5 -0
  125. package/dist/prod/checkbox/checkbox.js +232 -0
  126. package/dist/prod/checkbox/index.js +5 -0
  127. package/dist/prod/core/cssom.js +30 -0
  128. package/dist/prod/core/define.js +6 -0
  129. package/dist/prod/core/dev-mode.js +1 -0
  130. package/dist/prod/core/focus-ring.js +14 -0
  131. package/dist/prod/core/form-control.js +75 -0
  132. package/dist/prod/core/warn.js +2 -0
  133. package/dist/prod/dialog/dialog.js +253 -0
  134. package/dist/prod/dialog/index.js +5 -0
  135. package/dist/prod/menu/index.js +5 -0
  136. package/dist/prod/menu/menu.js +330 -0
  137. package/dist/prod/navbar/index.js +5 -0
  138. package/dist/prod/navbar/navbar.js +353 -0
  139. package/dist/prod/poll/index.js +5 -0
  140. package/dist/prod/poll/poll.js +301 -0
  141. package/dist/prod/switch/index.js +5 -0
  142. package/dist/prod/switch/switch.js +213 -0
  143. package/dist/prod/tabs/index.js +5 -0
  144. package/dist/prod/tabs/tabs.js +212 -0
  145. package/dist/prod/text-field/index.js +5 -0
  146. package/dist/prod/text-field/text-field.js +267 -0
  147. package/dist/prod/toast/index.js +5 -0
  148. package/dist/prod/toast/toast.js +258 -0
  149. 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,5 @@
1
+ import { define } from '../core/define.js';
2
+ import { VaSwitch } from './switch.js';
3
+ export { VaSwitch };
4
+ define('va-switch', VaSwitch);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,213 @@
1
+ import { css, html, nothing } from 'lit';
2
+ import { FormControl } from '../core/form-control.js';
3
+ import { focusRing } from '../core/focus-ring.js';
4
+ import { checkTokensLoaded, devWarn } from '../core/warn.js';
5
+ /**
6
+ * An on/off switch for a setting that takes effect immediately.
7
+ *
8
+ * @tag va-switch
9
+ * @summary Turns a setting on or off, with immediate effect.
10
+ *
11
+ * @slot - The visible label. Clicking it flips the switch.
12
+ * @slot help-text - Optional help text under the label. It becomes the switch's description.
13
+ *
14
+ * @csspart control - The switch's track (a native button with `role="switch"`).
15
+ * @csspart thumb - The round thumb.
16
+ * @csspart label - The label text.
17
+ * @csspart help-text - The help text.
18
+ *
19
+ * @cssprop {<color>} --va-switch-track-bg - Track colour when off.
20
+ * @cssprop {<color>} --va-switch-track-bg-checked - Track colour when on.
21
+ * @cssprop {<color>} --va-switch-thumb-bg - Thumb colour.
22
+ *
23
+ * @cssprop-applies --va-switch-track-bg control:unchecked background-color
24
+ * @cssprop-applies --va-switch-track-bg-checked control:checked background-color
25
+ * @cssprop-applies --va-switch-thumb-bg thumb background-color
26
+ *
27
+ * @keyboard Space | toggles | Turns the switch on or off.
28
+ * @keyboard Enter | toggles | Turns the switch on or off.
29
+ * @keyboard Tab | focus-next | Moves focus to the next control.
30
+ *
31
+ * @a11y-role switch
32
+ * @a11y-name-from text, accessible-label
33
+ * @replaces [role="switch"]
34
+ * @a11y-name The slotted label text. Use `accessible-label` only when there is no visible label.
35
+ * @a11y-note The state is announced as on or off, not checked or unchecked.
36
+ * @a11y-note Style the on state with `va-switch:state(checked)`; the `checked` attribute is the initial state only, as on a native checkbox.
37
+ *
38
+ * @guidance-use For a setting that applies as soon as it changes, such as "Dark theme" or "Email notifications".
39
+ * @guidance-avoid For a choice that is only applied when a form is submitted, or for agreeing to terms.
40
+ * @guidance-instead `va-checkbox` for choices submitted with a form.
41
+ *
42
+ * @fires change - When the user flips the switch. Read `event.target.checked`.
43
+ * @fires input - When the user flips the switch, just before `change`.
44
+ * @event-trigger change | click
45
+ * @event-trigger input | click
46
+ */
47
+ export class VaSwitch extends FormControl {
48
+ static { this.shadowRootOptions = { ...FormControl.shadowRootOptions, delegatesFocus: true }; }
49
+ static { this.properties = {
50
+ checked: { type: Boolean },
51
+ value: {},
52
+ accessibleLabel: { attribute: 'accessible-label' },
53
+ }; }
54
+ #defaultChecked;
55
+ constructor() {
56
+ super();
57
+ this.checked = false;
58
+ this.value = 'on';
59
+ this.accessibleLabel = '';
60
+ }
61
+ connectedCallback() {
62
+ super.connectedCallback();
63
+ // The default is the initial attribute, read once: moving the element must not change it.
64
+ this.#defaultChecked ??= this.hasAttribute('checked');
65
+ checkTokensLoaded(this);
66
+ }
67
+ firstUpdated() {
68
+ requestAnimationFrame(() => {
69
+ const text = this.#labelText();
70
+ if (!text && !this.accessibleLabel) {
71
+ devWarn('va-switch', 'This switch has no accessible name.', 'Put label text inside it (e.g. Dark theme), or add accessible-label="…" when there is no room for one.');
72
+ }
73
+ else if (text && this.accessibleLabel && !this.accessibleLabel.toLowerCase().includes(text.toLowerCase())) {
74
+ devWarn('va-switch', `accessible-label="${this.accessibleLabel}" replaces the visible label "${text}" for screen readers (WCAG 2.5.3).`, 'Remove accessible-label: the visible text is the name.');
75
+ }
76
+ });
77
+ }
78
+ /** The visible label: text not in the help-text slot. */
79
+ #labelText() {
80
+ return [...this.childNodes]
81
+ .filter((n) => !(n instanceof Element && n.slot))
82
+ .map((n) => n.textContent ?? '')
83
+ .join('')
84
+ .trim();
85
+ }
86
+ updated() {
87
+ this._setState('checked', this.checked);
88
+ this._internals.setFormValue(this.checked ? this.value : null);
89
+ this._internals.setValidity(this.required && !this.checked ? { valueMissing: true } : {}, this.required && !this.checked ? 'Turn this on to continue.' : '', this.shadowRoot?.querySelector('button') ?? undefined);
90
+ }
91
+ /** @internal */
92
+ formResetCallback() {
93
+ this.checked = this.#defaultChecked ?? false;
94
+ }
95
+ #toggle() {
96
+ if (this._isDisabled)
97
+ return;
98
+ this.checked = !this.checked;
99
+ this._emit('input');
100
+ this._emit('change');
101
+ }
102
+ render() {
103
+ return html `
104
+ <button
105
+ part="control"
106
+ class="control ${this.checked ? 'on' : ''}"
107
+ type="button"
108
+ role="switch"
109
+ aria-checked=${this.checked ? 'true' : 'false'}
110
+ aria-labelledby=${this.accessibleLabel ? nothing : 'label'}
111
+ aria-label=${this.accessibleLabel || nothing}
112
+ aria-describedby="help-text"
113
+ ?disabled=${this._isDisabled}
114
+ @click=${this.#toggle}
115
+ >
116
+ <span part="thumb" class="thumb"></span>
117
+ </button>
118
+ <span class="text">
119
+ <span part="label" id="label" class="label ${this._isDisabled ? 'disabled' : ''}" @click=${this.#toggle}><slot></slot></span>
120
+ <span part="help-text" id="help-text" class="help-text"><slot name="help-text"></slot></span>
121
+ </span>
122
+ `;
123
+ }
124
+ static { this.styles = [
125
+ focusRing,
126
+ css `
127
+ :host {
128
+ --_track: var(--va-switch-track-bg, var(--va-color-border-control));
129
+ --_track-on: var(--va-switch-track-bg-checked, var(--va-color-accent-default));
130
+ --_thumb: var(--va-switch-thumb-bg, var(--va-color-surface-default));
131
+ display: inline-flex;
132
+ align-items: flex-start;
133
+ gap: var(--va-space-inline-sm);
134
+ font-family: var(--va-font-family-ui);
135
+ font-size: var(--va-font-size-md);
136
+ color: var(--va-color-text-default);
137
+ vertical-align: middle;
138
+ }
139
+ :host([hidden]) {
140
+ display: none;
141
+ }
142
+ .control {
143
+ flex: none;
144
+ position: relative;
145
+ width: 2.75rem;
146
+ height: 1.5rem;
147
+ margin: 0;
148
+ padding: 0;
149
+ border: 1px solid transparent;
150
+ border-radius: var(--va-radius-round);
151
+ background-color: var(--_track);
152
+ cursor: pointer;
153
+ transition: background-color var(--va-motion-duration-fast) ease-out;
154
+ }
155
+ .control.on {
156
+ background-color: var(--_track-on);
157
+ }
158
+ .thumb {
159
+ position: absolute;
160
+ top: 2px;
161
+ left: 2px;
162
+ width: calc(1.5rem - 6px);
163
+ height: calc(1.5rem - 6px);
164
+ border-radius: 50%;
165
+ background-color: var(--_thumb);
166
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
167
+ transition: transform var(--va-motion-duration-fast) ease-out;
168
+ }
169
+ .control.on .thumb {
170
+ transform: translateX(1.25rem);
171
+ }
172
+ .control:disabled,
173
+ .label.disabled {
174
+ opacity: 0.5;
175
+ cursor: not-allowed;
176
+ }
177
+ .text {
178
+ display: flex;
179
+ flex-direction: column;
180
+ padding-top: 0.1rem;
181
+ }
182
+ .label {
183
+ cursor: pointer;
184
+ user-select: none;
185
+ }
186
+ .help-text {
187
+ color: var(--va-color-text-muted);
188
+ font-size: var(--va-font-size-sm);
189
+ }
190
+ @media (prefers-reduced-motion: reduce) {
191
+ .control,
192
+ .thumb {
193
+ transition: none;
194
+ }
195
+ }
196
+ @media (forced-colors: active) {
197
+ .control {
198
+ border-color: ButtonText;
199
+ }
200
+ .control.on {
201
+ background-color: Highlight;
202
+ }
203
+ .thumb {
204
+ background-color: ButtonText;
205
+ }
206
+ .control.on .thumb {
207
+ background-color: HighlightText;
208
+ }
209
+ }
210
+ `,
211
+ ]; }
212
+ }
213
+ //# sourceMappingURL=switch.js.map
@@ -0,0 +1,5 @@
1
+ import { define } from '../core/define.js';
2
+ import { VaTabs } from './tabs.js';
3
+ export { VaTabs };
4
+ define('va-tabs', VaTabs);
5
+ //# sourceMappingURL=index.js.map