@govuk-react/primitives 0.2.1 → 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/README.md +109 -25
- package/dist/accordion/element.d.ts +3 -0
- package/dist/accordion/element.js.map +1 -1
- package/dist/accordion/index.d.ts +3 -7
- package/dist/accordion/index.js +35 -33
- package/dist/accordion/index.js.map +1 -1
- package/dist/auto-focus/index.d.ts +3 -285
- package/dist/auto-focus/index.js +13 -3
- package/dist/auto-focus/index.js.map +1 -1
- package/dist/autocomplete/element.d.ts +7 -4
- package/dist/autocomplete/element.js +11 -11
- package/dist/autocomplete/element.js.map +1 -1
- package/dist/autocomplete/index.d.ts +7 -7
- package/dist/autocomplete/index.js +17 -25
- package/dist/autocomplete/index.js.map +1 -1
- package/dist/character-count/element.d.ts +5 -0
- package/dist/character-count/element.js +5 -5
- package/dist/character-count/element.js.map +1 -1
- package/dist/character-count/index.d.ts +8 -8
- package/dist/character-count/index.js +13 -14
- package/dist/character-count/index.js.map +1 -1
- package/dist/character-count/messages.d.ts +1 -1
- package/dist/character-count/messages.js +13 -13
- package/dist/character-count/messages.js.map +1 -1
- package/dist/character-count-text/index.d.ts +1 -0
- package/dist/character-count-text/index.js +6 -0
- package/dist/character-count-text/index.js.map +1 -0
- package/dist/choices/element.d.ts +5 -0
- package/dist/choices/element.js +6 -31
- package/dist/choices/element.js.map +1 -1
- package/dist/disclosure/element.d.ts +3 -0
- package/dist/disclosure/element.js +21 -12
- package/dist/disclosure/element.js.map +1 -1
- package/dist/disclosure/index.d.ts +6 -5
- package/dist/disclosure/index.js +21 -11
- package/dist/disclosure/index.js.map +1 -1
- package/dist/exit-this-page/element.d.ts +1 -1
- package/dist/exit-this-page/element.js +20 -21
- package/dist/exit-this-page/element.js.map +1 -1
- package/dist/exit-this-page/index.d.ts +11 -11
- package/dist/exit-this-page/index.js +7 -7
- package/dist/exit-this-page/index.js.map +1 -1
- package/dist/file-upload/element.d.ts +11 -6
- package/dist/file-upload/element.js +15 -15
- package/dist/file-upload/element.js.map +1 -1
- package/dist/file-upload/index.d.ts +9 -9
- package/dist/file-upload/index.js +11 -11
- package/dist/file-upload/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/inline/accordion-element.js +1 -1
- package/dist/inline/auto-focus-element.js +1 -1
- package/dist/inline/autocomplete-element.js +1 -1
- package/dist/inline/character-count-element.js +1 -1
- package/dist/inline/choices-element.js +1 -1
- package/dist/inline/disclosure-element.js +1 -1
- package/dist/inline/exit-this-page-element.js +1 -1
- package/dist/inline/file-upload-element.js +1 -1
- package/dist/inline/focus-links-element.js +1 -1
- package/dist/inline/modal-element.js +1 -1
- package/dist/inline/password-input-element.js +1 -1
- package/dist/inline/tabs-element.js +1 -1
- package/dist/inline-script/index.js +12 -12
- package/dist/modal/element.js +126 -11
- package/dist/modal/element.js.map +1 -1
- package/dist/modal/index.d.ts +8 -3
- package/dist/modal/index.js +56 -20
- package/dist/modal/index.js.map +1 -1
- package/dist/password-input/element.js +27 -11
- package/dist/password-input/element.js.map +1 -1
- package/dist/password-input/index.d.ts +3 -3
- package/dist/password-input/index.js +4 -2
- package/dist/password-input/index.js.map +1 -1
- package/dist/styles.css +11 -0
- package/dist/tabs/element.d.ts +3 -0
- package/dist/tabs/element.js +17 -10
- package/dist/tabs/element.js.map +1 -1
- package/dist/tabs/index.d.ts +9 -18
- package/dist/tabs/index.js +48 -44
- package/dist/tabs/index.js.map +1 -1
- package/dist/utils/change-details.d.ts +11 -0
- package/dist/utils/change-details.js +3 -0
- package/dist/utils/change-details.js.map +1 -0
- package/dist/utils/on-property-change.d.ts +7 -0
- package/dist/utils/on-property-change.js +19 -0
- package/dist/utils/on-property-change.js.map +1 -0
- package/dist/utils/set-focus.js.map +1 -1
- package/dist/utils/slot.d.ts +5 -3
- package/dist/utils/slot.js +24 -2
- package/dist/utils/slot.js.map +1 -1
- package/package.json +8 -2
- package/dist/utils/on-property-set.d.ts +0 -9
- package/dist/utils/on-property-set.js +0 -38
- package/dist/utils/on-property-set.js.map +0 -1
package/dist/modal/element.js
CHANGED
|
@@ -10,10 +10,19 @@
|
|
|
10
10
|
* The element only adds what HTML can't do on its own:
|
|
11
11
|
* - In a browser without invoker commands, it runs the commands of its own buttons.
|
|
12
12
|
* - `open` (when present) controls the dialog: "true" opens it with `showModal()`, anything
|
|
13
|
-
* else closes it. Changing `open` fires no change event.
|
|
13
|
+
* else closes it. Changing `open` fires no change event. While `open` is present, the buttons
|
|
14
|
+
* and Escape don't open or close the dialog: they fire the change event with the state asked
|
|
15
|
+
* for, and the dialog changes only when `open` does. That covers a `form` with
|
|
16
|
+
* `method="dialog"`, and a script calling `showModal()` where the browser fires `beforetoggle`
|
|
17
|
+
* on dialogs (Chrome 132, Firefox 133, Safari 26). Escape is cancelled at its `keydown`, since the `cancel` event of a
|
|
18
|
+
* second Escape in a row isn't cancelable. The browser doesn't let a page cancel a script
|
|
19
|
+
* calling `close()`: that fires the change event, and the dialog goes back to `open` a moment
|
|
20
|
+
* later unless `open` has changed.
|
|
14
21
|
* - It fires a bubbling `govuk-react-modal:change` event with `{ open }` when the dialog opens
|
|
15
22
|
* or closes, for code that doesn't use React.
|
|
16
23
|
*
|
|
24
|
+
* While upgraded it sets `data-upgraded`.
|
|
25
|
+
*
|
|
17
26
|
* Markup inside the element:
|
|
18
27
|
* - a `dialog` with an id
|
|
19
28
|
* - `button`s with `commandfor` set to the dialog's id, and `command` set to "show-modal",
|
|
@@ -21,13 +30,26 @@
|
|
|
21
30
|
*/
|
|
22
31
|
import { setUpWhenReady } from '../utils/set-up-when-ready.js';
|
|
23
32
|
export const tagName = 'govuk-react-modal';
|
|
33
|
+
// How long the page has to accept a change the element couldn't cancel, before it's undone.
|
|
34
|
+
const restoreDelay = 100;
|
|
24
35
|
/** Whether the browser runs invoker commands itself. */
|
|
25
36
|
export const supportsCommands = () => typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype;
|
|
37
|
+
/** Whether Escape would close a popover in the dialog, not the dialog. A manual popover stays. */
|
|
38
|
+
function hasOpenPopover(dialog) {
|
|
39
|
+
try {
|
|
40
|
+
return Array.from(dialog.querySelectorAll(':popover-open')).some((popover) => popover.getAttribute('popover') !== 'manual');
|
|
41
|
+
}
|
|
42
|
+
catch (_a) {
|
|
43
|
+
// No popovers in this browser.
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
26
47
|
if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
|
|
27
48
|
class GovukReactModal extends HTMLElement {
|
|
28
49
|
constructor() {
|
|
29
50
|
super(...arguments);
|
|
30
51
|
this.wasOpen = false;
|
|
52
|
+
this.syncing = false;
|
|
31
53
|
// Runs the commands of the element's own buttons when the browser can't.
|
|
32
54
|
this.onClick = (event) => {
|
|
33
55
|
var _a;
|
|
@@ -38,6 +60,8 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
38
60
|
if (!(dialog instanceof HTMLDialogElement))
|
|
39
61
|
return;
|
|
40
62
|
const command = button.getAttribute('command');
|
|
63
|
+
if (this.request(dialog, command))
|
|
64
|
+
return;
|
|
41
65
|
if (command === 'show-modal' && !dialog.open)
|
|
42
66
|
dialog.showModal();
|
|
43
67
|
else if (command === 'close' && dialog.open)
|
|
@@ -51,12 +75,72 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
51
75
|
dialog.close();
|
|
52
76
|
}
|
|
53
77
|
};
|
|
78
|
+
// While controlled, the browser's own commands and Escape only ask for a change.
|
|
79
|
+
this.onCommand = (event) => {
|
|
80
|
+
const { dialog } = this;
|
|
81
|
+
const command = Reflect.get(event, 'command');
|
|
82
|
+
if (!dialog || event.target !== dialog || typeof command !== 'string')
|
|
83
|
+
return;
|
|
84
|
+
if (this.request(dialog, command))
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
};
|
|
87
|
+
// A script opening the dialog, where the browser fires `beforetoggle` on dialogs.
|
|
88
|
+
this.onBeforeToggle = (event) => {
|
|
89
|
+
const { dialog } = this;
|
|
90
|
+
if (!dialog || event.target !== dialog || this.syncing || !event.cancelable)
|
|
91
|
+
return;
|
|
92
|
+
if (Reflect.get(event, 'newState') !== 'open' || this.getAttribute('open') === 'true')
|
|
93
|
+
return;
|
|
94
|
+
if (this.request(dialog, 'show-modal'))
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
};
|
|
97
|
+
// Escape, before the browser asks the dialog to close: it lets a page cancel that only once in
|
|
98
|
+
// a row. Heard on the window, so a key that anything else handled first, React's handlers
|
|
99
|
+
// included, is left. So is one that closes a popover open in the dialog.
|
|
100
|
+
this.onKeydown = (event) => {
|
|
101
|
+
const { dialog } = this;
|
|
102
|
+
const { target } = event;
|
|
103
|
+
if (!dialog || event.key !== 'Escape' || event.defaultPrevented || event.isComposing)
|
|
104
|
+
return;
|
|
105
|
+
if (!(target instanceof Element) || target.closest('dialog') !== dialog)
|
|
106
|
+
return;
|
|
107
|
+
if (dialog.getAttribute('closedby') === 'none' || hasOpenPopover(dialog))
|
|
108
|
+
return;
|
|
109
|
+
if (this.request(dialog, 'request-close'))
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
};
|
|
112
|
+
this.onCancel = (event) => {
|
|
113
|
+
const { dialog } = this;
|
|
114
|
+
if (!dialog || event.target !== dialog)
|
|
115
|
+
return;
|
|
116
|
+
if (this.request(dialog, 'request-close'))
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
};
|
|
119
|
+
// A form with `method="dialog"` closes its dialog when submitted.
|
|
120
|
+
this.onSubmit = (event) => {
|
|
121
|
+
const { dialog } = this;
|
|
122
|
+
const form = event.target;
|
|
123
|
+
if (!dialog || !(form instanceof HTMLFormElement) || form.closest('dialog') !== dialog)
|
|
124
|
+
return;
|
|
125
|
+
const { submitter } = event;
|
|
126
|
+
const method = (submitter instanceof HTMLButtonElement || submitter instanceof HTMLInputElement) &&
|
|
127
|
+
submitter.hasAttribute('formmethod')
|
|
128
|
+
? submitter.formMethod
|
|
129
|
+
: form.method;
|
|
130
|
+
if (method === 'dialog' && this.request(dialog, 'close'))
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
};
|
|
54
133
|
this.onToggle = () => {
|
|
55
134
|
const { dialog } = this;
|
|
56
135
|
if (!dialog || dialog.open === this.wasOpen)
|
|
57
136
|
return;
|
|
58
137
|
this.wasOpen = dialog.open;
|
|
59
138
|
this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open: dialog.open }, bubbles: true }));
|
|
139
|
+
// While controlled, this is a change the element couldn't cancel.
|
|
140
|
+
if (!this.hasAttribute('open'))
|
|
141
|
+
return;
|
|
142
|
+
window.clearTimeout(this.restoreTimeout);
|
|
143
|
+
this.restoreTimeout = window.setTimeout(() => this.sync(), restoreDelay);
|
|
60
144
|
};
|
|
61
145
|
}
|
|
62
146
|
static get observedAttributes() {
|
|
@@ -65,14 +149,21 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
65
149
|
connectedCallback() {
|
|
66
150
|
if (!supportsCommands())
|
|
67
151
|
this.addEventListener('click', this.onClick);
|
|
152
|
+
this.addEventListener('submit', this.onSubmit);
|
|
153
|
+
window.addEventListener('keydown', this.onKeydown);
|
|
68
154
|
this.observer = setUpWhenReady(this, () => this.setup());
|
|
69
155
|
}
|
|
70
156
|
disconnectedCallback() {
|
|
71
|
-
var _a, _b, _c;
|
|
157
|
+
var _a, _b, _c, _d, _e;
|
|
72
158
|
this.removeEventListener('click', this.onClick);
|
|
159
|
+
this.removeEventListener('submit', this.onSubmit);
|
|
160
|
+
window.removeEventListener('keydown', this.onKeydown);
|
|
161
|
+
window.clearTimeout(this.restoreTimeout);
|
|
73
162
|
(_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
74
|
-
(_b = this.
|
|
75
|
-
(_c = this.dialog) === null || _c === void 0 ? void 0 : _c.removeEventListener('
|
|
163
|
+
(_b = this.openObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
164
|
+
(_c = this.dialog) === null || _c === void 0 ? void 0 : _c.removeEventListener('command', this.onCommand);
|
|
165
|
+
(_d = this.dialog) === null || _d === void 0 ? void 0 : _d.removeEventListener('cancel', this.onCancel);
|
|
166
|
+
(_e = this.dialog) === null || _e === void 0 ? void 0 : _e.removeEventListener('beforetoggle', this.onBeforeToggle);
|
|
76
167
|
this.dialog = undefined;
|
|
77
168
|
}
|
|
78
169
|
attributeChangedCallback() {
|
|
@@ -85,13 +176,31 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
85
176
|
if (!dialog)
|
|
86
177
|
return false;
|
|
87
178
|
this.dialog = dialog;
|
|
179
|
+
this.dataset.upgraded = '';
|
|
88
180
|
this.wasOpen = dialog.open;
|
|
89
|
-
// `toggle`
|
|
90
|
-
|
|
91
|
-
|
|
181
|
+
// Not every browser fires `toggle` on dialogs, so watch the dialog's `open` attribute.
|
|
182
|
+
this.openObserver = new MutationObserver(this.onToggle);
|
|
183
|
+
this.openObserver.observe(dialog, { attributes: true, attributeFilter: ['open'] });
|
|
184
|
+
dialog.addEventListener('command', this.onCommand);
|
|
185
|
+
dialog.addEventListener('cancel', this.onCancel);
|
|
186
|
+
dialog.addEventListener('beforetoggle', this.onBeforeToggle);
|
|
92
187
|
this.sync();
|
|
93
188
|
return true;
|
|
94
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* While controlled, fires the change event for the state a command asks for, in place of
|
|
192
|
+
* running it. Returns whether the command was handled.
|
|
193
|
+
*/
|
|
194
|
+
request(dialog, command) {
|
|
195
|
+
if (!this.hasAttribute('open'))
|
|
196
|
+
return false;
|
|
197
|
+
const open = command === 'show-modal';
|
|
198
|
+
if (!open && command !== 'close' && command !== 'request-close')
|
|
199
|
+
return false;
|
|
200
|
+
if (open !== dialog.open)
|
|
201
|
+
this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open }, bubbles: true }));
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
95
204
|
/** Opens or closes the dialog to match `open`, without reporting it. */
|
|
96
205
|
sync() {
|
|
97
206
|
const { dialog } = this;
|
|
@@ -102,10 +211,16 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
102
211
|
if (open === dialog.open)
|
|
103
212
|
return;
|
|
104
213
|
this.wasOpen = open;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
214
|
+
this.syncing = true;
|
|
215
|
+
try {
|
|
216
|
+
if (open && dialog.isConnected)
|
|
217
|
+
dialog.showModal();
|
|
218
|
+
else if (!open)
|
|
219
|
+
dialog.close();
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
this.syncing = false;
|
|
223
|
+
}
|
|
109
224
|
}
|
|
110
225
|
}
|
|
111
226
|
if (!customElements.get(tagName))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/modal/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAQ/D,MAAM,CAAC,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAE3C,wDAAwD;AACxD,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE,CACnC,OAAO,iBAAiB,KAAK,WAAW,IAAI,SAAS,IAAI,iBAAiB,CAAC,SAAS,CAAC;AAEvF,IAAI,OAAO,WAAW,KAAK,WAAW,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE,CAAC;IAChF,MAAM,eAAgB,SAAQ,WAAW;QAAzC;;YASU,YAAO,GAAG,KAAK,CAAC;YAExB,yEAAyE;YACxD,YAAO,GAAG,CAAC,KAAiB,EAAE,EAAE;;gBAC/C,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,YAAY,OAAO,CAAC,CAA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C,MAAM;gBAAE,OAAO,KAAK,CAAC;YAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;YACrB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC;YAC3B,4EAA4E;YAC5E,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YACjD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAChD,IAAI,CAAC,IAAI,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;QAED,wEAAwE;QAChE,IAAI;YACV,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;YACxB,MAAM,UAAU,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;YAC7C,IAAI,CAAC,MAAM,IAAI,UAAU,KAAK,IAAI;gBAAE,OAAO;YAC3C,MAAM,IAAI,GAAG,UAAU,KAAK,MAAM,CAAC;YACnC,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI;gBAAE,OAAO;YACjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,IAAI,IAAI,IAAI,MAAM,CAAC,WAAW;gBAAE,MAAM,CAAC,SAAS,EAAE,CAAC;iBAC9C,IAAI,CAAC,IAAI;gBAAE,MAAM,CAAC,KAAK,EAAE,CAAC;QACjC,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;AACpF,CAAC","sourcesContent":["/*\n * <govuk-react-modal>: a headless, light-DOM custom element around a modal `dialog` and the\n * buttons that open and close it.\n *\n * The buttons use invoker commands (`command=\"show-modal\"` or `command=\"close\"`, with\n * `commandfor` naming the dialog), so a browser that supports them opens a true modal dialog\n * with no JavaScript: the page behind is inert, focus moves into the dialog and Escape closes\n * it. The `dialog` element is the state.\n *\n * The element only adds what HTML can't do on its own:\n * - In a browser without invoker commands, it runs the commands of its own buttons.\n * - `open` (when present) controls the dialog: \"true\" opens it with `showModal()`, anything\n * else closes it. Changing `open` fires no change event.\n * - It fires a bubbling `govuk-react-modal:change` event with `{ open }` when the dialog opens\n * or closes, for code that doesn't use React.\n *\n * Markup inside the element:\n * - a `dialog` with an id\n * - `button`s with `commandfor` set to the dialog's id, and `command` set to \"show-modal\",\n * \"close\" or \"request-close\"\n */\n\nimport { setUpWhenReady } from '../utils/set-up-when-ready.js';\n\ndeclare global {\n interface HTMLElementEventMap {\n 'govuk-react-modal:change': CustomEvent<{ open: boolean }>;\n }\n}\n\nexport const tagName = 'govuk-react-modal';\n\n/** Whether the browser runs invoker commands itself. */\nexport const supportsCommands = () =>\n typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype;\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactModal extends HTMLElement {\n static get observedAttributes() {\n return ['open'];\n }\n\n private dialog?: HTMLDialogElement;\n\n private observer?: MutationObserver;\n\n private wasOpen = false;\n\n // Runs the commands of the element's own buttons when the browser can't.\n private readonly onClick = (event: MouseEvent) => {\n const button = event.target instanceof Element ? event.target.closest('button[commandfor]') : null;\n if (!(button instanceof HTMLButtonElement) || button.closest(tagName) !== this) return;\n const dialog = document.getElementById(button.getAttribute('commandfor') ?? '');\n if (!(dialog instanceof HTMLDialogElement)) return;\n const command = button.getAttribute('command');\n if (command === 'show-modal' && !dialog.open) dialog.showModal();\n else if (command === 'close' && dialog.open) dialog.close();\n else if (command === 'request-close' && dialog.open) {\n // Newer than this package's DOM types.\n const requestClose: unknown = Reflect.get(dialog, 'requestClose');\n if (typeof requestClose === 'function') requestClose.call(dialog);\n else dialog.close();\n }\n };\n\n private readonly onToggle = () => {\n const { dialog } = this;\n if (!dialog || dialog.open === this.wasOpen) return;\n this.wasOpen = dialog.open;\n this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open: dialog.open }, bubbles: true }));\n };\n\n connectedCallback() {\n if (!supportsCommands()) this.addEventListener('click', this.onClick);\n this.observer = setUpWhenReady(this, () => this.setup());\n }\n\n disconnectedCallback() {\n this.removeEventListener('click', this.onClick);\n this.observer?.disconnect();\n this.dialog?.removeEventListener('close', this.onToggle);\n this.dialog?.removeEventListener('toggle', this.onToggle);\n this.dialog = undefined;\n }\n\n attributeChangedCallback() {\n this.sync();\n }\n\n private setup() {\n if (this.dialog) return true;\n const dialog = Array.from(this.querySelectorAll('dialog')).find((found) => found.closest(tagName) === this);\n if (!dialog) return false;\n this.dialog = dialog;\n this.wasOpen = dialog.open;\n // `toggle` fires on open and close where supported, and `close` everywhere.\n dialog.addEventListener('toggle', this.onToggle);\n dialog.addEventListener('close', this.onToggle);\n this.sync();\n return true;\n }\n\n /** Opens or closes the dialog to match `open`, without reporting it. */\n private sync() {\n const { dialog } = this;\n const controlled = this.getAttribute('open');\n if (!dialog || controlled === null) return;\n const open = controlled === 'true';\n if (open === dialog.open) return;\n this.wasOpen = open;\n if (open && dialog.isConnected) dialog.showModal();\n else if (!open) dialog.close();\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactModal);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/modal/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAQ/D,MAAM,CAAC,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAE3C,4FAA4F;AAC5F,MAAM,YAAY,GAAG,GAAG,CAAC;AAEzB,wDAAwD;AACxD,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE,CACnC,OAAO,iBAAiB,KAAK,WAAW,IAAI,SAAS,IAAI,iBAAiB,CAAC,SAAS,CAAC;AAEvF,kGAAkG;AAClG,SAAS,cAAc,CAAC,MAAyB;IAC/C,IA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KAAK,OAAO,IAAI,OAAO,KAAK,eAAe;gBAAE,OAAO,KAAK,CAAC;YAC9E,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI;gBACtB,IAAI,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,0BAA0B,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;YACvG,OAAO,IAAI,CAAC;QACd,CAAC;QAED,wEAAwE;QAChE,IAAI;YACV,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;YACxB,MAAM,UAAU,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;YAC7C,IAAI,CAAC,MAAM,IAAI,UAAU,KAAK,IAAI;gBAAE,OAAO;YAC3C,MAAM,IAAI,GAAG,UAAU,KAAK,MAAM,CAAC;YACnC,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI;gBAAE,OAAO;YACjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC;gBACH,IAAI,IAAI,IAAI,MAAM,CAAC,WAAW;oBAAE,MAAM,CAAC,SAAS,EAAE,CAAC;qBAC9C,IAAI,CAAC,IAAI;oBAAE,MAAM,CAAC,KAAK,EAAE,CAAC;YACjC,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;YACvB,CAAC;QACH,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;AACpF,CAAC","sourcesContent":["/*\n * <govuk-react-modal>: a headless, light-DOM custom element around a modal `dialog` and the\n * buttons that open and close it.\n *\n * The buttons use invoker commands (`command=\"show-modal\"` or `command=\"close\"`, with\n * `commandfor` naming the dialog), so a browser that supports them opens a true modal dialog\n * with no JavaScript: the page behind is inert, focus moves into the dialog and Escape closes\n * it. The `dialog` element is the state.\n *\n * The element only adds what HTML can't do on its own:\n * - In a browser without invoker commands, it runs the commands of its own buttons.\n * - `open` (when present) controls the dialog: \"true\" opens it with `showModal()`, anything\n * else closes it. Changing `open` fires no change event. While `open` is present, the buttons\n * and Escape don't open or close the dialog: they fire the change event with the state asked\n * for, and the dialog changes only when `open` does. That covers a `form` with\n * `method=\"dialog\"`, and a script calling `showModal()` where the browser fires `beforetoggle`\n * on dialogs (Chrome 132, Firefox 133, Safari 26). Escape is cancelled at its `keydown`, since the `cancel` event of a\n * second Escape in a row isn't cancelable. The browser doesn't let a page cancel a script\n * calling `close()`: that fires the change event, and the dialog goes back to `open` a moment\n * later unless `open` has changed.\n * - It fires a bubbling `govuk-react-modal:change` event with `{ open }` when the dialog opens\n * or closes, for code that doesn't use React.\n *\n * While upgraded it sets `data-upgraded`.\n *\n * Markup inside the element:\n * - a `dialog` with an id\n * - `button`s with `commandfor` set to the dialog's id, and `command` set to \"show-modal\",\n * \"close\" or \"request-close\"\n */\n\nimport { setUpWhenReady } from '../utils/set-up-when-ready.js';\n\ndeclare global {\n interface HTMLElementEventMap {\n 'govuk-react-modal:change': CustomEvent<{ open: boolean }>;\n }\n}\n\nexport const tagName = 'govuk-react-modal';\n\n// How long the page has to accept a change the element couldn't cancel, before it's undone.\nconst restoreDelay = 100;\n\n/** Whether the browser runs invoker commands itself. */\nexport const supportsCommands = () =>\n typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype;\n\n/** Whether Escape would close a popover in the dialog, not the dialog. A manual popover stays. */\nfunction hasOpenPopover(dialog: HTMLDialogElement) {\n try {\n return Array.from(dialog.querySelectorAll(':popover-open')).some(\n (popover) => popover.getAttribute('popover') !== 'manual'\n );\n } catch {\n // No popovers in this browser.\n return false;\n }\n}\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactModal extends HTMLElement {\n static get observedAttributes() {\n return ['open'];\n }\n\n private dialog?: HTMLDialogElement;\n\n private observer?: MutationObserver;\n\n private openObserver?: MutationObserver;\n\n private wasOpen = false;\n\n private restoreTimeout?: number;\n\n private syncing = false;\n\n // Runs the commands of the element's own buttons when the browser can't.\n private readonly onClick = (event: MouseEvent) => {\n const button = event.target instanceof Element ? event.target.closest('button[commandfor]') : null;\n if (!(button instanceof HTMLButtonElement) || button.closest(tagName) !== this) return;\n const dialog = document.getElementById(button.getAttribute('commandfor') ?? '');\n if (!(dialog instanceof HTMLDialogElement)) return;\n const command = button.getAttribute('command');\n if (this.request(dialog, command)) return;\n if (command === 'show-modal' && !dialog.open) dialog.showModal();\n else if (command === 'close' && dialog.open) dialog.close();\n else if (command === 'request-close' && dialog.open) {\n // Newer than this package's DOM types.\n const requestClose: unknown = Reflect.get(dialog, 'requestClose');\n if (typeof requestClose === 'function') requestClose.call(dialog);\n else dialog.close();\n }\n };\n\n // While controlled, the browser's own commands and Escape only ask for a change.\n private readonly onCommand = (event: Event) => {\n const { dialog } = this;\n const command: unknown = Reflect.get(event, 'command');\n if (!dialog || event.target !== dialog || typeof command !== 'string') return;\n if (this.request(dialog, command)) event.preventDefault();\n };\n\n // A script opening the dialog, where the browser fires `beforetoggle` on dialogs.\n private readonly onBeforeToggle = (event: Event) => {\n const { dialog } = this;\n if (!dialog || event.target !== dialog || this.syncing || !event.cancelable) return;\n if (Reflect.get(event, 'newState') !== 'open' || this.getAttribute('open') === 'true') return;\n if (this.request(dialog, 'show-modal')) event.preventDefault();\n };\n\n // Escape, before the browser asks the dialog to close: it lets a page cancel that only once in\n // a row. Heard on the window, so a key that anything else handled first, React's handlers\n // included, is left. So is one that closes a popover open in the dialog.\n private readonly onKeydown = (event: KeyboardEvent) => {\n const { dialog } = this;\n const { target } = event;\n if (!dialog || event.key !== 'Escape' || event.defaultPrevented || event.isComposing) return;\n if (!(target instanceof Element) || target.closest('dialog') !== dialog) return;\n if (dialog.getAttribute('closedby') === 'none' || hasOpenPopover(dialog)) return;\n if (this.request(dialog, 'request-close')) event.preventDefault();\n };\n\n private readonly onCancel = (event: Event) => {\n const { dialog } = this;\n if (!dialog || event.target !== dialog) return;\n if (this.request(dialog, 'request-close')) event.preventDefault();\n };\n\n // A form with `method=\"dialog\"` closes its dialog when submitted.\n private readonly onSubmit = (event: SubmitEvent) => {\n const { dialog } = this;\n const form = event.target;\n if (!dialog || !(form instanceof HTMLFormElement) || form.closest('dialog') !== dialog) return;\n const { submitter } = event;\n const method =\n (submitter instanceof HTMLButtonElement || submitter instanceof HTMLInputElement) &&\n submitter.hasAttribute('formmethod')\n ? submitter.formMethod\n : form.method;\n if (method === 'dialog' && this.request(dialog, 'close')) event.preventDefault();\n };\n\n private readonly onToggle = () => {\n const { dialog } = this;\n if (!dialog || dialog.open === this.wasOpen) return;\n this.wasOpen = dialog.open;\n this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open: dialog.open }, bubbles: true }));\n // While controlled, this is a change the element couldn't cancel.\n if (!this.hasAttribute('open')) return;\n window.clearTimeout(this.restoreTimeout);\n this.restoreTimeout = window.setTimeout(() => this.sync(), restoreDelay);\n };\n\n connectedCallback() {\n if (!supportsCommands()) this.addEventListener('click', this.onClick);\n this.addEventListener('submit', this.onSubmit);\n window.addEventListener('keydown', this.onKeydown);\n this.observer = setUpWhenReady(this, () => this.setup());\n }\n\n disconnectedCallback() {\n this.removeEventListener('click', this.onClick);\n this.removeEventListener('submit', this.onSubmit);\n window.removeEventListener('keydown', this.onKeydown);\n window.clearTimeout(this.restoreTimeout);\n this.observer?.disconnect();\n this.openObserver?.disconnect();\n this.dialog?.removeEventListener('command', this.onCommand);\n this.dialog?.removeEventListener('cancel', this.onCancel);\n this.dialog?.removeEventListener('beforetoggle', this.onBeforeToggle);\n this.dialog = undefined;\n }\n\n attributeChangedCallback() {\n this.sync();\n }\n\n private setup() {\n if (this.dialog) return true;\n const dialog = Array.from(this.querySelectorAll('dialog')).find((found) => found.closest(tagName) === this);\n if (!dialog) return false;\n this.dialog = dialog;\n this.dataset.upgraded = '';\n this.wasOpen = dialog.open;\n // Not every browser fires `toggle` on dialogs, so watch the dialog's `open` attribute.\n this.openObserver = new MutationObserver(this.onToggle);\n this.openObserver.observe(dialog, { attributes: true, attributeFilter: ['open'] });\n dialog.addEventListener('command', this.onCommand);\n dialog.addEventListener('cancel', this.onCancel);\n dialog.addEventListener('beforetoggle', this.onBeforeToggle);\n this.sync();\n return true;\n }\n\n /**\n * While controlled, fires the change event for the state a command asks for, in place of\n * running it. Returns whether the command was handled.\n */\n private request(dialog: HTMLDialogElement, command: string | null) {\n if (!this.hasAttribute('open')) return false;\n const open = command === 'show-modal';\n if (!open && command !== 'close' && command !== 'request-close') return false;\n if (open !== dialog.open)\n this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open }, bubbles: true }));\n return true;\n }\n\n /** Opens or closes the dialog to match `open`, without reporting it. */\n private sync() {\n const { dialog } = this;\n const controlled = this.getAttribute('open');\n if (!dialog || controlled === null) return;\n const open = controlled === 'true';\n if (open === dialog.open) return;\n this.wasOpen = open;\n this.syncing = true;\n try {\n if (open && dialog.isConnected) dialog.showModal();\n else if (!open) dialog.close();\n } finally {\n this.syncing = false;\n }\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactModal);\n}\n"]}
|
package/dist/modal/index.d.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import type { ComponentProps, ReactNode } from 'react';
|
|
3
|
+
import { type ChangeDetails } from '../utils/change-details.js';
|
|
4
|
+
export type { ChangeDetails };
|
|
3
5
|
export type RootProps = Omit<React.HTMLAttributes<HTMLElement>, 'onChange'> & {
|
|
4
6
|
children?: ReactNode;
|
|
5
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Whether the dialog is open, to control it once hydrated: the buttons and Escape then call
|
|
9
|
+
* `onOpenChange`, and the dialog changes only when `open` does. Without it, the buttons alone
|
|
10
|
+
* open and close it, which is best unless you need to open or close it yourself.
|
|
11
|
+
*/
|
|
6
12
|
open?: boolean;
|
|
7
13
|
/** Called when the dialog opens or closes. */
|
|
8
|
-
onOpenChange?: (open: boolean) => void;
|
|
14
|
+
onOpenChange?: (open: boolean, details: ChangeDetails) => void;
|
|
9
15
|
/** The dialog's id, which the buttons' `commandfor` names. Defaults to a generated one. */
|
|
10
16
|
dialogId?: string;
|
|
11
17
|
};
|
|
@@ -33,4 +39,3 @@ export declare function Content({ asChild, id, onClose, ref, ...props }: Content
|
|
|
33
39
|
export type TitleProps = PartProps<'h2'>;
|
|
34
40
|
/** The dialog's heading, which names it. */
|
|
35
41
|
export declare function Title({ asChild, ...props }: TitleProps): React.JSX.Element;
|
|
36
|
-
export {};
|
package/dist/modal/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import * as React from 'react';
|
|
3
|
-
import { createContext, useContext, useEffect, useId, useRef } from 'react';
|
|
4
|
-
import {
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useId, useReducer, useRef } from 'react';
|
|
4
|
+
import { early, live } from '../utils/change-details.js';
|
|
5
|
+
import { composeRefs, Slot } from '../utils/slot.js';
|
|
5
6
|
import { tagName } from './element.js';
|
|
6
7
|
/*
|
|
7
8
|
* Headless modal dialog: unstyled parts over a native `dialog` and the <govuk-react-modal>
|
|
@@ -19,9 +20,31 @@ const ModalContext = createContext({});
|
|
|
19
20
|
export function Root({ children, open, onOpenChange, dialogId: dialogIdProp, ...props }) {
|
|
20
21
|
const generatedId = useId();
|
|
21
22
|
const dialogId = dialogIdProp !== null && dialogIdProp !== void 0 ? dialogIdProp : generatedId;
|
|
23
|
+
const element = useRef(null);
|
|
24
|
+
const synced = useRef(false);
|
|
25
|
+
const [, render] = useReducer((count) => count + 1, 0);
|
|
26
|
+
// `open` is set once hydrated, so the server markup is uncontrolled and the buttons work before
|
|
27
|
+
// hydration. A dialog opened by then is reported by Content, rather than closed.
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
const node = element.current;
|
|
30
|
+
if (!node || open === undefined) {
|
|
31
|
+
node === null || node === void 0 ? void 0 : node.removeAttribute('open');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (!synced.current) {
|
|
35
|
+
synced.current = true;
|
|
36
|
+
const dialog = document.getElementById(dialogId);
|
|
37
|
+
if (onOpenChange && !open && dialog instanceof HTMLDialogElement && dialog.open) {
|
|
38
|
+
// The dialog stays open until the next render, which adopts it or closes it.
|
|
39
|
+
render();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
node.setAttribute('open', String(open));
|
|
44
|
+
});
|
|
22
45
|
return (React.createElement(ModalContext.Provider, { value: { dialogId, titleId: `${dialogId}-title`, onOpenChange } }, React.createElement(tagName, {
|
|
23
46
|
...props,
|
|
24
|
-
|
|
47
|
+
ref: element,
|
|
25
48
|
suppressHydrationWarning: true,
|
|
26
49
|
}, children)));
|
|
27
50
|
}
|
|
@@ -52,15 +75,17 @@ export function Content({ asChild, id, onClose, ref, ...props }) {
|
|
|
52
75
|
useEffect(() => {
|
|
53
76
|
onOpenChange.current = context.onOpenChange;
|
|
54
77
|
});
|
|
55
|
-
// Report each change once
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
var _a;
|
|
78
|
+
// Report each change once.
|
|
79
|
+
const report = useCallback((dialog) => {
|
|
80
|
+
var _a, _b;
|
|
59
81
|
if (reported.current === dialog.open)
|
|
60
82
|
return;
|
|
61
83
|
reported.current = dialog.open;
|
|
62
|
-
|
|
63
|
-
|
|
84
|
+
// A controlled dialog that arrives at `open` is no news.
|
|
85
|
+
if (((_a = dialog.closest(tagName)) === null || _a === void 0 ? void 0 : _a.getAttribute('open')) === String(dialog.open))
|
|
86
|
+
return;
|
|
87
|
+
(_b = onOpenChange.current) === null || _b === void 0 ? void 0 : _b.call(onOpenChange, dialog.open, live);
|
|
88
|
+
}, []);
|
|
64
89
|
// A dialog the user opened before hydration, through its invoker command or the element, fired
|
|
65
90
|
// nothing React heard, so report it once.
|
|
66
91
|
const dialogRef = useRef(null);
|
|
@@ -69,22 +94,33 @@ export function Content({ asChild, id, onClose, ref, ...props }) {
|
|
|
69
94
|
const dialog = dialogRef.current;
|
|
70
95
|
if ((dialog === null || dialog === void 0 ? void 0 : dialog.open) && reported.current === undefined) {
|
|
71
96
|
reported.current = true;
|
|
72
|
-
(_a = onOpenChange.current) === null || _a === void 0 ? void 0 : _a.call(onOpenChange, true);
|
|
97
|
+
(_a = onOpenChange.current) === null || _a === void 0 ? void 0 : _a.call(onOpenChange, true, early);
|
|
73
98
|
}
|
|
74
99
|
}, []);
|
|
75
|
-
//
|
|
76
|
-
const listen = (dialog) => {
|
|
100
|
+
// Not every browser fires `toggle` on dialogs, so watch the dialog's `open` attribute.
|
|
101
|
+
const listen = useCallback((dialog) => {
|
|
77
102
|
dialogRef.current = dialog;
|
|
78
|
-
if (typeof ref === 'function')
|
|
79
|
-
ref(dialog);
|
|
80
|
-
else if (ref)
|
|
81
|
-
ref.current = dialog;
|
|
82
103
|
if (!dialog)
|
|
83
104
|
return undefined;
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
105
|
+
const forget = composeRefs(ref)(dialog);
|
|
106
|
+
const observer = new MutationObserver(() => report(dialog));
|
|
107
|
+
observer.observe(dialog, { attributes: true, attributeFilter: ['open'] });
|
|
108
|
+
// While controlled, the element asks for a change the dialog hasn't made.
|
|
109
|
+
const root = dialog.closest(tagName);
|
|
110
|
+
const onRequest = (event) => {
|
|
111
|
+
var _a;
|
|
112
|
+
if (event.target !== root || event.detail.open === dialog.open)
|
|
113
|
+
return;
|
|
114
|
+
(_a = onOpenChange.current) === null || _a === void 0 ? void 0 : _a.call(onOpenChange, event.detail.open, live);
|
|
115
|
+
};
|
|
116
|
+
root === null || root === void 0 ? void 0 : root.addEventListener('govuk-react-modal:change', onRequest);
|
|
117
|
+
return () => {
|
|
118
|
+
observer.disconnect();
|
|
119
|
+
root === null || root === void 0 ? void 0 : root.removeEventListener('govuk-react-modal:change', onRequest);
|
|
120
|
+
dialogRef.current = null;
|
|
121
|
+
forget();
|
|
122
|
+
};
|
|
123
|
+
}, [ref, report]);
|
|
88
124
|
const shared = {
|
|
89
125
|
id: id !== null && id !== void 0 ? id : context.dialogId,
|
|
90
126
|
'aria-labelledby': props['aria-label'] ? undefined : context.titleId,
|
package/dist/modal/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/modal/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE5E,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;;;;;;;;;;GAWG;AAEH,MAAM,YAAY,GAAG,aAAa,CAI/B,EAAE,CAAC,CAAC;AAYP,MAAM,UAAU,IAAI,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,GAAG,KAAK,EAAa;IAChG,MAAM,WAAW,GAAG,KAAK,EAAE,CAAC;IAC5B,MAAM,QAAQ,GAAG,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAI,WAAW,CAAC;IAC7C,OAAO,CACL,oBAAC,YAAY,CAAC,QAAQ,IAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,QAAQ,QAAQ,EAAE,YAAY,EAAE,IACnF,KAAK,CAAC,aAAa,CAClB,OAAO,EACP;QACE,GAAG,KAAK;QACR,IAAI,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,I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IACF,MAAM,MAAM,GAAG;QACb,EAAE,EAAE,EAAE,aAAF,EAAE,cAAF,EAAE,GAAI,OAAO,CAAC,QAAQ;QAC1B,iBAAiB,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO;QACpE,GAAG,EAAE,MAAM;QACX,OAAO,EAAE,CAAC,KAAwC,EAAE,EAAE;YACpD,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAG,KAAK,CAAC,CAAC;YACjB,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;QAC9B,CAAC;QACD,kDAAkD;QAClD,wBAAwB,EAAE,IAAI;KAC/B,CAAC;IACF,IAAI,OAAO;QAAE,OAAO,oBAAC,IAAI,OAAK,MAAM,KAAM,KAAK,GAAI,CAAC;IACpD,OAAO,mCAAY,MAAM,KAAM,KAAK,GAAI,CAAC;AAC3C,CAAC;AAID,4CAA4C;AAC5C,MAAM,UAAU,KAAK,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAc;IACrD,MAAM,OAAO,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;IACzC,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACtC,OAAO,oBAAC,OAAO,IAAC,EAAE,EAAE,OAAO,CAAC,OAAO,KAAM,KAAK,GAAI,CAAC;AACrD,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createContext, useContext, useEffect, useId, useRef } from 'react';\nimport type { ComponentProps, ReactNode, SyntheticEvent } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName } from './element.js';\n\n/*\n * Headless modal dialog: unstyled parts over a native `dialog` and the <govuk-react-modal>\n * element (./element.ts). Every part except Root takes `asChild`.\n *\n * Trigger and Close are buttons with invoker commands (`command` and `commandfor`), so where\n * the browser supports them the dialog opens as a true modal with no JavaScript: the page behind\n * is inert, focus moves into the dialog, and Escape closes it. The element runs the commands in\n * browsers without them, and applies `open` when controlled.\n *\n * Context only fills in ids: in an island without a Root, pass `dialogId` to Trigger and Close,\n * and `id` to Content.\n */\n\nconst ModalContext = createContext<{\n dialogId?: string;\n titleId?: string;\n onOpenChange?: (open: boolean) => void;\n}>({});\n\nexport type RootProps = Omit<React.HTMLAttributes<HTMLElement>, 'onChange'> & {\n children?: ReactNode;\n /** Whether the dialog is open, to control it. Without it, the buttons alone open and close it. */\n open?: boolean;\n /** Called when the dialog opens or closes. */\n onOpenChange?: (open: boolean) => void;\n /** The dialog's id, which the buttons' `commandfor` names. Defaults to a generated one. */\n dialogId?: string;\n};\n\nexport function Root({ children, open, onOpenChange, dialogId: dialogIdProp, ...props }: RootProps) {\n const generatedId = useId();\n const dialogId = dialogIdProp ?? generatedId;\n return (\n <ModalContext.Provider value={{ dialogId, titleId: `${dialogId}-title`, onOpenChange }}>\n {React.createElement(\n tagName,\n {\n ...props,\n open: open === undefined ? undefined : String(open),\n suppressHydrationWarning: true,\n },\n children\n )}\n </ModalContext.Provider>\n );\n}\n\ntype PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n};\n\ntype CommandProps = PartProps<'button'> & {\n /** The dialog's id. Set by Root. */\n dialogId?: string;\n};\n\nfunction Command({ command, asChild, dialogId, ...props }: CommandProps & { command: string }) {\n const context = useContext(ModalContext);\n // `command` and `commandfor` aren't in React's types yet, so they go in as a spread.\n const invoker = { command, commandfor: dialogId ?? context.dialogId };\n if (asChild) return <Slot {...invoker} {...props} />;\n return <button type=\"button\" {...invoker} {...props} />;\n}\n\nexport type TriggerProps = CommandProps;\n\n/** A button that opens the dialog as a modal. */\nexport function Trigger(props: TriggerProps) {\n return <Command command=\"show-modal\" {...props} />;\n}\n\nexport type CloseProps = CommandProps;\n\n/** A button that closes the dialog. */\nexport function Close(props: CloseProps) {\n return <Command command=\"close\" {...props} />;\n}\n\nexport type ContentProps = PartProps<'dialog'>;\n\n/**\n * The dialog. It's named by Title, unless you pass `aria-labelledby` or `aria-label`. Pass\n * `closedby=\"any\"` to also close it when the user clicks outside it.\n */\nexport function Content({ asChild, id, onClose, ref, ...props }: ContentProps) {\n const context = useContext(ModalContext);\n const reported = useRef<boolean | undefined>(undefined);\n const onOpenChange = useRef(context.onOpenChange);\n useEffect(() => {\n onOpenChange.current = context.onOpenChange;\n });\n // Report each change once: `toggle` fires on opening and closing where the browser fires it on\n // dialogs, and `close` on closing everywhere.\n const report = (dialog: HTMLDialogElement) => {\n if (reported.current === dialog.open) return;\n reported.current = dialog.open;\n onOpenChange.current?.(dialog.open);\n };\n // A dialog the user opened before hydration, through its invoker command or the element, fired\n // nothing React heard, so report it once.\n const dialogRef = useRef<HTMLDialogElement | null>(null);\n useEffect(() => {\n const dialog = dialogRef.current;\n if (dialog?.open && reported.current === undefined) {\n reported.current = true;\n onOpenChange.current?.(true);\n }\n }, []);\n // React doesn't listen for `toggle` on a dialog, so listen natively.\n const listen = (dialog: HTMLDialogElement | null) => {\n dialogRef.current = dialog;\n if (typeof ref === 'function') ref(dialog);\n else if (ref) ref.current = dialog;\n if (!dialog) return undefined;\n const onToggle = () => report(dialog);\n dialog.addEventListener('toggle', onToggle);\n return () => dialog.removeEventListener('toggle', onToggle);\n };\n const shared = {\n id: id ?? context.dialogId,\n 'aria-labelledby': props['aria-label'] ? undefined : context.titleId,\n ref: listen,\n onClose: (event: SyntheticEvent<HTMLDialogElement>) => {\n onClose?.(event);\n report(event.currentTarget);\n },\n // The dialog's open state belongs to the browser.\n suppressHydrationWarning: true,\n };\n if (asChild) return <Slot {...shared} {...props} />;\n return <dialog {...shared} {...props} />;\n}\n\nexport type TitleProps = PartProps<'h2'>;\n\n/** The dialog's heading, which names it. */\nexport function Title({ asChild, ...props }: TitleProps) {\n const context = useContext(ModalContext);\n const Element = asChild ? Slot : 'h2';\n return <Element id={context.titleId} {...props} />;\n}\n"]}
|
|
1
|
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back, useContext, useEffect, useId, useReducer, useRef } from 'react';\nimport type { ComponentProps, ReactNode, SyntheticEvent } from 'react';\nimport { early, live, type ChangeDetails } from '../utils/change-details.js';\nimport { composeRefs, Slot } from '../utils/slot.js';\nimport { tagName } from './element.js';\n\nexport type { ChangeDetails };\n\n/*\n * Headless modal dialog: unstyled parts over a native `dialog` and the <govuk-react-modal>\n * element (./element.ts). Every part except Root takes `asChild`.\n *\n * Trigger and Close are buttons with invoker commands (`command` and `commandfor`), so where\n * the browser supports them the dialog opens as a true modal with no JavaScript: the page behind\n * is inert, focus moves into the dialog, and Escape closes it. The element runs the commands in\n * browsers without them, and applies `open` when controlled.\n *\n * Context only fills in ids: in an island without a Root, pass `dialogId` to Trigger and Close,\n * and `id` to Content.\n */\n\nconst ModalContext = createContext<{\n dialogId?: string;\n titleId?: string;\n onOpenChange?: (open: boolean, details: ChangeDetails) => void;\n}>({});\n\nexport type RootProps = Omit<React.HTMLAttributes<HTMLElement>, 'onChange'> & {\n children?: ReactNode;\n /**\n * Whether the dialog is open, to control it once hydrated: the buttons and Escape then call\n * `onOpenChange`, and the dialog changes only when `open` does. Without it, the buttons alone\n * open and close it, which is best unless you need to open or close it yourself.\n */\n open?: boolean;\n /** Called when the dialog opens or closes. */\n onOpenChange?: (open: boolean, details: ChangeDetails) => void;\n /** The dialog's id, which the buttons' `commandfor` names. Defaults to a generated one. */\n dialogId?: string;\n};\n\nexport function Root({ children, open, onOpenChange, dialogId: dialogIdProp, ...props }: RootProps) {\n const generatedId = useId();\n const dialogId = dialogIdProp ?? generatedId;\n const element = useRef<HTMLElement>(null);\n const synced = useRef(false);\n const [, render] = useReducer((count: number) => count + 1, 0);\n // `open` is set once hydrated, so the server markup is uncontrolled and the buttons work before\n // hydration. A dialog opened by then is reported by Content, rather than closed.\n useEffect(() => {\n const node = element.current;\n if (!node || open === undefined) {\n node?.removeAttribute('open');\n return;\n }\n if (!synced.current) {\n synced.current = true;\n const dialog = document.getElementById(dialogId);\n if (onOpenChange && !open && dialog instanceof HTMLDialogElement && dialog.open) {\n // The dialog stays open until the next render, which adopts it or closes it.\n render();\n return;\n }\n }\n node.setAttribute('open', String(open));\n });\n return (\n <ModalContext.Provider value={{ dialogId, titleId: `${dialogId}-title`, onOpenChange }}>\n {React.createElement(\n tagName,\n {\n ...props,\n ref: element,\n suppressHydrationWarning: true,\n },\n children\n )}\n </ModalContext.Provider>\n );\n}\n\ntype PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n};\n\ntype CommandProps = PartProps<'button'> & {\n /** The dialog's id. Set by Root. */\n dialogId?: string;\n};\n\nfunction Command({ command, asChild, dialogId, ...props }: CommandProps & { command: string }) {\n const context = useContext(ModalContext);\n // `command` and `commandfor` aren't in React's types yet, so they go in as a spread.\n const invoker = { command, commandfor: dialogId ?? context.dialogId };\n if (asChild) return <Slot {...invoker} {...props} />;\n return <button type=\"button\" {...invoker} {...props} />;\n}\n\nexport type TriggerProps = CommandProps;\n\n/** A button that opens the dialog as a modal. */\nexport function Trigger(props: TriggerProps) {\n return <Command command=\"show-modal\" {...props} />;\n}\n\nexport type CloseProps = CommandProps;\n\n/** A button that closes the dialog. */\nexport function Close(props: CloseProps) {\n return <Command command=\"close\" {...props} />;\n}\n\nexport type ContentProps = PartProps<'dialog'>;\n\n/**\n * The dialog. It's named by Title, unless you pass `aria-labelledby` or `aria-label`. Pass\n * `closedby=\"any\"` to also close it when the user clicks outside it.\n */\nexport function Content({ asChild, id, onClose, ref, ...props }: ContentProps) {\n const context = useContext(ModalContext);\n const reported = useRef<boolean | undefined>(undefined);\n const onOpenChange = useRef(context.onOpenChange);\n useEffect(() => {\n onOpenChange.current = context.onOpenChange;\n });\n // Report each change once.\n const report = useCallback((dialog: HTMLDialogElement) => {\n if (reported.current === dialog.open) return;\n reported.current = dialog.open;\n // A controlled dialog that arrives at `open` is no news.\n if (dialog.closest(tagName)?.getAttribute('open') === String(dialog.open)) return;\n onOpenChange.current?.(dialog.open, live);\n }, []);\n // A dialog the user opened before hydration, through its invoker command or the element, fired\n // nothing React heard, so report it once.\n const dialogRef = useRef<HTMLDialogElement | null>(null);\n useEffect(() => {\n const dialog = dialogRef.current;\n if (dialog?.open && reported.current === undefined) {\n reported.current = true;\n onOpenChange.current?.(true, early);\n }\n }, []);\n // Not every browser fires `toggle` on dialogs, so watch the dialog's `open` attribute.\n const listen = useCallback(\n (dialog: HTMLDialogElement | null) => {\n dialogRef.current = dialog;\n if (!dialog) return undefined;\n const forget = composeRefs(ref)(dialog);\n const observer = new MutationObserver(() => report(dialog));\n observer.observe(dialog, { attributes: true, attributeFilter: ['open'] });\n // While controlled, the element asks for a change the dialog hasn't made.\n const root = dialog.closest(tagName);\n const onRequest = (event: CustomEvent<{ open: boolean }>) => {\n if (event.target !== root || event.detail.open === dialog.open) return;\n onOpenChange.current?.(event.detail.open, live);\n };\n root?.addEventListener('govuk-react-modal:change', onRequest);\n return () => {\n observer.disconnect();\n root?.removeEventListener('govuk-react-modal:change', onRequest);\n dialogRef.current = null;\n forget();\n };\n },\n [ref, report]\n );\n const shared = {\n id: id ?? context.dialogId,\n 'aria-labelledby': props['aria-label'] ? undefined : context.titleId,\n ref: listen,\n onClose: (event: SyntheticEvent<HTMLDialogElement>) => {\n onClose?.(event);\n report(event.currentTarget);\n },\n // The dialog's open state belongs to the browser.\n suppressHydrationWarning: true,\n };\n if (asChild) return <Slot {...shared} {...props} />;\n return <dialog {...shared} {...props} />;\n}\n\nexport type TitleProps = PartProps<'h2'>;\n\n/** The dialog's heading, which names it. */\nexport function Title({ asChild, ...props }: TitleProps) {\n const context = useContext(ModalContext);\n const Element = asChild ? Slot : 'h2';\n return <Element id={context.titleId} {...props} />;\n}\n"]}
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Markup inside the element:
|
|
10
10
|
* - `[data-password-input]`: an `input` of type "password".
|
|
11
|
-
* - `[data-password-
|
|
11
|
+
* - `[data-password-trigger]`: a `button` of type "button", rendered `hidden`.
|
|
12
12
|
* - `[data-password-status]` (optional): a visually hidden live region for the announcements.
|
|
13
13
|
* Without one, the element adds one after the input.
|
|
14
14
|
*
|
|
15
|
+
* While upgraded it sets `data-upgraded`.
|
|
16
|
+
*
|
|
15
17
|
* Attributes, for the text (English by default):
|
|
16
18
|
* - `show-text`, `hide-text`: the button's text.
|
|
17
19
|
* - `show-label`, `hide-label`: the button's accessible name.
|
|
@@ -20,7 +22,7 @@
|
|
|
20
22
|
* As in govuk-frontend, the password is hidden again when its form is submitted, unless by a hidden
|
|
21
23
|
* submit button such as a form library's, and when the page is restored from the back-forward cache.
|
|
22
24
|
*/
|
|
23
|
-
import {
|
|
25
|
+
import { setText } from '../utils/dom.js';
|
|
24
26
|
import { setUpWhenReady } from '../utils/set-up-when-ready.js';
|
|
25
27
|
export const tagName = 'govuk-react-password-input';
|
|
26
28
|
const defaults = {
|
|
@@ -32,18 +34,29 @@ const defaults = {
|
|
|
32
34
|
'hidden-announcement': 'Your password is hidden',
|
|
33
35
|
};
|
|
34
36
|
// The same visually hidden styles as govuk-frontend's, for a live region the element adds itself.
|
|
35
|
-
const visuallyHidden =
|
|
37
|
+
const visuallyHidden = {
|
|
38
|
+
position: 'absolute',
|
|
39
|
+
width: '1px',
|
|
40
|
+
height: '1px',
|
|
41
|
+
margin: '0',
|
|
42
|
+
padding: '0',
|
|
43
|
+
overflow: 'hidden',
|
|
44
|
+
clip: 'rect(0 0 0 0)',
|
|
45
|
+
'clip-path': 'inset(50%)',
|
|
46
|
+
border: '0',
|
|
47
|
+
'white-space': 'nowrap',
|
|
48
|
+
};
|
|
36
49
|
if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
|
|
37
50
|
class GovukReactPasswordInput extends HTMLElement {
|
|
38
51
|
constructor() {
|
|
39
52
|
super(...arguments);
|
|
40
53
|
this.visible = false;
|
|
41
54
|
// React sets `type` again whenever it updates a controlled input.
|
|
42
|
-
this.
|
|
55
|
+
this.typeObserver = new MutationObserver(() => {
|
|
43
56
|
const type = this.visible ? 'text' : 'password';
|
|
44
57
|
if (this.input && this.input.type !== type)
|
|
45
58
|
this.input.type = type;
|
|
46
|
-
};
|
|
59
|
+
});
|
|
47
60
|
this.onToggle = (event) => {
|
|
48
61
|
event.preventDefault();
|
|
49
62
|
this.setVisible(!this.visible);
|
|
@@ -66,12 +79,12 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
66
79
|
this.observer = setUpWhenReady(this, () => this.setup());
|
|
67
80
|
}
|
|
68
81
|
disconnectedCallback() {
|
|
69
|
-
var _a, _b
|
|
82
|
+
var _a, _b;
|
|
70
83
|
(_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
71
84
|
(_b = this.toggle) === null || _b === void 0 ? void 0 : _b.removeEventListener('click', this.onToggle);
|
|
72
85
|
document.removeEventListener('submit', this.onSubmit);
|
|
73
86
|
window.removeEventListener('pageshow', this.onPageShow);
|
|
74
|
-
|
|
87
|
+
this.typeObserver.disconnect();
|
|
75
88
|
this.input = undefined;
|
|
76
89
|
this.toggle = undefined;
|
|
77
90
|
}
|
|
@@ -87,7 +100,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
87
100
|
if (this.input)
|
|
88
101
|
return true;
|
|
89
102
|
const input = this.own('[data-password-input]', HTMLInputElement);
|
|
90
|
-
const toggle = this.own('[data-password-
|
|
103
|
+
const toggle = this.own('[data-password-trigger]', HTMLButtonElement);
|
|
91
104
|
if (!input || !toggle)
|
|
92
105
|
return false;
|
|
93
106
|
this.input = input;
|
|
@@ -97,9 +110,12 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
97
110
|
this.status = document.createElement('div');
|
|
98
111
|
this.status.dataset.passwordStatus = '';
|
|
99
112
|
this.status.setAttribute('aria-live', 'polite');
|
|
100
|
-
|
|
113
|
+
// Set one by one: a Content Security Policy can block a whole `style` attribute.
|
|
114
|
+
const { style } = this.status;
|
|
115
|
+
Object.entries(visuallyHidden).forEach(([name, value]) => style.setProperty(name, value));
|
|
101
116
|
input.insertAdjacentElement('afterend', this.status);
|
|
102
117
|
}
|
|
118
|
+
this.dataset.upgraded = '';
|
|
103
119
|
toggle.hidden = false;
|
|
104
120
|
toggle.addEventListener('click', this.onToggle);
|
|
105
121
|
document.addEventListener('submit', this.onSubmit);
|
|
@@ -107,7 +123,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
107
123
|
// Start hidden, with the text set but nothing announced.
|
|
108
124
|
this.visible = false;
|
|
109
125
|
input.type = 'password';
|
|
110
|
-
this.
|
|
126
|
+
this.typeObserver.observe(input, { attributes: true, attributeFilter: ['type'] });
|
|
111
127
|
this.label(false);
|
|
112
128
|
return true;
|
|
113
129
|
}
|
|
@@ -124,7 +140,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
124
140
|
label(visible) {
|
|
125
141
|
if (!this.toggle)
|
|
126
142
|
return;
|
|
127
|
-
this.toggle
|
|
143
|
+
setText(this.toggle, this.text(visible ? 'hide-text' : 'show-text'));
|
|
128
144
|
this.toggle.setAttribute('aria-label', this.text(visible ? 'hide-label' : 'show-label'));
|
|
129
145
|
}
|
|
130
146
|
}
|