@ismailnakkar/ui-behaviours 0.7.0 → 0.8.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 CHANGED
@@ -8,8 +8,9 @@ the document, so markup added later works too. A `post()` helper sends a page sc
8
8
  one.
9
9
 
10
10
  A date range picker over [Cally](https://wicky.nillia.ms/cally/) has its own entry, so pages without one never load the
11
- calendar, and an optional stylesheet themed by custom properties. Nothing else ships styles, and nothing ships wording:
12
- the app styles the states the package sets, and every word comes from the app's markup.
11
+ calendar, and an optional stylesheet themed by custom properties. A second optional stylesheet draws
12
+ [scrollbars](#scrollbars) that stay in sight. Nothing else ships styles, and nothing ships wording: the app styles the
13
+ states the package sets, and every word comes from the app's markup.
13
14
 
14
15
  ## Install
15
16
 
@@ -194,15 +195,15 @@ by one form.
194
195
 
195
196
  <form id="two-factor-enable" method="POST" action="/user/two-factor-authentication">
196
197
  <section data-step="scan">
197
- <h3 tabindex="-1">Scan the QR code</h3>
198
+ <h3>Scan the QR code</h3>
198
199
  …
199
200
  </section>
200
201
  <section data-step="email" hidden>
201
- <h3 tabindex="-1">Confirm your email</h3>
202
+ <h3>Confirm your email</h3>
202
203
  <input name="email_code" required autocomplete="one-time-code">
203
204
  </section>
204
205
  <section data-step="app" hidden>
205
- <h3 tabindex="-1">Enter the code from your app</h3>
206
+ <h3>Enter the code from your app</h3>
206
207
  <input name="code" required aria-invalid="true" aria-describedby="code-error" autocomplete="one-time-code">
207
208
  <p id="code-error">The code is not valid.</p>
208
209
  </section>
@@ -219,9 +220,9 @@ by one form.
219
220
  value is a name, needed only for `data-open-at-step` to point at; `data-step` alone is fine. Only the step showing is
220
221
  not `hidden`. Its fields are the inputs, selects and textareas in it.
221
222
  - **`data-step-next`**: checks the step's fields (`reportValidity()`, so the browser says what is missing), then shows
222
- the next step and focuses its first heading; give the heading `tabindex="-1"`. From the last step it submits, as
223
- `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
224
- check; Enter while an input method composes is left alone.
223
+ the next step and focuses its first heading. From the last step it submits, as `data-step-submit` does. Enter in an
224
+ input of a step does the same, so the form's own submit never skips a step's check; Enter while an input method
225
+ composes is left alone.
225
226
  - **`data-step-back`** shows the step before.
226
227
  - **`data-step-submit`**: checks every step's fields; one left empty shows its step and the browser's message, else the
227
228
  form is sent (`requestSubmit()`, so `data-confirm` and `data-submit-once` apply).
@@ -386,16 +387,60 @@ The package sets attributes and the browser's own states; the app styles them:
386
387
  - A change that submits moves the page under the user, and on some desktop browsers an arrow key on a closed select
387
388
  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
388
389
  submit button for a page without script.
389
- - A stepper moves focus to the new step's heading, so a screen reader starts where the step does: give each a
390
- `tabindex="-1"`. Hide the progress marks (`aria-hidden="true"`) and give the `data-step-progress` element
391
- `aria-live="polite"`.
390
+ - A stepper moves focus to the new step's heading, so a screen reader starts where the step does. Hide the progress
391
+ marks (`aria-hidden="true"`) and give the `data-step-progress` element `aria-live="polite"`.
392
392
  - Point a field's `aria-describedby` at its `data-show-if` hints' box, in an `aria-live="polite"` region, so a warning
393
393
  shown on a pick is read out.
394
- - A dismissed banner hands focus to `<main>`: give it `tabindex="-1"` (a skip link's target already has it), or focus
395
- falls to `<body>`. Give a `data-dismiss` button that shows only an icon an `aria-label`.
394
+ - A dismissed banner hands focus to `<main>`; on a page without one, focus falls to `<body>`. Give a `data-dismiss`
395
+ button that shows only an icon an `aria-label`.
396
+ - A heading or a `<main>` the package focuses takes no focus of its own: one without a `tabindex` gets
397
+ `tabindex="-1"` first, so script can focus it and Tab still skips it. A `tabindex` of the page's own is kept.
396
398
  - A `data-dropdown`'s `<summary>` is its button: put the menu's name in it, and a `<details>` says itself whether it is
397
399
  open.
398
400
 
401
+ ## Scrollbars
402
+
403
+ On macOS a scrollbar shows only while the box moves, so a box cut short by a `max-height` reads as the whole list. The
404
+ scrollbar stylesheet draws a slim bar that stays in sight whenever a box with the `scrollbar` class overflows, in
405
+ Chrome, Edge and Safari. Firefox gets a thin bar in the same colours and keeps the system's choice of when to show it.
406
+ Import it once, from the page's script or the app's CSS:
407
+
408
+ ```js
409
+ import '@ismailnakkar/ui-behaviours/scrollbar.css';
410
+ ```
411
+
412
+ ```css
413
+ @import '@ismailnakkar/ui-behaviours/scrollbar.css' layer(components);
414
+ ```
415
+
416
+ Then give the scrolling box the class:
417
+
418
+ ```html
419
+ <div class="scrollbar" style="max-height: 32rem; overflow-y: auto">…</div>
420
+ ```
421
+
422
+ A Sass stylesheet can load it as a module and extend the class onto its own selectors instead of adding it to markup:
423
+
424
+ ```scss
425
+ @use '@ismailnakkar/ui-behaviours/scrollbar.css';
426
+
427
+ .sidebar {
428
+ overflow-y: auto;
429
+ @extend .scrollbar;
430
+ }
431
+ ```
432
+
433
+ Three custom properties theme it, set on `:root` for every bar or on a box for its own:
434
+
435
+ | Property | Default | Is |
436
+ |---|---|---|
437
+ | `--scrollbar-size` | `0.5rem` | the bar's width, or height along the bottom, and its thumb's corner radius |
438
+ | `--scrollbar-thumb-color` | `#b4bac9` | the part that moves |
439
+ | `--scrollbar-track-color` | `transparent` | the groove behind it, and the corner where two bars meet |
440
+
441
+ Set no `scrollbar-width` or `scrollbar-color` of your own on the box: Chrome 121+ draws its plain bar once either is
442
+ set, and ignores the stylesheet's.
443
+
399
444
  ## Range picker
400
445
 
401
446
  A date range for a GET form: a button opens a native popover with the app's preset ranges beside a Cally
@@ -599,6 +644,7 @@ stylesheet does.
599
644
  | `post(url, body)` | `fetch()`es a `FormData` or `URLSearchParams` body to `url` as a POST that Laravel answers as an AJAX request; resolves to the `Response` |
600
645
  | `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 |
601
646
  | `@ismailnakkar/ui-behaviours/range-picker.css` | the range picker's [stylesheet](#styling-the-picker) |
647
+ | `@ismailnakkar/ui-behaviours/scrollbar.css` | the `scrollbar` class's [stylesheet](#scrollbars) |
602
648
 
603
649
  `copyText()` serves a page's own script, copying a link it just made:
604
650
 
@@ -665,6 +711,16 @@ Run this package's suite with `npm test`: oxlint, then Vitest.
665
711
 
666
712
  ## Upgrading
667
713
 
714
+ ### From 0.7
715
+
716
+ Nothing changes for a page on 0.7. The new [scrollbar stylesheet](#scrollbars) is opt-in: a page that never imports it
717
+ looks the same.
718
+
719
+ ### From 0.7.0
720
+
721
+ Nothing changes for a page on 0.7.0. `<main>` and a stepper's headings no longer need `tabindex="-1"` in the markup
722
+ for the package to focus them: it sets one where there is none. A page may delete its own; leaving it changes nothing.
723
+
668
724
  ### From 0.6
669
725
 
670
726
  The package now acts on `data-dismiss` and `details[data-dropdown]`. A page without either works as before; one with
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ismailnakkar/ui-behaviours",
3
- "version": "0.7.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, dismiss a banner, light-dismiss a details menu), a Laravel-ready post(), and a date range picker over Cally with an optional stylesheet.",
3
+ "version": "0.8.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, dismiss a banner, light-dismiss a details menu), a Laravel-ready post(), a date range picker over Cally with an optional stylesheet, and an optional scrollbar stylesheet.",
5
5
  "keywords": [
6
6
  "clipboard",
7
7
  "dialog",
@@ -13,7 +13,8 @@
13
13
  "dropdown",
14
14
  "dismiss",
15
15
  "fetch",
16
- "laravel"
16
+ "laravel",
17
+ "scrollbar"
17
18
  ],
18
19
  "homepage": "https://github.com/ismailnakkar/ui-behaviours",
19
20
  "bugs": {
@@ -43,6 +44,10 @@
43
44
  "./range-picker.css": {
44
45
  "types": "./src/range-picker.d.css.ts",
45
46
  "default": "./src/range-picker.css"
47
+ },
48
+ "./scrollbar.css": {
49
+ "types": "./src/scrollbar.d.css.ts",
50
+ "default": "./src/scrollbar.css"
46
51
  }
47
52
  },
48
53
  "files": [
@@ -50,7 +55,8 @@
50
55
  ],
51
56
  "sideEffects": [
52
57
  "./src/range-picker.js",
53
- "./src/range-picker.css"
58
+ "./src/range-picker.css",
59
+ "./src/scrollbar.css"
54
60
  ],
55
61
  "publishConfig": {
56
62
  "access": "public"
package/src/helpers.js CHANGED
@@ -1,3 +1,6 @@
1
+ /** tabindex="-1": script may focus the element, Tab still skips it. */
2
+ const FOCUSABLE_FROM_SCRIPT = -1;
3
+
1
4
  /** Wraps an install function so only its first call runs. */
2
5
  export function once(install) {
3
6
  let installed = false;
@@ -22,3 +25,19 @@ export function isRadio(element) {
22
25
  export function isCheckbox(element) {
23
26
  return element instanceof HTMLInputElement && element.type === 'checkbox';
24
27
  }
28
+
29
+ /**
30
+ * Focuses an element that takes no focus of its own, such as <main> or a heading, giving it tabindex="-1" first, so the
31
+ * page needs none in its markup; a tabindex the page set is kept. A missing element leaves focus where it is.
32
+ */
33
+ export function moveFocusTo(element, options) {
34
+ if (!element) {
35
+ return;
36
+ }
37
+
38
+ if (!element.hasAttribute('tabindex')) {
39
+ element.tabIndex = FOCUSABLE_FROM_SCRIPT;
40
+ }
41
+
42
+ element.focus(options);
43
+ }
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import {closestTo, isCheckbox, isRadio, once} from './helpers.js';
1
+ import {closestTo, isCheckbox, isRadio, moveFocusTo, once} from './helpers.js';
2
2
  import {installSteps} from './steps.js';
3
3
 
4
4
  const COPY_FLASH_MS = 1500;
@@ -363,7 +363,7 @@ function dismissBanner(event) {
363
363
  }
364
364
 
365
365
  if (banner.contains(document.activeElement)) {
366
- document.querySelector('main')?.focus({preventScroll: true});
366
+ moveFocusTo(document.querySelector('main'), {preventScroll: true});
367
367
  }
368
368
 
369
369
  fadeOut(banner);
@@ -0,0 +1,38 @@
1
+ /*
2
+ * A styled scrollbar (README, "Scrollbars"): on any box with the `scrollbar` class, a slim bar that stays drawn while
3
+ * the box overflows, where macOS would otherwise show one only during a scroll. Every app value is a custom property;
4
+ * the defaults sit in :where(:root), so any rule of the app's wins, and a box may set its own.
5
+ *
6
+ * Chromium and Safari draw the ::-webkit-scrollbar parts. Firefox gets the standard pair, and only there: Chrome 121+
7
+ * reads scrollbar-width and scrollbar-color too, and once either is set it ignores every ::-webkit-scrollbar rule.
8
+ */
9
+ :where(:root) {
10
+ --scrollbar-size: 0.5rem;
11
+ --scrollbar-thumb-color: #b4bac9;
12
+ --scrollbar-track-color: transparent;
13
+ }
14
+
15
+ .scrollbar::-webkit-scrollbar {
16
+ width: var(--scrollbar-size);
17
+ height: var(--scrollbar-size);
18
+ }
19
+
20
+ .scrollbar::-webkit-scrollbar-track {
21
+ background: var(--scrollbar-track-color);
22
+ }
23
+
24
+ .scrollbar::-webkit-scrollbar-thumb {
25
+ border-radius: var(--scrollbar-size);
26
+ background: var(--scrollbar-thumb-color);
27
+ }
28
+
29
+ .scrollbar::-webkit-scrollbar-corner {
30
+ background: var(--scrollbar-track-color);
31
+ }
32
+
33
+ @supports not selector(::-webkit-scrollbar) {
34
+ .scrollbar {
35
+ scrollbar-width: thin;
36
+ scrollbar-color: var(--scrollbar-thumb-color) var(--scrollbar-track-color);
37
+ }
38
+ }
@@ -0,0 +1,2 @@
1
+ // Lets TypeScript accept `import '…/scrollbar.css'` without the app declaring *.css modules.
2
+ export {};
package/src/steps.js CHANGED
@@ -1,4 +1,4 @@
1
- import {closestTo} from './helpers.js';
1
+ import {closestTo, moveFocusTo} from './helpers.js';
2
2
 
3
3
  const STEPPER = '[data-steps]';
4
4
  const STEP = '[data-step]';
@@ -133,7 +133,7 @@ function submit(stepper) {
133
133
  // To the new step's heading, so a screen reader starts where the step does.
134
134
  function moveTo(stepper, step) {
135
135
  show(stepper, step);
136
- stepsOf(stepper)[step]?.querySelector(HEADING)?.focus();
136
+ moveFocusTo(stepsOf(stepper)[step]?.querySelector(HEADING));
137
137
  }
138
138
 
139
139
  function show(stepper, step) {