@uvalib/web-components 1.1.0 → 1.2.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 (111) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/README.md +563 -47
  3. package/dist/base/url-param.d.ts +23 -0
  4. package/dist/base/url-param.js +49 -0
  5. package/dist/base/url-param.js.map +1 -0
  6. package/dist/base.css +289 -1
  7. package/dist/components/uvalib-card/UvalibCard.d.ts +19 -5
  8. package/dist/components/uvalib-card/UvalibCard.js +112 -9
  9. package/dist/components/uvalib-card/UvalibCard.js.map +1 -1
  10. package/dist/components/uvalib-chip-group/UvalibChipGroup.d.ts +156 -0
  11. package/dist/components/uvalib-chip-group/UvalibChipGroup.js +747 -0
  12. package/dist/components/uvalib-chip-group/UvalibChipGroup.js.map +1 -0
  13. package/dist/components/uvalib-chip-group/uvalib-chip-group.d.ts +1 -0
  14. package/dist/components/uvalib-chip-group/uvalib-chip-group.js +4 -0
  15. package/dist/components/uvalib-chip-group/uvalib-chip-group.js.map +1 -0
  16. package/dist/components/uvalib-date-histogram/UvalibDateHistogram.d.ts +148 -0
  17. package/dist/components/uvalib-date-histogram/UvalibDateHistogram.js +1248 -0
  18. package/dist/components/uvalib-date-histogram/UvalibDateHistogram.js.map +1 -0
  19. package/dist/components/uvalib-date-histogram/uvalib-date-histogram.d.ts +1 -0
  20. package/dist/components/uvalib-date-histogram/uvalib-date-histogram.js +4 -0
  21. package/dist/components/uvalib-date-histogram/uvalib-date-histogram.js.map +1 -0
  22. package/dist/components/uvalib-lightbox/UvalibLightbox.js +8 -1
  23. package/dist/components/uvalib-lightbox/UvalibLightbox.js.map +1 -1
  24. package/dist/components/uvalib-lightbox/media.d.ts +1 -18
  25. package/dist/components/uvalib-lightbox/media.js +1 -42
  26. package/dist/components/uvalib-lightbox/media.js.map +1 -1
  27. package/dist/components/uvalib-pager/UvalibPager.d.ts +58 -4
  28. package/dist/components/uvalib-pager/UvalibPager.js +232 -8
  29. package/dist/components/uvalib-pager/UvalibPager.js.map +1 -1
  30. package/dist/components/uvalib-photo-grid/UvalibPhotoGrid.d.ts +27 -1
  31. package/dist/components/uvalib-photo-grid/UvalibPhotoGrid.js +192 -26
  32. package/dist/components/uvalib-photo-grid/UvalibPhotoGrid.js.map +1 -1
  33. package/dist/components/uvalib-results-count/UvalibResultsCount.d.ts +62 -0
  34. package/dist/components/uvalib-results-count/UvalibResultsCount.js +208 -0
  35. package/dist/components/uvalib-results-count/UvalibResultsCount.js.map +1 -0
  36. package/dist/components/uvalib-results-count/uvalib-results-count.d.ts +1 -0
  37. package/dist/components/uvalib-results-count/uvalib-results-count.js +4 -0
  38. package/dist/components/uvalib-results-count/uvalib-results-count.js.map +1 -0
  39. package/dist/components/uvalib-tabs/UvalibTabs.d.ts +127 -0
  40. package/dist/components/uvalib-tabs/UvalibTabs.js +821 -0
  41. package/dist/components/uvalib-tabs/UvalibTabs.js.map +1 -0
  42. package/dist/components/uvalib-tabs/uvalib-tabs.d.ts +1 -0
  43. package/dist/components/uvalib-tabs/uvalib-tabs.js +4 -0
  44. package/dist/components/uvalib-tabs/uvalib-tabs.js.map +1 -0
  45. package/dist/components/uvalib-tag-list/UvalibTagList.d.ts +89 -0
  46. package/dist/components/uvalib-tag-list/UvalibTagList.js +411 -0
  47. package/dist/components/uvalib-tag-list/UvalibTagList.js.map +1 -0
  48. package/dist/components/uvalib-tag-list/uvalib-tag-list.d.ts +1 -0
  49. package/dist/components/uvalib-tag-list/uvalib-tag-list.js +4 -0
  50. package/dist/components/uvalib-tag-list/uvalib-tag-list.js.map +1 -0
  51. package/dist/custom-elements.json +7140 -2630
  52. package/dist/index.d.ts +8 -0
  53. package/dist/index.js +5 -0
  54. package/dist/index.js.map +1 -1
  55. package/dist/styles/base.js +35 -1
  56. package/dist/styles/base.js.map +1 -1
  57. package/dist/styles/color-scheme.js +4 -0
  58. package/dist/styles/color-scheme.js.map +1 -1
  59. package/dist/styles/controls.js +2 -0
  60. package/dist/styles/controls.js.map +1 -1
  61. package/dist/styles/results.d.ts +1 -0
  62. package/dist/styles/results.js +257 -0
  63. package/dist/styles/results.js.map +1 -0
  64. package/dist/tokens.css +4 -0
  65. package/dist-cdn/base.css +289 -1
  66. package/dist-cdn/chunks/{base-CAFQWnZG.js → base-p5pk0vb2.js} +36 -2
  67. package/dist-cdn/chunks/{base-CAFQWnZG.js.map → base-p5pk0vb2.js.map} +1 -1
  68. package/dist-cdn/chunks/inline-copy-BANQSMIR.js +69 -0
  69. package/dist-cdn/chunks/inline-copy-BANQSMIR.js.map +1 -0
  70. package/dist-cdn/chunks/{site-header-C9V7Wuw2.js → site-header-BlOYvQg1.js} +5 -1
  71. package/dist-cdn/chunks/site-header-BlOYvQg1.js.map +1 -0
  72. package/dist-cdn/chunks/source-children-BHsDwCvL.js +53 -0
  73. package/dist-cdn/chunks/source-children-BHsDwCvL.js.map +1 -0
  74. package/dist-cdn/chunks/url-param-C1sk_F1d.js +28 -0
  75. package/dist-cdn/chunks/url-param-C1sk_F1d.js.map +1 -0
  76. package/dist-cdn/custom-elements.json +7140 -2630
  77. package/dist-cdn/manifest.json +7 -2
  78. package/dist-cdn/site-header.js +1 -1
  79. package/dist-cdn/site-page.js +2 -2
  80. package/dist-cdn/site-style.js +1 -1
  81. package/dist-cdn/skills/uvalib-web-components/SKILL.md +206 -19
  82. package/dist-cdn/skills/uvalib-web-components/reference/components.md +220 -6
  83. package/dist-cdn/tokens.css +4 -0
  84. package/dist-cdn/uvalib-card.js +94 -20
  85. package/dist-cdn/uvalib-card.js.map +1 -1
  86. package/dist-cdn/uvalib-chip-group.js +496 -0
  87. package/dist-cdn/uvalib-chip-group.js.map +1 -0
  88. package/dist-cdn/uvalib-contents.js +2 -1
  89. package/dist-cdn/uvalib-contents.js.map +1 -1
  90. package/dist-cdn/uvalib-date-histogram.js +850 -0
  91. package/dist-cdn/uvalib-date-histogram.js.map +1 -0
  92. package/dist-cdn/uvalib-lightbox.js +26 -48
  93. package/dist-cdn/uvalib-lightbox.js.map +1 -1
  94. package/dist-cdn/uvalib-metadata.js +2 -1
  95. package/dist-cdn/uvalib-metadata.js.map +1 -1
  96. package/dist-cdn/uvalib-pager.js +141 -7
  97. package/dist-cdn/uvalib-pager.js.map +1 -1
  98. package/dist-cdn/uvalib-photo-grid.js +121 -19
  99. package/dist-cdn/uvalib-photo-grid.js.map +1 -1
  100. package/dist-cdn/uvalib-results-count.js +101 -0
  101. package/dist-cdn/uvalib-results-count.js.map +1 -0
  102. package/dist-cdn/uvalib-tabs.js +436 -0
  103. package/dist-cdn/uvalib-tabs.js.map +1 -0
  104. package/dist-cdn/uvalib-tag-list.js +267 -0
  105. package/dist-cdn/uvalib-tag-list.js.map +1 -0
  106. package/package.json +24 -3
  107. package/skills/uvalib-web-components/SKILL.md +206 -19
  108. package/skills/uvalib-web-components/reference/components.md +220 -6
  109. package/dist-cdn/chunks/site-header-C9V7Wuw2.js.map +0 -1
  110. package/dist-cdn/chunks/source-children-RuAec527.js +0 -119
  111. package/dist-cdn/chunks/source-children-RuAec527.js.map +0 -1
@@ -0,0 +1,747 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { css, html, nothing, svg, unsafeCSS } from 'lit';
8
+ import { property, state } from 'lit/decorators.js';
9
+ import { trackEvent } from '../../base/analytics.js';
10
+ import { UvalibElement } from '../../base/UvalibElement.js';
11
+ import { installLightDomStyles } from '../../base/light-dom-styles.js';
12
+ import { normalize, safeHref, SourceChildren } from '../../base/source-children.js';
13
+ import { accessible } from '../../styles/accessible.js';
14
+ import { hrefWithParam, paramFromLocation, usesHashParam, } from '../../base/url-param.js';
15
+ const TAG = 'uvalib-chip-group';
16
+ const MODES = new Set(['single', 'multi', 'links']);
17
+ /**
18
+ * Single-select changes within `settle` ms count as one: one analytics event (none when the
19
+ * visitor ends where they started) and one history entry. Tests may shorten it.
20
+ */
21
+ export const chipGroupTiming = { settle: 1000 };
22
+ let uid = 0;
23
+ /**
24
+ * Kept with `no-style`: the wrapping (or scrolling) row, each chip's target of at least 44px,
25
+ * the radio laid over its chip, the joined segments and the visually hidden text. `host` is
26
+ * `:host` or the tag.
27
+ */
28
+ const structuralRules = (host) => `
29
+ ${host} { display: flow-root; min-width: 0; max-width: 100%; }
30
+ ${host} .chips {
31
+ display: flex;
32
+ flex-wrap: wrap;
33
+ align-items: center;
34
+ gap: 0.25rem 0.5rem;
35
+ min-width: 0;
36
+ }
37
+ ${host} .chips.scroll {
38
+ flex-wrap: nowrap;
39
+ overflow-x: auto;
40
+ overscroll-behavior-x: contain;
41
+ padding: 0.375rem;
42
+ scroll-padding-inline: 2.5rem;
43
+ }
44
+ ${host} .chips.scroll > .chip { flex: none; }
45
+ ${host} .rail { position: relative; min-width: 0; }
46
+ ${host} .chips.segmented {
47
+ flex-wrap: nowrap;
48
+ column-gap: 0;
49
+ overflow-x: auto;
50
+ padding: 0.375rem;
51
+ }
52
+ /* The padding only keeps the focus ring inside the scroll box: the bar stays 44px tall. */
53
+ ${host} .chips.segmented:not(.scroll) { margin-block: -0.375rem; }
54
+ ${host} .chips.segmented:not(.scroll) > .chip { flex: 0 1 auto; min-width: 44px; }
55
+ ${host} .chips.segmented > .chip + .chip { margin-inline-start: -1px; }
56
+ ${host} .chip {
57
+ position: relative;
58
+ display: inline-flex;
59
+ align-items: center;
60
+ box-sizing: border-box;
61
+ min-width: 44px;
62
+ min-height: 44px;
63
+ max-width: 100%;
64
+ margin: 0;
65
+ padding: 0;
66
+ border: 0;
67
+ background: none;
68
+ color: inherit;
69
+ font: inherit;
70
+ text-align: start;
71
+ text-decoration: none;
72
+ cursor: pointer;
73
+ }
74
+ ${host} .chip input {
75
+ position: absolute;
76
+ inset: 0;
77
+ width: 100%;
78
+ height: 100%;
79
+ margin: 0;
80
+ opacity: 0;
81
+ cursor: inherit;
82
+ }
83
+ ${host} .face {
84
+ display: inline-flex;
85
+ flex: 1;
86
+ align-items: center;
87
+ justify-content: center;
88
+ gap: 0.375rem;
89
+ box-sizing: border-box;
90
+ min-width: 0;
91
+ min-height: 2.25rem;
92
+ overflow-wrap: break-word;
93
+ }
94
+ ${host} .text { min-width: 0; }
95
+ ${host} .check { flex: none; width: 1em; height: 1em; }
96
+ ${host} .chip:focus-visible { outline: none; }
97
+ ${host} :is(.chip:focus-visible > .face, .chip input:focus-visible + .face) {
98
+ position: relative;
99
+ z-index: 1;
100
+ outline: 3px solid var(--uva-color-focus, var(--uva-brand-orange, #e57200));
101
+ outline-offset: 2px;
102
+ }
103
+ ${host} :is(.chip:disabled, .chip[aria-disabled='true'], .chip input:disabled) { cursor: not-allowed; }
104
+ ${host} .visually-hidden {
105
+ position: absolute;
106
+ width: 1px;
107
+ height: 1px;
108
+ margin: -1px;
109
+ padding: 0;
110
+ overflow: hidden;
111
+ clip: rect(0, 0, 0, 0);
112
+ white-space: nowrap;
113
+ border: 0;
114
+ }
115
+ ${host}[hidden], ${host} [hidden] { display: none !important; }
116
+ `;
117
+ const shadowStructure = unsafeCSS(structuralRules(':host'));
118
+ const BORDER = 'var(--uva-color-control-border, var(--uva-grey, #808080))';
119
+ const LINK = 'var(--uva-color-link, var(--uva-brand-blue, #232d4b))';
120
+ const ACCENT = 'var(--uva-color-accent-bg, var(--uva-brand-blue, #232d4b))';
121
+ /** The tick on a selected chip, so the state never rests on colour alone. */
122
+ const CHECK = svg `<svg class="check" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M3 8.5l3.2 3.2L13 4.8" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
123
+ /** The visual target of each face: selected (a radio checked or a toggle pressed). */
124
+ const ON = unsafeCSS(".chip[aria-pressed='true'] > .face, .chip input:checked + .face");
125
+ /** Disabled and not selected (a selected disabled chip keeps the selected fill and text). */
126
+ const OFF_DISABLED = unsafeCSS(".chip:disabled:not([aria-pressed='true']) > .face, .chip[aria-disabled='true'] > .face, .chip input:disabled:not(:checked) + .face");
127
+ /** Hover, never on a disabled chip. */
128
+ const HOVER = unsafeCSS(".chip:not(:disabled, [aria-disabled='true']):hover > .face, .chip:hover input:not(:disabled) + .face");
129
+ const FOCUSED = unsafeCSS('.chip:focus-visible > .face, .chip input:focus-visible + .face');
130
+ /**
131
+ * A row of chips: a segmented control or single-select filter (`mode="single"`), filter chips
132
+ * that toggle (`mode="multi"`), or suggestion chips that go somewhere (`mode="links"`), as in
133
+ * a year rail with counts (“2005 213”), a sort toggle (“Most photos | Date | Title A–Z”) or
134
+ * “Try: snow on the Lawn”.
135
+ *
136
+ * Give the chips as children, `<button value="2005" data-count="213">2005</button>` (the
137
+ * value is the `value` attribute, else `data-value`, else the text; `disabled` disables), or
138
+ * `<a href>` in `links` mode, or set the `items` property (`{ value, label, count?, href?,
139
+ * disabled? }`), which wins over the children while set. The chips follow later changes to
140
+ * the children.
141
+ *
142
+ * `single`: native radios drawn as chips, in a `role="radiogroup"` named by `label`. Tab
143
+ * enters the group at the selected chip, the arrow keys move and select (as any radio group),
144
+ * and `value` is the selected chip's value. `multi`: toggle buttons with `aria-pressed`, and
145
+ * `value` is an array (as an attribute, comma-separated). `links`: links, or buttons that fire
146
+ * `select`, with no selected state; a chip whose href is unsafe (`javascript:`) is plain text,
147
+ * and an empty href is a button.
148
+ *
149
+ * A count is shown in a smaller muted span after the label and read as part of the name: “2005,
150
+ * 213 items”, with `count-label` in place of “items”. A selected chip is filled, keeps its
151
+ * border and shows a tick. Chips wrap on a narrow screen; `scroll` keeps them on one row that
152
+ * scrolls sideways, with its scrollbar shown and a fade at an edge with more chips. Each chip
153
+ * is a target at least 44px square. `segmented` joins the chips into one bar that does not
154
+ * wrap (a long label wraps inside its segment), like the header's Light / Dark / System control.
155
+ *
156
+ * `url-param` (for example `year`) keeps the value in the address (`?year=2005`, a multi
157
+ * value comma-separated, inside the hash on a hash route). It is read on connect; the
158
+ * markup's `value` stands for the address without the parameter, and any other value is
159
+ * written, an empty one as `?year=`. Each change is one `pushState`: in `multi` at once, in
160
+ * `single` once the visitor settles (a second after the last change, so arrowing through a
161
+ * year rail adds one entry, none if it ends where it started). Back and Forward are followed
162
+ * and fire `change` with `restored: true`.
163
+ *
164
+ * @tagname uvalib-chip-group
165
+ * @fires change - The visitor changed the selection (`single`, `multi`), or Back or Forward changed it through `url-param`: `detail` is `{ value, restored? }` (a string in `single`, an array in `multi`; `restored` is true for Back and Forward). Not fired when the app sets `value`. Bubbles and is composed.
166
+ * @fires select - `links` mode: a chip was chosen. `detail` is `{ value, href? }`. For a link, a plain primary click fires it cancelable: cancel it (`preventDefault()`) to route in the app, and the click's default is prevented too. Bubbles and is composed.
167
+ * @fires uvalib-analytics-event - For `<site-analytics>`, on the visitor's actions only: `['uvalib-chip-group', 'change', '<label>: <chip label>', <count>?]` (in `single`, one second after the last change, for the chip then selected; in `multi`, for the chip toggled), and `['uvalib-chip-group', 'select', '<label>']` in `links` mode (never the chip or its URL). The count is sent when it is a number. Bubbles and is composed.
168
+ * @cssprop --uva-color-surface-raised - Dark-mode role token: chip background (else #fff).
169
+ * @cssprop --uva-color-control-border - Dark-mode role token: chip border (else `--uva-grey`, #808080).
170
+ * @cssprop --uva-color-link - Dark-mode role token: chip text and hover border (else `--uva-brand-blue`).
171
+ * @cssprop --uva-color-text-muted - Dark-mode role token: the count (else #4f4f4f).
172
+ * @cssprop --uva-color-accent-bg - Dark-mode role token: selected chip background and border (else `--uva-brand-blue`).
173
+ * @cssprop --uva-color-on-accent - Dark-mode role token: selected chip text (else #fff).
174
+ * @cssprop --uva-color-focus - Dark-mode role token: focus ring (else `--uva-brand-orange`).
175
+ * @cssprop --uva-color-surface - Dark-mode role token: the page background the scrolling row fades into at an edge with more chips (else #fff).
176
+ */
177
+ export class UvalibChipGroup extends UvalibElement {
178
+ static { this.styles = [
179
+ accessible,
180
+ shadowStructure,
181
+ css `
182
+ .face {
183
+ padding: 0.25rem 0.875rem;
184
+ border: 1px solid ${unsafeCSS(BORDER)};
185
+ border-radius: 999px;
186
+ background: var(--uva-color-surface-raised, #fff);
187
+ color: ${unsafeCSS(LINK)};
188
+ font-size: 0.875rem;
189
+ font-weight: 600;
190
+ line-height: 1.3;
191
+ }
192
+ .segmented .face {
193
+ padding-inline: 0.625rem;
194
+ border-radius: 0;
195
+ }
196
+ .segmented > .chip:first-child .face {
197
+ border-start-start-radius: 0.3rem;
198
+ border-end-start-radius: 0.3rem;
199
+ }
200
+ .segmented > .chip:last-child .face {
201
+ border-start-end-radius: 0.3rem;
202
+ border-end-end-radius: 0.3rem;
203
+ }
204
+ .count {
205
+ color: var(--uva-color-text-muted, #4f4f4f);
206
+ font-size: 0.8125rem;
207
+ font-weight: 400;
208
+ font-variant-numeric: tabular-nums;
209
+ }
210
+ ${HOVER} {
211
+ position: relative;
212
+ border-color: ${unsafeCSS(LINK)};
213
+ }
214
+ ${ON} {
215
+ position: relative;
216
+ border-color: ${unsafeCSS(ACCENT)};
217
+ background: ${unsafeCSS(ACCENT)};
218
+ color: var(--uva-color-on-accent, #fff);
219
+ }
220
+ .chip[aria-pressed='true'] .count,
221
+ .chip input:checked + .face .count {
222
+ color: inherit;
223
+ }
224
+ ${OFF_DISABLED} {
225
+ border-style: dashed;
226
+ color: var(--uva-color-text-muted, #4f4f4f);
227
+ }
228
+ /* One row that scrolls: the scrollbar stays visible, and the edge with more chips
229
+ fades out. */
230
+ .chips.scroll {
231
+ scrollbar-width: thin;
232
+ scrollbar-color: ${unsafeCSS(BORDER)} transparent;
233
+ }
234
+ .rail::before,
235
+ .rail::after {
236
+ content: '';
237
+ position: absolute;
238
+ inset-block: 0 0.75rem;
239
+ z-index: 2;
240
+ width: 2.5rem;
241
+ pointer-events: none;
242
+ opacity: 0;
243
+ }
244
+ .rail::before {
245
+ left: 0;
246
+ background: linear-gradient(to right, var(--uva-color-surface, #fff), transparent);
247
+ }
248
+ .rail::after {
249
+ right: 0;
250
+ background: linear-gradient(to left, var(--uva-color-surface, #fff), transparent);
251
+ }
252
+ .rail[data-more-left]::before,
253
+ .rail[data-more-right]::after {
254
+ opacity: 1;
255
+ }
256
+ @media (forced-colors: active) {
257
+ .face {
258
+ border-color: ButtonText;
259
+ color: ButtonText;
260
+ }
261
+ a.chip > .face {
262
+ border-color: LinkText;
263
+ color: LinkText;
264
+ }
265
+ ${HOVER} {
266
+ border-color: Highlight;
267
+ }
268
+ ${ON} {
269
+ forced-color-adjust: none;
270
+ border-color: Highlight;
271
+ background: Highlight;
272
+ color: HighlightText;
273
+ }
274
+ ${OFF_DISABLED} {
275
+ border-color: GrayText;
276
+ color: GrayText;
277
+ }
278
+ ${FOCUSED} {
279
+ outline-color: Highlight;
280
+ }
281
+ /* Forced colors keep gradients: a light fade would cover the edge chips. */
282
+ .rail::before,
283
+ .rail::after {
284
+ display: none;
285
+ }
286
+ }
287
+ `,
288
+ ]; }
289
+ constructor() {
290
+ super();
291
+ /** `single` (radios: one value), `multi` (toggles: an array of values) or `links`. */
292
+ this.mode = 'single';
293
+ /** The group's accessible name (required), also the first part of the analytics name. */
294
+ this.label = '';
295
+ /**
296
+ * The selection: a chip's value in `single`, an array of values in `multi` (as an
297
+ * attribute, comma-separated). Setting it does not fire `change`. Unused in `links`.
298
+ */
299
+ this.value = '';
300
+ /** The word after a count in each chip's accessible name: “2005, 213 items”. */
301
+ this.countLabel = 'items';
302
+ /** One row that scrolls sideways instead of wrapping. */
303
+ this.scrollRow = false;
304
+ /** Join the chips into one segmented bar. */
305
+ this.segmented = false;
306
+ /** Query parameter that keeps the value in the address, for example `year` → `?year=2005`. */
307
+ this.urlParam = '';
308
+ this.childItems = [];
309
+ /** `scroll`: whether more chips lie beyond the left and right edges. */
310
+ this.more = { left: false, right: false };
311
+ this.uid = `${TAG}-${++uid}`;
312
+ this.warned = new Set();
313
+ this.onPop = () => {
314
+ if (!this.urlParam || this.kind === 'links')
315
+ return;
316
+ this.cancel();
317
+ this.initial ??= Array.isArray(this.value) ? [...this.value] : this.value;
318
+ const v = this.urlValue() ?? this.initial ?? '';
319
+ const before = this.selected.join('\u0000');
320
+ this.value = Array.isArray(v) ? [...v] : v;
321
+ if (this.selected.join('\u0000') === before)
322
+ return;
323
+ this.emitChange(true);
324
+ };
325
+ /** `scroll`: which edges have more chips beyond them (in either writing direction). */
326
+ this.measure = () => {
327
+ const g = this.renderRoot.querySelector('.chips.scroll');
328
+ if (!g)
329
+ return;
330
+ const max = g.scrollWidth - g.clientWidth;
331
+ const x = Math.abs(g.scrollLeft);
332
+ const rtl = getComputedStyle(g).direction === 'rtl';
333
+ const before = x > 1;
334
+ const after = x < max - 1;
335
+ const more = { left: rtl ? after : before, right: rtl ? before : after };
336
+ if (more.left !== this.more.left || more.right !== this.more.right)
337
+ this.more = more;
338
+ };
339
+ new SourceChildren(this, () => this.lightDom, (nodes) => this.read(nodes));
340
+ }
341
+ createRenderRoot() {
342
+ const root = super.createRenderRoot();
343
+ if (root instanceof ShadowRoot && this.noStyle) {
344
+ const sheet = shadowStructure.styleSheet;
345
+ if (sheet)
346
+ root.adoptedStyleSheets = [sheet];
347
+ }
348
+ return root;
349
+ }
350
+ connectedCallback() {
351
+ super.connectedCallback();
352
+ if (this.lightDom && this.noStyle) {
353
+ const node = this.getRootNode();
354
+ installLightDomStyles(`${this.localName}::structure`, unsafeCSS(structuralRules(this.localName)), {
355
+ root: node instanceof ShadowRoot || node instanceof Document ? node : undefined,
356
+ scope: false,
357
+ });
358
+ }
359
+ window.addEventListener('popstate', this.onPop);
360
+ // Moved in the page: observe the scrolling row again.
361
+ if (this.hasUpdated)
362
+ this.requestUpdate();
363
+ }
364
+ disconnectedCallback() {
365
+ super.disconnectedCallback();
366
+ window.removeEventListener('popstate', this.onPop);
367
+ // Detached, it could not report through the page (nor honour an ancestor's
368
+ // no-analytics), and its address may belong to another route by now: drop it.
369
+ this.cancel();
370
+ this.resize?.disconnect();
371
+ this.observed = undefined;
372
+ }
373
+ willUpdate(changed) {
374
+ if (changed.has('urlParam') && this.urlParam)
375
+ this.readUrl();
376
+ if (changed.has('value') && this.value !== this.ownValue) {
377
+ // Set by the app (or read from the address): a pending change is no longer the visitor's.
378
+ this.cancel();
379
+ if (this.kind === 'multi' && !Array.isArray(this.value))
380
+ this.value = this.selected;
381
+ }
382
+ if (!this.label.trim())
383
+ this.warnOnce('label', 'set label: the group needs an accessible name.');
384
+ if (!MODES.has(this.mode))
385
+ this.warnOnce('mode', 'mode must be single, multi or links.');
386
+ }
387
+ warnOnce(kind, message) {
388
+ if (this.warned.has(kind))
389
+ return;
390
+ this.warned.add(kind);
391
+ console.warn(`${this.localName}: ${message}`);
392
+ }
393
+ get kind() {
394
+ return MODES.has(this.mode) ? this.mode : 'single';
395
+ }
396
+ /** Read the chips from the children: each element with text is one chip. */
397
+ read(nodes) {
398
+ const items = [];
399
+ for (const n of nodes) {
400
+ if (!(n instanceof Element))
401
+ continue;
402
+ const label = normalize(n.textContent);
403
+ if (!label) {
404
+ this.warnOnce('empty', 'ignoring chips with no text.');
405
+ continue;
406
+ }
407
+ const item = {
408
+ value: n.getAttribute('value') ?? n.getAttribute('data-value') ?? label,
409
+ label,
410
+ };
411
+ const count = n.getAttribute('data-count');
412
+ if (count !== null && count.trim())
413
+ item.count = count.trim();
414
+ if (n instanceof HTMLAnchorElement && n.hasAttribute('href'))
415
+ item.href = n.getAttribute('href');
416
+ if (n.hasAttribute('disabled') || n.getAttribute('aria-disabled') === 'true')
417
+ item.disabled = true;
418
+ items.push(item);
419
+ }
420
+ // Only a real change re-renders.
421
+ if (JSON.stringify(items) !== JSON.stringify(this.childItems))
422
+ this.childItems = items;
423
+ }
424
+ /** What is shown: `items` while set, else the children; entries without a label left out. */
425
+ get chips() {
426
+ const source = this.items ?? this.childItems;
427
+ return source
428
+ .filter((i) => i && normalize(i.label))
429
+ .map((i) => ({
430
+ ...i,
431
+ label: normalize(i.label),
432
+ value: String(i.value ?? normalize(i.label)),
433
+ }));
434
+ }
435
+ /** The selected values, whatever shape `value` has. */
436
+ get selected() {
437
+ const v = this.value;
438
+ if (Array.isArray(v))
439
+ return v.map(String);
440
+ if (v === null || v === undefined)
441
+ return [];
442
+ const s = String(v);
443
+ if (this.kind === 'multi')
444
+ return s
445
+ .split(',')
446
+ .map((x) => x.trim())
447
+ .filter(Boolean);
448
+ return [s];
449
+ }
450
+ get locale() {
451
+ return this.closest('[lang]')?.getAttribute('lang') || 'en';
452
+ }
453
+ formatCount(count) {
454
+ const n = typeof count === 'number' ? count : Number(String(count).replace(/,/g, ''));
455
+ if (typeof count === 'string' && !/^\s*[\d,]+(\.\d+)?\s*$/.test(count))
456
+ return count.trim();
457
+ if (!Number.isFinite(n))
458
+ return String(count);
459
+ try {
460
+ return n.toLocaleString(this.locale);
461
+ }
462
+ catch {
463
+ return n.toLocaleString('en');
464
+ }
465
+ }
466
+ numericCount(count) {
467
+ if (count === undefined)
468
+ return undefined;
469
+ if (typeof count === 'number')
470
+ return Number.isFinite(count) ? count : undefined;
471
+ // '' and ',' are not 0.
472
+ if (!/^\s*\d[\d,]*(\.\d+)?\s*$/.test(count))
473
+ return undefined;
474
+ return Number(count.replace(/,/g, ''));
475
+ }
476
+ /* ---------- the address ---------- */
477
+ loc() {
478
+ return { pathname: location.pathname, search: location.search, hash: location.hash };
479
+ }
480
+ urlValue() {
481
+ const loc = this.loc();
482
+ // A hash route keeps its parameter in the hash, even once the hash has no query left.
483
+ const raw = this.hashMode && !usesHashParam(loc.hash) ? null : paramFromLocation(loc, this.urlParam);
484
+ if (raw === null)
485
+ return null;
486
+ return this.kind === 'multi'
487
+ ? raw
488
+ .split(',')
489
+ .map((x) => x.trim())
490
+ .filter(Boolean)
491
+ : raw;
492
+ }
493
+ /** On connect (and a new `url-param`): the address wins over the markup, quietly. */
494
+ readUrl() {
495
+ this.initial ??= Array.isArray(this.value) ? [...this.value] : this.value;
496
+ this.hashMode ??= usesHashParam(location.hash);
497
+ const v = this.urlValue();
498
+ if (v !== null)
499
+ this.value = v;
500
+ }
501
+ /** The value as it is written in the address. */
502
+ encode(v) {
503
+ if (Array.isArray(v))
504
+ return v.join(',');
505
+ if (this.kind === 'multi')
506
+ return String(v ?? '')
507
+ .split(',')
508
+ .map((x) => x.trim())
509
+ .filter(Boolean)
510
+ .join(',');
511
+ return String(v ?? '');
512
+ }
513
+ /**
514
+ * Push the value into the address. The markup's value is the address without the
515
+ * parameter; any other value is written, an empty one as `?year=`.
516
+ */
517
+ writeUrl() {
518
+ if (!this.urlParam)
519
+ return;
520
+ this.initial ??= '';
521
+ this.hashMode ??= usesHashParam(location.hash);
522
+ const value = this.encode(this.selected);
523
+ const id = value === this.encode(this.initial) ? null : value;
524
+ const next = hrefWithParam(this.loc(), this.urlParam, id, this.hashMode, true);
525
+ const now = location.pathname + location.search + location.hash;
526
+ if (next === now)
527
+ return;
528
+ try {
529
+ history.pushState(history.state, '', next);
530
+ }
531
+ catch {
532
+ // A sandboxed document (an iframe srcdoc) cannot change its address.
533
+ }
534
+ }
535
+ /* ---------- events ---------- */
536
+ emitChange(restored = false) {
537
+ const value = this.kind === 'multi' ? this.selected : (this.selected[0] ?? '');
538
+ const detail = { value };
539
+ if (restored)
540
+ detail.restored = true;
541
+ this.dispatchEvent(new CustomEvent('change', { detail, bubbles: true, composed: true }));
542
+ }
543
+ send(item) {
544
+ const name = `${this.label.trim()}: ${item.label}`;
545
+ const count = this.numericCount(item.count);
546
+ trackEvent(this, count === undefined ? [TAG, 'change', name] : [TAG, 'change', name, count]);
547
+ }
548
+ /**
549
+ * Multi: report the toggled chip and write the address now. Single: once changes settle
550
+ * (arrowing through a year rail), report the chip then selected and push one history entry,
551
+ * unless the visitor ended where they started.
552
+ */
553
+ track(item, from) {
554
+ if (this.kind === 'multi') {
555
+ this.writeUrl();
556
+ this.send(item);
557
+ return;
558
+ }
559
+ this.pending = { from: this.pending?.from ?? from, item };
560
+ clearTimeout(this.timer);
561
+ this.timer = setTimeout(() => this.flush(), chipGroupTiming.settle);
562
+ }
563
+ flush() {
564
+ const p = this.pending;
565
+ this.cancel();
566
+ if (!p || p.item.value === p.from || this.selected[0] !== p.item.value)
567
+ return;
568
+ this.writeUrl();
569
+ this.send(p.item);
570
+ }
571
+ /** Forget a single-select change that has not settled, without reporting it. */
572
+ cancel() {
573
+ clearTimeout(this.timer);
574
+ this.timer = undefined;
575
+ this.pending = undefined;
576
+ }
577
+ onRadio(e, item) {
578
+ // The radio's own change event must not reach the page as this element's `change` (in
579
+ // light DOM it would bubble through the host).
580
+ e.stopPropagation();
581
+ const input = e.target;
582
+ if (!input.checked)
583
+ return;
584
+ const from = this.selected[0] ?? '';
585
+ this.value = this.ownValue = item.value;
586
+ this.track(item, from);
587
+ this.emitChange();
588
+ }
589
+ onToggle(item) {
590
+ const now = this.selected;
591
+ const on = now.includes(item.value);
592
+ this.value = this.ownValue = on ? now.filter((v) => v !== item.value) : [...now, item.value];
593
+ this.track(item, '');
594
+ this.emitChange();
595
+ }
596
+ onSelect(e, item, link) {
597
+ if (e.defaultPrevented || e.button !== 0)
598
+ return;
599
+ trackEvent(this, [TAG, 'select', this.label.trim()]);
600
+ if (link && (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey))
601
+ return;
602
+ const detail = { value: item.value };
603
+ if (link && item.href !== undefined)
604
+ detail.href = item.href;
605
+ const go = new CustomEvent('select', {
606
+ detail,
607
+ bubbles: true,
608
+ composed: true,
609
+ cancelable: link,
610
+ });
611
+ if (!this.dispatchEvent(go) && link)
612
+ e.preventDefault();
613
+ }
614
+ /* ---------- render ---------- */
615
+ face(item, on) {
616
+ const count = item.count === undefined ? '' : this.formatCount(item.count);
617
+ return html `<span class="face"
618
+ >${on ? CHECK : nothing}<span class="text">${item.label}</span>${count
619
+ ? html `<span class="visually-hidden">, </span><span class="count">${count}</span
620
+ ><span class="visually-hidden"> ${this.countLabel}</span>`
621
+ : nothing}</span
622
+ >`;
623
+ }
624
+ renderChip(item) {
625
+ const kind = this.kind;
626
+ if (kind === 'single') {
627
+ const on = this.selected[0] === item.value;
628
+ return html `<label class="chip"
629
+ ><input
630
+ type="radio"
631
+ name=${this.uid}
632
+ .value=${item.value}
633
+ .checked=${on}
634
+ ?disabled=${!!item.disabled}
635
+ @change=${(e) => this.onRadio(e, item)}
636
+ />${this.face(item, on)}</label
637
+ >`;
638
+ }
639
+ if (kind === 'multi') {
640
+ const on = this.selected.includes(item.value);
641
+ return html `<button
642
+ type="button"
643
+ class="chip"
644
+ aria-pressed=${on ? 'true' : 'false'}
645
+ ?disabled=${!!item.disabled}
646
+ @click=${() => this.onToggle(item)}
647
+ >
648
+ ${this.face(item, on)}
649
+ </button>`;
650
+ }
651
+ if (item.disabled)
652
+ return html `<span class="chip" role="link" aria-disabled="true"
653
+ >${this.face(item, false)}</span
654
+ >`;
655
+ const href = item.href?.trim();
656
+ if (href) {
657
+ if (safeHref(href, this.ownerDocument.baseURI))
658
+ return html `<a
659
+ class="chip"
660
+ href=${item.href}
661
+ @click=${(e) => this.onSelect(e, item, true)}
662
+ >${this.face(item, false)}</a
663
+ >`;
664
+ this.warnOnce('href', 'showing a chip as text: its href is not http(s), mailto, tel or relative.');
665
+ return html `<span class="chip">${this.face(item, false)}</span>`;
666
+ }
667
+ return html `<button
668
+ type="button"
669
+ class="chip"
670
+ @click=${(e) => this.onSelect(e, item, false)}
671
+ >
672
+ ${this.face(item, false)}
673
+ </button>`;
674
+ }
675
+ updated(changed) {
676
+ super.updated(changed);
677
+ const g = this.renderRoot.querySelector('.chips.scroll') ?? undefined;
678
+ if (g !== this.observed) {
679
+ this.resize?.disconnect();
680
+ this.observed = g;
681
+ if (g) {
682
+ this.resize ??= new ResizeObserver(() => this.measure());
683
+ this.resize.observe(g);
684
+ }
685
+ }
686
+ // New or changed chips can widen the row without resizing it.
687
+ if (g)
688
+ requestAnimationFrame(this.measure);
689
+ }
690
+ render() {
691
+ const chips = this.chips;
692
+ const classes = ['chips', this.scrollRow ? 'scroll' : '', this.segmented ? 'segmented' : '']
693
+ .filter(Boolean)
694
+ .join(' ');
695
+ const row = html `<div
696
+ class=${classes}
697
+ role=${this.kind === 'single' ? 'radiogroup' : 'group'}
698
+ aria-label=${this.label.trim() || nothing}
699
+ ?hidden=${!chips.length}
700
+ @scroll=${this.scrollRow ? this.measure : nothing}
701
+ >
702
+ ${chips.map((item) => this.renderChip(item))}
703
+ </div>`;
704
+ if (!this.scrollRow)
705
+ return row;
706
+ return html `<div
707
+ class="rail"
708
+ ?data-more-left=${this.more.left}
709
+ ?data-more-right=${this.more.right}
710
+ >
711
+ ${row}
712
+ </div>`;
713
+ }
714
+ }
715
+ __decorate([
716
+ property({ type: String, reflect: true })
717
+ ], UvalibChipGroup.prototype, "mode", void 0);
718
+ __decorate([
719
+ property({ type: String })
720
+ ], UvalibChipGroup.prototype, "label", void 0);
721
+ __decorate([
722
+ property({
723
+ converter: { fromAttribute: (v) => v ?? '' },
724
+ })
725
+ ], UvalibChipGroup.prototype, "value", void 0);
726
+ __decorate([
727
+ property({ attribute: false })
728
+ ], UvalibChipGroup.prototype, "items", void 0);
729
+ __decorate([
730
+ property({ type: String, attribute: 'count-label' })
731
+ ], UvalibChipGroup.prototype, "countLabel", void 0);
732
+ __decorate([
733
+ property({ type: Boolean, reflect: true, attribute: 'scroll' })
734
+ ], UvalibChipGroup.prototype, "scrollRow", void 0);
735
+ __decorate([
736
+ property({ type: Boolean, reflect: true })
737
+ ], UvalibChipGroup.prototype, "segmented", void 0);
738
+ __decorate([
739
+ property({ type: String, attribute: 'url-param' })
740
+ ], UvalibChipGroup.prototype, "urlParam", void 0);
741
+ __decorate([
742
+ state()
743
+ ], UvalibChipGroup.prototype, "childItems", void 0);
744
+ __decorate([
745
+ state()
746
+ ], UvalibChipGroup.prototype, "more", void 0);
747
+ //# sourceMappingURL=UvalibChipGroup.js.map