@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
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@firstprateek/vastu",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Vastu web components, built with Lit: accessible, themeable through tokens, and described in a manifest that agents can read",
5
5
  "type": "module",
6
6
  "exports": {
7
+ "./bar-chart": {
8
+ "types": "./dist/dev/bar-chart/index.d.ts",
9
+ "development": "./dist/dev/bar-chart/index.js",
10
+ "default": "./dist/prod/bar-chart/index.js"
11
+ },
7
12
  "./button": {
8
13
  "types": "./dist/dev/button/index.d.ts",
9
14
  "development": "./dist/dev/button/index.js",
@@ -24,6 +29,16 @@
24
29
  "development": "./dist/dev/menu/index.js",
25
30
  "default": "./dist/prod/menu/index.js"
26
31
  },
32
+ "./navbar": {
33
+ "types": "./dist/dev/navbar/index.d.ts",
34
+ "development": "./dist/dev/navbar/index.js",
35
+ "default": "./dist/prod/navbar/index.js"
36
+ },
37
+ "./poll": {
38
+ "types": "./dist/dev/poll/index.d.ts",
39
+ "development": "./dist/dev/poll/index.js",
40
+ "default": "./dist/prod/poll/index.js"
41
+ },
27
42
  "./switch": {
28
43
  "types": "./dist/dev/switch/index.d.ts",
29
44
  "development": "./dist/dev/switch/index.js",
@@ -80,11 +95,11 @@
80
95
  "lit": "^3.3.3"
81
96
  },
82
97
  "peerDependencies": {
83
- "@firstprateek/vastu-tokens": "^1.0.0"
98
+ "@firstprateek/vastu-tokens": "^1.1.0"
84
99
  },
85
100
  "devDependencies": {
86
101
  "@custom-elements-manifest/analyzer": "^0.11.0",
87
- "@firstprateek/vastu-tokens": "^1.0.0",
102
+ "@firstprateek/vastu-tokens": "^1.1.0",
88
103
  "typescript": "~5.9.3"
89
104
  },
90
105
  "bugs": {