@ismailnakkar/ui-behaviours 0.7.0 → 0.7.1

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
@@ -194,15 +194,15 @@ by one form.
194
194
 
195
195
  <form id="two-factor-enable" method="POST" action="/user/two-factor-authentication">
196
196
  <section data-step="scan">
197
- <h3 tabindex="-1">Scan the QR code</h3>
197
+ <h3>Scan the QR code</h3>
198
198
  …
199
199
  </section>
200
200
  <section data-step="email" hidden>
201
- <h3 tabindex="-1">Confirm your email</h3>
201
+ <h3>Confirm your email</h3>
202
202
  <input name="email_code" required autocomplete="one-time-code">
203
203
  </section>
204
204
  <section data-step="app" hidden>
205
- <h3 tabindex="-1">Enter the code from your app</h3>
205
+ <h3>Enter the code from your app</h3>
206
206
  <input name="code" required aria-invalid="true" aria-describedby="code-error" autocomplete="one-time-code">
207
207
  <p id="code-error">The code is not valid.</p>
208
208
  </section>
@@ -219,9 +219,9 @@ by one form.
219
219
  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
220
  not `hidden`. Its fields are the inputs, selects and textareas in it.
221
221
  - **`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.
222
+ the next step and focuses its first heading. From the last step it submits, as `data-step-submit` does. Enter in an
223
+ input of a step does the same, so the form's own submit never skips a step's check; Enter while an input method
224
+ composes is left alone.
225
225
  - **`data-step-back`** shows the step before.
226
226
  - **`data-step-submit`**: checks every step's fields; one left empty shows its step and the browser's message, else the
227
227
  form is sent (`requestSubmit()`, so `data-confirm` and `data-submit-once` apply).
@@ -386,13 +386,14 @@ The package sets attributes and the browser's own states; the app styles them:
386
386
  - A change that submits moves the page under the user, and on some desktop browsers an arrow key on a closed select
387
387
  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
388
  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"`.
389
+ - A stepper moves focus to the new step's heading, so a screen reader starts where the step does. Hide the progress
390
+ marks (`aria-hidden="true"`) and give the `data-step-progress` element `aria-live="polite"`.
392
391
  - Point a field's `aria-describedby` at its `data-show-if` hints' box, in an `aria-live="polite"` region, so a warning
393
392
  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`.
393
+ - A dismissed banner hands focus to `<main>`; on a page without one, focus falls to `<body>`. Give a `data-dismiss`
394
+ button that shows only an icon an `aria-label`.
395
+ - A heading or a `<main>` the package focuses takes no focus of its own: one without a `tabindex` gets
396
+ `tabindex="-1"` first, so script can focus it and Tab still skips it. A `tabindex` of the page's own is kept.
396
397
  - A `data-dropdown`'s `<summary>` is its button: put the menu's name in it, and a `<details>` says itself whether it is
397
398
  open.
398
399
 
@@ -665,6 +666,11 @@ Run this package's suite with `npm test`: oxlint, then Vitest.
665
666
 
666
667
  ## Upgrading
667
668
 
669
+ ### From 0.7.0
670
+
671
+ Nothing changes for a page on 0.7.0. `<main>` and a stepper's headings no longer need `tabindex="-1"` in the markup
672
+ for the package to focus them: it sets one where there is none. A page may delete its own; leaving it changes nothing.
673
+
668
674
  ### From 0.6
669
675
 
670
676
  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,6 +1,6 @@
1
1
  {
2
2
  "name": "@ismailnakkar/ui-behaviours",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
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.",
5
5
  "keywords": [
6
6
  "clipboard",
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);
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) {