@govuk-react/primitives 0.2.1 → 0.3.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 +100 -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 +7 -7
- package/dist/character-count/index.js +13 -13
- 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/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/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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/disclosure/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/disclosure/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEtD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAsB,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAA0B,MAAM,cAAc,CAAC;AAsB/D,MAAM,YAAY,GAAG,CAAC,OAAoB,EAAgC,EAAE,CAAC,YAAY,IAAI,OAAO,CAAC;AAErG,MAAM,UAAU,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,KAAK,EAAa;IAClF,MAAM,OAAO,GAAG,MAAM,CAAc,IAAI,CAAC,CAAC;IAC1C,MAAM,eAAe,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAC7C,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;IAE/D,SAAS,CAAC,GAAG,EAAE;QACb,eAAe,CAAC,OAAO,GAAG,YAAY,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC;QAC7B,IAAI,CAAC,IAAI;YAAE,OAAO,SAAS,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M,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE;QAClC,GAAG,KAAK;QACR,GAAG,EAAE,OAAO;QACZ,oCAAoC;QACpC,wBAAwB,EAAE,IAAI;QAC9B,KAAK;QACL,cAAc,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS;KAC7C,CAAC,CAAC;AACL,CAAC;AASD,mEAAmE;AACnE,MAAM,UAAU,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAgB;IACzD,8EAA8E;IAC9E,MAAM,MAAM,GAAG,EAAE,yBAAyB,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,wBAAwB,EAAE,IAAI,EAAE,CAAC;IAC/F,IAAI,OAAO;QAAE,OAAO,oBAAC,IAAI,OAAK,MAAM,KAAM,KAAK,GAAI,CAAC;IACpD,OAAO,gCAAQ,IAAI,EAAC,QAAQ,KAAK,MAAM,KAAM,KAAK,GAAI,CAAC;AACzD,CAAC;AAID,8FAA8F;AAC9F,MAAM,UAAU,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAgB;IACzD,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACvC,2CAA2C;IAC3C,OAAO,oBAAC,OAAO,+BAAyB,EAAE,EAAC,wBAAwB,WAAK,KAAK,GAAI,CAAC;AACpF,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useEffect, useReducer, useRef } from 'react';\nimport type { ComponentProps } from 'react';\nimport { early, live, type ChangeDetails } from '../utils/change-details.js';\nimport { Slot } from '../utils/slot.js';\nimport { tagName, type DisclosureElement } from './element.js';\n\nexport type { ChangeDetails };\n\n/*\n * Headless disclosure: unstyled parts over the <govuk-react-disclosure> element (./element.ts),\n * which adds the show and hide button on upgrade. Every part except Root takes `asChild`.\n *\n * Without JavaScript the content is shown and the button hidden.\n */\n\nexport type RootProps = Omit<React.HTMLAttributes<HTMLElement>, 'onChange'> & {\n /** While this media query matches, the content is always shown and the button hidden. */\n media?: string;\n /** Whether the content is shown, to control it once hydrated. */\n open?: boolean;\n /** Whether the content starts shown once the element upgrades. */\n defaultOpen?: boolean;\n /** Called when the user shows or hides the content. */\n onOpenChange?: (open: boolean, details: ChangeDetails) => void;\n};\n\nconst isDisclosure = (element: HTMLElement): element is DisclosureElement => 'pickedOpen' in element;\n\nexport function Root({ media, open, defaultOpen, onOpenChange, ...props }: RootProps) {\n const element = useRef<HTMLElement>(null);\n const onOpenChangeRef = useRef(onOpenChange);\n const synced = useRef(false);\n const [, render] = useReducer((count: number) => count + 1, 0);\n\n useEffect(() => {\n onOpenChangeRef.current = onOpenChange;\n });\n\n useEffect(() => {\n const node = element.current;\n if (!node) return undefined;\n // A nested disclosure's change bubbles through this one.\n const onChange = (event: CustomEvent<{ open: boolean }>) => {\n if (event.target === node) onOpenChangeRef.current?.(event.detail.open, live);\n };\n node.addEventListener('govuk-react-disclosure:change', onChange);\n return () => node.removeEventListener('govuk-react-disclosure:change', onChange);\n }, []);\n\n // `open` is set once hydrated, so the server markup is uncontrolled and a user's click before\n // hydration still works. That click is then reported, rather than undone.\n useEffect(() => {\n const node = element.current;\n if (!node) return;\n if (!synced.current) {\n synced.current = true;\n const picked = isDisclosure(node) ? node.pickedOpen : undefined;\n if (picked !== undefined && picked !== (open ?? defaultOpen ?? false) && onOpenChangeRef.current) {\n onOpenChangeRef.current(picked, early);\n if (open !== undefined) {\n // The click shows until the next render, which adopts it or goes back to `open`.\n render();\n return;\n }\n }\n }\n if (open === undefined) node.removeAttribute('open');\n else node.setAttribute('open', String(open));\n });\n\n return React.createElement(tagName, {\n ...props,\n ref: element,\n // The element owns `data-upgraded`.\n suppressHydrationWarning: true,\n media,\n 'default-open': defaultOpen ? '' : undefined,\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 TriggerProps = PartProps<'button'>;\n\n/** The show and hide button. Hidden until the element upgrades. */\nexport function Trigger({ asChild, ...props }: TriggerProps) {\n // The element owns `hidden`, `aria-hidden` and `aria-expanded` after upgrade.\n const shared = { 'data-disclosure-trigger': '', hidden: true, suppressHydrationWarning: true };\n if (asChild) return <Slot {...shared} {...props} />;\n return <button type=\"button\" {...shared} {...props} />;\n}\n\nexport type ContentProps = PartProps<'div'>;\n\n/** The content the button shows and hides. Give it an id for the button's `aria-controls`. */\nexport function Content({ asChild, ...props }: ContentProps) {\n const Element = asChild ? Slot : 'div';\n // The element owns `hidden` after upgrade.\n return <Element data-disclosure-content=\"\" suppressHydrationWarning {...props} />;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const tagName = "govuk-react-exit-this-page";
|
|
2
|
-
export declare const messageNames: readonly ["activated", "timed-out", "press-two-more-times", "press-one-more-time"];
|
|
2
|
+
export declare const messageNames: readonly ["activated-announcement", "timed-out-announcement", "press-two-more-times-announcement", "press-one-more-time-announcement"];
|
|
3
3
|
export type MessageName = (typeof messageNames)[number];
|
|
4
4
|
/** govuk-frontend's English messages. */
|
|
5
5
|
export declare const defaultMessages: Record<MessageName, string>;
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* and goes to the button's `href`. A status tells screen readers how many more presses are needed.
|
|
8
8
|
*
|
|
9
9
|
* Markup inside the element:
|
|
10
|
-
* - `[data-exit-this-page-
|
|
10
|
+
* - `[data-exit-this-page-trigger]`: the link to the safe page.
|
|
11
11
|
* - `[data-exit-this-page-indicator]`: shows how many times Shift has been pressed, with
|
|
12
12
|
* `[data-exit-this-page-light]` children. It gets `data-visible` while counting, and each light
|
|
13
13
|
* `data-on` once pressed.
|
|
@@ -20,22 +20,27 @@
|
|
|
20
20
|
* event. Cancel it to stay on the page.
|
|
21
21
|
*
|
|
22
22
|
* Attributes:
|
|
23
|
-
* - `activated`, `timed-out`, `press-two-more-times`, `press-one-more-time`: the messages,
|
|
23
|
+
* - `activated-announcement`, `timed-out-announcement`, `press-two-more-times-announcement`, `press-one-more-time-announcement`: the messages,
|
|
24
24
|
* English by default.
|
|
25
25
|
* - `indicator-visible-class`, `light-on-class`: classes for the indicator and its lights.
|
|
26
26
|
* - `hide-content-class`: classes for `body` that hide the page behind the overlay.
|
|
27
27
|
* - `overlay-class`: classes for the overlay. Without them, the overlay covers the page with
|
|
28
28
|
* inline styles.
|
|
29
29
|
*/
|
|
30
|
-
import { classesOf } from '../utils/dom.js';
|
|
30
|
+
import { classesOf, setText } from '../utils/dom.js';
|
|
31
31
|
export const tagName = 'govuk-react-exit-this-page';
|
|
32
|
-
export const messageNames = [
|
|
32
|
+
export const messageNames = [
|
|
33
|
+
'activated-announcement',
|
|
34
|
+
'timed-out-announcement',
|
|
35
|
+
'press-two-more-times-announcement',
|
|
36
|
+
'press-one-more-time-announcement',
|
|
37
|
+
];
|
|
33
38
|
/** govuk-frontend's English messages. */
|
|
34
39
|
export const defaultMessages = {
|
|
35
|
-
activated: 'Loading.',
|
|
36
|
-
'timed-out': 'Exit this page expired.',
|
|
37
|
-
'press-two-more-times': 'Shift, press 2 more times to exit.',
|
|
38
|
-
'press-one-more-time': 'Shift, press 1 more time to exit.',
|
|
40
|
+
'activated-announcement': 'Loading.',
|
|
41
|
+
'timed-out-announcement': 'Exit this page expired.',
|
|
42
|
+
'press-two-more-times-announcement': 'Shift, press 2 more times to exit.',
|
|
43
|
+
'press-one-more-time-announcement': 'Shift, press 1 more time to exit.',
|
|
39
44
|
};
|
|
40
45
|
const timeoutMs = 5000;
|
|
41
46
|
const overlayStyles = {
|
|
@@ -56,7 +61,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
56
61
|
const link = event.target instanceof Element ? event.target.closest('a') : null;
|
|
57
62
|
if (!link)
|
|
58
63
|
return;
|
|
59
|
-
const own = link.matches('[data-exit-this-page-
|
|
64
|
+
const own = link.matches('[data-exit-this-page-trigger]') && link.closest(tagName) === this;
|
|
60
65
|
const skipLink = link.matches('[data-exit-this-page-skiplink]') && this.listening;
|
|
61
66
|
if (!own && !skipLink)
|
|
62
67
|
return;
|
|
@@ -77,7 +82,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
77
82
|
this.exit();
|
|
78
83
|
}
|
|
79
84
|
else {
|
|
80
|
-
this.setStatus(this.message(this.presses === 1 ? 'press-two-more-times' : 'press-one-more-time'));
|
|
85
|
+
this.setStatus(this.message(this.presses === 1 ? 'press-two-more-times-announcement' : 'press-one-more-time-announcement'));
|
|
81
86
|
window.clearTimeout(this.pressTimeout);
|
|
82
87
|
this.pressTimeout = window.setTimeout(this.timeOut, timeoutMs);
|
|
83
88
|
}
|
|
@@ -91,7 +96,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
91
96
|
window.clearTimeout(this.pressTimeout);
|
|
92
97
|
this.pressTimeout = undefined;
|
|
93
98
|
this.presses = 0;
|
|
94
|
-
this.setStatus(this.message('timed-out'));
|
|
99
|
+
this.setStatus(this.message('timed-out-announcement'));
|
|
95
100
|
this.messageTimeout = window.setTimeout(() => this.setStatus(''), timeoutMs);
|
|
96
101
|
this.updateIndicator();
|
|
97
102
|
};
|
|
@@ -128,7 +133,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
128
133
|
}
|
|
129
134
|
exit() {
|
|
130
135
|
var _a;
|
|
131
|
-
const button = this.own('[data-exit-this-page-
|
|
136
|
+
const button = this.own('[data-exit-this-page-trigger]');
|
|
132
137
|
const href = button instanceof HTMLAnchorElement ? button.href : undefined;
|
|
133
138
|
const proceed = href !== undefined &&
|
|
134
139
|
this.dispatchEvent(new CustomEvent('govuk-react-exit-this-page:exit', { detail: { href }, bubbles: true, cancelable: true }));
|
|
@@ -149,7 +154,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
149
154
|
else
|
|
150
155
|
Object.entries(overlayStyles).forEach(([name, value]) => overlay.style.setProperty(name, value));
|
|
151
156
|
document.body.appendChild(overlay);
|
|
152
|
-
overlay.textContent = this.message('activated');
|
|
157
|
+
overlay.textContent = this.message('activated-announcement');
|
|
153
158
|
this.overlay = overlay;
|
|
154
159
|
window.location.href = href;
|
|
155
160
|
}
|
|
@@ -164,16 +169,10 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
164
169
|
var _a;
|
|
165
170
|
return (_a = this.getAttribute(name)) !== null && _a !== void 0 ? _a : defaultMessages[name];
|
|
166
171
|
}
|
|
167
|
-
// Keeps the text node React rendered, if any, so React can still update it.
|
|
168
172
|
setStatus(text) {
|
|
169
173
|
const status = this.own('[data-exit-this-page-status]');
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
const { firstChild } = status;
|
|
173
|
-
if (firstChild instanceof Text && status.childNodes.length === 1)
|
|
174
|
-
firstChild.nodeValue = text;
|
|
175
|
-
else
|
|
176
|
-
status.textContent = text;
|
|
174
|
+
if (status)
|
|
175
|
+
setText(status, text);
|
|
177
176
|
}
|
|
178
177
|
clearTimers() {
|
|
179
178
|
window.clearTimeout(this.pressTimeout);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/exit-this-page/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,CAAC,MAAM,OAAO,GAAG,4BAA4B,CAAC;AAEpD,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,WAAW,EAAE,WAAW,EAAE,sBAAsB,EAAE,qBAAqB,CAAU,CAAC;AAI/G,yCAAyC;AACzC,MAAM,CAAC,MAAM,eAAe,GAAgC;IAC1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GAAG,IAAI,CAAC,OAAO,CAAC;gBAChC,KAAK,CAAC,eAAe,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;gBACrC,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;YACrG,CAAC,CAAC,CAAC;QACL,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC;AAC3F,CAAC","sourcesContent":["/*\n * <govuk-react-exit-this-page>: a headless, light-DOM custom element that lets users leave a page\n * quickly, with or without React, as govuk-frontend's exit this page does.\n *\n * Without JavaScript the button is a link to a safe page. On upgrade, the button, a skip link, or\n * pressing Shift 3 times within 5 seconds hides the page, shows an overlay that says it's loading,\n * and goes to the button's `href`. A status tells screen readers how many more presses are needed.\n *\n * Markup inside the element:\n * - `[data-exit-this-page-button]`: the link to the safe page.\n * - `[data-exit-this-page-indicator]`: shows how many times Shift has been pressed, with\n * `[data-exit-this-page-light]` children. It gets `data-visible` while counting, and each light\n * `data-on` once pressed.\n * - `[data-exit-this-page-status]`: an empty status for screen readers. Make it visually hidden.\n *\n * Anywhere on the page, a link with `data-exit-this-page-skiplink` exits too. Only the page's\n * first element listens for Shift and the skip link.\n *\n * Before leaving, the element fires a cancelable, bubbling `govuk-react-exit-this-page:exit`\n * event. Cancel it to stay on the page.\n *\n * Attributes:\n * - `activated`, `timed-out`, `press-two-more-times`, `press-one-more-time`: the messages,\n * English by default.\n * - `indicator-visible-class`, `light-on-class`: classes for the indicator and its lights.\n * - `hide-content-class`: classes for `body` that hide the page behind the overlay.\n * - `overlay-class`: classes for the overlay. Without them, the overlay covers the page with\n * inline styles.\n */\n\nimport { classesOf } from '../utils/dom.js';\n\nexport const tagName = 'govuk-react-exit-this-page';\n\nexport const messageNames = ['activated', 'timed-out', 'press-two-more-times', 'press-one-more-time'] as const;\n\nexport type MessageName = (typeof messageNames)[number];\n\n/** govuk-frontend's English messages. */\nexport const defaultMessages: Record<MessageName, string> = {\n activated: 'Loading.',\n 'timed-out': 'Exit this page expired.',\n 'press-two-more-times': 'Shift, press 2 more times to exit.',\n 'press-one-more-time': 'Shift, press 1 more time to exit.',\n};\n\nexport interface ExitThisPageElement extends HTMLElement {\n /** Hides the page and goes to the button's `href`, unless the exit event is cancelled. */\n exit(): void;\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'govuk-react-exit-this-page': ExitThisPageElement;\n }\n interface HTMLElementEventMap {\n 'govuk-react-exit-this-page:exit': CustomEvent<{ href: string }>;\n }\n}\n\nconst timeoutMs = 5000;\n\nconst overlayStyles: Record<string, string> = {\n position: 'fixed',\n inset: '0',\n 'z-index': '2147483647',\n background: 'white',\n};\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactExitThisPage extends HTMLElement implements ExitThisPageElement {\n private presses = 0;\n private lastKeyWasModified = false;\n private pressTimeout?: number;\n private messageTimeout?: number;\n private overlay?: HTMLElement;\n\n connectedCallback() {\n this.dataset.upgraded = '';\n document.addEventListener('keyup', this.onKeyup, true);\n document.addEventListener('click', this.onClick);\n window.addEventListener('pageshow', this.reset);\n }\n\n disconnectedCallback() {\n document.removeEventListener('keyup', this.onKeyup, true);\n document.removeEventListener('click', this.onClick);\n window.removeEventListener('pageshow', this.reset);\n this.clearTimers();\n // Moved elements start counting again.\n this.presses = 0;\n this.setStatus('');\n this.updateIndicator();\n }\n\n exit() {\n const button = this.own('[data-exit-this-page-button]');\n const href = button instanceof HTMLAnchorElement ? button.href : undefined;\n const proceed =\n href !== undefined &&\n this.dispatchEvent(\n new CustomEvent('govuk-react-exit-this-page:exit', { detail: { href }, bubbles: true, cancelable: true })\n );\n if (!href || !proceed) {\n // Staying on the page: clear the presses.\n this.setStatus('');\n this.updateIndicator();\n return;\n }\n this.setStatus('');\n classesOf(this.getAttribute('hide-content-class')).forEach((name) => document.body.classList.add(name));\n this.overlay?.remove();\n const overlay = document.createElement('div');\n overlay.setAttribute('role', 'alert');\n const overlayClasses = classesOf(this.getAttribute('overlay-class'));\n if (overlayClasses.length) overlay.classList.add(...overlayClasses);\n else Object.entries(overlayStyles).forEach(([name, value]) => overlay.style.setProperty(name, value));\n document.body.appendChild(overlay);\n overlay.textContent = this.message('activated');\n this.overlay = overlay;\n window.location.href = href;\n }\n\n /** Only the page's first element listens to the page, as govuk-frontend's first instance does. */\n private get listening() {\n return document.querySelector(tagName) === this;\n }\n\n private own(selector: string) {\n return Array.from(this.querySelectorAll<HTMLElement>(selector)).find(\n (element) => element.closest(tagName) === this\n );\n }\n\n private message(name: MessageName) {\n return this.getAttribute(name) ?? defaultMessages[name];\n }\n\n // Keeps the text node React rendered, if any, so React can still update it.\n private setStatus(text: string) {\n const status = this.own('[data-exit-this-page-status]');\n if (!status || status.textContent === text) return;\n const { firstChild } = status;\n if (firstChild instanceof Text && status.childNodes.length === 1) firstChild.nodeValue = text;\n else status.textContent = text;\n }\n\n private readonly onClick = (event: MouseEvent) => {\n if (event.defaultPrevented || event.button !== 0) return;\n const link = event.target instanceof Element ? event.target.closest('a') : null;\n if (!link) return;\n const own = link.matches('[data-exit-this-page-button]') && link.closest(tagName) === this;\n const skipLink = link.matches('[data-exit-this-page-skiplink]') && this.listening;\n if (!own && !skipLink) return;\n event.preventDefault();\n this.exit();\n };\n\n private readonly onKeyup = (event: KeyboardEvent) => {\n if (!this.listening) return;\n if (event.key === 'Shift' && !this.lastKeyWasModified) {\n this.presses += 1;\n this.updateIndicator();\n window.clearTimeout(this.messageTimeout);\n if (this.presses >= 3) {\n this.presses = 0;\n window.clearTimeout(this.pressTimeout);\n this.pressTimeout = undefined;\n this.exit();\n } else {\n this.setStatus(this.message(this.presses === 1 ? 'press-two-more-times' : 'press-one-more-time'));\n window.clearTimeout(this.pressTimeout);\n this.pressTimeout = window.setTimeout(this.timeOut, timeoutMs);\n }\n } else if (this.pressTimeout !== undefined) {\n this.timeOut();\n }\n this.lastKeyWasModified = event.shiftKey;\n };\n\n private readonly timeOut = () => {\n window.clearTimeout(this.pressTimeout);\n this.pressTimeout = undefined;\n this.presses = 0;\n this.setStatus(this.message('timed-out'));\n this.messageTimeout = window.setTimeout(() => this.setStatus(''), timeoutMs);\n this.updateIndicator();\n };\n\n /** Shows the page again when the browser brings it back from its cache. */\n private readonly reset = (event: PageTransitionEvent) => {\n // `pageshow` fires on every load too, which mustn't lose presses made before it.\n if (!event.persisted) return;\n classesOf(this.getAttribute('hide-content-class')).forEach((name) => document.body.classList.remove(name));\n this.overlay?.remove();\n this.overlay = undefined;\n this.presses = 0;\n this.setStatus('');\n this.updateIndicator();\n this.clearTimers();\n };\n\n private clearTimers() {\n window.clearTimeout(this.pressTimeout);\n window.clearTimeout(this.messageTimeout);\n this.pressTimeout = undefined;\n this.messageTimeout = undefined;\n }\n\n private updateIndicator() {\n const indicator = this.own('[data-exit-this-page-indicator]');\n if (!indicator) return;\n const visible = this.presses > 0;\n indicator.toggleAttribute('data-visible', visible);\n classesOf(this.getAttribute('indicator-visible-class')).forEach((name) =>\n indicator.classList.toggle(name, visible)\n );\n indicator.querySelectorAll('[data-exit-this-page-light]').forEach((light, index) => {\n const on = index < this.presses;\n light.toggleAttribute('data-on', on);\n classesOf(this.getAttribute('light-on-class')).forEach((name) => light.classList.toggle(name, on));\n });\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactExitThisPage);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/exit-this-page/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD,MAAM,CAAC,MAAM,OAAO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K,CAAC,eAAe,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;gBACrC,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;YACrG,CAAC,CAAC,CAAC;QACL,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC;AAC3F,CAAC","sourcesContent":["/*\n * <govuk-react-exit-this-page>: a headless, light-DOM custom element that lets users leave a page\n * quickly, with or without React, as govuk-frontend's exit this page does.\n *\n * Without JavaScript the button is a link to a safe page. On upgrade, the button, a skip link, or\n * pressing Shift 3 times within 5 seconds hides the page, shows an overlay that says it's loading,\n * and goes to the button's `href`. A status tells screen readers how many more presses are needed.\n *\n * Markup inside the element:\n * - `[data-exit-this-page-trigger]`: the link to the safe page.\n * - `[data-exit-this-page-indicator]`: shows how many times Shift has been pressed, with\n * `[data-exit-this-page-light]` children. It gets `data-visible` while counting, and each light\n * `data-on` once pressed.\n * - `[data-exit-this-page-status]`: an empty status for screen readers. Make it visually hidden.\n *\n * Anywhere on the page, a link with `data-exit-this-page-skiplink` exits too. Only the page's\n * first element listens for Shift and the skip link.\n *\n * Before leaving, the element fires a cancelable, bubbling `govuk-react-exit-this-page:exit`\n * event. Cancel it to stay on the page.\n *\n * Attributes:\n * - `activated-announcement`, `timed-out-announcement`, `press-two-more-times-announcement`, `press-one-more-time-announcement`: the messages,\n * English by default.\n * - `indicator-visible-class`, `light-on-class`: classes for the indicator and its lights.\n * - `hide-content-class`: classes for `body` that hide the page behind the overlay.\n * - `overlay-class`: classes for the overlay. Without them, the overlay covers the page with\n * inline styles.\n */\n\nimport { classesOf, setText } from '../utils/dom.js';\n\nexport const tagName = 'govuk-react-exit-this-page';\n\nexport const messageNames = [\n 'activated-announcement',\n 'timed-out-announcement',\n 'press-two-more-times-announcement',\n 'press-one-more-time-announcement',\n] as const;\n\nexport type MessageName = (typeof messageNames)[number];\n\n/** govuk-frontend's English messages. */\nexport const defaultMessages: Record<MessageName, string> = {\n 'activated-announcement': 'Loading.',\n 'timed-out-announcement': 'Exit this page expired.',\n 'press-two-more-times-announcement': 'Shift, press 2 more times to exit.',\n 'press-one-more-time-announcement': 'Shift, press 1 more time to exit.',\n};\n\nexport interface ExitThisPageElement extends HTMLElement {\n /** Hides the page and goes to the button's `href`, unless the exit event is cancelled. */\n exit(): void;\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'govuk-react-exit-this-page': ExitThisPageElement;\n }\n interface HTMLElementEventMap {\n 'govuk-react-exit-this-page:exit': CustomEvent<{ href: string }>;\n }\n}\n\nconst timeoutMs = 5000;\n\nconst overlayStyles: Record<string, string> = {\n position: 'fixed',\n inset: '0',\n 'z-index': '2147483647',\n background: 'white',\n};\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactExitThisPage extends HTMLElement implements ExitThisPageElement {\n private presses = 0;\n private lastKeyWasModified = false;\n private pressTimeout?: number;\n private messageTimeout?: number;\n private overlay?: HTMLElement;\n\n connectedCallback() {\n this.dataset.upgraded = '';\n document.addEventListener('keyup', this.onKeyup, true);\n document.addEventListener('click', this.onClick);\n window.addEventListener('pageshow', this.reset);\n }\n\n disconnectedCallback() {\n document.removeEventListener('keyup', this.onKeyup, true);\n document.removeEventListener('click', this.onClick);\n window.removeEventListener('pageshow', this.reset);\n this.clearTimers();\n // Moved elements start counting again.\n this.presses = 0;\n this.setStatus('');\n this.updateIndicator();\n }\n\n exit() {\n const button = this.own('[data-exit-this-page-trigger]');\n const href = button instanceof HTMLAnchorElement ? button.href : undefined;\n const proceed =\n href !== undefined &&\n this.dispatchEvent(\n new CustomEvent('govuk-react-exit-this-page:exit', { detail: { href }, bubbles: true, cancelable: true })\n );\n if (!href || !proceed) {\n // Staying on the page: clear the presses.\n this.setStatus('');\n this.updateIndicator();\n return;\n }\n this.setStatus('');\n classesOf(this.getAttribute('hide-content-class')).forEach((name) => document.body.classList.add(name));\n this.overlay?.remove();\n const overlay = document.createElement('div');\n overlay.setAttribute('role', 'alert');\n const overlayClasses = classesOf(this.getAttribute('overlay-class'));\n if (overlayClasses.length) overlay.classList.add(...overlayClasses);\n else Object.entries(overlayStyles).forEach(([name, value]) => overlay.style.setProperty(name, value));\n document.body.appendChild(overlay);\n overlay.textContent = this.message('activated-announcement');\n this.overlay = overlay;\n window.location.href = href;\n }\n\n /** Only the page's first element listens to the page, as govuk-frontend's first instance does. */\n private get listening() {\n return document.querySelector(tagName) === this;\n }\n\n private own(selector: string) {\n return Array.from(this.querySelectorAll<HTMLElement>(selector)).find(\n (element) => element.closest(tagName) === this\n );\n }\n\n private message(name: MessageName) {\n return this.getAttribute(name) ?? defaultMessages[name];\n }\n\n private setStatus(text: string) {\n const status = this.own('[data-exit-this-page-status]');\n if (status) setText(status, text);\n }\n\n private readonly onClick = (event: MouseEvent) => {\n if (event.defaultPrevented || event.button !== 0) return;\n const link = event.target instanceof Element ? event.target.closest('a') : null;\n if (!link) return;\n const own = link.matches('[data-exit-this-page-trigger]') && link.closest(tagName) === this;\n const skipLink = link.matches('[data-exit-this-page-skiplink]') && this.listening;\n if (!own && !skipLink) return;\n event.preventDefault();\n this.exit();\n };\n\n private readonly onKeyup = (event: KeyboardEvent) => {\n if (!this.listening) return;\n if (event.key === 'Shift' && !this.lastKeyWasModified) {\n this.presses += 1;\n this.updateIndicator();\n window.clearTimeout(this.messageTimeout);\n if (this.presses >= 3) {\n this.presses = 0;\n window.clearTimeout(this.pressTimeout);\n this.pressTimeout = undefined;\n this.exit();\n } else {\n this.setStatus(\n this.message(this.presses === 1 ? 'press-two-more-times-announcement' : 'press-one-more-time-announcement')\n );\n window.clearTimeout(this.pressTimeout);\n this.pressTimeout = window.setTimeout(this.timeOut, timeoutMs);\n }\n } else if (this.pressTimeout !== undefined) {\n this.timeOut();\n }\n this.lastKeyWasModified = event.shiftKey;\n };\n\n private readonly timeOut = () => {\n window.clearTimeout(this.pressTimeout);\n this.pressTimeout = undefined;\n this.presses = 0;\n this.setStatus(this.message('timed-out-announcement'));\n this.messageTimeout = window.setTimeout(() => this.setStatus(''), timeoutMs);\n this.updateIndicator();\n };\n\n /** Shows the page again when the browser brings it back from its cache. */\n private readonly reset = (event: PageTransitionEvent) => {\n // `pageshow` fires on every load too, which mustn't lose presses made before it.\n if (!event.persisted) return;\n classesOf(this.getAttribute('hide-content-class')).forEach((name) => document.body.classList.remove(name));\n this.overlay?.remove();\n this.overlay = undefined;\n this.presses = 0;\n this.setStatus('');\n this.updateIndicator();\n this.clearTimers();\n };\n\n private clearTimers() {\n window.clearTimeout(this.pressTimeout);\n window.clearTimeout(this.messageTimeout);\n this.pressTimeout = undefined;\n this.messageTimeout = undefined;\n }\n\n private updateIndicator() {\n const indicator = this.own('[data-exit-this-page-indicator]');\n if (!indicator) return;\n const visible = this.presses > 0;\n indicator.toggleAttribute('data-visible', visible);\n classesOf(this.getAttribute('indicator-visible-class')).forEach((name) =>\n indicator.classList.toggle(name, visible)\n );\n indicator.querySelectorAll('[data-exit-this-page-light]').forEach((light, index) => {\n const on = index < this.presses;\n light.toggleAttribute('data-on', on);\n classesOf(this.getAttribute('light-on-class')).forEach((name) => light.classList.toggle(name, on));\n });\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactExitThisPage);\n}\n"]}
|
|
@@ -3,13 +3,13 @@ import type { ComponentProps } from 'react';
|
|
|
3
3
|
export { defaultMessages, type ExitThisPageElement, type MessageName } from './element.js';
|
|
4
4
|
export type RootProps = React.HTMLAttributes<HTMLElement> & {
|
|
5
5
|
/** Announced once activated. Defaults to "Loading.". */
|
|
6
|
-
|
|
6
|
+
activatedAnnouncement?: string;
|
|
7
7
|
/** Announced when the Shift presses time out. Defaults to "Exit this page expired.". */
|
|
8
|
-
|
|
8
|
+
timedOutAnnouncement?: string;
|
|
9
9
|
/** Announced after one press of Shift. Defaults to "Shift, press 2 more times to exit.". */
|
|
10
|
-
|
|
10
|
+
pressTwoMoreTimesAnnouncement?: string;
|
|
11
11
|
/** Announced after two presses of Shift. Defaults to "Shift, press 1 more time to exit.". */
|
|
12
|
-
|
|
12
|
+
pressOneMoreTimeAnnouncement?: string;
|
|
13
13
|
/** Classes for the indicator while counting presses. */
|
|
14
14
|
indicatorVisibleClass?: string;
|
|
15
15
|
/** Classes for each light of the indicator once pressed. */
|
|
@@ -19,11 +19,11 @@ export type RootProps = React.HTMLAttributes<HTMLElement> & {
|
|
|
19
19
|
/** Classes for the overlay. Without them, inline styles cover the page. */
|
|
20
20
|
overlayClass?: string;
|
|
21
21
|
};
|
|
22
|
-
export declare function Root({
|
|
23
|
-
activated: string;
|
|
24
|
-
'timed-out': string;
|
|
25
|
-
'press-two-more-times': string;
|
|
26
|
-
'press-one-more-time': string;
|
|
22
|
+
export declare function Root({ activatedAnnouncement, timedOutAnnouncement, pressTwoMoreTimesAnnouncement, pressOneMoreTimeAnnouncement, indicatorVisibleClass, lightOnClass, hideContentClass, overlayClass, ...props }: RootProps): React.DOMElement<{
|
|
23
|
+
'activated-announcement': string;
|
|
24
|
+
'timed-out-announcement': string;
|
|
25
|
+
'press-two-more-times-announcement': string;
|
|
26
|
+
'press-one-more-time-announcement': string;
|
|
27
27
|
'indicator-visible-class': string;
|
|
28
28
|
'light-on-class': string;
|
|
29
29
|
'hide-content-class': string;
|
|
@@ -313,9 +313,9 @@ type PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E>
|
|
|
313
313
|
/** Render the only child instead, with this part's props merged onto it. */
|
|
314
314
|
asChild?: boolean;
|
|
315
315
|
};
|
|
316
|
-
export type
|
|
316
|
+
export type TriggerProps = PartProps<'a'>;
|
|
317
317
|
/** The link to a safe page, which the element follows once it has hidden the page. */
|
|
318
|
-
export declare function
|
|
318
|
+
export declare function Trigger({ asChild, ...props }: TriggerProps): React.JSX.Element;
|
|
319
319
|
export type SkipLinkProps = PartProps<'a'>;
|
|
320
320
|
/** A link anywhere on the page, such as in the skip links, that exits too. */
|
|
321
321
|
export declare function SkipLink({ asChild, ...props }: SkipLinkProps): React.JSX.Element;
|
|
@@ -3,13 +3,13 @@ import * as React from 'react';
|
|
|
3
3
|
import { Slot } from '../utils/slot.js';
|
|
4
4
|
import { tagName } from './element.js';
|
|
5
5
|
export { defaultMessages } from './element.js';
|
|
6
|
-
export function Root({
|
|
6
|
+
export function Root({ activatedAnnouncement, timedOutAnnouncement, pressTwoMoreTimesAnnouncement, pressOneMoreTimeAnnouncement, indicatorVisibleClass, lightOnClass, hideContentClass, overlayClass, ...props }) {
|
|
7
7
|
return React.createElement(tagName, {
|
|
8
8
|
...props,
|
|
9
|
-
activated:
|
|
10
|
-
'timed-out':
|
|
11
|
-
'press-two-more-times':
|
|
12
|
-
'press-one-more-time':
|
|
9
|
+
'activated-announcement': activatedAnnouncement,
|
|
10
|
+
'timed-out-announcement': timedOutAnnouncement,
|
|
11
|
+
'press-two-more-times-announcement': pressTwoMoreTimesAnnouncement,
|
|
12
|
+
'press-one-more-time-announcement': pressOneMoreTimeAnnouncement,
|
|
13
13
|
'indicator-visible-class': indicatorVisibleClass,
|
|
14
14
|
'light-on-class': lightOnClass,
|
|
15
15
|
'hide-content-class': hideContentClass,
|
|
@@ -19,9 +19,9 @@ export function Root({ activatedText, timedOutText, pressTwoMoreTimesText, press
|
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
/** The link to a safe page, which the element follows once it has hidden the page. */
|
|
22
|
-
export function
|
|
22
|
+
export function Trigger({ asChild, ...props }) {
|
|
23
23
|
const Element = asChild ? Slot : 'a';
|
|
24
|
-
return React.createElement(Element, { "data-exit-this-page-
|
|
24
|
+
return React.createElement(Element, { "data-exit-this-page-trigger": "", ...props });
|
|
25
25
|
}
|
|
26
26
|
/** A link anywhere on the page, such as in the skip links, that exits too. */
|
|
27
27
|
export function SkipLink({ asChild, ...props }) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/exit-this-page/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,eAAe,EAA8C,MAAM,cAAc,CAAC;AA8B3F,MAAM,UAAU,IAAI,CAAC,EACnB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/exit-this-page/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,eAAe,EAA8C,MAAM,cAAc,CAAC;AA8B3F,MAAM,UAAU,IAAI,CAAC,EACnB,qBAAqB,EACrB,oBAAoB,EACpB,6BAA6B,EAC7B,4BAA4B,EAC5B,qBAAqB,EACrB,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,GAAG,KAAK,EACE;IACV,OAAO,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE;QAClC,GAAG,KAAK;QACR,wBAAwB,EAAE,qBAAqB;QAC/C,wBAAwB,EAAE,oBAAoB;QAC9C,mCAAmC,EAAE,6BAA6B;QAClE,kCAAkC,EAAE,4BAA4B;QAChE,yBAAyB,EAAE,qBAAqB;QAChD,gBAAgB,EAAE,YAAY;QAC9B,oBAAoB,EAAE,gBAAgB;QACtC,eAAe,EAAE,YAAY;QAC7B,oCAAoC;QACpC,wBAAwB,EAAE,IAAI;KAC/B,CAAC,CAAC;AACL,CAAC;A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,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAe;IACjE,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;IACxC,iGAAiG;IACjG,qEAAqE;IACrE,OAAO,CACL,oBAAC,OAAO,kCAA4B,EAAE,EAAC,IAAI,EAAC,QAAQ,EAAC,wBAAwB,WAAK,KAAK,IACpF,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,CACP,CACX,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName } from './element.js';\n\nexport { defaultMessages, type ExitThisPageElement, type MessageName } from './element.js';\n\n/*\n * Headless exit this page: unstyled parts over the <govuk-react-exit-this-page> element\n * (./element.ts). The button, a SkipLink, or pressing Shift 3 times hides the page and goes to\n * the button's `href`. Every part except Root takes `asChild`.\n *\n * Without JavaScript the button is a plain link. To stay on the page, such as in a test, listen for\n * `govuk-react-exit-this-page:exit` and cancel it.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** Announced once activated. Defaults to \"Loading.\". */\n activatedAnnouncement?: string;\n /** Announced when the Shift presses time out. Defaults to \"Exit this page expired.\". */\n timedOutAnnouncement?: string;\n /** Announced after one press of Shift. Defaults to \"Shift, press 2 more times to exit.\". */\n pressTwoMoreTimesAnnouncement?: string;\n /** Announced after two presses of Shift. Defaults to \"Shift, press 1 more time to exit.\". */\n pressOneMoreTimeAnnouncement?: string;\n /** Classes for the indicator while counting presses. */\n indicatorVisibleClass?: string;\n /** Classes for each light of the indicator once pressed. */\n lightOnClass?: string;\n /** Classes for `body` that hide the page behind the overlay. */\n hideContentClass?: string;\n /** Classes for the overlay. Without them, inline styles cover the page. */\n overlayClass?: string;\n};\n\nexport function Root({\n activatedAnnouncement,\n timedOutAnnouncement,\n pressTwoMoreTimesAnnouncement,\n pressOneMoreTimeAnnouncement,\n indicatorVisibleClass,\n lightOnClass,\n hideContentClass,\n overlayClass,\n ...props\n}: RootProps) {\n return React.createElement(tagName, {\n ...props,\n 'activated-announcement': activatedAnnouncement,\n 'timed-out-announcement': timedOutAnnouncement,\n 'press-two-more-times-announcement': pressTwoMoreTimesAnnouncement,\n 'press-one-more-time-announcement': pressOneMoreTimeAnnouncement,\n 'indicator-visible-class': indicatorVisibleClass,\n 'light-on-class': lightOnClass,\n 'hide-content-class': hideContentClass,\n 'overlay-class': overlayClass,\n // The element owns `data-upgraded`.\n suppressHydrationWarning: true,\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 TriggerProps = PartProps<'a'>;\n\n/** The link to a safe page, which the element follows once it has hidden the page. */\nexport function Trigger({ asChild, ...props }: TriggerProps) {\n const Element = asChild ? Slot : 'a';\n return <Element data-exit-this-page-trigger=\"\" {...props} />;\n}\n\nexport type SkipLinkProps = PartProps<'a'>;\n\n/** A link anywhere on the page, such as in the skip links, that exits too. */\nexport function SkipLink({ asChild, ...props }: SkipLinkProps) {\n const Element = asChild ? Slot : 'a';\n return <Element data-exit-this-page-skiplink=\"\" {...props} />;\n}\n\nexport type IndicatorProps = PartProps<'div'>;\n\n/** Shows how many times Shift has been pressed. Has three Lights unless given children. */\nexport function Indicator({ asChild, children, ...props }: IndicatorProps) {\n const Element = asChild ? Slot : 'div';\n return (\n // The element owns `data-visible` and its classes.\n <Element data-exit-this-page-indicator=\"\" aria-hidden=\"true\" suppressHydrationWarning {...props}>\n {children ?? (\n <>\n <Light />\n <Light />\n <Light />\n </>\n )}\n </Element>\n );\n}\n\nexport type LightProps = PartProps<'div'>;\n\n/** One press of Shift. */\nexport function Light({ asChild, ...props }: LightProps) {\n const Element = asChild ? Slot : 'div';\n // The element owns `data-on` and its classes.\n return <Element data-exit-this-page-light=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type StatusProps = PartProps<'span'>;\n\n/** Tells screen readers how many more presses are needed. Make it visually hidden. */\nexport function Status({ asChild, children, ...props }: StatusProps) {\n const Element = asChild ? Slot : 'span';\n // The element owns its text. A string child, even an empty one, is hydrated as text, so text the\n // element set before hydration is kept rather than being a mismatch.\n return (\n <Element data-exit-this-page-status=\"\" role=\"status\" suppressHydrationWarning {...props}>\n {children ?? ''}\n </Element>\n );\n}\n"]}
|
|
@@ -4,20 +4,25 @@ export interface FileUploadElement extends HTMLElement {
|
|
|
4
4
|
/** Updates the button from the input's files, such as after setting them. */
|
|
5
5
|
update(): void;
|
|
6
6
|
}
|
|
7
|
+
declare global {
|
|
8
|
+
interface HTMLElementTagNameMap {
|
|
9
|
+
'govuk-react-file-upload': FileUploadElement;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
7
12
|
/** govuk-frontend's English text. */
|
|
8
13
|
export declare const defaults: {
|
|
9
|
-
'no-file-chosen': string;
|
|
10
|
-
'multiple-files-chosen': {
|
|
14
|
+
'no-file-chosen-text': string;
|
|
15
|
+
'multiple-files-chosen-text': {
|
|
11
16
|
one: string;
|
|
12
17
|
other: string;
|
|
13
18
|
};
|
|
14
|
-
'entered-drop-zone': string;
|
|
15
|
-
'left-drop-zone': string;
|
|
19
|
+
'entered-drop-zone-announcement': string;
|
|
20
|
+
'left-drop-zone-announcement': string;
|
|
16
21
|
};
|
|
17
22
|
/** What the status says for the files chosen. */
|
|
18
23
|
export declare function statusText(files: ArrayLike<{
|
|
19
24
|
name: string;
|
|
20
25
|
}>, messages: {
|
|
21
|
-
'no-file-chosen'?: Translation;
|
|
22
|
-
'multiple-files-chosen'?: Translation;
|
|
26
|
+
'no-file-chosen-text'?: Translation;
|
|
27
|
+
'multiple-files-chosen-text'?: Translation;
|
|
23
28
|
}, locale?: string): string;
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Markup inside the element:
|
|
15
15
|
* - `input[type="file"]`: the field, with an `id` and a `label`. Hidden on upgrade.
|
|
16
|
-
* - `button[data-file-upload-
|
|
16
|
+
* - `button[data-file-upload-trigger]`: rendered `hidden`, with `type="button"`.
|
|
17
17
|
* - `[data-file-upload-status]`: which files are chosen.
|
|
18
18
|
* - `[data-file-upload-separator]` (optional): between the label and the button's text in its
|
|
19
19
|
* accessible name, such as a visually hidden comma.
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
* over the drop zone, and the drop zone gets `data-disabled`.
|
|
27
27
|
*
|
|
28
28
|
* Attributes:
|
|
29
|
-
* - `no-file-chosen`, `multiple-files-chosen`, `entered-drop-zone`, `left-drop-zone`: the text,
|
|
29
|
+
* - `no-file-chosen-text`, `multiple-files-chosen-text`, `entered-drop-zone-announcement`, `left-drop-zone-announcement`: the text,
|
|
30
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.
|
|
31
|
+
* such as `multiple-files-chosen-text-one`, for a message for each form.
|
|
32
32
|
* - `empty-class`, `dragging-class`: classes for the button while empty and while dragging.
|
|
33
33
|
* - `disabled-class`: classes for the drop zone while the input is disabled.
|
|
34
34
|
*/
|
|
@@ -37,19 +37,19 @@ import { classesOf, setText, setAttribute } from '../utils/dom.js';
|
|
|
37
37
|
export const tagName = 'govuk-react-file-upload';
|
|
38
38
|
/** govuk-frontend's English text. */
|
|
39
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',
|
|
40
|
+
'no-file-chosen-text': 'No file chosen',
|
|
41
|
+
'multiple-files-chosen-text': { one: '%{count} file chosen', other: '%{count} files chosen' },
|
|
42
|
+
'entered-drop-zone-announcement': 'Entered drop zone',
|
|
43
|
+
'left-drop-zone-announcement': 'Left drop zone',
|
|
44
44
|
};
|
|
45
45
|
/** What the status says for the files chosen. */
|
|
46
46
|
export function statusText(files, messages, locale) {
|
|
47
47
|
var _a, _b;
|
|
48
48
|
if (files.length === 0)
|
|
49
|
-
return translate((_a = messages['no-file-chosen']) !== null && _a !== void 0 ? _a : defaults['no-file-chosen'], 0, locale);
|
|
49
|
+
return translate((_a = messages['no-file-chosen-text']) !== null && _a !== void 0 ? _a : defaults['no-file-chosen-text'], 0, locale);
|
|
50
50
|
if (files.length === 1)
|
|
51
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);
|
|
52
|
+
return translate((_b = messages['multiple-files-chosen-text']) !== null && _b !== void 0 ? _b : defaults['multiple-files-chosen-text'], files.length, locale);
|
|
53
53
|
}
|
|
54
54
|
const countFiles = (items) => Array.from(items).filter((item) => item.kind === 'file').length;
|
|
55
55
|
if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
|
|
@@ -84,12 +84,12 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
84
84
|
if (this.dropZone.contains(event.target)) {
|
|
85
85
|
if (event.dataTransfer && this.canDrop(event.dataTransfer) && !this.dragging) {
|
|
86
86
|
this.setDragging(true);
|
|
87
|
-
this.announce('entered-drop-zone');
|
|
87
|
+
this.announce('entered-drop-zone-announcement');
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
else if (this.dragging) {
|
|
91
91
|
this.setDragging(false);
|
|
92
|
-
this.announce('left-drop-zone');
|
|
92
|
+
this.announce('left-drop-zone-announcement');
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
95
|
// Leaving the window, or dragging back out of it, fires `dragleave` with no `dragenter`.
|
|
@@ -97,7 +97,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
97
97
|
var _a;
|
|
98
98
|
if (!this.enteredAnotherElement && this.dragging && !((_a = this.button) === null || _a === void 0 ? void 0 : _a.disabled)) {
|
|
99
99
|
this.setDragging(false);
|
|
100
|
-
this.announce('left-drop-zone');
|
|
100
|
+
this.announce('left-drop-zone-announcement');
|
|
101
101
|
}
|
|
102
102
|
this.enteredAnotherElement = false;
|
|
103
103
|
};
|
|
@@ -153,7 +153,7 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
153
153
|
setup() {
|
|
154
154
|
var _a;
|
|
155
155
|
const input = this.own('input[type="file"]');
|
|
156
|
-
const button = this.own('button[data-file-upload-
|
|
156
|
+
const button = this.own('button[data-file-upload-trigger]');
|
|
157
157
|
if (!input || !button)
|
|
158
158
|
return;
|
|
159
159
|
// A new input, or a new id from React, rather than the one the element gave it.
|
|
@@ -218,8 +218,8 @@ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined')
|
|
|
218
218
|
const status = button.querySelector('[data-file-upload-status]');
|
|
219
219
|
if (status) {
|
|
220
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'),
|
|
221
|
+
'no-file-chosen-text': this.message('no-file-chosen-text'),
|
|
222
|
+
'multiple-files-chosen-text': this.message('multiple-files-chosen-text'),
|
|
223
223
|
}, this.locale));
|
|
224
224
|
}
|
|
225
225
|
// `toggle` with force leaves a class that's already there alone, so the observer isn't woken.
|
|
@@ -1 +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,G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AC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;AACzF,CAAC","sourcesContent":["/*\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"]}
|
|
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;AAajD,qCAAqC;AACrC,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,qBAAqB,EAAE,gBAAgB;IACvC,4BAA4B,EAAE,EAAE,GA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OM 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-trigger]`: 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-text`, `multiple-files-chosen-text`, `entered-drop-zone-announcement`, `left-drop-zone-announcement`: 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-text-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\ndeclare global {\n interface HTMLElementTagNameMap {\n 'govuk-react-file-upload': FileUploadElement;\n }\n}\n\n/** govuk-frontend's English text. */\nexport const defaults = {\n 'no-file-chosen-text': 'No file chosen',\n 'multiple-files-chosen-text': { one: '%{count} file chosen', other: '%{count} files chosen' },\n 'entered-drop-zone-announcement': 'Entered drop zone',\n 'left-drop-zone-announcement': '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-text'?: Translation; 'multiple-files-chosen-text'?: Translation },\n locale?: string\n) {\n if (files.length === 0)\n return translate(messages['no-file-chosen-text'] ?? defaults['no-file-chosen-text'], 0, locale);\n if (files.length === 1) return files[0].name;\n return translate(\n messages['multiple-files-chosen-text'] ?? defaults['multiple-files-chosen-text'],\n files.length,\n locale\n );\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-announcement');\n }\n } else if (this.dragging) {\n this.setDragging(false);\n this.announce('left-drop-zone-announcement');\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-announcement');\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-trigger]');\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-text': this.message('no-file-chosen-text'),\n 'multiple-files-chosen-text': this.message('multiple-files-chosen-text'),\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-announcement' | 'left-drop-zone-announcement') {\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"]}
|