@govuk-react/primitives 0.0.0-stage → 0.2.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.
Files changed (121) hide show
  1. package/README.md +271 -2
  2. package/dist/accordion/element.d.ts +14 -0
  3. package/dist/accordion/element.js +302 -0
  4. package/dist/accordion/element.js.map +1 -0
  5. package/dist/accordion/index.d.ts +74 -0
  6. package/dist/accordion/index.js +138 -0
  7. package/dist/accordion/index.js.map +1 -0
  8. package/dist/auto-focus/element.d.ts +1 -0
  9. package/dist/auto-focus/element.js +75 -0
  10. package/dist/auto-focus/element.js.map +1 -0
  11. package/dist/auto-focus/index.d.ts +297 -0
  12. package/dist/auto-focus/index.js +29 -0
  13. package/dist/auto-focus/index.js.map +1 -0
  14. package/dist/autocomplete/element.d.ts +27 -0
  15. package/dist/autocomplete/element.js +597 -0
  16. package/dist/autocomplete/element.js.map +1 -0
  17. package/dist/autocomplete/index.d.ts +350 -0
  18. package/dist/autocomplete/index.js +108 -0
  19. package/dist/autocomplete/index.js.map +1 -0
  20. package/dist/button/element.d.ts +1 -0
  21. package/dist/button/element.js +59 -0
  22. package/dist/button/element.js.map +1 -0
  23. package/dist/button/index.d.ts +10 -0
  24. package/dist/button/index.js +9 -0
  25. package/dist/button/index.js.map +1 -0
  26. package/dist/character-count/element.d.ts +7 -0
  27. package/dist/character-count/element.js +287 -0
  28. package/dist/character-count/element.js.map +1 -0
  29. package/dist/character-count/index.d.ts +57 -0
  30. package/dist/character-count/index.js +79 -0
  31. package/dist/character-count/index.js.map +1 -0
  32. package/dist/character-count/messages.d.ts +20 -0
  33. package/dist/character-count/messages.js +54 -0
  34. package/dist/character-count/messages.js.map +1 -0
  35. package/dist/choices/element.d.ts +5 -0
  36. package/dist/choices/element.js +202 -0
  37. package/dist/choices/element.js.map +1 -0
  38. package/dist/choices/index.d.ts +309 -0
  39. package/dist/choices/index.js +37 -0
  40. package/dist/choices/index.js.map +1 -0
  41. package/dist/disclosure/element.d.ts +14 -0
  42. package/dist/disclosure/element.js +123 -0
  43. package/dist/disclosure/element.js.map +1 -0
  44. package/dist/disclosure/index.d.ts +307 -0
  45. package/dist/disclosure/index.js +66 -0
  46. package/dist/disclosure/index.js.map +1 -0
  47. package/dist/exit-this-page/element.d.ts +19 -0
  48. package/dist/exit-this-page/element.js +201 -0
  49. package/dist/exit-this-page/element.js.map +1 -0
  50. package/dist/exit-this-page/index.d.ts +330 -0
  51. package/dist/exit-this-page/index.js +54 -0
  52. package/dist/exit-this-page/index.js.map +1 -0
  53. package/dist/file-upload/element.d.ts +23 -0
  54. package/dist/file-upload/element.js +270 -0
  55. package/dist/file-upload/element.js.map +1 -0
  56. package/dist/file-upload/index.d.ts +46 -0
  57. package/dist/file-upload/index.js +65 -0
  58. package/dist/file-upload/index.js.map +1 -0
  59. package/dist/focus-links/element.d.ts +1 -0
  60. package/dist/focus-links/element.js +90 -0
  61. package/dist/focus-links/element.js.map +1 -0
  62. package/dist/focus-links/index.d.ts +288 -0
  63. package/dist/focus-links/index.js +7 -0
  64. package/dist/focus-links/index.js.map +1 -0
  65. package/dist/index.d.ts +13 -0
  66. package/dist/index.js +14 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/inline/accordion-element.js +1 -0
  69. package/dist/inline/auto-focus-element.js +1 -0
  70. package/dist/inline/autocomplete-element.js +2 -0
  71. package/dist/inline/button-element.js +1 -0
  72. package/dist/inline/character-count-element.js +1 -0
  73. package/dist/inline/choices-element.js +1 -0
  74. package/dist/inline/disclosure-element.js +1 -0
  75. package/dist/inline/exit-this-page-element.js +1 -0
  76. package/dist/inline/file-upload-element.js +1 -0
  77. package/dist/inline/focus-links-element.js +1 -0
  78. package/dist/inline/modal-element.js +1 -0
  79. package/dist/inline/password-input-element.js +1 -0
  80. package/dist/inline/tabs-element.js +1 -0
  81. package/dist/inline-script/index.d.ts +26 -0
  82. package/dist/inline-script/index.js +31 -0
  83. package/dist/inline-script/index.js.map +1 -0
  84. package/dist/modal/element.d.ts +10 -0
  85. package/dist/modal/element.js +114 -0
  86. package/dist/modal/element.js.map +1 -0
  87. package/dist/modal/index.d.ts +36 -0
  88. package/dist/modal/index.js +109 -0
  89. package/dist/modal/index.js.map +1 -0
  90. package/dist/password-input/element.d.ts +1 -0
  91. package/dist/password-input/element.js +134 -0
  92. package/dist/password-input/element.js.map +1 -0
  93. package/dist/password-input/index.d.ts +318 -0
  94. package/dist/password-input/index.js +35 -0
  95. package/dist/password-input/index.js.map +1 -0
  96. package/dist/tabs/element.d.ts +19 -0
  97. package/dist/tabs/element.js +416 -0
  98. package/dist/tabs/element.js.map +1 -0
  99. package/dist/tabs/index.d.ts +83 -0
  100. package/dist/tabs/index.js +138 -0
  101. package/dist/tabs/index.js.map +1 -0
  102. package/dist/utils/dom.d.ts +9 -0
  103. package/dist/utils/dom.js +27 -0
  104. package/dist/utils/dom.js.map +1 -0
  105. package/dist/utils/on-property-set.d.ts +9 -0
  106. package/dist/utils/on-property-set.js +38 -0
  107. package/dist/utils/on-property-set.js.map +1 -0
  108. package/dist/utils/set-focus.d.ts +11 -0
  109. package/dist/utils/set-focus.js +38 -0
  110. package/dist/utils/set-focus.js.map +1 -0
  111. package/dist/utils/set-up-when-ready.d.ts +6 -0
  112. package/dist/utils/set-up-when-ready.js +16 -0
  113. package/dist/utils/set-up-when-ready.js.map +1 -0
  114. package/dist/utils/slot.d.ts +13 -0
  115. package/dist/utils/slot.js +40 -0
  116. package/dist/utils/slot.js.map +1 -0
  117. package/dist/utils/translation.d.ts +13 -0
  118. package/dist/utils/translation.js +57 -0
  119. package/dist/utils/translation.js.map +1 -0
  120. package/licences/accessible-autocomplete.txt +20 -0
  121. package/package.json +49 -4
@@ -0,0 +1,23 @@
1
+ import { type Translation } from '../utils/translation.js';
2
+ export declare const tagName = "govuk-react-file-upload";
3
+ export interface FileUploadElement extends HTMLElement {
4
+ /** Updates the button from the input's files, such as after setting them. */
5
+ update(): void;
6
+ }
7
+ /** govuk-frontend's English text. */
8
+ export declare const defaults: {
9
+ 'no-file-chosen': string;
10
+ 'multiple-files-chosen': {
11
+ one: string;
12
+ other: string;
13
+ };
14
+ 'entered-drop-zone': string;
15
+ 'left-drop-zone': string;
16
+ };
17
+ /** What the status says for the files chosen. */
18
+ export declare function statusText(files: ArrayLike<{
19
+ name: string;
20
+ }>, messages: {
21
+ 'no-file-chosen'?: Translation;
22
+ 'multiple-files-chosen'?: Translation;
23
+ }, locale?: string): string;
@@ -0,0 +1,270 @@
1
+ /*
2
+ * <govuk-react-file-upload>: a headless, light-DOM custom element that turns a file input into a
3
+ * button and drop zone, with or without React, as govuk-frontend's improved file upload does.
4
+ *
5
+ * The input's files are the state. Without JavaScript the native file input shows and the button
6
+ * stays hidden. On upgrade the element hides the input and shows the button, which opens the file
7
+ * picker, says which files are chosen, and takes files dropped on it. The rendered markup is never
8
+ * moved, so React can keep rendering it.
9
+ *
10
+ * As in govuk-frontend, the button takes the input's `id`, so its label names the button, and the
11
+ * input's id gets `-input` added. The button also takes the input's `aria-describedby` and
12
+ * `disabled`, and its accessible name is the label, the separator and its own text.
13
+ *
14
+ * Markup inside the element:
15
+ * - `input[type="file"]`: the field, with an `id` and a `label`. Hidden on upgrade.
16
+ * - `button[data-file-upload-button]`: rendered `hidden`, with `type="button"`.
17
+ * - `[data-file-upload-status]`: which files are chosen.
18
+ * - `[data-file-upload-separator]` (optional): between the label and the button's text in its
19
+ * accessible name, such as a visually hidden comma.
20
+ * - `[data-file-upload-drop-zone]` (optional): the area that shows dragging. The element itself
21
+ * without one.
22
+ * - `[data-file-upload-announcements]` (optional): a visually hidden live region for entering and
23
+ * leaving the drop zone.
24
+ *
25
+ * The button gets `data-empty` while no file is chosen and `data-dragging` while files are dragged
26
+ * over the drop zone, and the drop zone gets `data-disabled`.
27
+ *
28
+ * Attributes:
29
+ * - `no-file-chosen`, `multiple-files-chosen`, `entered-drop-zone`, `left-drop-zone`: the text,
30
+ * English by default, with `%{count}` for the number of files. Add a plural form to the name,
31
+ * such as `multiple-files-chosen-one`, for a message for each form.
32
+ * - `empty-class`, `dragging-class`: classes for the button while empty and while dragging.
33
+ * - `disabled-class`: classes for the drop zone while the input is disabled.
34
+ */
35
+ import { translate, translationAttributeNames, translationFromAttributes, } from '../utils/translation.js';
36
+ import { classesOf, setText, setAttribute } from '../utils/dom.js';
37
+ export const tagName = 'govuk-react-file-upload';
38
+ /** govuk-frontend's English text. */
39
+ export const defaults = {
40
+ 'no-file-chosen': 'No file chosen',
41
+ 'multiple-files-chosen': { one: '%{count} file chosen', other: '%{count} files chosen' },
42
+ 'entered-drop-zone': 'Entered drop zone',
43
+ 'left-drop-zone': 'Left drop zone',
44
+ };
45
+ /** What the status says for the files chosen. */
46
+ export function statusText(files, messages, locale) {
47
+ var _a, _b;
48
+ if (files.length === 0)
49
+ return translate((_a = messages['no-file-chosen']) !== null && _a !== void 0 ? _a : defaults['no-file-chosen'], 0, locale);
50
+ if (files.length === 1)
51
+ return files[0].name;
52
+ return translate((_b = messages['multiple-files-chosen']) !== null && _b !== void 0 ? _b : defaults['multiple-files-chosen'], files.length, locale);
53
+ }
54
+ const countFiles = (items) => Array.from(items).filter((item) => item.kind === 'file').length;
55
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
56
+ class GovukReactFileUpload extends HTMLElement {
57
+ constructor() {
58
+ super(...arguments);
59
+ /** The input's id before upgrade, which the button takes. */
60
+ this.fieldId = '';
61
+ this.dragging = false;
62
+ this.enteredAnotherElement = false;
63
+ this.onChange = () => this.update();
64
+ this.onReset = () => window.setTimeout(() => this.update());
65
+ this.onClick = () => { var _a; return (_a = this.input) === null || _a === void 0 ? void 0 : _a.click(); };
66
+ this.onDragOver = (event) => event.preventDefault();
67
+ this.onDrop = (event) => {
68
+ var _a;
69
+ event.preventDefault();
70
+ const { input } = this;
71
+ const files = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.files;
72
+ if (!input || !files || !this.matchesCapacity(files.length))
73
+ return;
74
+ input.files = files;
75
+ // Bubbles, unlike govuk-frontend's, so React's `onChange` hears dropped files too.
76
+ input.dispatchEvent(new Event('input', { bubbles: true }));
77
+ input.dispatchEvent(new Event('change', { bubbles: true }));
78
+ this.setDragging(false);
79
+ };
80
+ this.onDocumentDragEnter = (event) => {
81
+ this.enteredAnotherElement = true;
82
+ if (!this.button || this.button.disabled || !(event.target instanceof Node))
83
+ return;
84
+ if (this.dropZone.contains(event.target)) {
85
+ if (event.dataTransfer && this.canDrop(event.dataTransfer) && !this.dragging) {
86
+ this.setDragging(true);
87
+ this.announce('entered-drop-zone');
88
+ }
89
+ }
90
+ else if (this.dragging) {
91
+ this.setDragging(false);
92
+ this.announce('left-drop-zone');
93
+ }
94
+ };
95
+ // Leaving the window, or dragging back out of it, fires `dragleave` with no `dragenter`.
96
+ this.onDocumentDragLeave = () => {
97
+ var _a;
98
+ if (!this.enteredAnotherElement && this.dragging && !((_a = this.button) === null || _a === void 0 ? void 0 : _a.disabled)) {
99
+ this.setDragging(false);
100
+ this.announce('left-drop-zone');
101
+ }
102
+ this.enteredAnotherElement = false;
103
+ };
104
+ }
105
+ static get observedAttributes() {
106
+ return [
107
+ 'empty-class',
108
+ 'dragging-class',
109
+ 'disabled-class',
110
+ ...Object.keys(defaults).flatMap(translationAttributeNames),
111
+ ];
112
+ }
113
+ connectedCallback() {
114
+ // The parts may be parsed or rendered later, and React can rewrite the input's attributes.
115
+ this.observer = new MutationObserver(() => this.setup());
116
+ this.observer.observe(this, {
117
+ childList: true,
118
+ subtree: true,
119
+ attributes: true,
120
+ attributeFilter: ['class', 'disabled', 'id', 'aria-describedby'],
121
+ });
122
+ document.addEventListener('dragenter', this.onDocumentDragEnter);
123
+ document.addEventListener('dragleave', this.onDocumentDragLeave);
124
+ this.setup();
125
+ }
126
+ disconnectedCallback() {
127
+ var _a;
128
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
129
+ document.removeEventListener('dragenter', this.onDocumentDragEnter);
130
+ document.removeEventListener('dragleave', this.onDocumentDragLeave);
131
+ this.unbind();
132
+ }
133
+ attributeChangedCallback() {
134
+ // Wait for every attribute: React sets them one by one before connecting the element.
135
+ if (this.isConnected)
136
+ this.update();
137
+ }
138
+ own(selector) {
139
+ return Array.from(this.querySelectorAll(selector)).find((element) => element.closest(tagName) === this);
140
+ }
141
+ get dropZone() {
142
+ var _a;
143
+ return (_a = this.own('[data-file-upload-drop-zone]')) !== null && _a !== void 0 ? _a : this;
144
+ }
145
+ get locale() {
146
+ var _a;
147
+ return ((_a = this.closest('[lang]')) === null || _a === void 0 ? void 0 : _a.getAttribute('lang')) || 'en';
148
+ }
149
+ message(name) {
150
+ var _a;
151
+ return (_a = translationFromAttributes(this, name)) !== null && _a !== void 0 ? _a : defaults[name];
152
+ }
153
+ setup() {
154
+ var _a;
155
+ const input = this.own('input[type="file"]');
156
+ const button = this.own('button[data-file-upload-button]');
157
+ if (!input || !button)
158
+ return;
159
+ // A new input, or a new id from React, rather than the one the element gave it.
160
+ if (input.id !== `${this.fieldId}-input`)
161
+ this.fieldId = input.id;
162
+ if (!this.fieldId)
163
+ return;
164
+ if (input !== this.input || button !== this.button) {
165
+ this.unbind();
166
+ this.input = input;
167
+ this.button = button;
168
+ this.form = input.form;
169
+ input.addEventListener('change', this.onChange);
170
+ (_a = this.form) === null || _a === void 0 ? void 0 : _a.addEventListener('reset', this.onReset);
171
+ button.addEventListener('click', this.onClick);
172
+ button.addEventListener('dragover', this.onDragOver);
173
+ button.addEventListener('drop', this.onDrop);
174
+ }
175
+ this.upgrade(input, button);
176
+ this.update();
177
+ }
178
+ unbind() {
179
+ var _a, _b, _c, _d, _e;
180
+ (_a = this.input) === null || _a === void 0 ? void 0 : _a.removeEventListener('change', this.onChange);
181
+ (_b = this.form) === null || _b === void 0 ? void 0 : _b.removeEventListener('reset', this.onReset);
182
+ (_c = this.button) === null || _c === void 0 ? void 0 : _c.removeEventListener('click', this.onClick);
183
+ (_d = this.button) === null || _d === void 0 ? void 0 : _d.removeEventListener('dragover', this.onDragOver);
184
+ (_e = this.button) === null || _e === void 0 ? void 0 : _e.removeEventListener('drop', this.onDrop);
185
+ this.input = undefined;
186
+ this.button = undefined;
187
+ this.form = undefined;
188
+ }
189
+ /** Moves the id to the button, names it, and swaps it for the input, as govuk-frontend does. */
190
+ upgrade(input, button) {
191
+ const id = this.fieldId;
192
+ const root = this.getRootNode();
193
+ const label = root instanceof Document || root instanceof ShadowRoot
194
+ ? root.querySelector(`label[for="${CSS.escape(id)}"]`)
195
+ : null;
196
+ if (label && !label.id)
197
+ label.id = `${id}-label`;
198
+ setAttribute(input, 'id', `${id}-input`);
199
+ setAttribute(button, 'id', id);
200
+ const separator = button.querySelector('[data-file-upload-separator]');
201
+ if (separator)
202
+ setAttribute(separator, 'id', `${id}-comma`);
203
+ if (label) {
204
+ setAttribute(button, 'aria-labelledby', [label.id, separator === null || separator === void 0 ? void 0 : separator.id, id].filter(Boolean).join(' '));
205
+ }
206
+ setAttribute(button, 'aria-describedby', input.getAttribute('aria-describedby'));
207
+ setAttribute(input, 'hidden', 'hidden');
208
+ if (button.hidden)
209
+ button.hidden = false;
210
+ this.dataset.upgraded = '';
211
+ }
212
+ update() {
213
+ const { input, button } = this;
214
+ if (!input || !button)
215
+ return;
216
+ const { files } = input;
217
+ const empty = !(files === null || files === void 0 ? void 0 : files.length);
218
+ const status = button.querySelector('[data-file-upload-status]');
219
+ if (status) {
220
+ setText(status, statusText(files !== null && files !== void 0 ? files : [], {
221
+ 'no-file-chosen': this.message('no-file-chosen'),
222
+ 'multiple-files-chosen': this.message('multiple-files-chosen'),
223
+ }, this.locale));
224
+ }
225
+ // `toggle` with force leaves a class that's already there alone, so the observer isn't woken.
226
+ setAttribute(button, 'data-empty', empty ? '' : null);
227
+ classesOf(this.getAttribute('empty-class')).forEach((name) => button.classList.toggle(name, empty));
228
+ if (button.disabled !== input.disabled)
229
+ button.disabled = input.disabled;
230
+ const zone = this.dropZone;
231
+ setAttribute(zone, 'data-disabled', input.disabled ? '' : null);
232
+ classesOf(this.getAttribute('disabled-class')).forEach((name) => zone.classList.toggle(name, input.disabled));
233
+ if (input.disabled)
234
+ this.setDragging(false);
235
+ else
236
+ this.renderDragging();
237
+ }
238
+ setDragging(dragging) {
239
+ this.dragging = dragging;
240
+ this.renderDragging();
241
+ }
242
+ renderDragging() {
243
+ const { button, dragging } = this;
244
+ if (!button)
245
+ return;
246
+ setAttribute(button, 'data-dragging', dragging ? '' : null);
247
+ classesOf(this.getAttribute('dragging-class')).forEach((name) => button.classList.toggle(name, dragging));
248
+ }
249
+ announce(name) {
250
+ const announcements = this.own('[data-file-upload-announcements]');
251
+ // Replaces the text node every time, so screen readers hear the same message again.
252
+ if (announcements)
253
+ announcements.textContent = translate(this.message(name), 0, this.locale);
254
+ }
255
+ canDrop(dataTransfer) {
256
+ if (dataTransfer.items.length)
257
+ return this.matchesCapacity(countFiles(dataTransfer.items));
258
+ if (dataTransfer.types.length)
259
+ return dataTransfer.types.includes('Files');
260
+ return true;
261
+ }
262
+ matchesCapacity(count) {
263
+ var _a;
264
+ return ((_a = this.input) === null || _a === void 0 ? void 0 : _a.multiple) ? count > 0 : count === 1;
265
+ }
266
+ }
267
+ if (!customElements.get(tagName))
268
+ customElements.define(tagName, GovukReactFileUpload);
269
+ }
270
+ //# sourceMappingURL=element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/file-upload/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EACL,SAAS,EACT,yBAAyB,EACzB,yBAAyB,GAE1B,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEnE,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AAOjD,qCAAqC;AACrC,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,gBAAgB,EAAE,g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esContent":["/*\n * <govuk-react-file-upload>: a headless, light-DOM custom element that turns a file input into a\n * button and drop zone, with or without React, as govuk-frontend's improved file upload does.\n *\n * The input's files are the state. Without JavaScript the native file input shows and the button\n * stays hidden. On upgrade the element hides the input and shows the button, which opens the file\n * picker, says which files are chosen, and takes files dropped on it. The rendered markup is never\n * moved, so React can keep rendering it.\n *\n * As in govuk-frontend, the button takes the input's `id`, so its label names the button, and the\n * input's id gets `-input` added. The button also takes the input's `aria-describedby` and\n * `disabled`, and its accessible name is the label, the separator and its own text.\n *\n * Markup inside the element:\n * - `input[type=\"file\"]`: the field, with an `id` and a `label`. Hidden on upgrade.\n * - `button[data-file-upload-button]`: rendered `hidden`, with `type=\"button\"`.\n * - `[data-file-upload-status]`: which files are chosen.\n * - `[data-file-upload-separator]` (optional): between the label and the button's text in its\n * accessible name, such as a visually hidden comma.\n * - `[data-file-upload-drop-zone]` (optional): the area that shows dragging. The element itself\n * without one.\n * - `[data-file-upload-announcements]` (optional): a visually hidden live region for entering and\n * leaving the drop zone.\n *\n * The button gets `data-empty` while no file is chosen and `data-dragging` while files are dragged\n * over the drop zone, and the drop zone gets `data-disabled`.\n *\n * Attributes:\n * - `no-file-chosen`, `multiple-files-chosen`, `entered-drop-zone`, `left-drop-zone`: the text,\n * English by default, with `%{count}` for the number of files. Add a plural form to the name,\n * such as `multiple-files-chosen-one`, for a message for each form.\n * - `empty-class`, `dragging-class`: classes for the button while empty and while dragging.\n * - `disabled-class`: classes for the drop zone while the input is disabled.\n */\n\nimport {\n translate,\n translationAttributeNames,\n translationFromAttributes,\n type Translation,\n} from '../utils/translation.js';\nimport { classesOf, setText, setAttribute } from '../utils/dom.js';\n\nexport const tagName = 'govuk-react-file-upload';\n\nexport interface FileUploadElement extends HTMLElement {\n /** Updates the button from the input's files, such as after setting them. */\n update(): void;\n}\n\n/** govuk-frontend's English text. */\nexport const defaults = {\n 'no-file-chosen': 'No file chosen',\n 'multiple-files-chosen': { one: '%{count} file chosen', other: '%{count} files chosen' },\n 'entered-drop-zone': 'Entered drop zone',\n 'left-drop-zone': 'Left drop zone',\n} satisfies Record<string, Translation>;\n\n/** What the status says for the files chosen. */\nexport function statusText(\n files: ArrayLike<{ name: string }>,\n messages: { 'no-file-chosen'?: Translation; 'multiple-files-chosen'?: Translation },\n locale?: string\n) {\n if (files.length === 0) return translate(messages['no-file-chosen'] ?? defaults['no-file-chosen'], 0, locale);\n if (files.length === 1) return files[0].name;\n return translate(messages['multiple-files-chosen'] ?? defaults['multiple-files-chosen'], files.length, locale);\n}\n\nconst countFiles = (items: DataTransferItemList) => Array.from(items).filter((item) => item.kind === 'file').length;\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactFileUpload extends HTMLElement implements FileUploadElement {\n static get observedAttributes() {\n return [\n 'empty-class',\n 'dragging-class',\n 'disabled-class',\n ...Object.keys(defaults).flatMap(translationAttributeNames),\n ];\n }\n\n private input?: HTMLInputElement;\n\n private button?: HTMLButtonElement;\n\n private form?: HTMLFormElement | null;\n\n /** The input's id before upgrade, which the button takes. */\n private fieldId = '';\n\n private dragging = false;\n\n private enteredAnotherElement = false;\n\n private observer?: MutationObserver;\n\n private readonly onChange = () => this.update();\n\n private readonly onReset = () => window.setTimeout(() => this.update());\n\n private readonly onClick = () => this.input?.click();\n\n private readonly onDragOver = (event: DragEvent) => event.preventDefault();\n\n private readonly onDrop = (event: DragEvent) => {\n event.preventDefault();\n const { input } = this;\n const files = event.dataTransfer?.files;\n if (!input || !files || !this.matchesCapacity(files.length)) return;\n input.files = files;\n // Bubbles, unlike govuk-frontend's, so React's `onChange` hears dropped files too.\n input.dispatchEvent(new Event('input', { bubbles: true }));\n input.dispatchEvent(new Event('change', { bubbles: true }));\n this.setDragging(false);\n };\n\n private readonly onDocumentDragEnter = (event: DragEvent) => {\n this.enteredAnotherElement = true;\n if (!this.button || this.button.disabled || !(event.target instanceof Node)) return;\n if (this.dropZone.contains(event.target)) {\n if (event.dataTransfer && this.canDrop(event.dataTransfer) && !this.dragging) {\n this.setDragging(true);\n this.announce('entered-drop-zone');\n }\n } else if (this.dragging) {\n this.setDragging(false);\n this.announce('left-drop-zone');\n }\n };\n\n // Leaving the window, or dragging back out of it, fires `dragleave` with no `dragenter`.\n private readonly onDocumentDragLeave = () => {\n if (!this.enteredAnotherElement && this.dragging && !this.button?.disabled) {\n this.setDragging(false);\n this.announce('left-drop-zone');\n }\n this.enteredAnotherElement = false;\n };\n\n connectedCallback() {\n // The parts may be parsed or rendered later, and React can rewrite the input's attributes.\n this.observer = new MutationObserver(() => this.setup());\n this.observer.observe(this, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: ['class', 'disabled', 'id', 'aria-describedby'],\n });\n document.addEventListener('dragenter', this.onDocumentDragEnter);\n document.addEventListener('dragleave', this.onDocumentDragLeave);\n this.setup();\n }\n\n disconnectedCallback() {\n this.observer?.disconnect();\n document.removeEventListener('dragenter', this.onDocumentDragEnter);\n document.removeEventListener('dragleave', this.onDocumentDragLeave);\n this.unbind();\n }\n\n attributeChangedCallback() {\n // Wait for every attribute: React sets them one by one before connecting the element.\n if (this.isConnected) this.update();\n }\n\n private own<E extends Element>(selector: string) {\n return Array.from(this.querySelectorAll<E>(selector)).find((element) => element.closest(tagName) === this);\n }\n\n private get dropZone(): HTMLElement {\n return this.own<HTMLElement>('[data-file-upload-drop-zone]') ?? this;\n }\n\n private get locale() {\n return this.closest('[lang]')?.getAttribute('lang') || 'en';\n }\n\n private message(name: keyof typeof defaults) {\n return translationFromAttributes(this, name) ?? defaults[name];\n }\n\n private setup() {\n const input = this.own<HTMLInputElement>('input[type=\"file\"]');\n const button = this.own<HTMLButtonElement>('button[data-file-upload-button]');\n if (!input || !button) return;\n // A new input, or a new id from React, rather than the one the element gave it.\n if (input.id !== `${this.fieldId}-input`) this.fieldId = input.id;\n if (!this.fieldId) return;\n if (input !== this.input || button !== this.button) {\n this.unbind();\n this.input = input;\n this.button = button;\n this.form = input.form;\n input.addEventListener('change', this.onChange);\n this.form?.addEventListener('reset', this.onReset);\n button.addEventListener('click', this.onClick);\n button.addEventListener('dragover', this.onDragOver);\n button.addEventListener('drop', this.onDrop);\n }\n this.upgrade(input, button);\n this.update();\n }\n\n private unbind() {\n this.input?.removeEventListener('change', this.onChange);\n this.form?.removeEventListener('reset', this.onReset);\n this.button?.removeEventListener('click', this.onClick);\n this.button?.removeEventListener('dragover', this.onDragOver);\n this.button?.removeEventListener('drop', this.onDrop);\n this.input = undefined;\n this.button = undefined;\n this.form = undefined;\n }\n\n /** Moves the id to the button, names it, and swaps it for the input, as govuk-frontend does. */\n private upgrade(input: HTMLInputElement, button: HTMLButtonElement) {\n const id = this.fieldId;\n const root = this.getRootNode();\n const label =\n root instanceof Document || root instanceof ShadowRoot\n ? root.querySelector(`label[for=\"${CSS.escape(id)}\"]`)\n : null;\n if (label && !label.id) label.id = `${id}-label`;\n setAttribute(input, 'id', `${id}-input`);\n setAttribute(button, 'id', id);\n const separator = button.querySelector('[data-file-upload-separator]');\n if (separator) setAttribute(separator, 'id', `${id}-comma`);\n if (label) {\n setAttribute(button, 'aria-labelledby', [label.id, separator?.id, id].filter(Boolean).join(' '));\n }\n setAttribute(button, 'aria-describedby', input.getAttribute('aria-describedby'));\n setAttribute(input, 'hidden', 'hidden');\n if (button.hidden) button.hidden = false;\n this.dataset.upgraded = '';\n }\n\n update() {\n const { input, button } = this;\n if (!input || !button) return;\n const { files } = input;\n const empty = !files?.length;\n const status = button.querySelector('[data-file-upload-status]');\n if (status) {\n setText(\n status,\n statusText(\n files ?? [],\n {\n 'no-file-chosen': this.message('no-file-chosen'),\n 'multiple-files-chosen': this.message('multiple-files-chosen'),\n },\n this.locale\n )\n );\n }\n // `toggle` with force leaves a class that's already there alone, so the observer isn't woken.\n setAttribute(button, 'data-empty', empty ? '' : null);\n classesOf(this.getAttribute('empty-class')).forEach((name) => button.classList.toggle(name, empty));\n if (button.disabled !== input.disabled) button.disabled = input.disabled;\n const zone = this.dropZone;\n setAttribute(zone, 'data-disabled', input.disabled ? '' : null);\n classesOf(this.getAttribute('disabled-class')).forEach((name) => zone.classList.toggle(name, input.disabled));\n if (input.disabled) this.setDragging(false);\n else this.renderDragging();\n }\n\n private setDragging(dragging: boolean) {\n this.dragging = dragging;\n this.renderDragging();\n }\n\n private renderDragging() {\n const { button, dragging } = this;\n if (!button) return;\n setAttribute(button, 'data-dragging', dragging ? '' : null);\n classesOf(this.getAttribute('dragging-class')).forEach((name) => button.classList.toggle(name, dragging));\n }\n\n private announce(name: 'entered-drop-zone' | 'left-drop-zone') {\n const announcements = this.own('[data-file-upload-announcements]');\n // Replaces the text node every time, so screen readers hear the same message again.\n if (announcements) announcements.textContent = translate(this.message(name), 0, this.locale);\n }\n\n private canDrop(dataTransfer: DataTransfer) {\n if (dataTransfer.items.length) return this.matchesCapacity(countFiles(dataTransfer.items));\n if (dataTransfer.types.length) return dataTransfer.types.includes('Files');\n return true;\n }\n\n private matchesCapacity(count: number) {\n return this.input?.multiple ? count > 0 : count === 1;\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactFileUpload);\n}\n"]}
@@ -0,0 +1,46 @@
1
+ import * as React from 'react';
2
+ import type { ComponentProps } from 'react';
3
+ import { type Translation } from '../utils/translation.js';
4
+ export type { Translation } from '../utils/translation.js';
5
+ export type RootProps = React.HTMLAttributes<HTMLElement> & {
6
+ /** Shown while no file is chosen. Defaults to "No file chosen". */
7
+ noFileChosen?: string;
8
+ /** Shown when more than one file is chosen. `%{count}` is the number. Defaults to "%{count} files chosen". */
9
+ multipleFilesChosen?: Translation;
10
+ /** Announced when the user drags files into the drop zone. Defaults to "Entered drop zone". */
11
+ enteredDropZone?: string;
12
+ /** Announced when the user drags files out of the drop zone. Defaults to "Left drop zone". */
13
+ leftDropZone?: string;
14
+ /** Classes for the button while no file is chosen. */
15
+ emptyClass?: string;
16
+ /** Classes for the button while files are dragged over the drop zone. */
17
+ draggingClass?: string;
18
+ /** Classes for the drop zone while the input is disabled. */
19
+ disabledClass?: string;
20
+ };
21
+ export declare function Root({ noFileChosen, multipleFilesChosen, enteredDropZone, leftDropZone, emptyClass, draggingClass, disabledClass, ...props }: RootProps): React.JSX.Element;
22
+ type PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {
23
+ /** Render the only child instead, with this part's props merged onto it. */
24
+ asChild?: boolean;
25
+ };
26
+ export type DropZoneProps = PartProps<'div'>;
27
+ /** The area that shows files are being dragged over it. Holds the button and the input. */
28
+ export declare function DropZone({ asChild, ...props }: DropZoneProps): React.JSX.Element;
29
+ export type InputProps = Omit<PartProps<'input'>, 'type'>;
30
+ /** The file input, with an `id` and a `label`. Shown without JavaScript, and hidden on upgrade. */
31
+ export declare function Input({ asChild, ...props }: InputProps): React.JSX.Element;
32
+ export type ButtonProps = PartProps<'button'>;
33
+ /**
34
+ * Opens the file picker, and takes dropped files. Hidden until the element upgrades, when it
35
+ * takes the input's id, so the input's label names it. Holds `Status`.
36
+ */
37
+ export declare function Button({ asChild, ...props }: ButtonProps): React.JSX.Element;
38
+ export type StatusProps = PartProps<'span'>;
39
+ /** Which files are chosen: "No file chosen", the file's name, or how many files. In `Button`. */
40
+ export declare function Status({ asChild, children, ...props }: StatusProps): React.JSX.Element;
41
+ export type SeparatorProps = PartProps<'span'>;
42
+ /** Between the label and the button's text in the button's accessible name. Defaults to ", ". In `Button`. */
43
+ export declare function Separator({ asChild, children, ...props }: SeparatorProps): React.JSX.Element;
44
+ export type AnnouncementsProps = PartProps<'span'>;
45
+ /** Announces entering and leaving the drop zone. Make it visually hidden. */
46
+ export declare function Announcements({ asChild, ...props }: AnnouncementsProps): React.JSX.Element;
@@ -0,0 +1,65 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { createContext, useContext, useState } from 'react';
4
+ import { Slot } from '../utils/slot.js';
5
+ import { translationAttributes } from '../utils/translation.js';
6
+ import { defaults, statusText, tagName } from './element.js';
7
+ // What the status starts with, so the element only changes text React rendered.
8
+ const InitialStatus = createContext(defaults['no-file-chosen']);
9
+ export function Root({ noFileChosen, multipleFilesChosen, enteredDropZone, leftDropZone, emptyClass, draggingClass, disabledClass, ...props }) {
10
+ // Rendered once and then left to the element, so a re-render never puts back a stale status.
11
+ const [initial] = useState(() => statusText([], { 'no-file-chosen': noFileChosen }, props.lang || 'en'));
12
+ return (React.createElement(InitialStatus.Provider, { value: initial }, React.createElement(tagName, {
13
+ ...props,
14
+ // The element owns `data-upgraded`.
15
+ suppressHydrationWarning: true,
16
+ 'no-file-chosen': noFileChosen,
17
+ ...translationAttributes('multiple-files-chosen', multipleFilesChosen),
18
+ 'entered-drop-zone': enteredDropZone,
19
+ 'left-drop-zone': leftDropZone,
20
+ 'empty-class': emptyClass,
21
+ 'dragging-class': draggingClass,
22
+ 'disabled-class': disabledClass,
23
+ })));
24
+ }
25
+ /** The area that shows files are being dragged over it. Holds the button and the input. */
26
+ export function DropZone({ asChild, ...props }) {
27
+ const Element = asChild ? Slot : 'div';
28
+ // The element owns its disabled classes after upgrade.
29
+ return React.createElement(Element, { "data-file-upload-drop-zone": "", suppressHydrationWarning: true, ...props });
30
+ }
31
+ /** The file input, with an `id` and a `label`. Shown without JavaScript, and hidden on upgrade. */
32
+ export function Input({ asChild, ...props }) {
33
+ const Element = asChild ? Slot : 'input';
34
+ // The element owns `id` and `hidden` after upgrade.
35
+ return React.createElement(Element, { type: "file", suppressHydrationWarning: true, ...props });
36
+ }
37
+ /**
38
+ * Opens the file picker, and takes dropped files. Hidden until the element upgrades, when it
39
+ * takes the input's id, so the input's label names it. Holds `Status`.
40
+ */
41
+ export function Button({ asChild, ...props }) {
42
+ const Element = asChild ? Slot : 'button';
43
+ // The element owns `id`, `hidden`, `disabled`, its ARIA and its classes after upgrade.
44
+ return React.createElement(Element, { "data-file-upload-button": "", type: "button", hidden: true, suppressHydrationWarning: true, ...props });
45
+ }
46
+ /** Which files are chosen: "No file chosen", the file's name, or how many files. In `Button`. */
47
+ export function Status({ asChild, children, ...props }) {
48
+ const initial = useContext(InitialStatus);
49
+ const Element = asChild ? Slot : 'span';
50
+ // The element owns its text after upgrade.
51
+ return (React.createElement(Element, { "data-file-upload-status": "", "aria-live": "polite", suppressHydrationWarning: true, ...props }, children !== null && children !== void 0 ? children : initial));
52
+ }
53
+ /** Between the label and the button's text in the button's accessible name. Defaults to ", ". In `Button`. */
54
+ export function Separator({ asChild, children = ', ', ...props }) {
55
+ const Element = asChild ? Slot : 'span';
56
+ // The element owns its `id` after upgrade.
57
+ return (React.createElement(Element, { "data-file-upload-separator": "", suppressHydrationWarning: true, ...props }, children));
58
+ }
59
+ /** Announces entering and leaving the drop zone. Make it visually hidden. */
60
+ export function Announcements({ asChild, ...props }) {
61
+ const Element = asChild ? Slot : 'span';
62
+ // The element owns its text after upgrade.
63
+ return React.createElement(Element, { "data-file-upload-announcements": "", "aria-live": "assertive", suppressHydrationWarning: true, ...props });
64
+ }
65
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/file-upload/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE5D,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,qBAAqB,EAAoB,MAAM,yBAAyB,CAAC;AAClF,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AA8B7D,gFAAgF;AAChF,MAAM,aAAa,GAAG,aAAa,CAAS,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAExE,MAAM,UAAU,IAAI,CAAC,EACnB,YAAY,EACZ,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,UAAU,EACV,aAAa,EACb,aAAa,EACb,GAAG,KAAK,EACE;IACV,6FAA6F;IA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AC;AACjH,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createContext, useContext, useState } from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { translationAttributes, type Translation } from '../utils/translation.js';\nimport { defaults, statusText, tagName } from './element.js';\n\nexport type { Translation } from '../utils/translation.js';\n\n/*\n * Headless file upload: unstyled parts over the <govuk-react-file-upload> element (./element.ts),\n * which shows the button on upgrade, hides the input and handles dropped files. Every part except\n * Root takes `asChild`.\n *\n * The input's files are the state, owned by the browser, so there is nothing to control. Without\n * JavaScript the native file input shows and the button stays hidden.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** Shown while no file is chosen. Defaults to \"No file chosen\". */\n noFileChosen?: string;\n /** Shown when more than one file is chosen. `%{count}` is the number. Defaults to \"%{count} files chosen\". */\n multipleFilesChosen?: Translation;\n /** Announced when the user drags files into the drop zone. Defaults to \"Entered drop zone\". */\n enteredDropZone?: string;\n /** Announced when the user drags files out of the drop zone. Defaults to \"Left drop zone\". */\n leftDropZone?: string;\n /** Classes for the button while no file is chosen. */\n emptyClass?: string;\n /** Classes for the button while files are dragged over the drop zone. */\n draggingClass?: string;\n /** Classes for the drop zone while the input is disabled. */\n disabledClass?: string;\n};\n\n// What the status starts with, so the element only changes text React rendered.\nconst InitialStatus = createContext<string>(defaults['no-file-chosen']);\n\nexport function Root({\n noFileChosen,\n multipleFilesChosen,\n enteredDropZone,\n leftDropZone,\n emptyClass,\n draggingClass,\n disabledClass,\n ...props\n}: RootProps) {\n // Rendered once and then left to the element, so a re-render never puts back a stale status.\n const [initial] = useState(() => statusText([], { 'no-file-chosen': noFileChosen }, props.lang || 'en'));\n return (\n <InitialStatus.Provider value={initial}>\n {React.createElement(tagName, {\n ...props,\n // The element owns `data-upgraded`.\n suppressHydrationWarning: true,\n 'no-file-chosen': noFileChosen,\n ...translationAttributes('multiple-files-chosen', multipleFilesChosen),\n 'entered-drop-zone': enteredDropZone,\n 'left-drop-zone': leftDropZone,\n 'empty-class': emptyClass,\n 'dragging-class': draggingClass,\n 'disabled-class': disabledClass,\n })}\n </InitialStatus.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\nexport type DropZoneProps = PartProps<'div'>;\n\n/** The area that shows files are being dragged over it. Holds the button and the input. */\nexport function DropZone({ asChild, ...props }: DropZoneProps) {\n const Element = asChild ? Slot : 'div';\n // The element owns its disabled classes after upgrade.\n return <Element data-file-upload-drop-zone=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type InputProps = Omit<PartProps<'input'>, 'type'>;\n\n/** The file input, with an `id` and a `label`. Shown without JavaScript, and hidden on upgrade. */\nexport function Input({ asChild, ...props }: InputProps) {\n const Element = asChild ? Slot : 'input';\n // The element owns `id` and `hidden` after upgrade.\n return <Element type=\"file\" suppressHydrationWarning {...props} />;\n}\n\nexport type ButtonProps = PartProps<'button'>;\n\n/**\n * Opens the file picker, and takes dropped files. Hidden until the element upgrades, when it\n * takes the input's id, so the input's label names it. Holds `Status`.\n */\nexport function Button({ asChild, ...props }: ButtonProps) {\n const Element = asChild ? Slot : 'button';\n // The element owns `id`, `hidden`, `disabled`, its ARIA and its classes after upgrade.\n return <Element data-file-upload-button=\"\" type=\"button\" hidden suppressHydrationWarning {...props} />;\n}\n\nexport type StatusProps = PartProps<'span'>;\n\n/** Which files are chosen: \"No file chosen\", the file's name, or how many files. In `Button`. */\nexport function Status({ asChild, children, ...props }: StatusProps) {\n const initial = useContext(InitialStatus);\n const Element = asChild ? Slot : 'span';\n // The element owns its text after upgrade.\n return (\n <Element data-file-upload-status=\"\" aria-live=\"polite\" suppressHydrationWarning {...props}>\n {children ?? initial}\n </Element>\n );\n}\n\nexport type SeparatorProps = PartProps<'span'>;\n\n/** Between the label and the button's text in the button's accessible name. Defaults to \", \". In `Button`. */\nexport function Separator({ asChild, children = ', ', ...props }: SeparatorProps) {\n const Element = asChild ? Slot : 'span';\n // The element owns its `id` after upgrade.\n return (\n <Element data-file-upload-separator=\"\" suppressHydrationWarning {...props}>\n {children}\n </Element>\n );\n}\n\nexport type AnnouncementsProps = PartProps<'span'>;\n\n/** Announces entering and leaving the drop zone. Make it visually hidden. */\nexport function Announcements({ asChild, ...props }: AnnouncementsProps) {\n const Element = asChild ? Slot : 'span';\n // The element owns its text after upgrade.\n return <Element data-file-upload-announcements=\"\" aria-live=\"assertive\" suppressHydrationWarning {...props} />;\n}\n"]}
@@ -0,0 +1 @@
1
+ export declare const tagName = "govuk-react-focus-links";
@@ -0,0 +1,90 @@
1
+ /*
2
+ * <govuk-react-focus-links>: a headless, light-DOM custom element that makes links to a part of the
3
+ * same page move focus there, with or without React, as govuk-frontend's error summary and skip
4
+ * link do.
5
+ *
6
+ * Without JavaScript the browser goes to the target as usual. With it:
7
+ * - A link to a form field focuses the field and scrolls its label into view, or its fieldset's
8
+ * legend for a radio, a checkbox, or a field near the legend, so users see the question.
9
+ * - A link to anything else, such as the main content, focuses the target, giving it
10
+ * `tabindex="-1"` and `focused-class` until it loses focus.
11
+ *
12
+ * Attributes:
13
+ * - `focused-class`: classes for a target that isn't a form field while it has focus.
14
+ */
15
+ import { setFocus } from '../utils/set-focus.js';
16
+ import { classesOf } from '../utils/dom.js';
17
+ export const tagName = 'govuk-react-focus-links';
18
+ const isField = (element) => element instanceof HTMLInputElement ||
19
+ element instanceof HTMLSelectElement ||
20
+ element instanceof HTMLTextAreaElement ||
21
+ element instanceof HTMLButtonElement;
22
+ /** As govuk-frontend: the legend users need to see to understand the field, or else its label. */
23
+ function legendOrLabel(field) {
24
+ var _a, _b;
25
+ const legend = (_a = field.closest('fieldset')) === null || _a === void 0 ? void 0 : _a.getElementsByTagName('legend')[0];
26
+ if (legend) {
27
+ if (field instanceof HTMLInputElement && (field.type === 'checkbox' || field.type === 'radio'))
28
+ return legend;
29
+ const legendTop = legend.getBoundingClientRect().top;
30
+ const fieldRect = field.getBoundingClientRect();
31
+ if (fieldRect.height &&
32
+ window.innerHeight &&
33
+ fieldRect.top + fieldRect.height - legendTop < window.innerHeight / 2) {
34
+ return legend;
35
+ }
36
+ }
37
+ return (_b = document.querySelector(`label[for="${CSS.escape(field.id)}"]`)) !== null && _b !== void 0 ? _b : field.closest('label');
38
+ }
39
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
40
+ class GovukReactFocusLinks extends HTMLElement {
41
+ constructor() {
42
+ super(...arguments);
43
+ this.onClick = (event) => {
44
+ var _a;
45
+ if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey)
46
+ return;
47
+ const link = event.target instanceof Element ? event.target.closest('a[href]') : null;
48
+ if (!(link instanceof HTMLAnchorElement) || !this.contains(link))
49
+ return;
50
+ // A bare `#id` is this page whatever the `<base>`, as govuk-frontend's error summary treats it.
51
+ const samePage = ((_a = link.getAttribute('href')) === null || _a === void 0 ? void 0 : _a.startsWith('#')) ||
52
+ (link.origin === window.location.origin &&
53
+ link.pathname === window.location.pathname &&
54
+ link.search === window.location.search);
55
+ if (!samePage)
56
+ return;
57
+ let id = link.hash.slice(1);
58
+ try {
59
+ id = decodeURIComponent(id);
60
+ }
61
+ catch (_b) {
62
+ // A stray `%`: use the id as written.
63
+ }
64
+ const target = id ? document.getElementById(id) : null;
65
+ if (!target)
66
+ return;
67
+ if (isField(target)) {
68
+ const label = legendOrLabel(target);
69
+ if (!label)
70
+ return;
71
+ // Scroll to the question, not the field, which can hide it.
72
+ event.preventDefault();
73
+ label.scrollIntoView();
74
+ target.focus({ preventScroll: true });
75
+ return;
76
+ }
77
+ setFocus(target, { focusedClasses: classesOf(this.getAttribute('focused-class')) });
78
+ };
79
+ }
80
+ connectedCallback() {
81
+ this.addEventListener('click', this.onClick);
82
+ }
83
+ disconnectedCallback() {
84
+ this.removeEventListener('click', this.onClick);
85
+ }
86
+ }
87
+ if (!customElements.get(tagName))
88
+ customElements.define(tagName, GovukReactFocusLinks);
89
+ }
90
+ //# sourceMappingURL=element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/focus-links/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,CAAC;AAEjD,MAAM,OAAO,GAAG,CAAC,OAAgB,EAAE,EAAE,CACnC,OAAO,YAAY,gBAAgB;IACnC,OAAO,YAAY,iBAAiB;IACpC,OAAO,YAAY,mBAAmB;IACtC,OAAO,YAAY,iBAAiB,CAAC;AAEvC,kGAAkG;AAClG,SAAS,aAAa,CAAC,KAAkB;;IACvC,MAAM,MAAM,GAAG,MAAA,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,0CAAE,oBAAoB,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;IAC5E,IAAI,MAAM,EAAE,CAAC;QACX,IAAI,KAAK,YAAY,gBAAgB,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,UAAU,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC;YAAE,OAAO,MAAM,CAAC;QAC9G,MAAM,SAAS,GAAG,MAAM,CAAC,qBAAqB,EAAE,CAAC,GAAG,CAAC;QACrD,MAAM,SAAS,GAAG,KAAK,CAAC,qBAAqB,EAAE,CAAC;QAChD,IACE,SAAS,CAAC,MAAM;YAChB,MAAM,CAAC,WAAW;YAClB,SAAS,CAAC,GAAG,GAAG,SAAS,CAAC,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC,WAAW,GAAG,CAAC,EACrE,CAAC;YACD,OAAO,MAAM,CAAC;QAChB,CAAC;IACH,CAAC;IACD,OAAO,MAAA,QAAQ,CAAC,aAAa,CAAC,cAAc,GAAG,CAAC,MAAM,CAAC,KAAK,CAA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;oBACP,sCAAsC;gBACxC,CAAC;gBACD,MAAM,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;gBACvD,IAAI,CAAC,MAAM;oBAAE,OAAO;gBACpB,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;oBACpB,MAAM,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;oBACpC,IAAI,CAAC,KAAK;wBAAE,OAAO;oBACnB,4DAA4D;oBAC5D,KAAK,CAAC,cAAc,EAAE,CAAC;oBACvB,KAAK,CAAC,cAAc,EAAE,CAAC;oBACvB,MAAM,CAAC,KAAK,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;oBACtC,OAAO;gBACT,CAAC;gBACD,QAAQ,CAAC,MAAM,EAAE,EAAE,cAAc,EAAE,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC;YACtF,CAAC,CAAC;QASJ,CAAC;QAPC,iBAAiB;YACf,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC/C,CAAC;QAED,oBAAoB;YAClB,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAClD,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;AACzF,CAAC","sourcesContent":["/*\n * <govuk-react-focus-links>: a headless, light-DOM custom element that makes links to a part of the\n * same page move focus there, with or without React, as govuk-frontend's error summary and skip\n * link do.\n *\n * Without JavaScript the browser goes to the target as usual. With it:\n * - A link to a form field focuses the field and scrolls its label into view, or its fieldset's\n * legend for a radio, a checkbox, or a field near the legend, so users see the question.\n * - A link to anything else, such as the main content, focuses the target, giving it\n * `tabindex=\"-1\"` and `focused-class` until it loses focus.\n *\n * Attributes:\n * - `focused-class`: classes for a target that isn't a form field while it has focus.\n */\n\nimport { setFocus } from '../utils/set-focus.js';\nimport { classesOf } from '../utils/dom.js';\n\nexport const tagName = 'govuk-react-focus-links';\n\nconst isField = (element: Element) =>\n element instanceof HTMLInputElement ||\n element instanceof HTMLSelectElement ||\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLButtonElement;\n\n/** As govuk-frontend: the legend users need to see to understand the field, or else its label. */\nfunction legendOrLabel(field: HTMLElement) {\n const legend = field.closest('fieldset')?.getElementsByTagName('legend')[0];\n if (legend) {\n if (field instanceof HTMLInputElement && (field.type === 'checkbox' || field.type === 'radio')) return legend;\n const legendTop = legend.getBoundingClientRect().top;\n const fieldRect = field.getBoundingClientRect();\n if (\n fieldRect.height &&\n window.innerHeight &&\n fieldRect.top + fieldRect.height - legendTop < window.innerHeight / 2\n ) {\n return legend;\n }\n }\n return document.querySelector(`label[for=\"${CSS.escape(field.id)}\"]`) ?? field.closest('label');\n}\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactFocusLinks extends HTMLElement {\n private readonly onClick = (event: MouseEvent) => {\n if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey) return;\n const link = event.target instanceof Element ? event.target.closest('a[href]') : null;\n if (!(link instanceof HTMLAnchorElement) || !this.contains(link)) return;\n // A bare `#id` is this page whatever the `<base>`, as govuk-frontend's error summary treats it.\n const samePage =\n link.getAttribute('href')?.startsWith('#') ||\n (link.origin === window.location.origin &&\n link.pathname === window.location.pathname &&\n link.search === window.location.search);\n if (!samePage) return;\n let id = link.hash.slice(1);\n try {\n id = decodeURIComponent(id);\n } catch {\n // A stray `%`: use the id as written.\n }\n const target = id ? document.getElementById(id) : null;\n if (!target) return;\n if (isField(target)) {\n const label = legendOrLabel(target);\n if (!label) return;\n // Scroll to the question, not the field, which can hide it.\n event.preventDefault();\n label.scrollIntoView();\n target.focus({ preventScroll: true });\n return;\n }\n setFocus(target, { focusedClasses: classesOf(this.getAttribute('focused-class')) });\n };\n\n connectedCallback() {\n this.addEventListener('click', this.onClick);\n }\n\n disconnectedCallback() {\n this.removeEventListener('click', this.onClick);\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactFocusLinks);\n}\n"]}