@ismailnakkar/ui-behaviours 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import {closestTo, once} from './helpers.js';
1
+ import {closestTo, isCheckbox, isRadio, once} from './helpers.js';
2
+ import {installSteps} from './steps.js';
2
3
 
3
4
  const COPY_FLASH_MS = 1500;
4
5
 
@@ -9,6 +10,9 @@ const SUBMITTING = 'data-submitting';
9
10
  /** closedby's light-dismiss value, polyfilled where the browser lacks it. */
10
11
  const CLOSED_BY_ANY = 'any';
11
12
 
13
+ /** Between a field's name and its value in data-show-if: "method=paypal". */
14
+ const SHOW_IF_SEPARATOR = '=';
15
+
12
16
  const copyTimers = new WeakMap();
13
17
 
14
18
  /** The dialog a press landed on outside its box: a selection dragged out of the body must not close it. */
@@ -20,6 +24,10 @@ export const installUiBehaviours = once(() => {
20
24
  document.addEventListener('submit', submitOnce);
21
25
  window.addEventListener('pageshow', releaseSubmitted);
22
26
  document.addEventListener('change', checkAll);
27
+ document.addEventListener('change', showOnChange);
28
+ document.addEventListener('change', clearOnPick);
29
+ // Last: the form goes as the other behaviours leave it.
30
+ document.addEventListener('change', submitOnChange);
23
31
  document.addEventListener('click', autoselect);
24
32
  document.addEventListener('click', copyOnClick);
25
33
  document.addEventListener('click', showModal);
@@ -27,6 +35,8 @@ export const installUiBehaviours = once(() => {
27
35
  document.addEventListener('pointerdown', pressBackdrop);
28
36
  document.addEventListener('click', closeOnBackdrop);
29
37
 
38
+ // First: a stepper dialog opened on load shows its step before showModal() moves focus in.
39
+ installSteps();
30
40
  openOnLoad();
31
41
  });
32
42
 
@@ -87,12 +97,19 @@ function releaseSubmitted({persisted}) {
87
97
  }
88
98
  }
89
99
 
100
+ // requestSubmit(), not submit(): the browser checks the fields, and data-confirm and data-submit-once apply.
101
+ function submitOnChange({target}) {
102
+ if (target instanceof Element && target.hasAttribute('data-submit-on-change')) {
103
+ target.form?.requestSubmit();
104
+ }
105
+ }
106
+
90
107
  function checkboxesNamed(name) {
91
- return [...document.getElementsByName(name)].filter((box) => box.type === 'checkbox' && !box.disabled);
108
+ return [...document.getElementsByName(name)].filter((box) => isCheckbox(box) && !box.disabled);
92
109
  }
93
110
 
94
111
  function checkAll({target}) {
95
- if (!(target instanceof HTMLInputElement) || target.type !== 'checkbox') {
112
+ if (!isCheckbox(target)) {
96
113
  return;
97
114
  }
98
115
 
@@ -117,6 +134,56 @@ function checkAll({target}) {
117
134
  }
118
135
  }
119
136
 
137
+ /**
138
+ * A radio picked, a box ticked or a select changed shows its form's data-show-if="name=value" elements while it holds
139
+ * that value and hides them once it does not.
140
+ */
141
+ function showOnChange({target}) {
142
+ if (!isRadio(target) && !isCheckbox(target) && !(target instanceof HTMLSelectElement)) {
143
+ return;
144
+ }
145
+
146
+ for (const element of (target.form ?? document).querySelectorAll('[data-show-if]')) {
147
+ const condition = showIfOf(element);
148
+
149
+ if (condition?.name === target.name && speaksFor(target, condition.value)) {
150
+ element.hidden = !holds(target, condition.value);
151
+ }
152
+ }
153
+ }
154
+
155
+ /** A box speaks for its own value alone, as other boxes of its name keep theirs; a radio or select for every value. */
156
+ function speaksFor(field, value) {
157
+ return !isCheckbox(field) || field.value === value;
158
+ }
159
+
160
+ // options, not selectedOptions: happy-dom's list goes stale after its first read, so app tests would see an old pick.
161
+ function holds(field, value) {
162
+ return field instanceof HTMLSelectElement
163
+ ? [...field.options].some((option) => option.selected && option.value === value)
164
+ : field.checked && field.value === value;
165
+ }
166
+
167
+ /** A radio picked empties its form's fields marked data-clear-on="name", which belong to the value picked before. */
168
+ function clearOnPick({target}) {
169
+ if (!isRadio(target)) {
170
+ return;
171
+ }
172
+
173
+ for (const field of (target.form ?? document).querySelectorAll('[data-clear-on]')) {
174
+ if (field.dataset.clearOn === target.name) {
175
+ field.value = '';
176
+ }
177
+ }
178
+ }
179
+
180
+ function showIfOf(element) {
181
+ const condition = element.dataset.showIf;
182
+ const at = condition.indexOf(SHOW_IF_SEPARATOR);
183
+
184
+ return at > 0 ? {name: condition.slice(0, at), value: condition.slice(at + 1)} : null;
185
+ }
186
+
120
187
  // Tabbing in selects a field's text natively; a click only places a caret.
121
188
  function autoselect({target}) {
122
189
  if (isField(target) && target.matches('[data-autoselect]')) {
@@ -145,25 +212,44 @@ async function copyOnClick(event) {
145
212
  return;
146
213
  }
147
214
 
215
+ const status = button.dataset.copyStatus ? document.getElementById(button.dataset.copyStatus) : null;
216
+
217
+ // Emptied first, so the same words said again are read out again.
218
+ status?.replaceChildren();
219
+
148
220
  const copied = await copyText(copiedTextOf(source));
149
221
 
150
- flashCopyResult(button, copied);
222
+ flashCopyResult(button, copied, status);
151
223
 
152
224
  if (!copied) {
153
225
  selectForManualCopy(source);
154
226
  }
155
227
  }
156
228
 
157
- function flashCopyResult(button, copied) {
229
+ function flashCopyResult(button, copied, status) {
230
+ const said = status ? sayCopyResult(status, copied) : null;
231
+
158
232
  clearTimeout(copyTimers.get(button));
159
233
  button.toggleAttribute(COPIED, copied);
160
234
  button.toggleAttribute(COPY_FAILED, !copied);
161
235
  copyTimers.set(button, setTimeout(() => {
162
236
  button.removeAttribute(COPIED);
163
237
  button.removeAttribute(COPY_FAILED);
238
+
239
+ // Its own words only: the page may have said something else there since.
240
+ if (status?.textContent === said) {
241
+ status.textContent = '';
242
+ }
164
243
  }, COPY_FLASH_MS));
165
244
  }
166
245
 
246
+ /** Writes the live region's words for the result, its data-copied-text or data-copy-failed-text, and returns them. */
247
+ function sayCopyResult(status, copied) {
248
+ status.textContent = (copied ? status.dataset.copiedText : status.dataset.copyFailedText) ?? '';
249
+
250
+ return status.textContent;
251
+ }
252
+
167
253
  // select() alone may leave the field unfocused; focused, Ctrl/Cmd+C copies it by hand.
168
254
  function selectForManualCopy(source) {
169
255
  if (isField(source)) {
@@ -0,0 +1,410 @@
1
+ /*
2
+ * The range picker's look (README, "Styling the picker"): its button, the popover with the presets beside the calendar,
3
+ * and Cally's parts. Every app value is a custom property; the defaults sit in :where(:root), so any rule of the app's
4
+ * wins. The properties left out here default, where they are used, to the one named beside them in the README.
5
+ *
6
+ * Light colours only: Lightning CSS (Vite's, Tailwind v4's) lowers light-dark() to variables it sets only where the
7
+ * same stylesheet declares color-scheme, so a pair here breaks in an app's build. A dark page sets its own.
8
+ */
9
+ :where(:root) {
10
+ --range-picker-text-color: #1c2130;
11
+ --range-picker-muted-color: #687086;
12
+ --range-picker-background: #fff;
13
+ --range-picker-hover-background: #f4f6fb;
14
+ --range-picker-border-color: #e6e8ef;
15
+ --range-picker-control-border-color: #cfd4df;
16
+ --range-picker-accent-color: #3d5afe;
17
+ --range-picker-accent-hover-color: #2f48d4;
18
+ --range-picker-on-accent-color: #fff;
19
+ --range-picker-selected-background: #eef1ff;
20
+ --range-picker-band-color: #2f48d4;
21
+ --range-picker-disabled-color: #aeb5c6;
22
+ --range-picker-radius: 0.75rem;
23
+ --range-picker-control-radius: 0.5rem;
24
+ --range-picker-shadow: 0 16px 40px -8px rgb(16 24 40 / 0.22);
25
+ --range-picker-font-size: 0.875rem;
26
+ --range-picker-padding: 1.25rem;
27
+ --range-picker-window-gap: 0.75rem;
28
+ --range-picker-offset: 0.375rem;
29
+ --range-picker-day-size: 2.5rem;
30
+ --range-picker-preset-height: 2.5rem;
31
+ --range-picker-position-try-order: most-height;
32
+ }
33
+
34
+ /* The button: the app draws the control; this lays out its icon, name and days. */
35
+ .range-picker-button {
36
+ display: flex;
37
+ align-items: center;
38
+ gap: 0.625rem;
39
+ inline-size: 100%;
40
+ text-align: start;
41
+ /* ponytail: one name per page, so a second picker anchors to the last button; anchor-scope if a page needs two. */
42
+ anchor-name: --range-picker;
43
+ }
44
+
45
+ /* Its popover is a later sibling: the button's parent holds both. */
46
+ .range-picker-button:has(~ [data-range-picker]:popover-open) {
47
+ border-color: var(--range-picker-accent-color);
48
+ background-color: var(--range-picker-hover-background);
49
+ }
50
+
51
+ .range-picker-button > [data-range-name] {
52
+ font-weight: 600;
53
+ white-space: nowrap;
54
+ }
55
+
56
+ .range-picker-button > [data-range-dates] {
57
+ min-inline-size: 0;
58
+ overflow: hidden;
59
+ color: var(--range-picker-muted-color);
60
+ font-size: 0.8125rem;
61
+ text-overflow: ellipsis;
62
+ white-space: nowrap;
63
+ }
64
+
65
+ /* A custom range's button names its days alone. */
66
+ .range-picker-button[data-range-state="custom"] > [data-range-dates] {
67
+ color: var(--range-picker-text-color);
68
+ font-size: inherit;
69
+ font-weight: 600;
70
+ }
71
+
72
+ /* The words for each data-range-state, in the app's markup. */
73
+ [data-range-state] .when-pending,
74
+ [data-range-state="custom"] .when-preset,
75
+ [data-range-state="pending"] .when-preset,
76
+ [data-range-state="custom"] > [data-range-name] {
77
+ display: none;
78
+ }
79
+
80
+ [data-range-state="pending"] .when-pending {
81
+ display: inline;
82
+ }
83
+
84
+ /*
85
+ * The popover, never given a display (that would show it closed): centred, as the browser places one, and under its
86
+ * button on a wide window (below). margin: auto puts back the browser's centring, which a reset zeroes.
87
+ */
88
+ [data-range-picker] {
89
+ margin: auto;
90
+ width: max-content;
91
+ max-width: calc(100vw - 2 * var(--range-picker-window-gap));
92
+ max-height: calc(100dvh - 2 * var(--range-picker-window-gap));
93
+ padding: var(--range-picker-padding);
94
+ border: 1px solid var(--range-picker-border-color);
95
+ border-radius: var(--range-picker-radius);
96
+ background: var(--range-picker-background);
97
+ box-shadow: var(--range-picker-shadow);
98
+ color: var(--range-picker-text-color);
99
+ font-size: var(--range-picker-font-size);
100
+ }
101
+
102
+ .range-picker-panel {
103
+ display: grid;
104
+ grid-template-columns: 12rem auto;
105
+ gap: 0 1.5rem;
106
+ }
107
+
108
+ .range-picker-presets {
109
+ display: grid;
110
+ align-content: start;
111
+ gap: 2px;
112
+ padding-inline-end: 1.25rem;
113
+ border-inline-end: 1px solid var(--range-picker-rule-color, var(--range-picker-border-color));
114
+ }
115
+
116
+ .range-picker-preset {
117
+ position: relative;
118
+ display: flex;
119
+ align-items: center;
120
+ gap: 0.625rem;
121
+ min-block-size: var(--range-picker-preset-height);
122
+ margin: 0;
123
+ padding-inline: 0.75rem;
124
+ border-radius: var(--range-picker-control-radius);
125
+ color: var(--range-picker-preset-color, var(--range-picker-muted-color));
126
+ font-weight: 500;
127
+ white-space: nowrap;
128
+ cursor: pointer;
129
+ }
130
+
131
+ .range-picker-preset:hover {
132
+ background: var(--range-picker-preset-hover-background, var(--range-picker-hover-background));
133
+ color: var(--range-picker-preset-hover-color, var(--range-picker-text-color));
134
+ }
135
+
136
+ .range-picker-preset input {
137
+ position: absolute;
138
+ opacity: 0;
139
+ pointer-events: none;
140
+ }
141
+
142
+ .range-picker-preset:has(:focus-visible) {
143
+ outline: 2px solid var(--range-picker-accent-color);
144
+ outline-offset: -2px;
145
+ }
146
+
147
+ /*
148
+ * The picked preset; while only the start day is picked, Custom range, which the calendar is making, though the checked
149
+ * radio stays the range the form sends.
150
+ */
151
+ [data-range-picker]:not([data-range-state="pending"]) .range-picker-preset:has(:checked),
152
+ [data-range-picker][data-range-state="pending"] .range-picker-preset:has([data-range-custom]) {
153
+ background: var(--range-picker-selected-background);
154
+ color: var(--range-picker-accent-color);
155
+ font-weight: 600;
156
+ }
157
+
158
+ /*
159
+ * Cally's parts, styled from outside: square ends joined by a tinted band, today in bold, and focus an outline only
160
+ * (its accent, which fills a focused day, made transparent).
161
+ */
162
+ [data-range-picker] calendar-range {
163
+ display: block;
164
+ }
165
+
166
+ [data-range-picker] calendar-range::part(container) {
167
+ gap: 0.75rem;
168
+ }
169
+
170
+ [data-range-picker] calendar-range::part(button) {
171
+ display: grid;
172
+ place-items: center;
173
+ inline-size: 2.25rem;
174
+ block-size: 2.25rem;
175
+ border: 1px solid var(--range-picker-control-border-color);
176
+ border-radius: var(--range-picker-control-radius);
177
+ background: var(--range-picker-background);
178
+ color: var(--range-picker-arrow-color, var(--range-picker-muted-color));
179
+ cursor: pointer;
180
+ }
181
+
182
+ [data-range-picker] calendar-range::part(button):hover {
183
+ background: var(--range-picker-hover-background);
184
+ color: var(--range-picker-arrow-hover-color, var(--range-picker-text-color));
185
+ }
186
+
187
+ [data-range-picker] calendar-range::part(button):focus-visible {
188
+ outline: 2px solid var(--range-picker-accent-color);
189
+ outline-offset: 2px;
190
+ }
191
+
192
+ [data-range-picker] calendar-range::part(disabled) {
193
+ opacity: 0.35;
194
+ cursor: default;
195
+ }
196
+
197
+ /* Read out, not shown: the year select's label slot. */
198
+ [data-range-picker] calendar-select-year::part(label) {
199
+ position: absolute;
200
+ width: 1px;
201
+ height: 1px;
202
+ overflow: hidden;
203
+ clip-path: inset(50%);
204
+ white-space: nowrap;
205
+ }
206
+
207
+ [data-range-picker] calendar-select-year::part(select) {
208
+ block-size: 2.25rem;
209
+ padding: 0 0.625rem;
210
+ border: 1px solid var(--range-picker-control-border-color);
211
+ border-radius: var(--range-picker-control-radius);
212
+ background: var(--range-picker-background);
213
+ color: var(--range-picker-text-color);
214
+ font: inherit;
215
+ font-size: 1rem;
216
+ font-weight: 600;
217
+ cursor: pointer;
218
+ }
219
+
220
+ [data-range-picker] calendar-select-year::part(select):hover {
221
+ background: var(--range-picker-hover-background);
222
+ }
223
+
224
+ [data-range-picker] calendar-select-year::part(select):focus-visible {
225
+ outline: 2px solid var(--range-picker-accent-color);
226
+ outline-offset: 2px;
227
+ }
228
+
229
+ [data-range-picker] calendar-month {
230
+ --color-accent: transparent;
231
+ --color-text-on-accent: var(--range-picker-accent-color);
232
+ }
233
+
234
+ [data-range-picker] calendar-month::part(heading) {
235
+ margin-bottom: 0.25rem;
236
+ color: var(--range-picker-text-color);
237
+ font-weight: 600;
238
+ }
239
+
240
+ [data-range-picker] calendar-month::part(th) {
241
+ inline-size: var(--range-picker-day-size);
242
+ block-size: 1.75rem;
243
+ color: var(--range-picker-weekday-color, var(--range-picker-muted-color));
244
+ font-size: 0.75rem;
245
+ font-weight: 600;
246
+ }
247
+
248
+ [data-range-picker] calendar-month::part(day) {
249
+ inline-size: var(--range-picker-day-size);
250
+ block-size: var(--range-picker-day-size);
251
+ border-radius: var(--range-picker-control-radius);
252
+ color: var(--range-picker-text-color);
253
+ }
254
+
255
+ [data-range-picker] calendar-month::part(day):hover {
256
+ background: var(--range-picker-hover-background);
257
+ }
258
+
259
+ [data-range-picker] calendar-month::part(day):focus-visible {
260
+ outline: 2px solid var(--range-picker-accent-color);
261
+ outline-offset: -2px;
262
+ }
263
+
264
+ [data-range-picker] calendar-month::part(today) {
265
+ color: var(--range-picker-accent-color);
266
+ font-weight: 700;
267
+ }
268
+
269
+ [data-range-picker] calendar-month::part(range-inner) {
270
+ border-radius: 0;
271
+ background: var(--range-picker-band-background, var(--range-picker-selected-background));
272
+ color: var(--range-picker-band-color);
273
+ }
274
+
275
+ [data-range-picker] calendar-month::part(range-start) {
276
+ border-start-end-radius: 0;
277
+ border-end-end-radius: 0;
278
+ background: var(--range-picker-accent-color);
279
+ color: var(--range-picker-on-accent-color);
280
+ font-weight: 700;
281
+ }
282
+
283
+ [data-range-picker] calendar-month::part(range-end) {
284
+ border-start-start-radius: 0;
285
+ border-end-start-radius: 0;
286
+ background: var(--range-picker-accent-color);
287
+ color: var(--range-picker-on-accent-color);
288
+ font-weight: 700;
289
+ }
290
+
291
+ [data-range-picker] calendar-month::part(range-start range-end) {
292
+ border-radius: var(--range-picker-control-radius);
293
+ }
294
+
295
+ /*
296
+ * A picked day keeps its own ground under the pointer: the day's hover, a pseudo-class heavier than a part, would lay
297
+ * the hover tint under the ends' text. A disabled picked day's rules below still win.
298
+ */
299
+ [data-range-picker] calendar-month::part(range-inner):hover {
300
+ background: var(--range-picker-band-background, var(--range-picker-selected-background));
301
+ }
302
+
303
+ [data-range-picker] calendar-month::part(range-start):hover,
304
+ [data-range-picker] calendar-month::part(range-end):hover {
305
+ background: var(--range-picker-accent-hover-color);
306
+ }
307
+
308
+ /*
309
+ * Out of the calendar's bounds: Cally disables those days, with no part of their own. Picked days before the first day
310
+ * (a preset wholly before it keeps its own days) are a grey range.
311
+ */
312
+ [data-range-picker] calendar-month::part(disabled) {
313
+ color: var(--range-picker-disabled-color);
314
+ }
315
+
316
+ [data-range-picker] calendar-month::part(range-inner):disabled,
317
+ [data-range-picker] calendar-month::part(range-start):disabled,
318
+ [data-range-picker] calendar-month::part(range-end):disabled {
319
+ opacity: 1;
320
+ background: var(--range-picker-disabled-band-background, var(--range-picker-border-color));
321
+ color: var(--range-picker-disabled-band-color, var(--range-picker-muted-color));
322
+ }
323
+
324
+ [data-range-picker] calendar-month::part(range-start):disabled,
325
+ [data-range-picker] calendar-month::part(range-end):disabled {
326
+ background: var(--range-picker-disabled-end-background, var(--range-picker-control-border-color));
327
+ color: var(--range-picker-disabled-end-color, var(--range-picker-text-color));
328
+ }
329
+
330
+ .range-picker-months {
331
+ display: flex;
332
+ flex-wrap: wrap;
333
+ justify-content: center;
334
+ gap: 1.5rem 2rem;
335
+ }
336
+
337
+ .range-picker-foot {
338
+ display: flex;
339
+ flex-wrap: wrap;
340
+ align-items: center;
341
+ justify-content: space-between;
342
+ gap: 0.75rem 1.25rem;
343
+ margin-top: 1.125rem;
344
+ padding-top: 1rem;
345
+ border-top: 1px solid var(--range-picker-rule-color, var(--range-picker-border-color));
346
+ }
347
+
348
+ .range-picker-summary {
349
+ margin: 0;
350
+ color: var(--range-picker-muted-color);
351
+ }
352
+
353
+ .range-picker-summary [data-range-name] {
354
+ color: var(--range-picker-text-color);
355
+ font-weight: 600;
356
+ }
357
+
358
+ .range-picker-pick-end {
359
+ color: var(--range-picker-accent-color);
360
+ font-weight: 600;
361
+ }
362
+
363
+ /* The presets as a row of chips over the calendar, where both don't fit beside it. */
364
+ @media (width < 56rem) {
365
+ .range-picker-panel {
366
+ grid-template-columns: minmax(0, 1fr);
367
+ gap: 1rem;
368
+ }
369
+
370
+ .range-picker-presets {
371
+ display: flex;
372
+ flex-wrap: wrap;
373
+ gap: 0.375rem;
374
+ padding: 0 0 1rem;
375
+ border: 0;
376
+ border-bottom: 1px solid var(--range-picker-rule-color, var(--range-picker-border-color));
377
+ }
378
+
379
+ .range-picker-preset {
380
+ min-block-size: var(--range-picker-chip-height, var(--range-picker-preset-height));
381
+ border: 1px solid var(--range-picker-control-border-color);
382
+ border-radius: 999px;
383
+ }
384
+ }
385
+
386
+ /*
387
+ * Under its button on a wide window, opening toward the line's end, or flipped where that runs out of room. The gap at
388
+ * its far end keeps it off the window's edge: flip-inline swaps the inline margins with the side it opens toward, and
389
+ * where no placement fits the browser shifts the margin box, gap and all, back into the window.
390
+ */
391
+ @media (width >= 64rem) {
392
+ @supports (position-area: bottom) {
393
+ [data-range-picker] {
394
+ inset: auto;
395
+ margin-block: var(--range-picker-offset);
396
+ margin-inline: 0 var(--range-picker-window-gap);
397
+ position-anchor: --range-picker;
398
+ position-area: bottom span-right;
399
+ position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;
400
+ /* No taller than the room on the side it opens toward, which is the roomier one by default: a short window
401
+ scrolls it inside, so its Set button stays reachable. */
402
+ max-height: var(--range-picker-anchored-max-height, calc(100% - 2 * var(--range-picker-offset)));
403
+ position-try-order: var(--range-picker-position-try-order);
404
+ }
405
+
406
+ [data-range-picker]:dir(rtl) {
407
+ position-area: bottom span-left;
408
+ }
409
+ }
410
+ }
@@ -0,0 +1,2 @@
1
+ // Lets TypeScript accept `import '…/range-picker.css'` without the app declaring *.css modules.
2
+ export {};
@@ -1,5 +1,5 @@
1
1
  import 'cally';
2
- import {closestTo, once} from './helpers.js';
2
+ import {closestTo, isRadio, once} from './helpers.js';
3
3
 
4
4
  const PICKER = '[data-range-picker]';
5
5
  const CALENDAR = 'calendar-range';
@@ -38,10 +38,6 @@ function isCalendar(element) {
38
38
  return element.localName === CALENDAR;
39
39
  }
40
40
 
41
- function isRadio(element) {
42
- return element instanceof HTMLInputElement && element.type === 'radio';
43
- }
44
-
45
41
  function pickRange(event) {
46
42
  const picker = closestTo(event, PICKER);
47
43