@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/src/steps.js ADDED
@@ -0,0 +1,197 @@
1
+ import {closestTo} from './helpers.js';
2
+
3
+ const STEPPER = '[data-steps]';
4
+ const STEP = '[data-step]';
5
+ const FIELDS = 'input, select, textarea';
6
+ const HEADING = 'h1, h2, h3, h4, h5, h6';
7
+ const BACK = '[data-step-back]';
8
+ const NEXT = '[data-step-next]';
9
+ const SUBMIT = '[data-step-submit]';
10
+ const MARK_STATE = 'data-step-state';
11
+
12
+ /** Steps are numbered from 1, in the order they come. */
13
+ const FIRST_STEP = 1;
14
+
15
+ /** The values of data-step-state on a progress mark: the steps behind are done, the one showing is current. */
16
+ const MarkState = Object.freeze({done: 'done', current: 'current'});
17
+
18
+ /** Listens for the steppers' buttons and Enter, and shows each stepper on the page at the step it opens at. */
19
+ export function installSteps() {
20
+ document.addEventListener('click', stepOnClick);
21
+ document.addEventListener('keydown', stepOnEnter);
22
+
23
+ for (const stepper of document.querySelectorAll(STEPPER)) {
24
+ openStepper(stepper);
25
+ }
26
+ }
27
+
28
+ // preventDefault(): a control that is a submit button moves the steps without sending the form.
29
+ function stepOnClick(event) {
30
+ const control = closestTo(event, `${BACK}, ${NEXT}, ${SUBMIT}`);
31
+ const stepper = control?.closest(STEPPER);
32
+
33
+ if (!stepper || event.defaultPrevented) {
34
+ return;
35
+ }
36
+
37
+ event.preventDefault();
38
+
39
+ if (control.matches(BACK)) {
40
+ back(stepper);
41
+ } else if (control.matches(NEXT)) {
42
+ advance(stepper);
43
+ } else {
44
+ submit(stepper);
45
+ }
46
+ }
47
+
48
+ // Enter in a field moves on, as Next does: the form's own submit would skip the steps' checks.
49
+ function stepOnEnter(event) {
50
+ const field = event.target;
51
+ const stepper = field instanceof HTMLInputElement ? field.closest(STEP)?.closest(STEPPER) : null;
52
+
53
+ if (event.key !== 'Enter' || event.isComposing || event.defaultPrevented || !stepper) {
54
+ return;
55
+ }
56
+
57
+ event.preventDefault();
58
+ advance(stepper);
59
+ }
60
+
61
+ /**
62
+ * 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.
65
+ */
66
+ function openStepper(stepper) {
67
+ const opening = openingStepOf(stepper);
68
+
69
+ show(stepper, opening ?? currentStepOf(stepper));
70
+
71
+ if (opening !== null && stepper instanceof HTMLDialogElement && !stepper.open) {
72
+ stepper.showModal();
73
+ }
74
+ }
75
+
76
+ function openingStepOf(stepper) {
77
+ const fields = fieldsOf(stepper);
78
+
79
+ if (fields.some(isRefused)) {
80
+ return stepOf(fields.find((field) => isRefused(field) || !field.checkValidity()));
81
+ }
82
+
83
+ const asked = Number(stepper.dataset.openAtStep);
84
+
85
+ return sectionOf(stepper, asked) ? asked : null;
86
+ }
87
+
88
+ /** A field the server refused: aria-invalid="true" beside its message. */
89
+ function isRefused(field) {
90
+ return field.getAttribute('aria-invalid') === 'true';
91
+ }
92
+
93
+ function back(stepper) {
94
+ const step = currentStepOf(stepper);
95
+
96
+ if (step > FIRST_STEP) {
97
+ moveTo(stepper, step - 1);
98
+ }
99
+ }
100
+
101
+ /** Moves on once the step's fields pass, or submits from the last step. */
102
+ function advance(stepper) {
103
+ const step = currentStepOf(stepper);
104
+
105
+ if (isLast(stepper, step)) {
106
+ submit(stepper);
107
+ } else if (fieldsOf(sectionOf(stepper, step)).every((field) => field.reportValidity())) {
108
+ moveTo(stepper, step + 1);
109
+ }
110
+ }
111
+
112
+ // An empty field on another step would block the submit with nothing on screen to say why: show its step instead.
113
+ function submit(stepper) {
114
+ const invalid = fieldsOf(stepper).find((field) => !field.checkValidity());
115
+
116
+ if (invalid) {
117
+ show(stepper, stepOf(invalid));
118
+ invalid.reportValidity();
119
+
120
+ return;
121
+ }
122
+
123
+ stepper.querySelector(STEP)?.closest('form')?.requestSubmit();
124
+ }
125
+
126
+ // To the new step's heading, so a screen reader starts where the step does.
127
+ function moveTo(stepper, step) {
128
+ show(stepper, step);
129
+ sectionOf(stepper, step)?.querySelector(HEADING)?.focus();
130
+ }
131
+
132
+ function show(stepper, step) {
133
+ for (const section of stepsOf(stepper)) {
134
+ section.hidden = stepOf(section) !== step;
135
+ }
136
+
137
+ for (const mark of stepper.querySelectorAll('[data-step-mark]')) {
138
+ const state = markStateOf(Number(mark.dataset.stepMark), step);
139
+
140
+ if (state === null) {
141
+ mark.removeAttribute(MARK_STATE);
142
+ } else {
143
+ mark.setAttribute(MARK_STATE, state);
144
+ }
145
+ }
146
+
147
+ for (const label of stepper.querySelectorAll('[data-step-label]')) {
148
+ label.hidden = Number(label.dataset.stepLabel) !== step;
149
+ }
150
+
151
+ hideAll(stepper, BACK, step === FIRST_STEP);
152
+ hideAll(stepper, NEXT, isLast(stepper, step));
153
+ hideAll(stepper, SUBMIT, !isLast(stepper, step));
154
+ }
155
+
156
+ function markStateOf(mark, step) {
157
+ if (mark < step) {
158
+ return MarkState.done;
159
+ }
160
+
161
+ return mark === step ? MarkState.current : null;
162
+ }
163
+
164
+ function hideAll(stepper, selector, hidden) {
165
+ for (const element of stepper.querySelectorAll(selector)) {
166
+ element.hidden = hidden;
167
+ }
168
+ }
169
+
170
+ function stepsOf(stepper) {
171
+ return [...stepper.querySelectorAll(STEP)];
172
+ }
173
+
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);
181
+ }
182
+
183
+ /** The step showing; the first when none does. */
184
+ function currentStepOf(stepper) {
185
+ const showing = stepsOf(stepper).find((section) => !section.hidden);
186
+
187
+ return showing ? stepOf(showing) : FIRST_STEP;
188
+ }
189
+
190
+ function isLast(stepper, step) {
191
+ return step >= stepsOf(stepper).length;
192
+ }
193
+
194
+ /** The fields of a step, or of every step in a stepper. */
195
+ function fieldsOf(element) {
196
+ return [...element.querySelectorAll(FIELDS)].filter((field) => field.closest(STEP));
197
+ }