@ismailnakkar/ui-behaviours 0.1.0 → 0.3.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ismailnakkar/ui-behaviours",
3
- "version": "0.1.0",
4
- "description": "Page behaviours from plain HTML attributes (copy, confirm, submit once, native dialogs, check all, autoselect), and a date range picker over Cally.",
3
+ "version": "0.3.0",
4
+ "description": "Page behaviours from plain HTML attributes (copy, confirm, submit once, native dialogs, steps, show by a radio, check all, autoselect), and a date range picker over Cally with an optional stylesheet.",
5
5
  "keywords": [
6
6
  "clipboard",
7
7
  "dialog",
@@ -9,6 +9,7 @@
9
9
  "date-range",
10
10
  "cally",
11
11
  "data-attributes",
12
+ "stepper",
12
13
  "laravel"
13
14
  ],
14
15
  "homepage": "https://github.com/ismailnakkar/ui-behaviours",
@@ -35,13 +36,18 @@
35
36
  "./range-picker": {
36
37
  "types": "./src/range-picker.d.ts",
37
38
  "default": "./src/range-picker.js"
39
+ },
40
+ "./range-picker.css": {
41
+ "types": "./src/range-picker.d.css.ts",
42
+ "default": "./src/range-picker.css"
38
43
  }
39
44
  },
40
45
  "files": [
41
46
  "src"
42
47
  ],
43
48
  "sideEffects": [
44
- "./src/range-picker.js"
49
+ "./src/range-picker.js",
50
+ "./src/range-picker.css"
45
51
  ],
46
52
  "publishConfig": {
47
53
  "access": "public"
package/src/helpers.js CHANGED
@@ -14,3 +14,7 @@ export function once(install) {
14
14
  export function closestTo({target}, selector) {
15
15
  return target instanceof Element ? target.closest(selector) : null;
16
16
  }
17
+
18
+ export function isRadio(element) {
19
+ return element instanceof HTMLInputElement && element.type === 'radio';
20
+ }
package/src/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- /** Listens on the document for every behaviour and opens the data-open-on-load dialogs. Later calls do nothing. */
1
+ /**
2
+ * Listens on the document for every behaviour, opens the data-open-on-load dialogs and shows each data-steps stepper at
3
+ * the step it opens at. Later calls do nothing.
4
+ */
2
5
  export function installUiBehaviours(): void;
3
6
 
4
7
  /** Writes text to the clipboard: false where navigator.clipboard is absent (plain http) or refuses. */
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import {closestTo, once} from './helpers.js';
1
+ import {closestTo, 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 radio'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,13 +24,16 @@ 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', switchOnPick);
23
28
  document.addEventListener('click', autoselect);
24
- document.addEventListener('click', copyButton);
29
+ document.addEventListener('click', copyOnClick);
25
30
  document.addEventListener('click', showModal);
26
31
  document.addEventListener('click', closeModal);
27
32
  document.addEventListener('pointerdown', pressBackdrop);
28
33
  document.addEventListener('click', closeOnBackdrop);
29
34
 
35
+ // First: a stepper dialog opened on load shows its step before showModal() moves focus in.
36
+ installSteps();
30
37
  openOnLoad();
31
38
  });
32
39
 
@@ -88,7 +95,7 @@ function releaseSubmitted({persisted}) {
88
95
  }
89
96
 
90
97
  function checkboxesNamed(name) {
91
- return [...document.querySelectorAll('input[type="checkbox"]')].filter((box) => box.name === name && !box.disabled);
98
+ return [...document.getElementsByName(name)].filter((box) => box.type === 'checkbox' && !box.disabled);
92
99
  }
93
100
 
94
101
  function checkAll({target}) {
@@ -117,6 +124,39 @@ function checkAll({target}) {
117
124
  }
118
125
  }
119
126
 
127
+ /**
128
+ * A radio picked shows its form's data-show-if="name=value" elements for its own value and hides those for another,
129
+ * and empties the fields marked data-clear-on="name", which belong to the value picked before.
130
+ */
131
+ function switchOnPick({target}) {
132
+ if (!isRadio(target)) {
133
+ return;
134
+ }
135
+
136
+ const scope = target.form ?? document;
137
+
138
+ for (const element of scope.querySelectorAll('[data-show-if]')) {
139
+ const condition = showIfOf(element);
140
+
141
+ if (condition?.name === target.name) {
142
+ element.hidden = condition.value !== target.value;
143
+ }
144
+ }
145
+
146
+ for (const field of scope.querySelectorAll('[data-clear-on]')) {
147
+ if (field.dataset.clearOn === target.name) {
148
+ field.value = '';
149
+ }
150
+ }
151
+ }
152
+
153
+ function showIfOf(element) {
154
+ const condition = element.dataset.showIf;
155
+ const at = condition.indexOf(SHOW_IF_SEPARATOR);
156
+
157
+ return at > 0 ? {name: condition.slice(0, at), value: condition.slice(at + 1)} : null;
158
+ }
159
+
120
160
  // Tabbing in selects a field's text natively; a click only places a caret.
121
161
  function autoselect({target}) {
122
162
  if (isField(target) && target.matches('[data-autoselect]')) {
@@ -134,10 +174,10 @@ function copiedTextOf(source) {
134
174
  return source.value;
135
175
  }
136
176
 
137
- return (source.innerText ?? source.textContent ?? '').trim();
177
+ return (source.innerText ?? source.textContent).trim();
138
178
  }
139
179
 
140
- async function copyButton(event) {
180
+ async function copyOnClick(event) {
141
181
  const button = closestTo(event, '[data-copy]');
142
182
  const source = button ? document.getElementById(button.dataset.copy) : null;
143
183
 
@@ -147,14 +187,14 @@ async function copyButton(event) {
147
187
 
148
188
  const copied = await copyText(copiedTextOf(source));
149
189
 
150
- flashCopied(button, copied);
190
+ flashCopyResult(button, copied);
151
191
 
152
192
  if (!copied) {
153
193
  selectForManualCopy(source);
154
194
  }
155
195
  }
156
196
 
157
- function flashCopied(button, copied) {
197
+ function flashCopyResult(button, copied) {
158
198
  clearTimeout(copyTimers.get(button));
159
199
  button.toggleAttribute(COPIED, copied);
160
200
  button.toggleAttribute(COPY_FAILED, !copied);
@@ -164,7 +204,7 @@ function flashCopied(button, copied) {
164
204
  }, COPY_FLASH_MS));
165
205
  }
166
206
 
167
- // Explicit focus(): Safari does not focus a clicked button.
207
+ // select() alone may leave the field unfocused; focused, Ctrl/Cmd+C copies it by hand.
168
208
  function selectForManualCopy(source) {
169
209
  if (isField(source)) {
170
210
  source.focus();
@@ -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 {};