@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/README.md +395 -47
- package/package.json +9 -3
- package/src/helpers.js +8 -0
- package/src/index.d.ts +4 -1
- package/src/index.js +91 -5
- package/src/range-picker.css +410 -0
- package/src/range-picker.d.css.ts +2 -0
- package/src/range-picker.js +1 -5
- package/src/steps.js +197 -0
package/README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# @ismailnakkar/ui-behaviours
|
|
2
2
|
|
|
3
|
-
Page behaviours from plain HTML attributes: copy to the clipboard
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Page behaviours from plain HTML attributes: copy to the clipboard and say the result to screen readers, confirm
|
|
4
|
+
a submit, send a form once, send a form on a change, open, close and light-dismiss a native `<dialog>`, walk a form's
|
|
5
|
+
steps one at a time, show what a radio, a checkbox or a select chose, check every row from a header box, and select a
|
|
6
|
+
field's text on a click. They listen on the document, so markup added later works too.
|
|
6
7
|
|
|
7
8
|
A date range picker over [Cally](https://wicky.nillia.ms/cally/) has its own entry, so pages without one never load the
|
|
8
|
-
calendar
|
|
9
|
-
app's markup.
|
|
9
|
+
calendar, and an optional stylesheet themed by custom properties. Nothing else ships styles, and nothing ships wording:
|
|
10
|
+
the app styles the states the package sets, and every word comes from the app's markup.
|
|
10
11
|
|
|
11
12
|
## Install
|
|
12
13
|
|
|
@@ -20,7 +21,8 @@ optional peer dependency (`npm i cally`). Type declarations are included.
|
|
|
20
21
|
## Usage
|
|
21
22
|
|
|
22
23
|
Call `installUiBehaviours()` once from the layout's script, on every page, after the page is parsed (a module or a
|
|
23
|
-
deferred script): it also opens the page's `data-open-on-load` dialogs
|
|
24
|
+
deferred script): it also opens the page's `data-open-on-load` dialogs and shows each stepper at the step it opens at. A
|
|
25
|
+
second call does nothing.
|
|
24
26
|
|
|
25
27
|
```js
|
|
26
28
|
import {installUiBehaviours} from '@ismailnakkar/ui-behaviours';
|
|
@@ -34,12 +36,17 @@ Then mark up the page:
|
|
|
34
36
|
|---|---|---|
|
|
35
37
|
| [`data-copy="id"`](#data-copyid) | a button | copies the element with that id |
|
|
36
38
|
| [`data-copy-value`](#data-copyid) | the copied element | the text to copy instead of what it shows |
|
|
39
|
+
| [`data-copy-status="id"`](#data-copy-statusid) | a copy button | says the result in the live region with that id |
|
|
37
40
|
| [`data-confirm="question"`](#data-confirmquestion) | a form or a submit button | asks before submitting |
|
|
38
41
|
| [`data-submit-once`](#data-submit-once) | a form | refuses a second submit |
|
|
42
|
+
| [`data-submit-on-change`](#data-submit-on-change) | a select, a checkbox or a radio in a form | submits the form when it changes |
|
|
39
43
|
| [`data-show-modal="id"`](#data-show-modalid) | a button or a link | opens the dialog with that id as a modal |
|
|
40
44
|
| [`data-close-modal`](#data-close-modal) | a button in a dialog | closes that dialog |
|
|
41
45
|
| [`closedby="any"`](#closedbyany) | a dialog | closes it on a backdrop click |
|
|
42
46
|
| [`data-open-on-load`](#data-open-on-load) | a dialog | opens it on install |
|
|
47
|
+
| [`data-steps`](#data-steps) | a dialog or any box | shows its `data-step` sections one at a time |
|
|
48
|
+
| [`data-show-if="name=value"`](#data-show-ifnamevalue-and-data-clear-onname) | any element in a form | shows it only while the radio, checkbox or select `name` holds that value |
|
|
49
|
+
| [`data-clear-on="name"`](#data-show-ifnamevalue-and-data-clear-onname) | a field in a form | empties it when another radio `name` is picked |
|
|
43
50
|
| [`data-check-all="name"`](#data-check-allname) | a header checkbox | checks every row named `name` |
|
|
44
51
|
| [`data-autoselect`](#data-autoselect) | an input or a textarea | selects its text on a click |
|
|
45
52
|
|
|
@@ -66,6 +73,26 @@ focused too, so the user copies it by hand. A missing source copies nothing.
|
|
|
66
73
|
To refuse a copy (asking for a missing field first), call `event.preventDefault()` in a click listener of your own on
|
|
67
74
|
the button or an element around it: the copy skips a cancelled click.
|
|
68
75
|
|
|
76
|
+
### `data-copy-status="id"`
|
|
77
|
+
|
|
78
|
+
On a copy button: names a live region (`role="status"`) that says the result, as the button's own marks are not read
|
|
79
|
+
out. The region holds the words: its `data-copied-text` for a copy that worked, its `data-copy-failed-text` for a
|
|
80
|
+
refusal; a result it has no words for says nothing.
|
|
81
|
+
|
|
82
|
+
```html
|
|
83
|
+
<input id="short-url" value="https://example.com/abcd" readonly data-autoselect>
|
|
84
|
+
<button type="button" data-copy="short-url" data-copy-status="short-url-status">
|
|
85
|
+
<span>Copy</span><span>Copied</span>
|
|
86
|
+
</button>
|
|
87
|
+
<p id="short-url-status" role="status" class="sr-only"
|
|
88
|
+
data-copied-text="Link copied" data-copy-failed-text="Copy blocked: copy the selected link by hand"></p>
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The click empties the region, so the same words said again are read out again; the words go in once the clipboard
|
|
92
|
+
answers and leave with the button's marks, 1.5 seconds later, unless the page has written something else there since.
|
|
93
|
+
Buttons may share one region, and the page may use it for its own news too. Keep the region in place, never `hidden`:
|
|
94
|
+
a hidden element is out of the accessibility tree, and one that appears as it speaks is not reliably read.
|
|
95
|
+
|
|
69
96
|
### `data-confirm="question"`
|
|
70
97
|
|
|
71
98
|
On a form, or on one of its submit buttons: the browser's `confirm()` asks that question, and only a yes submits. The
|
|
@@ -93,6 +120,20 @@ forms send again.
|
|
|
93
120
|
</form>
|
|
94
121
|
```
|
|
95
122
|
|
|
123
|
+
### `data-submit-on-change`
|
|
124
|
+
|
|
125
|
+
On a select, a checkbox or a radio in a form: a change submits the form with `requestSubmit()`, as its submit button
|
|
126
|
+
would, so the browser checks the fields and `data-confirm` and `data-submit-once` apply. A field in no form sends
|
|
127
|
+
nothing. Mark only choices whose result the label makes plain, such as a sort or a filter.
|
|
128
|
+
|
|
129
|
+
```html
|
|
130
|
+
<form method="GET" action="/admin/reports">
|
|
131
|
+
<label for="sort">Sort by</label>
|
|
132
|
+
<select id="sort" name="sort" data-submit-on-change>…</select>
|
|
133
|
+
<noscript><button>Sort</button></noscript>
|
|
134
|
+
</form>
|
|
135
|
+
```
|
|
136
|
+
|
|
96
137
|
### `data-show-modal="id"`
|
|
97
138
|
|
|
98
139
|
On a button or a link: a click opens the `<dialog>` with that id with `showModal()`. On a link the `href` is the
|
|
@@ -133,6 +174,117 @@ submit inside it, or a one-time result.
|
|
|
133
174
|
<dialog id="recovery-codes" data-open-on-load>…</dialog>
|
|
134
175
|
```
|
|
135
176
|
|
|
177
|
+
### `data-steps`
|
|
178
|
+
|
|
179
|
+
On a dialog or any other box: a form's fields in steps, one showing at a time, each checked before the next shows, and
|
|
180
|
+
the form sent from the last. Two-factor enrolment is one: scan the code, the mailed code, the app's code, all posted
|
|
181
|
+
by one form.
|
|
182
|
+
|
|
183
|
+
```html
|
|
184
|
+
<dialog id="two-factor-dialog" aria-labelledby="two-factor-title" data-steps>
|
|
185
|
+
<h2 id="two-factor-title">Turn on two-factor</h2>
|
|
186
|
+
<div aria-hidden="true">
|
|
187
|
+
<i data-step-mark="1" data-step-state="current"></i><i data-step-mark="2"></i><i data-step-mark="3"></i>
|
|
188
|
+
</div>
|
|
189
|
+
<p aria-live="polite">
|
|
190
|
+
<span data-step-label="1">Step 1 of 3</span>
|
|
191
|
+
<span data-step-label="2" hidden>Step 2 of 3</span>
|
|
192
|
+
<span data-step-label="3" hidden>Step 3 of 3</span>
|
|
193
|
+
</p>
|
|
194
|
+
|
|
195
|
+
<form id="two-factor-enable" method="POST" action="/user/two-factor-authentication">
|
|
196
|
+
<section data-step="1">
|
|
197
|
+
<h3 tabindex="-1">Scan the QR code</h3>
|
|
198
|
+
…
|
|
199
|
+
</section>
|
|
200
|
+
<section data-step="2" hidden>
|
|
201
|
+
<h3 tabindex="-1">Confirm your email</h3>
|
|
202
|
+
<input name="email_code" required autocomplete="one-time-code">
|
|
203
|
+
</section>
|
|
204
|
+
<section data-step="3" hidden>
|
|
205
|
+
<h3 tabindex="-1">Enter the code from your app</h3>
|
|
206
|
+
<input name="code" required aria-invalid="true" aria-describedby="code-error" autocomplete="one-time-code">
|
|
207
|
+
<p id="code-error">The code is not valid.</p>
|
|
208
|
+
</section>
|
|
209
|
+
</form>
|
|
210
|
+
|
|
211
|
+
<button type="button" data-step-back hidden>Back</button>
|
|
212
|
+
<button type="button" data-close-modal>Cancel</button>
|
|
213
|
+
<button type="button" data-step-next>Next</button>
|
|
214
|
+
<button type="button" data-step-submit hidden>Turn on</button>
|
|
215
|
+
</dialog>
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
- **`data-step="n"`** on each step, numbered from 1 in the order they come, all inside one form. Only the step showing
|
|
219
|
+
is not `hidden`. Its fields are the inputs, selects and textareas in it.
|
|
220
|
+
- **`data-step-next`**: checks the step's fields (`reportValidity()`, so the browser says what is missing), then shows
|
|
221
|
+
the next step and focuses its first heading; give the heading `tabindex="-1"`. From the last step it submits, as
|
|
222
|
+
`data-step-submit` does. Enter in an input of a step does the same, so the form's own submit never skips a step's
|
|
223
|
+
check; Enter while an input method composes is left alone.
|
|
224
|
+
- **`data-step-back`** shows the step before.
|
|
225
|
+
- **`data-step-submit`**: checks every step's fields; one left empty shows its step and the browser's message, else the
|
|
226
|
+
form is sent (`requestSubmit()`, so `data-confirm` and `data-submit-once` apply).
|
|
227
|
+
- **`data-step-mark="n"`**, a progress mark: gets `data-step-state="done"` behind the step showing and `"current"` for
|
|
228
|
+
it; the marks ahead have none.
|
|
229
|
+
- **`data-step-label="n"`**: shown only while step `n` shows, such as "Step 2 of 3" in a live region.
|
|
230
|
+
|
|
231
|
+
The controls may sit anywhere in the stepper, in or out of the form; one that is a submit button moves the steps without
|
|
232
|
+
sending the form. Back is hidden on the first step, Next on the last, and Submit on every other.
|
|
233
|
+
|
|
234
|
+
On install, a stepper opens (`showModal()` on a dialog) at:
|
|
235
|
+
|
|
236
|
+
1. the earliest step with a refused or an empty field, when a field carries **`aria-invalid="true"`**: the page a
|
|
237
|
+
refused submit came back as. A field the server never hands back, such as a mailed code, is empty again, so its step
|
|
238
|
+
comes first. `aria-invalid="true"` is the one error mark the stepper reads: set it on each field the server refused,
|
|
239
|
+
with its message.
|
|
240
|
+
2. else **`data-open-at-step="n"`** on the stepper, such as the email step on the page after "Email me a code";
|
|
241
|
+
3. else nowhere: a dialog stays shut, and the stepper shows the step the server drew.
|
|
242
|
+
|
|
243
|
+
Draw the first state on the server too (step 1 showing, its mark current, Back and Submit hidden): it is what shows
|
|
244
|
+
before the script runs. A stepper added later works, but opens at nothing.
|
|
245
|
+
|
|
246
|
+
### `data-show-if="name=value"` and `data-clear-on="name"`
|
|
247
|
+
|
|
248
|
+
In a form, for a choice: an element marked `data-show-if="name=value"` shows while the field named `name` holds that
|
|
249
|
+
value, and hides once it does not.
|
|
250
|
+
|
|
251
|
+
- **A radio**: picking one shows the elements naming its value and hides those naming another.
|
|
252
|
+
- **A checkbox**: ticking the box with that value shows them, unticking it hides them, and another box of the same name
|
|
253
|
+
leaves them alone. A box without a `value` has the value `on`.
|
|
254
|
+
- **A select**: a change shows the elements naming a selected option's value and hides the rest.
|
|
255
|
+
|
|
256
|
+
Picking a radio named `name` also empties the fields marked `data-clear-on="name"`, which belonged to the value picked
|
|
257
|
+
before. A payout destination is one: the account depends on the method.
|
|
258
|
+
|
|
259
|
+
```html
|
|
260
|
+
<form method="POST" action="/withdrawals/destination">
|
|
261
|
+
<label><input type="radio" name="method" value="paypal" checked> PayPal</label>
|
|
262
|
+
<label><input type="radio" name="method" value="usdt_polygon"> USDT on Polygon</label>
|
|
263
|
+
|
|
264
|
+
<input name="account" required data-clear-on="method" aria-describedby="account-hint">
|
|
265
|
+
<div id="account-hint" aria-live="polite">
|
|
266
|
+
<p data-show-if="method=paypal">Your PayPal email address.</p>
|
|
267
|
+
<p data-show-if="method=usdt_polygon" hidden>Your USDT address on the Polygon network.</p>
|
|
268
|
+
</div>
|
|
269
|
+
</form>
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
```html
|
|
273
|
+
<select name="driver">
|
|
274
|
+
<option value="local" selected>Local disk</option>
|
|
275
|
+
<option value="s3">S3</option>
|
|
276
|
+
</select>
|
|
277
|
+
<fieldset data-show-if="driver=s3" hidden>…</fieldset>
|
|
278
|
+
|
|
279
|
+
<label><input type="checkbox" name="report" value="1"> Report it</label>
|
|
280
|
+
<div data-show-if="report=1" hidden>…</div>
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
The server draws the chosen value's elements shown and the others `hidden`, a refused submit's page from the values
|
|
284
|
+
sent back; the script changes them on a change only. It reads within the changed field's form (the page, for a field in
|
|
285
|
+
no form). A hidden element's fields still submit, and a hidden `required` one blocks the submit: leave `required` off
|
|
286
|
+
them and let the server require each for its own value.
|
|
287
|
+
|
|
136
288
|
### `data-check-all="name"`
|
|
137
289
|
|
|
138
290
|
On a header checkbox: checking or unchecking it does the same to every enabled checkbox named `name`. When a row
|
|
@@ -160,6 +312,7 @@ The package sets attributes and the browser's own states; the app styles them:
|
|
|
160
312
|
- `[data-copied]` and `[data-copy-failed]` on a copy button: show its "Copied" or "Copy it by hand" label instead of
|
|
161
313
|
"Copy". The words are the app's, in the button.
|
|
162
314
|
- `[data-submitting]` on a form sent once, to show the wait.
|
|
315
|
+
- `[data-step-state="done"]` and `[data-step-state="current"]` on a stepper's progress marks.
|
|
163
316
|
- `dialog`, `dialog::backdrop`, and `dialog[open]` for an opening transition.
|
|
164
317
|
- `:indeterminate` on a header checkbox, if the app restyles checkboxes.
|
|
165
318
|
|
|
@@ -191,8 +344,16 @@ The package sets attributes and the browser's own states; the app styles them:
|
|
|
191
344
|
and focus goes back to the opener on close. Give each one an `aria-labelledby`.
|
|
192
345
|
- Put the copy state in the button's text (two spans, one hidden), so it never relies on colour alone.
|
|
193
346
|
- A refused copy selects and focuses the field, so the user can copy it with the keyboard.
|
|
194
|
-
- The copy marks change an attribute only,
|
|
195
|
-
`
|
|
347
|
+
- The copy marks change an attribute only, which screen readers do not read out: give the button a
|
|
348
|
+
[`data-copy-status`](#data-copy-statusid) region with the words for both results, so the result is heard.
|
|
349
|
+
- A change that submits moves the page under the user, and on some desktop browsers an arrow key on a closed select
|
|
350
|
+
changes it at once: keep `data-submit-on-change` to a sort or a filter that is cheap to redo, and give the form a
|
|
351
|
+
submit button for a page without script.
|
|
352
|
+
- A stepper moves focus to the new step's heading, so a screen reader starts where the step does: give each a
|
|
353
|
+
`tabindex="-1"`. Hide the progress marks (`aria-hidden="true"`) and put the step's words in a `data-step-label` inside
|
|
354
|
+
an `aria-live="polite"` region.
|
|
355
|
+
- Point a field's `aria-describedby` at its `data-show-if` hints' box, in an `aria-live="polite"` region, so a warning
|
|
356
|
+
shown on a pick is read out.
|
|
196
357
|
|
|
197
358
|
## Range picker
|
|
198
359
|
|
|
@@ -214,43 +375,56 @@ opens nothing and the box shows in place, where its radios and fields still subm
|
|
|
214
375
|
|
|
215
376
|
### The markup
|
|
216
377
|
|
|
378
|
+
The classes are the [stylesheet](#styling-the-picker)'s, for an app that uses it.
|
|
379
|
+
|
|
217
380
|
```html
|
|
218
381
|
<form method="get" action="/reports">
|
|
219
|
-
<
|
|
220
|
-
<
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
<div
|
|
227
|
-
<
|
|
228
|
-
|
|
229
|
-
|
|
382
|
+
<div class="field">
|
|
383
|
+
<button type="button" class="range-picker-button" popovertarget="report-range" data-range-state="preset">
|
|
384
|
+
<svg aria-hidden="true">…</svg>
|
|
385
|
+
<span data-range-name>Last 7 days</span>
|
|
386
|
+
<span data-range-dates="compact">14 Nov – 20 Nov</span>
|
|
387
|
+
</button>
|
|
388
|
+
|
|
389
|
+
<div id="report-range" popover data-range-picker data-range-state="preset">
|
|
390
|
+
<div class="range-picker-panel">
|
|
391
|
+
<div class="range-picker-presets" role="radiogroup" aria-label="Range">
|
|
392
|
+
<label class="range-picker-preset"><input type="radio" name="range" value="today" data-range-preset="2026-11-20/2026-11-20"> Today</label>
|
|
393
|
+
<label class="range-picker-preset"><input type="radio" name="range" value="last7" data-range-preset="2026-11-14/2026-11-20" checked> Last 7 days</label>
|
|
394
|
+
<label class="range-picker-preset"><input type="radio" name="range" value="custom" data-range-custom> Custom range</label>
|
|
395
|
+
</div>
|
|
396
|
+
|
|
397
|
+
<calendar-range months="2" page-by="single" value="2026-11-14/2026-11-20" focused-date="2026-11-20"
|
|
398
|
+
min="2025-03-02" max="2026-11-20" today="2026-11-20" locale="en-GB" first-day-of-week="1">
|
|
399
|
+
<svg slot="previous" role="img" aria-label="Previous">…</svg>
|
|
400
|
+
<svg slot="next" role="img" aria-label="Next">…</svg>
|
|
401
|
+
<calendar-select-year slot="heading"><span slot="label">Year</span></calendar-select-year>
|
|
402
|
+
<div class="range-picker-months">
|
|
403
|
+
<calendar-month></calendar-month>
|
|
404
|
+
<calendar-month offset="1"></calendar-month>
|
|
405
|
+
</div>
|
|
406
|
+
</calendar-range>
|
|
407
|
+
</div>
|
|
408
|
+
|
|
409
|
+
<div class="range-picker-foot">
|
|
410
|
+
<p class="range-picker-summary" aria-live="polite">
|
|
411
|
+
<span class="when-preset"><b data-range-name>Last 7 days</b>, </span>
|
|
412
|
+
<span data-range-dates>14 Nov – 20 Nov 2026</span>
|
|
413
|
+
<span class="when-pending">– <span class="range-picker-pick-end">pick the end day</span></span>
|
|
414
|
+
</p>
|
|
415
|
+
<input type="hidden" name="from" value="2026-11-14" data-range-from>
|
|
416
|
+
<input type="hidden" name="to" value="2026-11-20" data-range-to>
|
|
417
|
+
<button type="button" popovertarget="report-range" popovertargetaction="hide">Set</button>
|
|
418
|
+
</div>
|
|
230
419
|
</div>
|
|
231
|
-
|
|
232
|
-
<calendar-range months="2" page-by="single" value="2026-11-14/2026-11-20" focused-date="2026-11-20"
|
|
233
|
-
min="2025-03-02" max="2026-11-20" today="2026-11-20" locale="en-GB" first-day-of-week="1">
|
|
234
|
-
<svg slot="previous" role="img" aria-label="Previous">…</svg>
|
|
235
|
-
<svg slot="next" role="img" aria-label="Next">…</svg>
|
|
236
|
-
<calendar-select-year slot="heading"><span slot="label">Year</span></calendar-select-year>
|
|
237
|
-
<calendar-month></calendar-month>
|
|
238
|
-
<calendar-month offset="1"></calendar-month>
|
|
239
|
-
</calendar-range>
|
|
240
|
-
|
|
241
|
-
<p aria-live="polite">
|
|
242
|
-
<span class="when-preset"><b data-range-name>Last 7 days</b>, </span>
|
|
243
|
-
<span data-range-dates>14 Nov – 20 Nov 2026</span>
|
|
244
|
-
<span class="when-pending">– pick the end day</span>
|
|
245
|
-
</p>
|
|
246
|
-
|
|
247
|
-
<input type="hidden" name="from" value="2026-11-14" data-range-from>
|
|
248
|
-
<input type="hidden" name="to" value="2026-11-20" data-range-to>
|
|
249
|
-
<button>Show</button>
|
|
250
420
|
</div>
|
|
421
|
+
|
|
422
|
+
<button>Show</button>
|
|
251
423
|
</form>
|
|
252
424
|
```
|
|
253
425
|
|
|
426
|
+
Set only closes the popover, with the picks already in the fields: the form's own submit button runs it.
|
|
427
|
+
|
|
254
428
|
- **`data-range-picker`** marks the box holding the rest, a `popover` here. Its buttons are the elements whose
|
|
255
429
|
`popovertarget` names its id.
|
|
256
430
|
- **`data-range-preset="from/to"`** on a preset's radio gives its days as `YYYY-MM-DD/YYYY-MM-DD`, the format of
|
|
@@ -262,8 +436,8 @@ opens nothing and the box shows in place, where its radios and fields still subm
|
|
|
262
436
|
the buttons keep the range the form sends. Paging the calendar or changing its year or month only moves the view and
|
|
263
437
|
keeps the pick; picking a radio, this one too, drops it.
|
|
264
438
|
- **`data-range-from` and `data-range-to`** on the two hidden fields the form sends, inside the picker.
|
|
265
|
-
- **`<calendar-range>`** is set up
|
|
266
|
-
|
|
439
|
+
- **`<calendar-range>`** is set up by the app: its months, `min`, `max`, `today`, `locale`, `page-by` and slots. The
|
|
440
|
+
package sets only its `value`, `focusedDate` and `tentative`; the stylesheet styles its parts.
|
|
267
441
|
- **`data-range-name`**, in the picker or a button: the checked radio's label text.
|
|
268
442
|
- **`data-range-dates`**, in the picker or a button: each end by `Intl.DateTimeFormat` (day, short month) in the
|
|
269
443
|
calendar's `locale`, else the page's `lang`, joined by a spaced en dash, with the year once when both ends share it
|
|
@@ -278,10 +452,93 @@ later until its first pick.
|
|
|
278
452
|
|
|
279
453
|
### Styling the picker
|
|
280
454
|
|
|
281
|
-
The
|
|
455
|
+
The stylesheet styles the button's layout and open look, the popover, the presets, Cally's parts and the state words.
|
|
456
|
+
Import it once, from the page's script or the app's CSS:
|
|
457
|
+
|
|
458
|
+
```js
|
|
459
|
+
import '@ismailnakkar/ui-behaviours/range-picker.css';
|
|
460
|
+
```
|
|
282
461
|
|
|
283
462
|
```css
|
|
284
|
-
.
|
|
463
|
+
@import '@ismailnakkar/ui-behaviours/range-picker.css' layer(components);
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
It is in no cascade layer, so it beats layered utility classes (Tailwind v4's) on the picker; the second form puts it in
|
|
467
|
+
the app's `components` layer, under its utilities.
|
|
468
|
+
|
|
469
|
+
It styles these, and nothing outside them:
|
|
470
|
+
|
|
471
|
+
| Selector | On |
|
|
472
|
+
|---|---|
|
|
473
|
+
| `.range-picker-button` | the button that opens the picker: the app draws the control (border, height, padding); the stylesheet lays out its icon, name and days, anchors the popover to it, and marks it while the popover is open. Put the popover after it in the same parent. |
|
|
474
|
+
| `[data-range-picker]` | the popover |
|
|
475
|
+
| `.range-picker-panel` | the presets beside the calendar |
|
|
476
|
+
| `.range-picker-presets`, `.range-picker-preset` | the radio group, and each preset's label around its radio |
|
|
477
|
+
| `.range-picker-months` | the calendar's months, inside `<calendar-range>` |
|
|
478
|
+
| `.range-picker-foot`, `.range-picker-summary`, `.range-picker-pick-end` | the row under the calendar, its summary line, and the "pick the end day" words in it |
|
|
479
|
+
| `.when-preset`, `.when-pending` | the words for each `data-range-state` |
|
|
480
|
+
|
|
481
|
+
It draws no icons: the app sizes the button's, the arrows' in Cally's `previous` and `next` slots and any in a preset,
|
|
482
|
+
and mirrors the arrows on a right-to-left page.
|
|
483
|
+
|
|
484
|
+
Under 56rem the presets turn into a row of chips over the calendar. From 64rem, where the browser has anchor
|
|
485
|
+
positioning (Chrome 129, Safari 26), the popover opens under its button toward the line's end, flipped where that runs
|
|
486
|
+
out of room and no taller than the room it opens into, so a short window scrolls it; under 64rem it is centred. A
|
|
487
|
+
second picker on the same page anchors to the last button.
|
|
488
|
+
|
|
489
|
+
Override these custom properties on `:root`, or on one picker and its button. The defaults sit in `:where(:root)`, so
|
|
490
|
+
any rule of yours wins; the ones defaulting to another property follow it.
|
|
491
|
+
|
|
492
|
+
| Property | Default | Styles |
|
|
493
|
+
|---|---|---|
|
|
494
|
+
| `--range-picker-text-color` | `#1c2130` | The popover's text, the days, the month and year, the summary's range name, and a custom range's days on the button |
|
|
495
|
+
| `--range-picker-muted-color` | `#687086` | The button's days and the summary |
|
|
496
|
+
| `--range-picker-preset-color` | `--range-picker-muted-color` | The presets |
|
|
497
|
+
| `--range-picker-preset-hover-color` | `--range-picker-text-color` | A preset under the pointer |
|
|
498
|
+
| `--range-picker-preset-hover-background` | `--range-picker-hover-background` | Its ground |
|
|
499
|
+
| `--range-picker-arrow-color` | `--range-picker-muted-color` | Cally's previous and next buttons |
|
|
500
|
+
| `--range-picker-arrow-hover-color` | `--range-picker-text-color` | An arrow under the pointer |
|
|
501
|
+
| `--range-picker-weekday-color` | `--range-picker-muted-color` | The weekday names |
|
|
502
|
+
| `--range-picker-background` | `#fff` | The popover, the arrows and the year select |
|
|
503
|
+
| `--range-picker-hover-background` | `#f4f6fb` | A day, an arrow and the year select under the pointer, and the button while the popover is open |
|
|
504
|
+
| `--range-picker-border-color` | `#e6e8ef` | The popover's border |
|
|
505
|
+
| `--range-picker-rule-color` | `--range-picker-border-color` | The rule beside the presets (under them as chips) and the one over the summary |
|
|
506
|
+
| `--range-picker-control-border-color` | `#cfd4df` | The arrows, the year select and the presets as chips |
|
|
507
|
+
| `--range-picker-accent-color` | `#3d5afe` | The picked preset's text, the range's two ends, today, the "pick the end day" words, focus rings, and the button's border while the popover is open |
|
|
508
|
+
| `--range-picker-accent-hover-color` | `#2f48d4` | The range's ends under the pointer: set it to the accent to keep them as they are |
|
|
509
|
+
| `--range-picker-on-accent-color` | `#fff` | The range's ends' text |
|
|
510
|
+
| `--range-picker-selected-background` | `#eef1ff` | The picked preset |
|
|
511
|
+
| `--range-picker-band-background` | `--range-picker-selected-background` | The days between the ends |
|
|
512
|
+
| `--range-picker-band-color` | `#2f48d4` | Their text |
|
|
513
|
+
| `--range-picker-disabled-color` | `#aeb5c6` | The days out of the calendar's bounds |
|
|
514
|
+
| `--range-picker-disabled-band-background` | `--range-picker-border-color` | Picked days out of the bounds (a preset reaching before the first day) |
|
|
515
|
+
| `--range-picker-disabled-band-color` | `--range-picker-muted-color` | Their text |
|
|
516
|
+
| `--range-picker-disabled-end-background` | `--range-picker-control-border-color` | A range end out of the bounds |
|
|
517
|
+
| `--range-picker-disabled-end-color` | `--range-picker-text-color` | Its text |
|
|
518
|
+
| `--range-picker-radius` | `0.75rem` | The popover's corners |
|
|
519
|
+
| `--range-picker-control-radius` | `0.5rem` | The presets', the days', the arrows' and the year select's corners |
|
|
520
|
+
| `--range-picker-shadow` | `0 16px 40px -8px rgb(16 24 40 / 0.22)` | The popover's shadow |
|
|
521
|
+
| `--range-picker-font-size` | `0.875rem` | The popover's text size |
|
|
522
|
+
| `--range-picker-padding` | `1.25rem` | The popover's padding: set a smaller one under your phone width |
|
|
523
|
+
| `--range-picker-window-gap` | `0.75rem` | The least room the popover leaves to the window's edges |
|
|
524
|
+
| `--range-picker-offset` | `0.375rem` | The gap between the anchored popover and its button |
|
|
525
|
+
| `--range-picker-anchored-max-height` | `calc(100% - 2 * var(--range-picker-offset))` | The anchored popover's height limit, against the room on the side it opens toward |
|
|
526
|
+
| `--range-picker-position-try-order` | `most-height` | Which side the anchored popover prefers: the roomier one by default; `normal` keeps the order below, then above |
|
|
527
|
+
| `--range-picker-day-size` | `2.5rem` | A day's width and height |
|
|
528
|
+
| `--range-picker-preset-height` | `2.5rem` | A preset's least height |
|
|
529
|
+
| `--range-picker-chip-height` | `--range-picker-preset-height` | A chip's least height, where the presets are chips |
|
|
530
|
+
|
|
531
|
+
The weights and the text sizes are the stylesheet's, all from `--range-picker-font-size` but the year select (`1rem`),
|
|
532
|
+
the button's days (`0.8125rem`) and the weekday names (`0.75rem`).
|
|
533
|
+
|
|
534
|
+
The colours are light; a dark page sets its own under its dark selector. The stylesheet uses no `light-dark()`:
|
|
535
|
+
Lightning CSS (in Vite and Tailwind v4) lowers it to variables it sets only where the same stylesheet declares
|
|
536
|
+
`color-scheme`.
|
|
537
|
+
|
|
538
|
+
Without the stylesheet, the app shows the words each state calls for itself:
|
|
539
|
+
|
|
540
|
+
```css
|
|
541
|
+
[data-range-state] .when-pending,
|
|
285
542
|
[data-range-state="custom"] .when-preset,
|
|
286
543
|
[data-range-state="pending"] .when-preset,
|
|
287
544
|
[data-range-state="custom"] > [data-range-name] {
|
|
@@ -294,7 +551,8 @@ The words for each state are the app's, in the markup; the app shows the ones `d
|
|
|
294
551
|
```
|
|
295
552
|
|
|
296
553
|
While `pending` the checked radio is still the range the form sends. To show Custom range as the one being picked,
|
|
297
|
-
style `[data-range-state="pending"] label:has([data-range-custom])` as checked, and the checked label as not
|
|
554
|
+
style `[data-range-state="pending"] label:has([data-range-custom])` as checked, and the checked label as not, as the
|
|
555
|
+
stylesheet does.
|
|
298
556
|
|
|
299
557
|
### The picker's accessibility
|
|
300
558
|
|
|
@@ -306,9 +564,10 @@ style `[data-range-state="pending"] label:has([data-range-custom])` as checked,
|
|
|
306
564
|
|
|
307
565
|
| Import | Does |
|
|
308
566
|
|---|---|
|
|
309
|
-
| `installUiBehaviours()` | listens on the document for every behaviour above
|
|
567
|
+
| `installUiBehaviours()` | listens on the document for every behaviour above, opens the `data-open-on-load` dialogs and shows each stepper at the step it opens at; later calls do nothing |
|
|
310
568
|
| `copyText(text)` | the copy buttons' clipboard write: resolves to `true` when the clipboard took the text, `false` where `navigator.clipboard` is absent (plain http) or refuses |
|
|
311
569
|
| `installRangePicker()`, from `@ismailnakkar/ui-behaviours/range-picker` | listens on the document for range pickers and writes those already on the page; later calls do nothing |
|
|
570
|
+
| `@ismailnakkar/ui-behaviours/range-picker.css` | the range picker's [stylesheet](#styling-the-picker) |
|
|
312
571
|
|
|
313
572
|
`copyText()` serves a page's own script, copying a link it just made:
|
|
314
573
|
|
|
@@ -322,9 +581,9 @@ if (!(await copyText(url))) {
|
|
|
322
581
|
|
|
323
582
|
## Events
|
|
324
583
|
|
|
325
|
-
The package fires none of its own. A dialog's native `close` fires when a close button or the backdrop closes it,
|
|
326
|
-
|
|
327
|
-
the capture phase, as the package does.
|
|
584
|
+
The package fires none of its own. A dialog's native `close` fires when a close button or the backdrop closes it, the
|
|
585
|
+
range picker's radios fire the native `change`, and a `data-submit-on-change` field's form the native `submit`. Cally's
|
|
586
|
+
`change` and `rangestart` do not bubble: listen to them in the capture phase, as the package does.
|
|
328
587
|
|
|
329
588
|
## Testing
|
|
330
589
|
|
|
@@ -345,10 +604,99 @@ customElements.define('calendar-range', class extends HTMLElement {
|
|
|
345
604
|
});
|
|
346
605
|
```
|
|
347
606
|
|
|
607
|
+
A stepper needs nothing more: happy-dom and jsdom check `required` fields and send a form with `requestSubmit()`. To
|
|
608
|
+
test one opening on load, put its markup in place before installing, in a test file of its own or after
|
|
609
|
+
`vi.resetModules()` with a fresh `import()`.
|
|
610
|
+
|
|
348
611
|
Run this package's suite with `npm test`.
|
|
349
612
|
|
|
350
613
|
## Upgrading
|
|
351
614
|
|
|
615
|
+
### From 0.3
|
|
616
|
+
|
|
617
|
+
Nothing changes for a page on 0.3: copy buttons without `data-copy-status` and radios work as before, a select or a
|
|
618
|
+
checkbox changes nothing unless a `data-show-if` names its field, and only a field marked `data-submit-on-change`
|
|
619
|
+
submits. An app replacing its own copies changes these:
|
|
620
|
+
|
|
621
|
+
- **cuty, the Full Page Script copy** (`resources/js/members/tools/fps.js`, `members/tools/fps.blade.php`). Delete the
|
|
622
|
+
`MutationObserver` at the end of `fps.js` with its `COPIED` and `status` constants, give the Copy button
|
|
623
|
+
`data-copy-status="fps-copy-status"`, and give the `.copy-status` span that id; its `data-copied-text` stays. Add a
|
|
624
|
+
`data-copy-failed-text` to say a refusal too.
|
|
625
|
+
- **exe, both shorten forms' copy** (`resources/js/site/shorten.ts` and `shorten.test.ts`, the home page's form in
|
|
626
|
+
`public/home.blade.php` and the nav dialog's in `layouts/member.blade.php`: one `bind()` serves every
|
|
627
|
+
`form[data-shorten]`). In each form, give the `data-shorten-copy` button `data-copy` naming the form's link
|
|
628
|
+
(`shorten-link`, `shorten-dialog-link`) and `data-copy-status` naming its `data-shorten-status` paragraph, give that
|
|
629
|
+
paragraph the id (`shorten-status`, `shorten-dialog-status`), rename its `data-copied` to `data-copied-text`, and add
|
|
630
|
+
a `data-copy-failed-text` holding the `general.shorten.copy-blocked` words.
|
|
631
|
+
- Restyle each form from the button's own marks, as `x-ui.copy-button` does: the button becomes a `group`,
|
|
632
|
+
`group-data-[copy-state=done]:` becomes `data-copied:` on it (hover repeats included) and `group-data-copied:` on
|
|
633
|
+
its labels, `=failed` the same with `data-copy-failed`, and the copy-blocked line shows with
|
|
634
|
+
`group-has-[[data-copy-failed]]:block`.
|
|
635
|
+
- Delete the copy state machine from `shorten.ts`: the `CopyState` and `Timer` types, `copyState` and its calls, the
|
|
636
|
+
`copied` timer, `copyLink()` and the copy listener. Drop the `failed` hook and the `data-shorten-copy-failed`
|
|
637
|
+
attribute of both copy-blocked lines in the same change: `hooks()` binds nothing while a hook it asks for is
|
|
638
|
+
missing. `copy` and `copyUrl` stay: `showResult()` focuses the one and fills the other.
|
|
639
|
+
- In `shorten.test.ts`, drop `data-shorten-copy-failed` and `data-copied` from the markup, delete "selects the link
|
|
640
|
+
when the clipboard refuses" (this package's tests select a refused copy), and keep "clears a Copied state when a
|
|
641
|
+
new link arrives" as "shows a new link and says it is ready": its submit and answer, without the copy click and the
|
|
642
|
+
`copyState` assertions.
|
|
643
|
+
- Both marks then last 1.5 seconds, as on exe's other copy buttons. Today a copy shows for 2 seconds, a refusal and
|
|
644
|
+
its copy-blocked line stay until the next copy or submit, and a new link clears either at once.
|
|
645
|
+
- **upfiles, the admin's conditional fields** (`resources/js/admin/conditional-fields.js`, its
|
|
646
|
+
`tests/js/conditional-fields.test.mjs`, the `initConditionalFields()` call in `resources/js/admin/index.js`). Delete
|
|
647
|
+
the three, and turn each `data-cond-field="x" data-cond-value="v"` into `data-show-if="x=v"`
|
|
648
|
+
(`admin/delete.blade.php`, `components/storages/form.blade.php`). The storages form now draws its first state on the
|
|
649
|
+
server: each role hint and driver section `hidden` unless it is the role or driver its select shows (`old()`, else the
|
|
650
|
+
storage's, else the default role or the first driver), as the CSAM origin field already is.
|
|
651
|
+
- **upfiles, the auto-submitting select** (`resources/js/lib/form-affordances.js`, called from
|
|
652
|
+
`resources/js/admin/index.js` and `resources/js/member/index.js`). Delete it and its two calls, and rename
|
|
653
|
+
`data-autosubmit` to `data-submit-on-change` (`admin/moderation/index.blade.php`); both bundles already call
|
|
654
|
+
`installUiBehaviours()`. Rename it in the two `assertDontSee('data-autosubmit', false)` of
|
|
655
|
+
`tests/Feature/StatisticsDashboardsTest.php` too, or they pass whatever the month pickers carry.
|
|
656
|
+
- **centratag, the dashboard's select** (`resources/js/dashboard.ts`). Add `@ismailnakkar/ui-behaviours` and replace the
|
|
657
|
+
file's change listener with `installUiBehaviours()`; `data-submit-on-change` in `tags/show.blade.php` stays as it is.
|
|
658
|
+
|
|
659
|
+
### From 0.2
|
|
660
|
+
|
|
661
|
+
Nothing changes for a page on 0.2: the stepper, `data-show-if` and `data-clear-on` read only attributes of their own,
|
|
662
|
+
and the stylesheet loads only when imported. An app replacing its own copies changes these:
|
|
663
|
+
|
|
664
|
+
- **The two-factor stepper.** Delete the app's stepper script, its steps module and their tests, then in the dialog:
|
|
665
|
+
- add `data-steps` to the dialog;
|
|
666
|
+
- replace `data-email-code-sent` with `data-open-at-step="2"`, the email step;
|
|
667
|
+
- rename `data-step-turn-on` to `data-step-submit` (`data-step`, `data-step-back` and `data-step-next` stay);
|
|
668
|
+
- on the progress bars, rename `data-progress-step="n"` to `data-step-mark="n"`, draw mark 1 with
|
|
669
|
+
`data-step-state="current"`, and style `[data-step-state="done"]` and `[data-step-state="current"]` in place of the
|
|
670
|
+
`is-done` and `is-current` classes (or `data-state`);
|
|
671
|
+
- on the progress line's spans, rename `data-progress-step="n"` to `data-step-label="n"`;
|
|
672
|
+
- give each field the server refused `aria-invalid="true"`: the stepper reads no error class (`.validation-error`,
|
|
673
|
+
`.invalid-feedback`).
|
|
674
|
+
- **The payout destination form.** Delete the app's destination script, drop `data-destination-form`, add
|
|
675
|
+
`data-clear-on="method"` to the account field, and turn each `data-method-hint="paypal"` into
|
|
676
|
+
`data-show-if="method=paypal"`.
|
|
677
|
+
- **The range picker's look.** Import `range-picker.css` and set the custom properties from the app's palette and
|
|
678
|
+
sizes, with a smaller `--range-picker-padding` under the app's phone width (`1rem` for a popover that had 16px). In
|
|
679
|
+
the markup, `r-range-button` becomes `range-picker-button`, `r-range-panel` `range-picker-panel`, `r-presets`
|
|
680
|
+
`range-picker-presets`, `r-preset` `range-picker-preset`, `r-months` `range-picker-months`, `r-range-foot`
|
|
681
|
+
`range-picker-foot`, `r-range-sum` `range-picker-summary` and `r-pick-end` `range-picker-pick-end`; `r-range`,
|
|
682
|
+
`r-calendar`, `r-range-name` and `r-range-dates` go, as the stylesheet styles `[data-range-picker]`, its
|
|
683
|
+
`calendar-range`, `[data-range-name]` and `[data-range-dates]`. Utility classes that restyle the popover (border,
|
|
684
|
+
background, padding, shadow, text colour) go too; an opening animation stays.
|
|
685
|
+
|
|
686
|
+
Then delete the app's own picker rules, except those that draw the app's controls and icons, which the stylesheet
|
|
687
|
+
leaves to the app. Keep these, under the new class names:
|
|
688
|
+
- the range button's control: its height, padding, border, corners, background, text colour and size, hover and
|
|
689
|
+
transition, its icons' size and colour, and the chevron's `margin-inline-start: auto` (cuty's `.r-range-button`
|
|
690
|
+
and its desktop font size, upfiles' `.r-range-button > i`; exe draws it with utility classes);
|
|
691
|
+
- the arrows' icons in Cally's slots, both nested in `.r-calendar`, so move them out before deleting that block:
|
|
692
|
+
their size (cuty's `[slot] > svg`, as `[data-range-picker] [slot] > svg`) and their mirror on a right-to-left
|
|
693
|
+
page (upfiles' `.r-flip:dir(rtl)`; cuty's `flip-rtl` and exe's `rtl:-scale-x-100` sit outside the picker's
|
|
694
|
+
rules);
|
|
695
|
+
- a preset's icon size (cuty's `.r-preset svg`);
|
|
696
|
+
- the Set button: cuty's `.r-range-foot button` (height, padding, size, weight), and its full width on a phone,
|
|
697
|
+
cuty's `.r-range-foot button { flex: 1 }` and upfiles' `.r-range-foot .btn` (exe's `max-sm:flex-1` is in the
|
|
698
|
+
markup).
|
|
699
|
+
|
|
352
700
|
### From 0.1
|
|
353
701
|
|
|
354
702
|
- A preset radio gives its days in one attribute, `data-range-preset="from/to"`, in place of `data-from` and `data-to`:
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ismailnakkar/ui-behaviours",
|
|
3
|
-
"version": "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.4.0",
|
|
4
|
+
"description": "Page behaviours from plain HTML attributes (copy with a spoken result, confirm, submit once or on a change, native dialogs, steps, show by a radio, checkbox or select, 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,11 @@ 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
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isCheckbox(element) {
|
|
23
|
+
return element instanceof HTMLInputElement && element.type === 'checkbox';
|
|
24
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
/**
|
|
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. */
|