@ismailnakkar/ui-behaviours 0.5.0 → 0.6.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.
Files changed (3) hide show
  1. package/README.md +27 -17
  2. package/package.json +1 -1
  3. package/src/steps.js +37 -28
package/README.md CHANGED
@@ -184,24 +184,20 @@ by one form.
184
184
  <dialog id="two-factor-dialog" aria-labelledby="two-factor-title" data-steps>
185
185
  <h2 id="two-factor-title">Turn on two-factor</h2>
186
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>
187
+ <i data-step-mark data-step-state="current"></i><i data-step-mark></i><i data-step-mark></i>
188
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>
189
+ <p aria-live="polite" data-step-progress="Step :step of :steps"></p>
194
190
 
195
191
  <form id="two-factor-enable" method="POST" action="/user/two-factor-authentication">
196
- <section data-step="1">
192
+ <section data-step="scan">
197
193
  <h3 tabindex="-1">Scan the QR code</h3>
198
194
  …
199
195
  </section>
200
- <section data-step="2" hidden>
196
+ <section data-step="email" hidden>
201
197
  <h3 tabindex="-1">Confirm your email</h3>
202
198
  <input name="email_code" required autocomplete="one-time-code">
203
199
  </section>
204
- <section data-step="3" hidden>
200
+ <section data-step="app" hidden>
205
201
  <h3 tabindex="-1">Enter the code from your app</h3>
206
202
  <input name="code" required aria-invalid="true" aria-describedby="code-error" autocomplete="one-time-code">
207
203
  <p id="code-error">The code is not valid.</p>
@@ -215,8 +211,9 @@ by one form.
215
211
  </dialog>
216
212
  ```
217
213
 
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.
214
+ - **`data-step`** on each step, all inside one form: the steps go in the order they come, so none is numbered. Its
215
+ value is a name, needed only for `data-open-at-step` to point at; `data-step` alone is fine. Only the step showing is
216
+ not `hidden`. Its fields are the inputs, selects and textareas in it.
220
217
  - **`data-step-next`**: checks the step's fields (`reportValidity()`, so the browser says what is missing), then shows
221
218
  the next step and focuses its first heading; give the heading `tabindex="-1"`. From the last step it submits, as
222
219
  `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,9 +221,12 @@ by one form.
224
221
  - **`data-step-back`** shows the step before.
225
222
  - **`data-step-submit`**: checks every step's fields; one left empty shows its step and the browser's message, else the
226
223
  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.
224
+ - **`data-step-mark`**, a progress mark, one per step in the same order: gets `data-step-state="done"` behind the step
225
+ showing and `"current"` for it; the marks ahead have none.
226
+ - **`data-step-progress="…"`**: the stepper writes this wording as the element's text, with `:step` as the showing
227
+ step's place from 1 and `:steps` as the count, such as "Step 2 of 3" in a live region. Laravel's `__()` given no
228
+ replacements leaves both placeholders in, so `data-step-progress="{{ __('enrolment.progress') }}"` works in every
229
+ locale.
230
230
 
231
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
232
  sending the form. Back is hidden on the first step, Next on the last, and Submit on every other.
@@ -237,11 +237,12 @@ On install, a stepper opens (`showModal()` on a dialog) at:
237
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
238
  comes first. `aria-invalid="true"` is the one error mark the stepper reads: set it on each field the server refused,
239
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";
240
+ 2. else **`data-open-at-step="name"`** on the stepper, the step whose `data-step` has that name, such as the email
241
+ step on the page after "Email me a code";
241
242
  3. else nowhere: a dialog stays shut, and the stepper shows the step the server drew.
242
243
 
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.
244
+ Draw the first state on the server too (the first step showing, its mark current, Back and Submit hidden): it is what
245
+ shows before the script runs. The `data-step-progress` text is written on install. A stepper added later works, but opens at nothing.
245
246
 
246
247
  ### `data-show-if="name=value"` and `data-clear-on="name"`
247
248
 
@@ -601,6 +602,15 @@ Run this package's suite with `npm test`.
601
602
 
602
603
  ## Upgrading
603
604
 
605
+ ### From 0.5
606
+
607
+ Steppers go by order and name, never by number:
608
+
609
+ - `data-step="1"`, `"2"`, … become `data-step`, or `data-step="name"` for a step that `data-open-at-step` points at.
610
+ - `data-open-at-step` takes that name: `data-open-at-step="2"` opens nothing now.
611
+ - `data-step-mark="n"` becomes `data-step-mark`, one per step in the same order.
612
+ - `data-step-label="n"` is gone: one `data-step-progress="Step :step of :steps"` element replaces the labels.
613
+
604
614
  ### From 0.4
605
615
 
606
616
  - The stylesheet no longer reads `--range-picker-shadow`, `--range-picker-font-size`, `--range-picker-window-gap`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ismailnakkar/ui-behaviours",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
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), and a date range picker over Cally with an optional stylesheet.",
5
5
  "keywords": [
6
6
  "clipboard",
package/src/steps.js CHANGED
@@ -2,6 +2,8 @@ import {closestTo} from './helpers.js';
2
2
 
3
3
  const STEPPER = '[data-steps]';
4
4
  const STEP = '[data-step]';
5
+ const MARK = '[data-step-mark]';
6
+ const PROGRESS = '[data-step-progress]';
5
7
  const FIELDS = 'input, select, textarea';
6
8
  const HEADING = 'h1, h2, h3, h4, h5, h6';
7
9
  const BACK = '[data-step-back]';
@@ -9,8 +11,11 @@ const NEXT = '[data-step-next]';
9
11
  const SUBMIT = '[data-step-submit]';
10
12
  const MARK_STATE = 'data-step-state';
11
13
 
12
- /** Steps are numbered from 1, in the order they come. */
13
- const FIRST_STEP = 1;
14
+ /** Steps go by their place in the markup: the first is 0 here, and 1 in data-step-progress's wording. */
15
+ const FIRST_STEP = 0;
16
+
17
+ /** data-step-progress's placeholders, as Laravel's __() leaves them when given none: the showing step's place and the count. */
18
+ const PROGRESS_PLACEHOLDER = /:(steps|step)\b/g;
14
19
 
15
20
  /** The values of data-step-state on a progress mark: the steps behind are done, the one showing is current. */
16
21
  const MarkState = Object.freeze({done: 'done', current: 'current'});
@@ -60,8 +65,8 @@ function stepOnEnter(event) {
60
65
 
61
66
  /**
62
67
  * A refused submit comes back with its errors in the stepper: it opens at the earliest step with a refused or an empty
63
- * field, as a field the server never hands back (a mailed code) is empty again. Otherwise at data-open-at-step, or
64
- * shut at the step it shows.
68
+ * field, as a field the server never hands back (a mailed code) is empty again. Otherwise at the step
69
+ * data-open-at-step names, or shut at the step it shows.
65
70
  */
66
71
  function openStepper(stepper) {
67
72
  const opening = openingStepOf(stepper);
@@ -77,12 +82,14 @@ function openingStepOf(stepper) {
77
82
  const fields = fieldsOf(stepper);
78
83
 
79
84
  if (fields.some(isRefused)) {
80
- return stepOf(fields.find((field) => isRefused(field) || !field.checkValidity()));
85
+ return stepOf(stepper, fields.find((field) => isRefused(field) || !field.checkValidity()));
81
86
  }
82
87
 
83
- const asked = Number(stepper.dataset.openAtStep);
88
+ // Unnamed steps carry an empty data-step, which an empty data-open-at-step must not pick.
89
+ const name = stepper.dataset.openAtStep;
90
+ const named = name ? stepsOf(stepper).findIndex((section) => section.dataset.step === name) : -1;
84
91
 
85
- return sectionOf(stepper, asked) ? asked : null;
92
+ return named === -1 ? null : named;
86
93
  }
87
94
 
88
95
  /** A field the server refused: aria-invalid="true" beside its message. */
@@ -104,7 +111,7 @@ function advance(stepper) {
104
111
 
105
112
  if (isLast(stepper, step)) {
106
113
  submit(stepper);
107
- } else if (fieldsOf(sectionOf(stepper, step)).every((field) => field.reportValidity())) {
114
+ } else if (fieldsOf(stepsOf(stepper)[step]).every((field) => field.reportValidity())) {
108
115
  moveTo(stepper, step + 1);
109
116
  }
110
117
  }
@@ -114,7 +121,7 @@ function submit(stepper) {
114
121
  const invalid = fieldsOf(stepper).find((field) => !field.checkValidity());
115
122
 
116
123
  if (invalid) {
117
- show(stepper, stepOf(invalid));
124
+ show(stepper, stepOf(stepper, invalid));
118
125
  invalid.reportValidity();
119
126
 
120
127
  return;
@@ -126,26 +133,32 @@ function submit(stepper) {
126
133
  // To the new step's heading, so a screen reader starts where the step does.
127
134
  function moveTo(stepper, step) {
128
135
  show(stepper, step);
129
- sectionOf(stepper, step)?.querySelector(HEADING)?.focus();
136
+ stepsOf(stepper)[step]?.querySelector(HEADING)?.focus();
130
137
  }
131
138
 
132
139
  function show(stepper, step) {
133
- for (const section of stepsOf(stepper)) {
134
- section.hidden = stepOf(section) !== step;
135
- }
140
+ const steps = stepsOf(stepper);
141
+
142
+ steps.forEach((section, place) => {
143
+ section.hidden = place !== step;
144
+ });
136
145
 
137
- for (const mark of stepper.querySelectorAll('[data-step-mark]')) {
138
- const state = markStateOf(Number(mark.dataset.stepMark), step);
146
+ // Marks pair with the steps by place: the first mark is the first step's.
147
+ stepper.querySelectorAll(MARK).forEach((mark, place) => {
148
+ const state = markStateOf(place, step);
139
149
 
140
150
  if (state === null) {
141
151
  mark.removeAttribute(MARK_STATE);
142
152
  } else {
143
153
  mark.setAttribute(MARK_STATE, state);
144
154
  }
145
- }
155
+ });
146
156
 
147
- for (const label of stepper.querySelectorAll('[data-step-label]')) {
148
- label.hidden = Number(label.dataset.stepLabel) !== step;
157
+ for (const progress of stepper.querySelectorAll(PROGRESS)) {
158
+ progress.textContent = progress.dataset.stepProgress.replace(
159
+ PROGRESS_PLACEHOLDER,
160
+ (_placeholder, name) => String(name === 'steps' ? steps.length : step + 1),
161
+ );
149
162
  }
150
163
 
151
164
  hideAll(stepper, BACK, step === FIRST_STEP);
@@ -171,24 +184,20 @@ function stepsOf(stepper) {
171
184
  return [...stepper.querySelectorAll(STEP)];
172
185
  }
173
186
 
174
- function sectionOf(stepper, step) {
175
- return stepsOf(stepper).find((section) => stepOf(section) === step);
176
- }
177
-
178
- /** The step a section or a field belongs to. */
179
- function stepOf(element) {
180
- return Number(element.closest(STEP).dataset.step);
187
+ /** The place of the step a field belongs to. */
188
+ function stepOf(stepper, field) {
189
+ return stepsOf(stepper).indexOf(field.closest(STEP));
181
190
  }
182
191
 
183
192
  /** The step showing; the first when none does. */
184
193
  function currentStepOf(stepper) {
185
- const showing = stepsOf(stepper).find((section) => !section.hidden);
194
+ const showing = stepsOf(stepper).findIndex((section) => !section.hidden);
186
195
 
187
- return showing ? stepOf(showing) : FIRST_STEP;
196
+ return showing === -1 ? FIRST_STEP : showing;
188
197
  }
189
198
 
190
199
  function isLast(stepper, step) {
191
- return step >= stepsOf(stepper).length;
200
+ return step >= stepsOf(stepper).length - 1;
192
201
  }
193
202
 
194
203
  /** The fields of a step, or of every step in a stepper. */