@firstprateek/vastu 1.0.0 → 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 (43) hide show
  1. package/README.md +2 -1
  2. package/custom-elements.json +691 -0
  3. package/dist/dev/all.d.ts +3 -0
  4. package/dist/dev/all.d.ts.map +1 -1
  5. package/dist/dev/all.js +3 -0
  6. package/dist/dev/all.js.map +1 -1
  7. package/dist/dev/bar-chart/bar-chart.d.ts +63 -0
  8. package/dist/dev/bar-chart/bar-chart.d.ts.map +1 -0
  9. package/dist/dev/bar-chart/bar-chart.js +316 -0
  10. package/dist/dev/bar-chart/bar-chart.js.map +1 -0
  11. package/dist/dev/bar-chart/index.d.ts +3 -0
  12. package/dist/dev/bar-chart/index.d.ts.map +1 -0
  13. package/dist/dev/bar-chart/index.js +5 -0
  14. package/dist/dev/bar-chart/index.js.map +1 -0
  15. package/dist/dev/core/cssom.d.ts +15 -0
  16. package/dist/dev/core/cssom.d.ts.map +1 -0
  17. package/dist/dev/core/cssom.js +30 -0
  18. package/dist/dev/core/cssom.js.map +1 -0
  19. package/dist/dev/navbar/index.d.ts +3 -0
  20. package/dist/dev/navbar/index.d.ts.map +1 -0
  21. package/dist/dev/navbar/index.js +5 -0
  22. package/dist/dev/navbar/index.js.map +1 -0
  23. package/dist/dev/navbar/navbar.d.ts +69 -0
  24. package/dist/dev/navbar/navbar.d.ts.map +1 -0
  25. package/dist/dev/navbar/navbar.js +353 -0
  26. package/dist/dev/navbar/navbar.js.map +1 -0
  27. package/dist/dev/poll/index.d.ts +3 -0
  28. package/dist/dev/poll/index.d.ts.map +1 -0
  29. package/dist/dev/poll/index.js +5 -0
  30. package/dist/dev/poll/index.js.map +1 -0
  31. package/dist/dev/poll/poll.d.ts +67 -0
  32. package/dist/dev/poll/poll.d.ts.map +1 -0
  33. package/dist/dev/poll/poll.js +301 -0
  34. package/dist/dev/poll/poll.js.map +1 -0
  35. package/dist/prod/all.js +3 -0
  36. package/dist/prod/bar-chart/bar-chart.js +316 -0
  37. package/dist/prod/bar-chart/index.js +5 -0
  38. package/dist/prod/core/cssom.js +30 -0
  39. package/dist/prod/navbar/index.js +5 -0
  40. package/dist/prod/navbar/navbar.js +353 -0
  41. package/dist/prod/poll/index.js +5 -0
  42. package/dist/prod/poll/poll.js +301 -0
  43. package/package.json +18 -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,CAAC;AAC5F,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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C,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"]}
package/dist/prod/all.js CHANGED
@@ -1,8 +1,11 @@
1
1
  // Generated by build.js: registers every Vastu component.
2
+ export * from './bar-chart/index.js';
2
3
  export * from './button/index.js';
3
4
  export * from './checkbox/index.js';
4
5
  export * from './dialog/index.js';
5
6
  export * from './menu/index.js';
7
+ export * from './navbar/index.js';
8
+ export * from './poll/index.js';
6
9
  export * from './switch/index.js';
7
10
  export * from './tabs/index.js';
8
11
  export * from './text-field/index.js';
@@ -0,0 +1,316 @@
1
+ import { LitElement, css, html } from 'lit';
2
+ import { cssom } from '../core/cssom.js';
3
+ import { checkTokensLoaded, devWarn } from '../core/warn.js';
4
+ /** A step of 1, 2 or 5 times a power of ten that splits `range` into about `count` parts. */
5
+ function niceStep(range, count) {
6
+ const raw = range / count;
7
+ const power = 10 ** Math.floor(Math.log10(raw));
8
+ const f = raw / power;
9
+ return (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * power;
10
+ }
11
+ /**
12
+ * A bar chart that compares values across a few categories. The data is plain markup, and the
13
+ * bars animate when it changes, so it can follow live values.
14
+ *
15
+ * @tag va-bar-chart
16
+ * @summary Compares a number across a few categories, one bar per category.
17
+ *
18
+ * @slot - The data, as `<data value="N">Category</data>` elements, in order. Change them and the bars animate to the new values.
19
+ *
20
+ * @csspart label - The chart's title.
21
+ * @csspart plot - The area the bars are drawn in.
22
+ * @csspart bar - Each bar.
23
+ * @csspart axis - The value labels along the left edge.
24
+ *
25
+ * @cssprop {<color>} --va-bar-chart-bar-bg - Colour of the bars.
26
+ * @cssprop {<length>} --va-bar-chart-bar-radius - Corner radius of the bars' outer end.
27
+ * @cssprop {<length>} --va-bar-chart-size - Height of the plot area.
28
+ *
29
+ * @cssprop-applies --va-bar-chart-bar-bg bar background-color
30
+ * @cssprop-applies --va-bar-chart-bar-radius bar border-top-left-radius
31
+ * @cssprop-applies --va-bar-chart-size plot height
32
+ *
33
+ * @keyboard-items data
34
+ * @keyboard items: ArrowRight | next | Moves to the next bar, wrapping round.
35
+ * @keyboard items: ArrowLeft | previous | Moves to the previous bar, wrapping round.
36
+ * @keyboard items: Home | first | Moves to the first bar.
37
+ * @keyboard items: End | last | Moves to the last bar.
38
+ *
39
+ * @a11y-role figure
40
+ * @a11y-name-from label
41
+ * @a11y-name The `label`, which is the chart's visible title.
42
+ * @a11y-note Each `<data>` element is in the Tab order once (roving tabindex) and is announced as "Category: value". Arrow keys move between bars.
43
+ * @a11y-note The value axis is hidden from assistive technology, because every bar announces its own value.
44
+ *
45
+ * @guidance-use To compare one number across a few categories (up to about 12), such as visitors per day or sales per region.
46
+ * @guidance-avoid For trends over many points in time, for parts of a whole, or when readers need to look up exact values.
47
+ * @guidance-instead A line chart for trends; a table when exact values matter.
48
+ */
49
+ export class VaBarChart extends LitElement {
50
+ static { this.shadowRootOptions = { ...LitElement.shadowRootOptions, slotAssignment: 'manual' }; }
51
+ static { this.properties = {
52
+ label: {},
53
+ max: { type: Number },
54
+ _revision: { state: true },
55
+ _focused: { state: true },
56
+ }; }
57
+ #observer = new MutationObserver(() => this._revision++);
58
+ constructor() {
59
+ super();
60
+ this.label = '';
61
+ this.max = 0;
62
+ this._revision = 0;
63
+ this._focused = -1;
64
+ this.addEventListener('keydown', this.#onKeydown);
65
+ this.addEventListener('focusin', this.#onFocusin);
66
+ this.addEventListener('focusout', () => (this._focused = -1));
67
+ }
68
+ connectedCallback() {
69
+ super.connectedCallback();
70
+ checkTokensLoaded(this);
71
+ this.#observer.observe(this, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['value'] });
72
+ // Children may have changed while the chart was detached and unobserved.
73
+ if (this.hasUpdated)
74
+ this._revision++;
75
+ }
76
+ disconnectedCallback() {
77
+ super.disconnectedCallback();
78
+ this.#observer.disconnect();
79
+ }
80
+ get #items() {
81
+ return [...this.children];
82
+ }
83
+ #valueOf(item) {
84
+ const value = Number(item.getAttribute('value'));
85
+ return Number.isFinite(value) ? value : 0;
86
+ }
87
+ get #format() {
88
+ return new Intl.NumberFormat(this.closest('[lang]')?.getAttribute('lang') || undefined);
89
+ }
90
+ firstUpdated() {
91
+ if (!this.label)
92
+ devWarn('va-bar-chart', 'This chart has no label, so it has no title or accessible name.', 'Add label="…" that says what the bars show (e.g. label="Visitors per day").');
93
+ }
94
+ updated(changed) {
95
+ const items = this.#items;
96
+ const slots = [...this.shadowRoot.querySelectorAll('slot')];
97
+ items.forEach((item, i) => slots[i]?.assign(item));
98
+ const format = this.#format;
99
+ const current = Math.max(0, items.findIndex((i) => i.tabIndex === 0));
100
+ items.forEach((item, i) => {
101
+ item.tabIndex = i === current ? 0 : -1;
102
+ item.setAttribute('role', 'img');
103
+ item.setAttribute('aria-label', `${item.textContent?.trim()}: ${format.format(this.#valueOf(item))}`);
104
+ });
105
+ if (changed.has('_revision')) {
106
+ if (items.some((i) => i.localName !== 'data')) {
107
+ devWarn('va-bar-chart', 'A child is not a <data> element, so it is drawn as a bar with value 0.', 'Use <data value="N">Category</data> for each bar.');
108
+ }
109
+ for (const item of items.filter((i) => i.localName === 'data' && !Number.isFinite(Number(i.getAttribute('value') ?? 'x')))) {
110
+ devWarn('va-bar-chart', `"${item.textContent?.trim()}" has value="${item.getAttribute('value') ?? ''}", which is not a number.`, 'Set value to a plain number, without units or separators (e.g. value="1200").');
111
+ }
112
+ }
113
+ }
114
+ #onFocusin = (event) => {
115
+ const items = this.#items;
116
+ const index = items.indexOf(event.target);
117
+ if (index < 0)
118
+ return;
119
+ items.forEach((item, i) => (item.tabIndex = i === index ? 0 : -1));
120
+ this._focused = index;
121
+ };
122
+ #onKeydown = (event) => {
123
+ const items = this.#items;
124
+ const current = items.indexOf(event.target);
125
+ if (current < 0 || !items.length)
126
+ return;
127
+ const next = { ArrowRight: current + 1, ArrowLeft: current - 1, Home: 0, End: items.length - 1 }[event.key];
128
+ if (next === undefined)
129
+ return;
130
+ event.preventDefault();
131
+ items[(next + items.length) % items.length].focus();
132
+ };
133
+ render() {
134
+ const items = this.#items;
135
+ const values = items.map((i) => this.#valueOf(i));
136
+ const format = this.#format;
137
+ // The axis always includes 0, so bars start from a baseline and their lengths compare honestly.
138
+ let lo = Math.min(0, ...values);
139
+ let hi = this.max > 0 ? this.max : Math.max(0, ...values);
140
+ if (hi === lo)
141
+ hi = lo + 1;
142
+ const step = niceStep(hi - lo, 5);
143
+ lo = Math.floor(lo / step) * step;
144
+ if (!(this.max > 0))
145
+ hi = Math.ceil(hi / step) * step;
146
+ const ticks = [];
147
+ // Count steps instead of adding them up, and round to the step's decimals, so float error
148
+ // never gives a "-0" label or a missing zero line.
149
+ const decimals = Math.max(0, -Math.floor(Math.log10(step)));
150
+ for (let i = 0; lo + i * step <= hi + step / 1e6; i++)
151
+ ticks.push(Number((lo + i * step).toFixed(decimals)) + 0);
152
+ const pos = (v) => ((Math.min(Math.max(v, lo), hi) - lo) / (hi - lo)) * 100;
153
+ return html `
154
+ <div class="chart" role="figure" aria-labelledby="label">
155
+ <div part="label" id="label" class="label">${this.label}</div>
156
+ <div class="body">
157
+ <div part="axis" class="axis" aria-hidden="true">
158
+ ${ticks.map((t) => html `<span class="tick" ${cssom({ bottom: `${pos(t)}%` })}>${format.format(t)}</span>`)}
159
+ </div>
160
+ <div class="columns">
161
+ <div part="plot" class="plot">
162
+ ${ticks.map((t) => html `<span class=${t === 0 ? 'grid zero' : 'grid'} ${cssom({ bottom: `${pos(t)}%` })}></span>`)}
163
+ ${values.map((v, i) => {
164
+ const top = pos(Math.max(v, 0));
165
+ const bottom = pos(Math.min(v, 0));
166
+ return html `
167
+ <div class=${i === this._focused ? 'column focused' : 'column'}>
168
+ <span
169
+ part="bar"
170
+ class=${v < 0 ? 'bar negative' : 'bar'}
171
+ ${cssom({ bottom: `${bottom}%`, height: `${top - bottom}%` })}
172
+ ></span>
173
+ <span class="value" aria-hidden="true" ${cssom(v < 0 ? { top: `${100 - bottom}%` } : { bottom: `${top}%` })}>${format.format(v)}</span>
174
+ </div>
175
+ `;
176
+ })}
177
+ </div>
178
+ <div class="labels">${items.map(() => html `<div class="category"><slot></slot></div>`)}</div>
179
+ </div>
180
+ </div>
181
+ </div>
182
+ `;
183
+ }
184
+ static { this.styles = css `
185
+ :host {
186
+ display: block;
187
+ box-sizing: border-box;
188
+ width: 100%;
189
+ font-family: var(--va-font-family-ui);
190
+ font-size: var(--va-font-size-sm);
191
+ color: var(--va-color-text-default);
192
+ }
193
+ :host([hidden]) {
194
+ display: none;
195
+ }
196
+ .label {
197
+ margin-bottom: var(--va-space-gap-md);
198
+ font-size: var(--va-font-size-md);
199
+ font-weight: var(--va-font-weight-bold);
200
+ }
201
+ .body {
202
+ display: grid;
203
+ grid-template-columns: auto 1fr;
204
+ gap: var(--va-space-gap-md);
205
+ }
206
+ .axis {
207
+ position: relative;
208
+ height: var(--va-bar-chart-size, 12rem);
209
+ min-width: 2ch;
210
+ color: var(--va-color-text-muted);
211
+ font-variant-numeric: tabular-nums;
212
+ }
213
+ .tick {
214
+ position: absolute;
215
+ right: 0;
216
+ transform: translateY(50%);
217
+ line-height: 1;
218
+ white-space: nowrap;
219
+ }
220
+ .columns {
221
+ min-width: 0;
222
+ }
223
+ .plot {
224
+ position: relative;
225
+ display: flex;
226
+ height: var(--va-bar-chart-size, 12rem);
227
+ }
228
+ .grid {
229
+ position: absolute;
230
+ left: 0;
231
+ right: 0;
232
+ border-top: 1px solid var(--va-color-border-subtle);
233
+ }
234
+ .grid.zero {
235
+ border-top-color: var(--va-color-border-control);
236
+ }
237
+ .column {
238
+ position: relative;
239
+ flex: 1;
240
+ min-width: 0;
241
+ }
242
+ .bar {
243
+ position: absolute;
244
+ left: 18%;
245
+ right: 18%;
246
+ border-radius: var(--va-bar-chart-bar-radius, 3px) var(--va-bar-chart-bar-radius, 3px) 0 0;
247
+ background-color: var(--va-bar-chart-bar-bg, var(--va-color-accent-default));
248
+ transition:
249
+ height var(--va-motion-duration-slow) ease-out,
250
+ bottom var(--va-motion-duration-slow) ease-out;
251
+ }
252
+ .bar.negative {
253
+ border-radius: 0 0 var(--va-bar-chart-bar-radius, 3px) var(--va-bar-chart-bar-radius, 3px);
254
+ }
255
+ .column:hover .bar,
256
+ .focused .bar {
257
+ background-color: var(--va-bar-chart-bar-bg, var(--va-color-accent-hover));
258
+ filter: brightness(1.1);
259
+ }
260
+ .value {
261
+ position: absolute;
262
+ left: 0;
263
+ right: 0;
264
+ padding: 2px 0;
265
+ font-variant-numeric: tabular-nums;
266
+ font-weight: var(--va-font-weight-medium);
267
+ text-align: center;
268
+ visibility: hidden;
269
+ }
270
+ .column:hover .value,
271
+ .focused .value {
272
+ visibility: visible;
273
+ }
274
+ .labels {
275
+ display: flex;
276
+ margin-top: var(--va-space-gap-sm);
277
+ }
278
+ .category {
279
+ flex: 1;
280
+ min-width: 0;
281
+ text-align: center;
282
+ }
283
+ ::slotted(*) {
284
+ display: block;
285
+ overflow: hidden;
286
+ padding: 2px;
287
+ border-radius: var(--va-radius-control);
288
+ color: var(--va-color-text-muted);
289
+ text-overflow: ellipsis;
290
+ white-space: nowrap;
291
+ }
292
+ ::slotted(:focus-visible) {
293
+ outline: var(--va-size-focus-ring, 2px) solid var(--va-color-focus);
294
+ outline-offset: 0;
295
+ color: var(--va-color-text-default);
296
+ }
297
+ @media (prefers-reduced-motion: reduce) {
298
+ .bar {
299
+ transition: none;
300
+ }
301
+ }
302
+ @media (forced-colors: active) {
303
+ .bar {
304
+ background-color: CanvasText;
305
+ forced-color-adjust: none;
306
+ }
307
+ .grid {
308
+ border-top-color: GrayText;
309
+ }
310
+ ::slotted(:focus-visible) {
311
+ outline-color: Highlight;
312
+ }
313
+ }
314
+ `; }
315
+ }
316
+ //# sourceMappingURL=bar-chart.js.map