@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/README.md +366 -110
- package/package.json +9 -3
- package/src/helpers.js +4 -0
- package/src/index.d.ts +4 -1
- package/src/index.js +48 -8
- package/src/range-picker.css +410 -0
- package/src/range-picker.d.css.ts +2 -0
- package/src/range-picker.js +21 -17
- package/src/steps.js +197 -0
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# @ismailnakkar/ui-behaviours
|
|
2
2
|
|
|
3
3
|
Page behaviours from plain HTML attributes: copy to the clipboard, confirm a submit, send a form once, open, close and
|
|
4
|
-
light-dismiss a native `<dialog>`,
|
|
5
|
-
|
|
4
|
+
light-dismiss a native `<dialog>`, walk a form's steps one at a time, show a radio's own hints, check every row from a
|
|
5
|
+
header box, and select a field's text on a click. They listen on the document, so markup added later works too.
|
|
6
6
|
|
|
7
7
|
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.
|
|
8
|
+
calendar, and an optional stylesheet themed by custom properties. Nothing else ships styles, and nothing ships wording:
|
|
9
|
+
the app styles the states the package sets, and every word comes from the app's markup.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -14,12 +14,14 @@ app's markup.
|
|
|
14
14
|
npm i @ismailnakkar/ui-behaviours
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
`npm i cally
|
|
17
|
+
The behaviours have no dependencies. The [range picker](#range-picker) needs Cally 0.9.2 or a later 0.9 release, an
|
|
18
|
+
optional peer dependency (`npm i cally`). Type declarations are included.
|
|
19
19
|
|
|
20
20
|
## Usage
|
|
21
21
|
|
|
22
|
-
Call
|
|
22
|
+
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 and shows each stepper at the step it opens at. A
|
|
24
|
+
second call does nothing.
|
|
23
25
|
|
|
24
26
|
```js
|
|
25
27
|
import {installUiBehaviours} from '@ismailnakkar/ui-behaviours';
|
|
@@ -27,22 +29,7 @@ import {installUiBehaviours} from '@ismailnakkar/ui-behaviours';
|
|
|
27
29
|
installUiBehaviours();
|
|
28
30
|
```
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
then. A second call does nothing.
|
|
32
|
-
|
|
33
|
-
`copyText(text)` is the clipboard write the copy buttons use, for a page's own script (copying a link it just made):
|
|
34
|
-
it resolves to `true` when the clipboard took the text, `false` where `navigator.clipboard` is absent (plain http) or
|
|
35
|
-
refuses.
|
|
36
|
-
|
|
37
|
-
```js
|
|
38
|
-
import {copyText} from '@ismailnakkar/ui-behaviours';
|
|
39
|
-
|
|
40
|
-
if (!(await copyText(url))) {
|
|
41
|
-
field.select();
|
|
42
|
-
}
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## The attributes
|
|
32
|
+
Then mark up the page:
|
|
46
33
|
|
|
47
34
|
| Attribute | On | Does |
|
|
48
35
|
|---|---|---|
|
|
@@ -54,14 +41,19 @@ if (!(await copyText(url))) {
|
|
|
54
41
|
| [`data-close-modal`](#data-close-modal) | a button in a dialog | closes that dialog |
|
|
55
42
|
| [`closedby="any"`](#closedbyany) | a dialog | closes it on a backdrop click |
|
|
56
43
|
| [`data-open-on-load`](#data-open-on-load) | a dialog | opens it on install |
|
|
44
|
+
| [`data-steps`](#data-steps) | a dialog or any box | shows its `data-step` sections one at a time |
|
|
45
|
+
| [`data-show-if="name=value"`](#data-show-ifnamevalue-and-data-clear-onname) | any element in a form | shows it only while the radio `name` has that value |
|
|
46
|
+
| [`data-clear-on="name"`](#data-show-ifnamevalue-and-data-clear-onname) | a field in a form | empties it when another radio `name` is picked |
|
|
57
47
|
| [`data-check-all="name"`](#data-check-allname) | a header checkbox | checks every row named `name` |
|
|
58
48
|
| [`data-autoselect`](#data-autoselect) | an input or a textarea | selects its text on a click |
|
|
59
49
|
|
|
60
50
|
### `data-copy="id"`
|
|
61
51
|
|
|
62
|
-
On a button: a click copies the element with that id
|
|
63
|
-
|
|
64
|
-
|
|
52
|
+
On a button: a click copies the element with that id, taking the first of:
|
|
53
|
+
|
|
54
|
+
1. its `data-copy-value`, for text grouped on screen for reading, such as a setup key;
|
|
55
|
+
2. an input's or a textarea's current value;
|
|
56
|
+
3. its text as laid out (`innerText`: one line per block, so a list copies one item a line), trimmed.
|
|
65
57
|
|
|
66
58
|
```html
|
|
67
59
|
<input id="short-url" value="https://example.com/abcd" readonly data-autoselect>
|
|
@@ -71,12 +63,12 @@ screen for reading, such as a setup key), else an input's or a textarea's curren
|
|
|
71
63
|
<button type="button" data-copy="setup-key">Copy key</button>
|
|
72
64
|
```
|
|
73
65
|
|
|
74
|
-
The button gets `data-copied`
|
|
75
|
-
the
|
|
76
|
-
|
|
66
|
+
The button gets `data-copied` when the copy worked, or `data-copy-failed` when the browser refused, for 1.5 seconds; a
|
|
67
|
+
second click restarts the 1.5 seconds and replaces the other mark. On a refusal the field or text is selected, a field
|
|
68
|
+
focused too, so the user copies it by hand. A missing source copies nothing.
|
|
77
69
|
|
|
78
|
-
To refuse a copy (asking for a missing field first), call `event.preventDefault()` in a click listener of
|
|
79
|
-
|
|
70
|
+
To refuse a copy (asking for a missing field first), call `event.preventDefault()` in a click listener of your own on
|
|
71
|
+
the button or an element around it: the copy skips a cancelled click.
|
|
80
72
|
|
|
81
73
|
### `data-confirm="question"`
|
|
82
74
|
|
|
@@ -93,9 +85,10 @@ form's. A submit the page already cancelled asks nothing.
|
|
|
93
85
|
### `data-submit-once`
|
|
94
86
|
|
|
95
87
|
On a form: the first submit goes and marks the form `data-submitting`; every later one is refused, so a double click on
|
|
96
|
-
a payment button pays once. The buttons stay enabled, as a disabled submitter would drop its `name` and `value
|
|
97
|
-
the
|
|
98
|
-
restored from the back-forward cache (Back from a provider's page) drops the mark, so its
|
|
88
|
+
a payment button pays once. The buttons stay enabled, as a disabled submitter would drop its `name` and `value`; style
|
|
89
|
+
the mark to show the wait. A submit refused by `data-confirm`, or cancelled by a listener on the form, was never sent
|
|
90
|
+
and leaves no mark. A page restored from the back-forward cache (Back from a provider's page) drops the mark, so its
|
|
91
|
+
forms send again.
|
|
99
92
|
|
|
100
93
|
```html
|
|
101
94
|
<form method="POST" action="/subscription" data-submit-once>
|
|
@@ -106,9 +99,9 @@ restored from the back-forward cache (Back from a provider's page) drops the mar
|
|
|
106
99
|
|
|
107
100
|
### `data-show-modal="id"`
|
|
108
101
|
|
|
109
|
-
On a button or a link: a click opens the `<dialog>` with that id with `showModal()
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
On a button or a link: a click opens the `<dialog>` with that id with `showModal()`. On a link the `href` is the
|
|
103
|
+
fallback: a modified click (new tab, new window) follows it, as does any click on a page without that dialog. A click
|
|
104
|
+
the page cancelled opens nothing, and an open dialog stays open.
|
|
112
105
|
|
|
113
106
|
```html
|
|
114
107
|
<a href="/links/create" data-show-modal="shorten-dialog">Shorten a link</a>
|
|
@@ -116,20 +109,20 @@ does any click on a page without that dialog. A click the page cancelled opens n
|
|
|
116
109
|
|
|
117
110
|
### `data-close-modal`
|
|
118
111
|
|
|
119
|
-
On a button inside a dialog: a click closes that dialog
|
|
120
|
-
without sending the form, so a Cancel button may sit in the dialog's form.
|
|
112
|
+
On a button inside a dialog: a click closes that dialog, and the native `close` event fires. On a submit button it
|
|
113
|
+
closes without sending the form, so a Cancel button may sit in the dialog's form.
|
|
121
114
|
|
|
122
115
|
```html
|
|
123
116
|
<button type="button" data-close-modal>Cancel</button>
|
|
124
117
|
```
|
|
125
118
|
|
|
126
|
-
|
|
119
|
+
Without script, `<form method="dialog">` and Escape close a dialog natively.
|
|
127
120
|
|
|
128
121
|
### `closedby="any"`
|
|
129
122
|
|
|
130
|
-
On a dialog: the native
|
|
131
|
-
dragged out onto the backdrop (a text selection) does not.
|
|
132
|
-
|
|
123
|
+
On a dialog: the native light-dismiss attribute. A click on the backdrop closes the dialog; a press inside its box
|
|
124
|
+
dragged out onto the backdrop (a text selection) does not. A browser that supports `closedby` does this itself;
|
|
125
|
+
elsewhere the package does.
|
|
133
126
|
|
|
134
127
|
```html
|
|
135
128
|
<dialog id="shorten-dialog" aria-labelledby="shorten-dialog-title" closedby="any">…</dialog>
|
|
@@ -144,6 +137,97 @@ submit inside it, or a one-time result.
|
|
|
144
137
|
<dialog id="recovery-codes" data-open-on-load>…</dialog>
|
|
145
138
|
```
|
|
146
139
|
|
|
140
|
+
### `data-steps`
|
|
141
|
+
|
|
142
|
+
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
|
|
143
|
+
the form sent from the last. Two-factor enrolment is one: scan the code, the mailed code, the app's code, all posted
|
|
144
|
+
by one form.
|
|
145
|
+
|
|
146
|
+
```html
|
|
147
|
+
<dialog id="two-factor-dialog" aria-labelledby="two-factor-title" data-steps>
|
|
148
|
+
<h2 id="two-factor-title">Turn on two-factor</h2>
|
|
149
|
+
<div aria-hidden="true">
|
|
150
|
+
<i data-step-mark="1" data-step-state="current"></i><i data-step-mark="2"></i><i data-step-mark="3"></i>
|
|
151
|
+
</div>
|
|
152
|
+
<p aria-live="polite">
|
|
153
|
+
<span data-step-label="1">Step 1 of 3</span>
|
|
154
|
+
<span data-step-label="2" hidden>Step 2 of 3</span>
|
|
155
|
+
<span data-step-label="3" hidden>Step 3 of 3</span>
|
|
156
|
+
</p>
|
|
157
|
+
|
|
158
|
+
<form id="two-factor-enable" method="POST" action="/user/two-factor-authentication">
|
|
159
|
+
<section data-step="1">
|
|
160
|
+
<h3 tabindex="-1">Scan the QR code</h3>
|
|
161
|
+
…
|
|
162
|
+
</section>
|
|
163
|
+
<section data-step="2" hidden>
|
|
164
|
+
<h3 tabindex="-1">Confirm your email</h3>
|
|
165
|
+
<input name="email_code" required autocomplete="one-time-code">
|
|
166
|
+
</section>
|
|
167
|
+
<section data-step="3" hidden>
|
|
168
|
+
<h3 tabindex="-1">Enter the code from your app</h3>
|
|
169
|
+
<input name="code" required aria-invalid="true" aria-describedby="code-error" autocomplete="one-time-code">
|
|
170
|
+
<p id="code-error">The code is not valid.</p>
|
|
171
|
+
</section>
|
|
172
|
+
</form>
|
|
173
|
+
|
|
174
|
+
<button type="button" data-step-back hidden>Back</button>
|
|
175
|
+
<button type="button" data-close-modal>Cancel</button>
|
|
176
|
+
<button type="button" data-step-next>Next</button>
|
|
177
|
+
<button type="button" data-step-submit hidden>Turn on</button>
|
|
178
|
+
</dialog>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
- **`data-step="n"`** on each step, numbered from 1 in the order they come, all inside one form. Only the step showing
|
|
182
|
+
is not `hidden`. Its fields are the inputs, selects and textareas in it.
|
|
183
|
+
- **`data-step-next`**: checks the step's fields (`reportValidity()`, so the browser says what is missing), then shows
|
|
184
|
+
the next step and focuses its first heading; give the heading `tabindex="-1"`. From the last step it submits, as
|
|
185
|
+
`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
|
|
186
|
+
check; Enter while an input method composes is left alone.
|
|
187
|
+
- **`data-step-back`** shows the step before.
|
|
188
|
+
- **`data-step-submit`**: checks every step's fields; one left empty shows its step and the browser's message, else the
|
|
189
|
+
form is sent (`requestSubmit()`, so `data-confirm` and `data-submit-once` apply).
|
|
190
|
+
- **`data-step-mark="n"`**, a progress mark: gets `data-step-state="done"` behind the step showing and `"current"` for
|
|
191
|
+
it; the marks ahead have none.
|
|
192
|
+
- **`data-step-label="n"`**: shown only while step `n` shows, such as "Step 2 of 3" in a live region.
|
|
193
|
+
|
|
194
|
+
The controls may sit anywhere in the stepper, in or out of the form; one that is a submit button moves the steps without
|
|
195
|
+
sending the form. Back is hidden on the first step, Next on the last, and Submit on every other.
|
|
196
|
+
|
|
197
|
+
On install, a stepper opens (`showModal()` on a dialog) at:
|
|
198
|
+
|
|
199
|
+
1. the earliest step with a refused or an empty field, when a field carries **`aria-invalid="true"`**: the page a
|
|
200
|
+
refused submit came back as. A field the server never hands back, such as a mailed code, is empty again, so its step
|
|
201
|
+
comes first. `aria-invalid="true"` is the one error mark the stepper reads: set it on each field the server refused,
|
|
202
|
+
with its message.
|
|
203
|
+
2. else **`data-open-at-step="n"`** on the stepper, such as the email step on the page after "Email me a code";
|
|
204
|
+
3. else nowhere: a dialog stays shut, and the stepper shows the step the server drew.
|
|
205
|
+
|
|
206
|
+
Draw the first state on the server too (step 1 showing, its mark current, Back and Submit hidden): it is what shows
|
|
207
|
+
before the script runs. A stepper added later works, but opens at nothing.
|
|
208
|
+
|
|
209
|
+
### `data-show-if="name=value"` and `data-clear-on="name"`
|
|
210
|
+
|
|
211
|
+
In a form, for a choice made with radios: picking a radio named `name` shows the form's elements whose `data-show-if`
|
|
212
|
+
names that radio's value and hides those naming another, and empties the fields marked `data-clear-on="name"`, which
|
|
213
|
+
belonged to the value picked before. A payout destination is one: the account depends on the method.
|
|
214
|
+
|
|
215
|
+
```html
|
|
216
|
+
<form method="POST" action="/withdrawals/destination">
|
|
217
|
+
<label><input type="radio" name="method" value="paypal" checked> PayPal</label>
|
|
218
|
+
<label><input type="radio" name="method" value="usdt_polygon"> USDT on Polygon</label>
|
|
219
|
+
|
|
220
|
+
<input name="account" required data-clear-on="method" aria-describedby="account-hint">
|
|
221
|
+
<div id="account-hint" aria-live="polite">
|
|
222
|
+
<p data-show-if="method=paypal">Your PayPal email address.</p>
|
|
223
|
+
<p data-show-if="method=usdt_polygon" hidden>Your USDT address on the Polygon network.</p>
|
|
224
|
+
</div>
|
|
225
|
+
</form>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
The server draws the checked radio's elements shown and the others `hidden`; the script changes them on a pick only. It
|
|
229
|
+
reads radios only, within the picked radio's form (the page, for a radio in no form).
|
|
230
|
+
|
|
147
231
|
### `data-check-all="name"`
|
|
148
232
|
|
|
149
233
|
On a header checkbox: checking or unchecking it does the same to every enabled checkbox named `name`. When a row
|
|
@@ -154,11 +238,11 @@ changes, the header follows: checked when every row is, indeterminate when some
|
|
|
154
238
|
<input type="checkbox" name="ids[]" value="5">
|
|
155
239
|
```
|
|
156
240
|
|
|
157
|
-
|
|
241
|
+
The header sets the rows without firing `change` on them, so a page counting selected rows listens to the header too.
|
|
158
242
|
|
|
159
243
|
### `data-autoselect`
|
|
160
244
|
|
|
161
|
-
On an input or a textarea: a click selects all of its text, as tabbing in already does
|
|
245
|
+
On an input or a textarea: a click selects all of its text, as tabbing in already does.
|
|
162
246
|
|
|
163
247
|
```html
|
|
164
248
|
<input value="https://example.com/abcd" readonly data-autoselect>
|
|
@@ -168,8 +252,10 @@ On an input or a textarea: a click selects all of its text, as tabbing in alread
|
|
|
168
252
|
|
|
169
253
|
The package sets attributes and the browser's own states; the app styles them:
|
|
170
254
|
|
|
171
|
-
- `[data-copied]` and `[data-copy-failed]` on a copy button: show its "Copied" or "Copy it by hand" label
|
|
255
|
+
- `[data-copied]` and `[data-copy-failed]` on a copy button: show its "Copied" or "Copy it by hand" label instead of
|
|
172
256
|
"Copy". The words are the app's, in the button.
|
|
257
|
+
- `[data-submitting]` on a form sent once, to show the wait.
|
|
258
|
+
- `[data-step-state="done"]` and `[data-step-state="current"]` on a stepper's progress marks.
|
|
173
259
|
- `dialog`, `dialog::backdrop`, and `dialog[open]` for an opening transition.
|
|
174
260
|
- `:indeterminate` on a header checkbox, if the app restyles checkboxes.
|
|
175
261
|
|
|
@@ -195,10 +281,6 @@ The package sets attributes and the browser's own states; the app styles them:
|
|
|
195
281
|
}
|
|
196
282
|
```
|
|
197
283
|
|
|
198
|
-
## Events
|
|
199
|
-
|
|
200
|
-
The package fires none of its own. A dialog's native `close` event fires when a close button or the backdrop closes it.
|
|
201
|
-
|
|
202
284
|
## Accessibility
|
|
203
285
|
|
|
204
286
|
- Dialogs are native `<dialog>` opened with `showModal()`: focus moves in, the page behind is inert, Escape closes,
|
|
@@ -207,18 +289,16 @@ The package fires none of its own. A dialog's native `close` event fires when a
|
|
|
207
289
|
- A refused copy selects and focuses the field, so the user can copy it with the keyboard.
|
|
208
290
|
- The copy marks change an attribute only, so screen readers announce nothing. A page that needs it adds a
|
|
209
291
|
`role="status"` line and fills it when `data-copied` appears (a `MutationObserver`).
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
on
|
|
215
|
-
|
|
216
|
-
Run this package's suite with `npm test`.
|
|
292
|
+
- A stepper moves focus to the new step's heading, so a screen reader starts where the step does: give each a
|
|
293
|
+
`tabindex="-1"`. Hide the progress marks (`aria-hidden="true"`) and put the step's words in a `data-step-label` inside
|
|
294
|
+
an `aria-live="polite"` region.
|
|
295
|
+
- Point a field's `aria-describedby` at its `data-show-if` hints' box, in an `aria-live="polite"` region, so a warning
|
|
296
|
+
shown on a pick is read out.
|
|
217
297
|
|
|
218
298
|
## Range picker
|
|
219
299
|
|
|
220
300
|
A date range for a GET form: a button opens a native popover with the app's preset ranges beside a Cally
|
|
221
|
-
`<calendar-range>`, and the picks fill the form's `from` and `to` fields.
|
|
301
|
+
`<calendar-range>`, and the picks fill the form's `from` and `to` fields. Its own entry loads Cally:
|
|
222
302
|
|
|
223
303
|
```js
|
|
224
304
|
import {installRangePicker} from '@ismailnakkar/ui-behaviours/range-picker';
|
|
@@ -226,64 +306,78 @@ import {installRangePicker} from '@ismailnakkar/ui-behaviours/range-picker';
|
|
|
226
306
|
installRangePicker();
|
|
227
307
|
```
|
|
228
308
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
309
|
+
Call it after the page is parsed; a second call does nothing. For each picker already on the page it rewrites the
|
|
310
|
+
labels from the fields and moves the calendar to the end day. A picker added later works too, but keeps the server's
|
|
311
|
+
labels and view until its first pick.
|
|
232
312
|
|
|
233
313
|
It needs the browser's Popover API (Chrome 114, Safari 17, Firefox 125), which npm cannot check: without it the button
|
|
234
314
|
opens nothing and the box shows in place, where its radios and fields still submit.
|
|
235
315
|
|
|
236
316
|
### The markup
|
|
237
317
|
|
|
318
|
+
The classes are the [stylesheet](#styling-the-picker)'s, for an app that uses it.
|
|
319
|
+
|
|
238
320
|
```html
|
|
239
321
|
<form method="get" action="/reports">
|
|
240
|
-
<
|
|
241
|
-
<
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
<div
|
|
248
|
-
<
|
|
249
|
-
|
|
250
|
-
|
|
322
|
+
<div class="field">
|
|
323
|
+
<button type="button" class="range-picker-button" popovertarget="report-range" data-range-state="preset">
|
|
324
|
+
<svg aria-hidden="true">…</svg>
|
|
325
|
+
<span data-range-name>Last 7 days</span>
|
|
326
|
+
<span data-range-dates="compact">14 Nov – 20 Nov</span>
|
|
327
|
+
</button>
|
|
328
|
+
|
|
329
|
+
<div id="report-range" popover data-range-picker data-range-state="preset">
|
|
330
|
+
<div class="range-picker-panel">
|
|
331
|
+
<div class="range-picker-presets" role="radiogroup" aria-label="Range">
|
|
332
|
+
<label class="range-picker-preset"><input type="radio" name="range" value="today" data-range-preset="2026-11-20/2026-11-20"> Today</label>
|
|
333
|
+
<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>
|
|
334
|
+
<label class="range-picker-preset"><input type="radio" name="range" value="custom" data-range-custom> Custom range</label>
|
|
335
|
+
</div>
|
|
336
|
+
|
|
337
|
+
<calendar-range months="2" page-by="single" value="2026-11-14/2026-11-20" focused-date="2026-11-20"
|
|
338
|
+
min="2025-03-02" max="2026-11-20" today="2026-11-20" locale="en-GB" first-day-of-week="1">
|
|
339
|
+
<svg slot="previous" role="img" aria-label="Previous">…</svg>
|
|
340
|
+
<svg slot="next" role="img" aria-label="Next">…</svg>
|
|
341
|
+
<calendar-select-year slot="heading"><span slot="label">Year</span></calendar-select-year>
|
|
342
|
+
<div class="range-picker-months">
|
|
343
|
+
<calendar-month></calendar-month>
|
|
344
|
+
<calendar-month offset="1"></calendar-month>
|
|
345
|
+
</div>
|
|
346
|
+
</calendar-range>
|
|
347
|
+
</div>
|
|
348
|
+
|
|
349
|
+
<div class="range-picker-foot">
|
|
350
|
+
<p class="range-picker-summary" aria-live="polite">
|
|
351
|
+
<span class="when-preset"><b data-range-name>Last 7 days</b>, </span>
|
|
352
|
+
<span data-range-dates>14 Nov – 20 Nov 2026</span>
|
|
353
|
+
<span class="when-pending">– <span class="range-picker-pick-end">pick the end day</span></span>
|
|
354
|
+
</p>
|
|
355
|
+
<input type="hidden" name="from" value="2026-11-14" data-range-from>
|
|
356
|
+
<input type="hidden" name="to" value="2026-11-20" data-range-to>
|
|
357
|
+
<button type="button" popovertarget="report-range" popovertargetaction="hide">Set</button>
|
|
358
|
+
</div>
|
|
251
359
|
</div>
|
|
252
|
-
|
|
253
|
-
<calendar-range months="2" page-by="single" value="2026-11-14/2026-11-20" focused-date="2026-11-20"
|
|
254
|
-
min="2025-03-02" max="2026-11-20" today="2026-11-20" locale="en-GB" first-day-of-week="1">
|
|
255
|
-
<svg slot="previous" role="img" aria-label="Previous">…</svg>
|
|
256
|
-
<svg slot="next" role="img" aria-label="Next">…</svg>
|
|
257
|
-
<calendar-select-year slot="heading"><span slot="label">Year</span></calendar-select-year>
|
|
258
|
-
<calendar-month></calendar-month>
|
|
259
|
-
<calendar-month offset="1"></calendar-month>
|
|
260
|
-
</calendar-range>
|
|
261
|
-
|
|
262
|
-
<p aria-live="polite">
|
|
263
|
-
<span class="when-preset"><b data-range-name>Last 7 days</b>, </span>
|
|
264
|
-
<span data-range-dates>14 Nov – 20 Nov 2026</span>
|
|
265
|
-
<span class="when-pending">– pick the end day</span>
|
|
266
|
-
</p>
|
|
267
|
-
|
|
268
|
-
<input type="hidden" name="from" value="2026-11-14" data-range-from>
|
|
269
|
-
<input type="hidden" name="to" value="2026-11-20" data-range-to>
|
|
270
|
-
<button>Show</button>
|
|
271
360
|
</div>
|
|
361
|
+
|
|
362
|
+
<button>Show</button>
|
|
272
363
|
</form>
|
|
273
364
|
```
|
|
274
365
|
|
|
366
|
+
Set only closes the popover, with the picks already in the fields: the form's own submit button runs it.
|
|
367
|
+
|
|
275
368
|
- **`data-range-picker`** marks the box holding the rest, a `popover` here. Its buttons are the elements whose
|
|
276
369
|
`popovertarget` names its id.
|
|
277
|
-
- **`data-from
|
|
278
|
-
package does no calendar maths. Picking it sets the calendar's
|
|
279
|
-
the end day's, with keyboard focus on that day (this needs
|
|
370
|
+
- **`data-range-preset="from/to"`** on a preset's radio gives its days as `YYYY-MM-DD/YYYY-MM-DD`, the format of
|
|
371
|
+
Cally's `value`; the server works them out, the package does no calendar maths. Picking it sets the calendar's
|
|
372
|
+
`value` and moves its view so the two months end on the end day's, with keyboard focus on that day (this needs
|
|
373
|
+
`page-by="single"`), then fills the fields and labels.
|
|
280
374
|
- **`data-range-custom`** on the Custom range radio: two days picked on the calendar check it and fill the fields.
|
|
281
375
|
While only the start is picked, the picker shows that day alone, named as Custom range, and the radios, the fields and
|
|
282
376
|
the buttons keep the range the form sends. Paging the calendar or changing its year or month only moves the view and
|
|
283
377
|
keeps the pick; picking a radio, this one too, drops it.
|
|
284
378
|
- **`data-range-from` and `data-range-to`** on the two hidden fields the form sends, inside the picker.
|
|
285
|
-
- **`<calendar-range>`** is set up
|
|
286
|
-
|
|
379
|
+
- **`<calendar-range>`** is set up by the app: its months, `min`, `max`, `today`, `locale`, `page-by` and slots. The
|
|
380
|
+
package sets only its `value`, `focusedDate` and `tentative`; the stylesheet styles its parts.
|
|
287
381
|
- **`data-range-name`**, in the picker or a button: the checked radio's label text.
|
|
288
382
|
- **`data-range-dates`**, in the picker or a button: each end by `Intl.DateTimeFormat` (day, short month) in the
|
|
289
383
|
calendar's `locale`, else the page's `lang`, joined by a spaced en dash, with the year once when both ends share it
|
|
@@ -296,12 +390,95 @@ opens nothing and the box shows in place, where its radios and fields still subm
|
|
|
296
390
|
Render the labels and the states on the server too: they are what shows before the script runs, and on a picker added
|
|
297
391
|
later until its first pick.
|
|
298
392
|
|
|
299
|
-
### Styling
|
|
393
|
+
### Styling the picker
|
|
300
394
|
|
|
301
|
-
The
|
|
395
|
+
The stylesheet styles the button's layout and open look, the popover, the presets, Cally's parts and the state words.
|
|
396
|
+
Import it once, from the page's script or the app's CSS:
|
|
397
|
+
|
|
398
|
+
```js
|
|
399
|
+
import '@ismailnakkar/ui-behaviours/range-picker.css';
|
|
400
|
+
```
|
|
302
401
|
|
|
303
402
|
```css
|
|
304
|
-
.
|
|
403
|
+
@import '@ismailnakkar/ui-behaviours/range-picker.css' layer(components);
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
It is in no cascade layer, so it beats layered utility classes (Tailwind v4's) on the picker; the second form puts it in
|
|
407
|
+
the app's `components` layer, under its utilities.
|
|
408
|
+
|
|
409
|
+
It styles these, and nothing outside them:
|
|
410
|
+
|
|
411
|
+
| Selector | On |
|
|
412
|
+
|---|---|
|
|
413
|
+
| `.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. |
|
|
414
|
+
| `[data-range-picker]` | the popover |
|
|
415
|
+
| `.range-picker-panel` | the presets beside the calendar |
|
|
416
|
+
| `.range-picker-presets`, `.range-picker-preset` | the radio group, and each preset's label around its radio |
|
|
417
|
+
| `.range-picker-months` | the calendar's months, inside `<calendar-range>` |
|
|
418
|
+
| `.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 |
|
|
419
|
+
| `.when-preset`, `.when-pending` | the words for each `data-range-state` |
|
|
420
|
+
|
|
421
|
+
It draws no icons: the app sizes the button's, the arrows' in Cally's `previous` and `next` slots and any in a preset,
|
|
422
|
+
and mirrors the arrows on a right-to-left page.
|
|
423
|
+
|
|
424
|
+
Under 56rem the presets turn into a row of chips over the calendar. From 64rem, where the browser has anchor
|
|
425
|
+
positioning (Chrome 129, Safari 26), the popover opens under its button toward the line's end, flipped where that runs
|
|
426
|
+
out of room and no taller than the room it opens into, so a short window scrolls it; under 64rem it is centred. A
|
|
427
|
+
second picker on the same page anchors to the last button.
|
|
428
|
+
|
|
429
|
+
Override these custom properties on `:root`, or on one picker and its button. The defaults sit in `:where(:root)`, so
|
|
430
|
+
any rule of yours wins; the ones defaulting to another property follow it.
|
|
431
|
+
|
|
432
|
+
| Property | Default | Styles |
|
|
433
|
+
|---|---|---|
|
|
434
|
+
| `--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 |
|
|
435
|
+
| `--range-picker-muted-color` | `#687086` | The button's days and the summary |
|
|
436
|
+
| `--range-picker-preset-color` | `--range-picker-muted-color` | The presets |
|
|
437
|
+
| `--range-picker-preset-hover-color` | `--range-picker-text-color` | A preset under the pointer |
|
|
438
|
+
| `--range-picker-preset-hover-background` | `--range-picker-hover-background` | Its ground |
|
|
439
|
+
| `--range-picker-arrow-color` | `--range-picker-muted-color` | Cally's previous and next buttons |
|
|
440
|
+
| `--range-picker-arrow-hover-color` | `--range-picker-text-color` | An arrow under the pointer |
|
|
441
|
+
| `--range-picker-weekday-color` | `--range-picker-muted-color` | The weekday names |
|
|
442
|
+
| `--range-picker-background` | `#fff` | The popover, the arrows and the year select |
|
|
443
|
+
| `--range-picker-hover-background` | `#f4f6fb` | A day, an arrow and the year select under the pointer, and the button while the popover is open |
|
|
444
|
+
| `--range-picker-border-color` | `#e6e8ef` | The popover's border |
|
|
445
|
+
| `--range-picker-rule-color` | `--range-picker-border-color` | The rule beside the presets (under them as chips) and the one over the summary |
|
|
446
|
+
| `--range-picker-control-border-color` | `#cfd4df` | The arrows, the year select and the presets as chips |
|
|
447
|
+
| `--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 |
|
|
448
|
+
| `--range-picker-accent-hover-color` | `#2f48d4` | The range's ends under the pointer: set it to the accent to keep them as they are |
|
|
449
|
+
| `--range-picker-on-accent-color` | `#fff` | The range's ends' text |
|
|
450
|
+
| `--range-picker-selected-background` | `#eef1ff` | The picked preset |
|
|
451
|
+
| `--range-picker-band-background` | `--range-picker-selected-background` | The days between the ends |
|
|
452
|
+
| `--range-picker-band-color` | `#2f48d4` | Their text |
|
|
453
|
+
| `--range-picker-disabled-color` | `#aeb5c6` | The days out of the calendar's bounds |
|
|
454
|
+
| `--range-picker-disabled-band-background` | `--range-picker-border-color` | Picked days out of the bounds (a preset reaching before the first day) |
|
|
455
|
+
| `--range-picker-disabled-band-color` | `--range-picker-muted-color` | Their text |
|
|
456
|
+
| `--range-picker-disabled-end-background` | `--range-picker-control-border-color` | A range end out of the bounds |
|
|
457
|
+
| `--range-picker-disabled-end-color` | `--range-picker-text-color` | Its text |
|
|
458
|
+
| `--range-picker-radius` | `0.75rem` | The popover's corners |
|
|
459
|
+
| `--range-picker-control-radius` | `0.5rem` | The presets', the days', the arrows' and the year select's corners |
|
|
460
|
+
| `--range-picker-shadow` | `0 16px 40px -8px rgb(16 24 40 / 0.22)` | The popover's shadow |
|
|
461
|
+
| `--range-picker-font-size` | `0.875rem` | The popover's text size |
|
|
462
|
+
| `--range-picker-padding` | `1.25rem` | The popover's padding: set a smaller one under your phone width |
|
|
463
|
+
| `--range-picker-window-gap` | `0.75rem` | The least room the popover leaves to the window's edges |
|
|
464
|
+
| `--range-picker-offset` | `0.375rem` | The gap between the anchored popover and its button |
|
|
465
|
+
| `--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 |
|
|
466
|
+
| `--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 |
|
|
467
|
+
| `--range-picker-day-size` | `2.5rem` | A day's width and height |
|
|
468
|
+
| `--range-picker-preset-height` | `2.5rem` | A preset's least height |
|
|
469
|
+
| `--range-picker-chip-height` | `--range-picker-preset-height` | A chip's least height, where the presets are chips |
|
|
470
|
+
|
|
471
|
+
The weights and the text sizes are the stylesheet's, all from `--range-picker-font-size` but the year select (`1rem`),
|
|
472
|
+
the button's days (`0.8125rem`) and the weekday names (`0.75rem`).
|
|
473
|
+
|
|
474
|
+
The colours are light; a dark page sets its own under its dark selector. The stylesheet uses no `light-dark()`:
|
|
475
|
+
Lightning CSS (in Vite and Tailwind v4) lowers it to variables it sets only where the same stylesheet declares
|
|
476
|
+
`color-scheme`.
|
|
477
|
+
|
|
478
|
+
Without the stylesheet, the app shows the words each state calls for itself:
|
|
479
|
+
|
|
480
|
+
```css
|
|
481
|
+
[data-range-state] .when-pending,
|
|
305
482
|
[data-range-state="custom"] .when-preset,
|
|
306
483
|
[data-range-state="pending"] .when-preset,
|
|
307
484
|
[data-range-state="custom"] > [data-range-name] {
|
|
@@ -314,24 +491,48 @@ The words for each state are the app's, in the markup; the app shows the ones `d
|
|
|
314
491
|
```
|
|
315
492
|
|
|
316
493
|
While `pending` the checked radio is still the range the form sends. To show Custom range as the one being picked,
|
|
317
|
-
style `[data-range-state="pending"] label:has([data-range-custom])` as checked, and the checked label as not
|
|
318
|
-
|
|
319
|
-
### Events
|
|
320
|
-
|
|
321
|
-
The package fires none of its own. Cally's `change` and `rangestart` do not bubble, so listen to them in the capture
|
|
322
|
-
phase, as the package does. The radios fire the native `change`.
|
|
494
|
+
style `[data-range-state="pending"] label:has([data-range-custom])` as checked, and the checked label as not, as the
|
|
495
|
+
stylesheet does.
|
|
323
496
|
|
|
324
|
-
###
|
|
497
|
+
### The picker's accessibility
|
|
325
498
|
|
|
326
499
|
- Label the radio group (`aria-label`) and the button (`aria-labelledby` on its field label and itself), and give the
|
|
327
500
|
summary `aria-live="polite"`, so a pick is read out.
|
|
328
501
|
- Give Cally's previous and next slots an `aria-label`, and its year select a `label` slot.
|
|
329
502
|
|
|
330
|
-
|
|
503
|
+
## API
|
|
331
504
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
`
|
|
505
|
+
| Import | Does |
|
|
506
|
+
|---|---|
|
|
507
|
+
| `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 |
|
|
508
|
+
| `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 |
|
|
509
|
+
| `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 |
|
|
510
|
+
| `@ismailnakkar/ui-behaviours/range-picker.css` | the range picker's [stylesheet](#styling-the-picker) |
|
|
511
|
+
|
|
512
|
+
`copyText()` serves a page's own script, copying a link it just made:
|
|
513
|
+
|
|
514
|
+
```js
|
|
515
|
+
import {copyText} from '@ismailnakkar/ui-behaviours';
|
|
516
|
+
|
|
517
|
+
if (!(await copyText(url))) {
|
|
518
|
+
field.select();
|
|
519
|
+
}
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
## Events
|
|
523
|
+
|
|
524
|
+
The package fires none of its own. A dialog's native `close` fires when a close button or the backdrop closes it, and
|
|
525
|
+
the range picker's radios fire the native `change`. Cally's `change` and `rangestart` do not bubble: listen to them in
|
|
526
|
+
the capture phase, as the package does.
|
|
527
|
+
|
|
528
|
+
## Testing
|
|
529
|
+
|
|
530
|
+
The listeners need a DOM, so run your app's tests in happy-dom or jsdom (Vitest: `environment: 'happy-dom'`). They sit
|
|
531
|
+
on the document and outlive a test's markup: install them once per test file (`beforeAll`).
|
|
532
|
+
|
|
533
|
+
happy-dom and jsdom cannot run Cally. For the range picker, mock it, and define a `calendar-range` of your own that
|
|
534
|
+
holds `value`, `focusedDate` and `tentative`, and dispatches Cally's events, not bubbling: `change` once `value` holds
|
|
535
|
+
both days, `rangestart` with the start day as a `Date` in `detail`.
|
|
335
536
|
|
|
336
537
|
```js
|
|
337
538
|
vi.mock('cally', () => ({}));
|
|
@@ -342,3 +543,58 @@ customElements.define('calendar-range', class extends HTMLElement {
|
|
|
342
543
|
tentative = '';
|
|
343
544
|
});
|
|
344
545
|
```
|
|
546
|
+
|
|
547
|
+
A stepper needs nothing more: happy-dom and jsdom check `required` fields and send a form with `requestSubmit()`. To
|
|
548
|
+
test one opening on load, put its markup in place before installing, in a test file of its own or after
|
|
549
|
+
`vi.resetModules()` with a fresh `import()`.
|
|
550
|
+
|
|
551
|
+
Run this package's suite with `npm test`.
|
|
552
|
+
|
|
553
|
+
## Upgrading
|
|
554
|
+
|
|
555
|
+
### From 0.2
|
|
556
|
+
|
|
557
|
+
Nothing changes for a page on 0.2: the stepper, `data-show-if` and `data-clear-on` read only attributes of their own,
|
|
558
|
+
and the stylesheet loads only when imported. An app replacing its own copies changes these:
|
|
559
|
+
|
|
560
|
+
- **The two-factor stepper.** Delete the app's stepper script, its steps module and their tests, then in the dialog:
|
|
561
|
+
- add `data-steps` to the dialog;
|
|
562
|
+
- replace `data-email-code-sent` with `data-open-at-step="2"`, the email step;
|
|
563
|
+
- rename `data-step-turn-on` to `data-step-submit` (`data-step`, `data-step-back` and `data-step-next` stay);
|
|
564
|
+
- on the progress bars, rename `data-progress-step="n"` to `data-step-mark="n"`, draw mark 1 with
|
|
565
|
+
`data-step-state="current"`, and style `[data-step-state="done"]` and `[data-step-state="current"]` in place of the
|
|
566
|
+
`is-done` and `is-current` classes (or `data-state`);
|
|
567
|
+
- on the progress line's spans, rename `data-progress-step="n"` to `data-step-label="n"`;
|
|
568
|
+
- give each field the server refused `aria-invalid="true"`: the stepper reads no error class (`.validation-error`,
|
|
569
|
+
`.invalid-feedback`).
|
|
570
|
+
- **The payout destination form.** Delete the app's destination script, drop `data-destination-form`, add
|
|
571
|
+
`data-clear-on="method"` to the account field, and turn each `data-method-hint="paypal"` into
|
|
572
|
+
`data-show-if="method=paypal"`.
|
|
573
|
+
- **The range picker's look.** Import `range-picker.css` and set the custom properties from the app's palette and
|
|
574
|
+
sizes, with a smaller `--range-picker-padding` under the app's phone width (`1rem` for a popover that had 16px). In
|
|
575
|
+
the markup, `r-range-button` becomes `range-picker-button`, `r-range-panel` `range-picker-panel`, `r-presets`
|
|
576
|
+
`range-picker-presets`, `r-preset` `range-picker-preset`, `r-months` `range-picker-months`, `r-range-foot`
|
|
577
|
+
`range-picker-foot`, `r-range-sum` `range-picker-summary` and `r-pick-end` `range-picker-pick-end`; `r-range`,
|
|
578
|
+
`r-calendar`, `r-range-name` and `r-range-dates` go, as the stylesheet styles `[data-range-picker]`, its
|
|
579
|
+
`calendar-range`, `[data-range-name]` and `[data-range-dates]`. Utility classes that restyle the popover (border,
|
|
580
|
+
background, padding, shadow, text colour) go too; an opening animation stays.
|
|
581
|
+
|
|
582
|
+
Then delete the app's own picker rules, except those that draw the app's controls and icons, which the stylesheet
|
|
583
|
+
leaves to the app. Keep these, under the new class names:
|
|
584
|
+
- the range button's control: its height, padding, border, corners, background, text colour and size, hover and
|
|
585
|
+
transition, its icons' size and colour, and the chevron's `margin-inline-start: auto` (cuty's `.r-range-button`
|
|
586
|
+
and its desktop font size, upfiles' `.r-range-button > i`; exe draws it with utility classes);
|
|
587
|
+
- the arrows' icons in Cally's slots, both nested in `.r-calendar`, so move them out before deleting that block:
|
|
588
|
+
their size (cuty's `[slot] > svg`, as `[data-range-picker] [slot] > svg`) and their mirror on a right-to-left
|
|
589
|
+
page (upfiles' `.r-flip:dir(rtl)`; cuty's `flip-rtl` and exe's `rtl:-scale-x-100` sit outside the picker's
|
|
590
|
+
rules);
|
|
591
|
+
- a preset's icon size (cuty's `.r-preset svg`);
|
|
592
|
+
- the Set button: cuty's `.r-range-foot button` (height, padding, size, weight), and its full width on a phone,
|
|
593
|
+
cuty's `.r-range-foot button { flex: 1 }` and upfiles' `.r-range-foot .btn` (exe's `max-sm:flex-1` is in the
|
|
594
|
+
markup).
|
|
595
|
+
|
|
596
|
+
### From 0.1
|
|
597
|
+
|
|
598
|
+
- A preset radio gives its days in one attribute, `data-range-preset="from/to"`, in place of `data-from` and `data-to`:
|
|
599
|
+
`data-from="2026-11-14" data-to="2026-11-20"` becomes `data-range-preset="2026-11-14/2026-11-20"`. The old pair is
|
|
600
|
+
no longer read: picking a radio marked with it fills nothing, and checked it shows as `custom`.
|