@govuk-react/primitives 0.2.0 → 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.
- package/dist/{accordion-element.d.ts → accordion/element.d.ts} +2 -5
- package/dist/accordion/element.js +302 -0
- package/dist/accordion/element.js.map +1 -0
- package/dist/accordion/index.d.ts +74 -0
- package/dist/accordion/index.js +138 -0
- package/dist/accordion/index.js.map +1 -0
- package/dist/auto-focus/element.d.ts +1 -0
- package/dist/auto-focus/element.js +75 -0
- package/dist/auto-focus/element.js.map +1 -0
- package/dist/{index-Bul0NJh_.d.ts → auto-focus/index.d.ts} +27 -40
- package/dist/auto-focus/index.js +29 -0
- package/dist/{chunk-6ZFU4VYW.js.map → auto-focus/index.js.map} +1 -1
- package/dist/{autocomplete-element.d.ts → autocomplete/element.d.ts} +4 -7
- package/dist/autocomplete/element.js +597 -0
- package/dist/autocomplete/element.js.map +1 -0
- package/dist/{index-D7lMYMUR.d.ts → autocomplete/index.d.ts} +34 -57
- package/dist/autocomplete/index.js +108 -0
- package/dist/autocomplete/index.js.map +1 -0
- package/dist/button/element.d.ts +1 -0
- package/dist/button/element.js +59 -0
- package/dist/button/element.js.map +1 -0
- package/dist/button/index.d.ts +10 -0
- package/dist/button/index.js +9 -0
- package/dist/button/index.js.map +1 -0
- package/dist/{character-count-element.d.ts → character-count/element.d.ts} +2 -5
- package/dist/character-count/element.js +287 -0
- package/dist/character-count/element.js.map +1 -0
- package/dist/character-count/index.d.ts +57 -0
- package/dist/character-count/index.js +79 -0
- package/dist/character-count/index.js.map +1 -0
- package/dist/character-count/messages.d.ts +20 -0
- package/dist/character-count/messages.js +54 -0
- package/dist/character-count/messages.js.map +1 -0
- package/dist/choices/element.d.ts +5 -0
- package/dist/choices/element.js +202 -0
- package/dist/choices/element.js.map +1 -0
- package/dist/{index-WuGvkTxu.d.ts → choices/index.d.ts} +30 -44
- package/dist/choices/index.js +37 -0
- package/dist/choices/index.js.map +1 -0
- package/dist/{disclosure-element.d.ts → disclosure/element.d.ts} +2 -5
- package/dist/disclosure/element.js +123 -0
- package/dist/disclosure/element.js.map +1 -0
- package/dist/{index-D4Gzyvfu.d.ts → disclosure/index.d.ts} +40 -54
- package/dist/disclosure/index.js +66 -0
- package/dist/disclosure/index.js.map +1 -0
- package/dist/exit-this-page/element.d.ts +19 -0
- package/dist/exit-this-page/element.js +201 -0
- package/dist/exit-this-page/element.js.map +1 -0
- package/dist/{index-qF0jIQCW.d.ts → exit-this-page/index.d.ts} +36 -60
- package/dist/exit-this-page/index.js +54 -0
- package/dist/exit-this-page/index.js.map +1 -0
- package/dist/{file-upload-element.d.ts → file-upload/element.d.ts} +5 -9
- package/dist/file-upload/element.js +270 -0
- package/dist/file-upload/element.js.map +1 -0
- package/dist/file-upload/index.d.ts +46 -0
- package/dist/file-upload/index.js +65 -0
- package/dist/file-upload/index.js.map +1 -0
- package/dist/focus-links/element.d.ts +1 -0
- package/dist/focus-links/element.js +90 -0
- package/dist/focus-links/element.js.map +1 -0
- package/dist/{index-BR3MAGF8.d.ts → focus-links/index.d.ts} +24 -35
- package/dist/focus-links/index.js +7 -0
- package/dist/focus-links/index.js.map +1 -0
- package/dist/index.d.ts +13 -17
- package/dist/index.js +13 -72
- package/dist/index.js.map +1 -1
- package/dist/inline/accordion-element.js +1 -1
- package/dist/inline/auto-focus-element.js +1 -1
- package/dist/inline/autocomplete-element.js +1 -1
- package/dist/inline/character-count-element.js +1 -1
- package/dist/inline/choices-element.js +1 -1
- package/dist/inline/disclosure-element.js +1 -1
- package/dist/inline/exit-this-page-element.js +1 -1
- package/dist/inline/file-upload-element.js +1 -1
- package/dist/inline/focus-links-element.js +1 -1
- package/dist/inline/modal-element.js +1 -1
- package/dist/inline/password-input-element.js +1 -1
- package/dist/inline/tabs-element.js +1 -1
- package/dist/inline-script/index.d.ts +26 -0
- package/dist/inline-script/index.js +31 -0
- package/dist/inline-script/index.js.map +1 -0
- package/dist/{modal-element.d.ts → modal/element.d.ts} +2 -4
- package/dist/modal/element.js +114 -0
- package/dist/modal/element.js.map +1 -0
- package/dist/modal/index.d.ts +36 -0
- package/dist/modal/index.js +109 -0
- package/dist/modal/index.js.map +1 -0
- package/dist/password-input/element.d.ts +1 -0
- package/dist/password-input/element.js +134 -0
- package/dist/password-input/element.js.map +1 -0
- package/dist/{index-sEHnx4-M.d.ts → password-input/index.d.ts} +32 -48
- package/dist/password-input/index.js +35 -0
- package/dist/password-input/index.js.map +1 -0
- package/dist/{tabs-element.d.ts → tabs/element.d.ts} +2 -5
- package/dist/tabs/element.js +416 -0
- package/dist/tabs/element.js.map +1 -0
- package/dist/tabs/index.d.ts +83 -0
- package/dist/tabs/index.js +138 -0
- package/dist/tabs/index.js.map +1 -0
- package/dist/utils/dom.d.ts +9 -0
- package/dist/utils/dom.js +27 -0
- package/dist/utils/dom.js.map +1 -0
- package/dist/utils/on-property-set.d.ts +9 -0
- package/dist/utils/on-property-set.js +38 -0
- package/dist/utils/on-property-set.js.map +1 -0
- package/dist/utils/set-focus.d.ts +11 -0
- package/dist/utils/set-focus.js +38 -0
- package/dist/utils/set-focus.js.map +1 -0
- package/dist/utils/set-up-when-ready.d.ts +6 -0
- package/dist/utils/set-up-when-ready.js +16 -0
- package/dist/utils/set-up-when-ready.js.map +1 -0
- package/dist/utils/slot.d.ts +13 -0
- package/dist/utils/slot.js +40 -0
- package/dist/utils/slot.js.map +1 -0
- package/dist/utils/translation.d.ts +13 -0
- package/dist/utils/translation.js +57 -0
- package/dist/utils/translation.js.map +1 -0
- package/package.json +11 -4
- package/dist/accordion-element.js +0 -9
- package/dist/accordion-element.js.map +0 -1
- package/dist/accordion.d.ts +0 -2
- package/dist/accordion.js +0 -34
- package/dist/accordion.js.map +0 -1
- package/dist/auto-focus-element.d.ts +0 -3
- package/dist/auto-focus-element.js +0 -10
- package/dist/auto-focus-element.js.map +0 -1
- package/dist/auto-focus.d.ts +0 -2
- package/dist/auto-focus.js +0 -15
- package/dist/auto-focus.js.map +0 -1
- package/dist/autocomplete-element.js +0 -12
- package/dist/autocomplete-element.js.map +0 -1
- package/dist/autocomplete.d.ts +0 -4
- package/dist/autocomplete.js +0 -25
- package/dist/autocomplete.js.map +0 -1
- package/dist/button-element.d.ts +0 -2
- package/dist/button-element.js +0 -3
- package/dist/button-element.js.map +0 -1
- package/dist/button.d.ts +0 -2
- package/dist/button.js +0 -12
- package/dist/button.js.map +0 -1
- package/dist/character-count-element.js +0 -10
- package/dist/character-count-element.js.map +0 -1
- package/dist/character-count.d.ts +0 -3
- package/dist/character-count.js +0 -26
- package/dist/character-count.js.map +0 -1
- package/dist/choices-element.d.ts +0 -8
- package/dist/choices-element.js +0 -9
- package/dist/choices-element.js.map +0 -1
- package/dist/choices.d.ts +0 -2
- package/dist/choices.js +0 -16
- package/dist/choices.js.map +0 -1
- package/dist/chunk-2IHAQAYF.js +0 -23
- package/dist/chunk-2IHAQAYF.js.map +0 -1
- package/dist/chunk-2ZJVSNC2.js +0 -31
- package/dist/chunk-2ZJVSNC2.js.map +0 -1
- package/dist/chunk-47NMY4SS.js +0 -108
- package/dist/chunk-47NMY4SS.js.map +0 -1
- package/dist/chunk-4XAR4FFG.js +0 -83
- package/dist/chunk-4XAR4FFG.js.map +0 -1
- package/dist/chunk-5HWRMQK2.js +0 -91
- package/dist/chunk-5HWRMQK2.js.map +0 -1
- package/dist/chunk-6ZFU4VYW.js +0 -41
- package/dist/chunk-B2I4J5NL.js +0 -264
- package/dist/chunk-B2I4J5NL.js.map +0 -1
- package/dist/chunk-DPERPCUH.js +0 -188
- package/dist/chunk-DPERPCUH.js.map +0 -1
- package/dist/chunk-ECYGKE4D.js +0 -168
- package/dist/chunk-ECYGKE4D.js.map +0 -1
- package/dist/chunk-ESBMTI25.js +0 -11
- package/dist/chunk-ESBMTI25.js.map +0 -1
- package/dist/chunk-ET7DCQM7.js +0 -133
- package/dist/chunk-ET7DCQM7.js.map +0 -1
- package/dist/chunk-F5XFC4QJ.js +0 -177
- package/dist/chunk-F5XFC4QJ.js.map +0 -1
- package/dist/chunk-GVGBDSDN.js +0 -78
- package/dist/chunk-GVGBDSDN.js.map +0 -1
- package/dist/chunk-IZQ2C4BC.js +0 -351
- package/dist/chunk-IZQ2C4BC.js.map +0 -1
- package/dist/chunk-L7E6GN2D.js +0 -87
- package/dist/chunk-L7E6GN2D.js.map +0 -1
- package/dist/chunk-LWIA644X.js +0 -34
- package/dist/chunk-LWIA644X.js.map +0 -1
- package/dist/chunk-NX4B3NKM.js +0 -35
- package/dist/chunk-NX4B3NKM.js.map +0 -1
- package/dist/chunk-PNWPGD7B.js +0 -68
- package/dist/chunk-PNWPGD7B.js.map +0 -1
- package/dist/chunk-QLQ6O7OW.js +0 -111
- package/dist/chunk-QLQ6O7OW.js.map +0 -1
- package/dist/chunk-RDUPELOK.js +0 -55
- package/dist/chunk-RDUPELOK.js.map +0 -1
- package/dist/chunk-RPMHI67T.js +0 -104
- package/dist/chunk-RPMHI67T.js.map +0 -1
- package/dist/chunk-TE4RUYVK.js +0 -97
- package/dist/chunk-TE4RUYVK.js.map +0 -1
- package/dist/chunk-TJVBFRIE.js +0 -60
- package/dist/chunk-TJVBFRIE.js.map +0 -1
- package/dist/chunk-U5OKNKYC.js +0 -510
- package/dist/chunk-U5OKNKYC.js.map +0 -1
- package/dist/chunk-UXJO4AJP.js +0 -290
- package/dist/chunk-UXJO4AJP.js.map +0 -1
- package/dist/chunk-VNXXENCU.js +0 -39
- package/dist/chunk-VNXXENCU.js.map +0 -1
- package/dist/chunk-XGK73UCD.js +0 -62
- package/dist/chunk-XGK73UCD.js.map +0 -1
- package/dist/chunk-XJYLFTYE.js +0 -157
- package/dist/chunk-XJYLFTYE.js.map +0 -1
- package/dist/chunk-YCESNTXQ.js +0 -241
- package/dist/chunk-YCESNTXQ.js.map +0 -1
- package/dist/chunk-ZTJLVNR4.js +0 -62
- package/dist/chunk-ZTJLVNR4.js.map +0 -1
- package/dist/disclosure-element.js +0 -9
- package/dist/disclosure-element.js.map +0 -1
- package/dist/disclosure.d.ts +0 -2
- package/dist/disclosure.js +0 -16
- package/dist/disclosure.js.map +0 -1
- package/dist/exit-this-page-element.d.ts +0 -22
- package/dist/exit-this-page-element.js +0 -13
- package/dist/exit-this-page-element.js.map +0 -1
- package/dist/exit-this-page.d.ts +0 -3
- package/dist/exit-this-page.js +0 -25
- package/dist/exit-this-page.js.map +0 -1
- package/dist/file-upload-element.js +0 -14
- package/dist/file-upload-element.js.map +0 -1
- package/dist/file-upload.d.ts +0 -3
- package/dist/file-upload.js +0 -25
- package/dist/file-upload.js.map +0 -1
- package/dist/focus-links-element.d.ts +0 -3
- package/dist/focus-links-element.js +0 -10
- package/dist/focus-links-element.js.map +0 -1
- package/dist/focus-links.d.ts +0 -2
- package/dist/focus-links.js +0 -12
- package/dist/focus-links.js.map +0 -1
- package/dist/index--FxJgSOS.d.ts +0 -108
- package/dist/index-CikOyFud.d.ts +0 -54
- package/dist/index-D8N5vxTL.d.ts +0 -90
- package/dist/index-DMWpkC_d.d.ts +0 -105
- package/dist/index-DN7fGMc3.d.ts +0 -20
- package/dist/index-DPXR1Yua.d.ts +0 -69
- package/dist/inline-script.d.ts +0 -29
- package/dist/inline-script.js +0 -63
- package/dist/inline-script.js.map +0 -1
- package/dist/modal-element.js +0 -11
- package/dist/modal-element.js.map +0 -1
- package/dist/modal.d.ts +0 -2
- package/dist/modal.js +0 -20
- package/dist/modal.js.map +0 -1
- package/dist/password-input-element.d.ts +0 -3
- package/dist/password-input-element.js +0 -9
- package/dist/password-input-element.js.map +0 -1
- package/dist/password-input.d.ts +0 -2
- package/dist/password-input.js +0 -18
- package/dist/password-input.js.map +0 -1
- package/dist/tabs-element.js +0 -9
- package/dist/tabs-element.js.map +0 -1
- package/dist/tabs.d.ts +0 -2
- package/dist/tabs.js +0 -28
- package/dist/tabs.js.map +0 -1
- package/dist/translation-BnpO2mN2.d.ts +0 -4
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* <govuk-react-auto-focus>: a headless, light-DOM custom element that moves focus to its target
|
|
3
|
+
* when it appears, such as an error summary or a success banner, with or without React. It moves
|
|
4
|
+
* focus once: give the element a new `key` in React to move it again, such as after each submit.
|
|
5
|
+
*
|
|
6
|
+
* The target is server-rendered with `tabindex="-1"` and `autofocus`, so the browser focuses it
|
|
7
|
+
* when the page loads, even without JavaScript. When the element connects later, such as when React
|
|
8
|
+
* renders the target after a form is submitted, the element focuses it. As in govuk-frontend, the
|
|
9
|
+
* target loses its `tabindex` when it loses focus. If the user has moved focus on before the script
|
|
10
|
+
* runs, it leaves them there. Without JavaScript the target keeps its `tabindex`.
|
|
11
|
+
*
|
|
12
|
+
* Markup inside the element:
|
|
13
|
+
* - `[data-auto-focus]`: the target. Without one, the element's first child.
|
|
14
|
+
*
|
|
15
|
+
* Attributes:
|
|
16
|
+
* - `disabled`: don't move focus.
|
|
17
|
+
*/
|
|
18
|
+
import { dropAutoFocus, setFocus } from '../utils/set-focus.js';
|
|
19
|
+
import { setUpWhenReady } from '../utils/set-up-when-ready.js';
|
|
20
|
+
export const tagName = 'govuk-react-auto-focus';
|
|
21
|
+
if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
|
|
22
|
+
class GovukReactAutoFocus extends HTMLElement {
|
|
23
|
+
constructor() {
|
|
24
|
+
super(...arguments);
|
|
25
|
+
this.focused = false;
|
|
26
|
+
}
|
|
27
|
+
connectedCallback() {
|
|
28
|
+
// The target may not be parsed or rendered yet.
|
|
29
|
+
this.observer = setUpWhenReady(this, () => this.focusTarget());
|
|
30
|
+
}
|
|
31
|
+
disconnectedCallback() {
|
|
32
|
+
var _a;
|
|
33
|
+
(_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
34
|
+
}
|
|
35
|
+
get target() {
|
|
36
|
+
var _a;
|
|
37
|
+
const target = (_a = Array.from(this.querySelectorAll('[data-auto-focus]')).find((found) => found.closest(tagName) === this)) !== null && _a !== void 0 ? _a : this.firstElementChild;
|
|
38
|
+
return target instanceof HTMLElement ? target : undefined;
|
|
39
|
+
}
|
|
40
|
+
/** Focuses the target once, and says whether there was one to focus. */
|
|
41
|
+
focusTarget() {
|
|
42
|
+
if (this.focused)
|
|
43
|
+
return true;
|
|
44
|
+
const { target } = this;
|
|
45
|
+
if (!target)
|
|
46
|
+
return false;
|
|
47
|
+
this.focused = true;
|
|
48
|
+
if (this.hasAttribute('disabled'))
|
|
49
|
+
return true;
|
|
50
|
+
// Rendered by the server with `autofocus`, which React never sets on the client.
|
|
51
|
+
if (target.hasAttribute('autofocus')) {
|
|
52
|
+
const active = document.activeElement;
|
|
53
|
+
if (active === target) {
|
|
54
|
+
setFocus(target, { preventScroll: true });
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (active && active !== document.body) {
|
|
58
|
+
// The user moved on before this script ran: leave them there.
|
|
59
|
+
dropAutoFocus(target);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// React puts focus back where it was at the end of a commit, which can connect the element.
|
|
64
|
+
queueMicrotask(() => {
|
|
65
|
+
// Not in place yet, such as in a streamed boundary: try again when it moves.
|
|
66
|
+
if (!target.isConnected || !setFocus(target))
|
|
67
|
+
this.focused = false;
|
|
68
|
+
});
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!customElements.get(tagName))
|
|
73
|
+
customElements.define(tagName, GovukReactAutoFocus);
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=element.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/auto-focus/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAE/D,MAAM,CAAC,MAAM,OAAO,GAAG,wBAAwB,CAAC;AAEhD,IAAI,OAAO,WAAW,KAAK,WAAW,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE,CAAC;IAChF,MAAM,mBAAoB,SAAQ,WAAW;QAA7C;;YACU,YAAO,GAAG,KAAK,CAAC;QA+C1B,CAAC;QA3CC,iBAAiB;YACf,gDAAgD;YAChD,IAAI,CAAC,QAAQ,GAAG,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;QACjE,CAAC;QAED,oBAAoB;;YAClB,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,EAAE,CAAC;QAC9B,CAAC;QAED,IAAY,MAAM;;YAChB,MAAM,MAAM,GACV,MAAA,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,mBAAmB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,mCACvG,IAAI,CAAC,iBAAiB,CAAC;YACzB,OAAO,MAAM,YAAY,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;QAC5D,CAAC;QAED,wEAAwE;QAChE,WAAW;YACjB,IAAI,IAAI,CAAC,OAAO;gBAAE,OAAO,IAAI,CAAC;YAC9B,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;YACxB,IAAI,CAAC,MAAM;gBAAE,OAAO,KAAK,CAAC;YAC1B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,IAAI,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;gBAAE,OAAO,IAAI,CAAC;YAC/C,iFAAiF;YACjF,IAAI,MAAM,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC;gBACrC,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC;gBACtC,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;oBACtB,QAAQ,CAAC,MAAM,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;oBAC1C,OAAO,IAAI,CAAC;gBACd,CAAC;gBACD,IAAI,MAAM,IAAI,MAAM,KAAK,QAAQ,CAAC,IAAI,EAAE,CAAC;oBACvC,8DAA8D;oBAC9D,aAAa,CAAC,MAAM,CAAC,CAAC;oBACtB,OAAO,IAAI,CAAC;gBACd,CAAC;YACH,CAAC;YACD,4FAA4F;YAC5F,cAAc,CAAC,GAAG,EAAE;gBAClB,6EAA6E;gBAC7E,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;oBAAE,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;YACrE,CAAC,CAAC,CAAC;YACH,OAAO,IAAI,CAAC;QACd,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC;AACxF,CAAC","sourcesContent":["/*\n * <govuk-react-auto-focus>: a headless, light-DOM custom element that moves focus to its target\n * when it appears, such as an error summary or a success banner, with or without React. It moves\n * focus once: give the element a new `key` in React to move it again, such as after each submit.\n *\n * The target is server-rendered with `tabindex=\"-1\"` and `autofocus`, so the browser focuses it\n * when the page loads, even without JavaScript. When the element connects later, such as when React\n * renders the target after a form is submitted, the element focuses it. As in govuk-frontend, the\n * target loses its `tabindex` when it loses focus. If the user has moved focus on before the script\n * runs, it leaves them there. Without JavaScript the target keeps its `tabindex`.\n *\n * Markup inside the element:\n * - `[data-auto-focus]`: the target. Without one, the element's first child.\n *\n * Attributes:\n * - `disabled`: don't move focus.\n */\n\nimport { dropAutoFocus, setFocus } from '../utils/set-focus.js';\nimport { setUpWhenReady } from '../utils/set-up-when-ready.js';\n\nexport const tagName = 'govuk-react-auto-focus';\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactAutoFocus extends HTMLElement {\n private focused = false;\n\n private observer?: MutationObserver;\n\n connectedCallback() {\n // The target may not be parsed or rendered yet.\n this.observer = setUpWhenReady(this, () => this.focusTarget());\n }\n\n disconnectedCallback() {\n this.observer?.disconnect();\n }\n\n private get target() {\n const target =\n Array.from(this.querySelectorAll('[data-auto-focus]')).find((found) => found.closest(tagName) === this) ??\n this.firstElementChild;\n return target instanceof HTMLElement ? target : undefined;\n }\n\n /** Focuses the target once, and says whether there was one to focus. */\n private focusTarget() {\n if (this.focused) return true;\n const { target } = this;\n if (!target) return false;\n this.focused = true;\n if (this.hasAttribute('disabled')) return true;\n // Rendered by the server with `autofocus`, which React never sets on the client.\n if (target.hasAttribute('autofocus')) {\n const active = document.activeElement;\n if (active === target) {\n setFocus(target, { preventScroll: true });\n return true;\n }\n if (active && active !== document.body) {\n // The user moved on before this script ran: leave them there.\n dropAutoFocus(target);\n return true;\n }\n }\n // React puts focus back where it was at the end of a commit, which can connect the element.\n queueMicrotask(() => {\n // Not in place yet, such as in a streamed boundary: try again when it moves.\n if (!target.isConnected || !setFocus(target)) this.focused = false;\n });\n return true;\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactAutoFocus);\n}\n"]}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { ComponentProps } from 'react';
|
|
3
|
-
|
|
4
|
-
type RootProps = React.HTMLAttributes<HTMLElement> & {
|
|
2
|
+
import type { ComponentProps } from 'react';
|
|
3
|
+
export type RootProps = React.HTMLAttributes<HTMLElement> & {
|
|
5
4
|
/** Don't move focus. */
|
|
6
5
|
disabled?: boolean;
|
|
7
6
|
};
|
|
8
|
-
declare function Root({ disabled, ...props }: RootProps): React.DOMElement<{
|
|
7
|
+
export declare function Root({ disabled, ...props }: RootProps): React.DOMElement<{
|
|
9
8
|
disabled: string;
|
|
10
9
|
defaultChecked?: boolean;
|
|
11
10
|
defaultValue?: string | number | readonly string[];
|
|
@@ -15,17 +14,17 @@ declare function Root({ disabled, ...props }: RootProps): React.DOMElement<{
|
|
|
15
14
|
autoCapitalize?: "off" | "none" | "on" | "sentences" | "words" | "characters" | (string & {});
|
|
16
15
|
autoFocus?: boolean;
|
|
17
16
|
className?: string;
|
|
18
|
-
contentEditable?: (boolean | "
|
|
17
|
+
contentEditable?: (boolean | "false" | "true") | "inherit" | "plaintext-only";
|
|
19
18
|
contextMenu?: string;
|
|
20
19
|
dir?: string;
|
|
21
|
-
draggable?: boolean | "
|
|
20
|
+
draggable?: boolean | "false" | "true";
|
|
22
21
|
enterKeyHint?: "search" | "enter" | "done" | "go" | "next" | "previous" | "send";
|
|
23
22
|
hidden?: boolean;
|
|
24
23
|
id?: string;
|
|
25
24
|
lang?: string;
|
|
26
25
|
nonce?: string;
|
|
27
26
|
slot?: string;
|
|
28
|
-
spellCheck?: boolean | "
|
|
27
|
+
spellCheck?: boolean | "false" | "true";
|
|
29
28
|
style?: React.CSSProperties;
|
|
30
29
|
tabIndex?: number;
|
|
31
30
|
title?: string;
|
|
@@ -58,57 +57,57 @@ declare function Root({ disabled, ...props }: RootProps): React.DOMElement<{
|
|
|
58
57
|
popoverTargetAction?: "toggle" | "show" | "hide";
|
|
59
58
|
popoverTarget?: string;
|
|
60
59
|
inert?: boolean;
|
|
61
|
-
inputMode?: "search" | "
|
|
60
|
+
inputMode?: "search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal";
|
|
62
61
|
is?: string;
|
|
63
62
|
exportparts?: string;
|
|
64
63
|
part?: string;
|
|
65
64
|
"aria-activedescendant"?: string;
|
|
66
|
-
"aria-atomic"?: boolean | "
|
|
65
|
+
"aria-atomic"?: boolean | "false" | "true";
|
|
67
66
|
"aria-autocomplete"?: "none" | "list" | "inline" | "both";
|
|
68
67
|
"aria-braillelabel"?: string;
|
|
69
68
|
"aria-brailleroledescription"?: string;
|
|
70
|
-
"aria-busy"?: boolean | "
|
|
71
|
-
"aria-checked"?: boolean | "
|
|
69
|
+
"aria-busy"?: boolean | "false" | "true";
|
|
70
|
+
"aria-checked"?: boolean | "false" | "true" | "mixed";
|
|
72
71
|
"aria-colcount"?: number;
|
|
73
72
|
"aria-colindex"?: number;
|
|
74
73
|
"aria-colindextext"?: string;
|
|
75
74
|
"aria-colspan"?: number;
|
|
76
75
|
"aria-controls"?: string;
|
|
77
|
-
"aria-current"?: boolean | "time" | "
|
|
76
|
+
"aria-current"?: boolean | "time" | "false" | "true" | "page" | "step" | "location" | "date";
|
|
78
77
|
"aria-describedby"?: string;
|
|
79
78
|
"aria-description"?: string;
|
|
80
79
|
"aria-details"?: string;
|
|
81
|
-
"aria-disabled"?: boolean | "
|
|
82
|
-
"aria-dropeffect"?: "link" | "
|
|
80
|
+
"aria-disabled"?: boolean | "false" | "true";
|
|
81
|
+
"aria-dropeffect"?: "link" | "copy" | "none" | "execute" | "move" | "popup";
|
|
83
82
|
"aria-errormessage"?: string;
|
|
84
|
-
"aria-expanded"?: boolean | "
|
|
83
|
+
"aria-expanded"?: boolean | "false" | "true";
|
|
85
84
|
"aria-flowto"?: string;
|
|
86
|
-
"aria-grabbed"?: boolean | "
|
|
87
|
-
"aria-haspopup"?: boolean | "dialog" | "menu" | "
|
|
88
|
-
"aria-hidden"?: boolean | "
|
|
89
|
-
"aria-invalid"?: boolean | "
|
|
85
|
+
"aria-grabbed"?: boolean | "false" | "true";
|
|
86
|
+
"aria-haspopup"?: boolean | "dialog" | "menu" | "false" | "true" | "grid" | "listbox" | "tree";
|
|
87
|
+
"aria-hidden"?: boolean | "false" | "true";
|
|
88
|
+
"aria-invalid"?: boolean | "false" | "true" | "grammar" | "spelling";
|
|
90
89
|
"aria-keyshortcuts"?: string;
|
|
91
90
|
"aria-label"?: string;
|
|
92
91
|
"aria-labelledby"?: string;
|
|
93
92
|
"aria-level"?: number;
|
|
94
93
|
"aria-live"?: "off" | "assertive" | "polite";
|
|
95
|
-
"aria-modal"?: boolean | "
|
|
96
|
-
"aria-multiline"?: boolean | "
|
|
97
|
-
"aria-multiselectable"?: boolean | "
|
|
94
|
+
"aria-modal"?: boolean | "false" | "true";
|
|
95
|
+
"aria-multiline"?: boolean | "false" | "true";
|
|
96
|
+
"aria-multiselectable"?: boolean | "false" | "true";
|
|
98
97
|
"aria-orientation"?: "horizontal" | "vertical";
|
|
99
98
|
"aria-owns"?: string;
|
|
100
99
|
"aria-placeholder"?: string;
|
|
101
100
|
"aria-posinset"?: number;
|
|
102
|
-
"aria-pressed"?: boolean | "
|
|
103
|
-
"aria-readonly"?: boolean | "
|
|
101
|
+
"aria-pressed"?: boolean | "false" | "true" | "mixed";
|
|
102
|
+
"aria-readonly"?: boolean | "false" | "true";
|
|
104
103
|
"aria-relevant"?: "text" | "additions" | "additions removals" | "additions text" | "all" | "removals" | "removals additions" | "removals text" | "text additions" | "text removals";
|
|
105
|
-
"aria-required"?: boolean | "
|
|
104
|
+
"aria-required"?: boolean | "false" | "true";
|
|
106
105
|
"aria-roledescription"?: string;
|
|
107
106
|
"aria-rowcount"?: number;
|
|
108
107
|
"aria-rowindex"?: number;
|
|
109
108
|
"aria-rowindextext"?: string;
|
|
110
109
|
"aria-rowspan"?: number;
|
|
111
|
-
"aria-selected"?: boolean | "
|
|
110
|
+
"aria-selected"?: boolean | "false" | "true";
|
|
112
111
|
"aria-setsize"?: number;
|
|
113
112
|
"aria-sort"?: "none" | "ascending" | "descending" | "other";
|
|
114
113
|
"aria-valuemax"?: number;
|
|
@@ -288,23 +287,11 @@ declare function Root({ disabled, ...props }: RootProps): React.DOMElement<{
|
|
|
288
287
|
onTransitionStart?: React.TransitionEventHandler<HTMLElement>;
|
|
289
288
|
onTransitionStartCapture?: React.TransitionEventHandler<HTMLElement>;
|
|
290
289
|
}, Element>;
|
|
291
|
-
type TargetProps = ComponentProps<'div'> & {
|
|
290
|
+
export type TargetProps = ComponentProps<'div'> & {
|
|
292
291
|
/** Render the only child instead, with this part's props merged onto it. */
|
|
293
292
|
asChild?: boolean;
|
|
294
293
|
/** Don't move focus. Pass the same as to `Root`. */
|
|
295
294
|
disabled?: boolean;
|
|
296
295
|
};
|
|
297
296
|
/** What gets focus. */
|
|
298
|
-
declare function Target({ asChild, disabled, ...props }: TargetProps): React.JSX.Element;
|
|
299
|
-
|
|
300
|
-
declare const index_Root: typeof Root;
|
|
301
|
-
type index_RootProps = RootProps;
|
|
302
|
-
declare const index_Target: typeof Target;
|
|
303
|
-
type index_TargetProps = TargetProps;
|
|
304
|
-
declare namespace index {
|
|
305
|
-
export { index_Root as Root, index_Target as Target };
|
|
306
|
-
export type { index_RootProps as RootProps, index_TargetProps as TargetProps };
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export { Root as R, Target as T, index as i };
|
|
310
|
-
export type { RootProps as a, TargetProps as b };
|
|
297
|
+
export declare function Target({ asChild, disabled, ...props }: TargetProps): React.JSX.Element;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { isValidElement } from 'react';
|
|
4
|
+
import { Slot } from '../utils/slot.js';
|
|
5
|
+
import { tagName } from './element.js';
|
|
6
|
+
export function Root({ disabled, ...props }) {
|
|
7
|
+
return React.createElement(tagName, { ...props, disabled: disabled ? '' : undefined });
|
|
8
|
+
}
|
|
9
|
+
/** The tabindex of the target, or with `asChild`, of its child. */
|
|
10
|
+
function tabIndexOf({ asChild, tabIndex, children }) {
|
|
11
|
+
if (tabIndex !== undefined || !asChild)
|
|
12
|
+
return tabIndex;
|
|
13
|
+
return isValidElement(children) ? children.props.tabIndex : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** What gets focus. */
|
|
16
|
+
export function Target({ asChild, disabled, ...props }) {
|
|
17
|
+
const Element = asChild ? Slot : 'div';
|
|
18
|
+
let focus = {};
|
|
19
|
+
if (!disabled) {
|
|
20
|
+
// `data-focus-tabindex` says the tabindex is only for this, so it goes when focus moves on.
|
|
21
|
+
focus =
|
|
22
|
+
tabIndexOf({ asChild, ...props }) === undefined
|
|
23
|
+
? { tabIndex: -1, autoFocus: true, 'data-focus-tabindex': '' }
|
|
24
|
+
: { autoFocus: true };
|
|
25
|
+
}
|
|
26
|
+
// The element owns `tabindex` and `autofocus` after it has moved focus.
|
|
27
|
+
return React.createElement(Element, { "data-auto-focus": "", suppressHydrationWarning: true, ...focus, ...props });
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auto-focus/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAEvC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAavC,MAAM,UAAU,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAa;IACpD,OAAO,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC;AACzF,CAAC;AASD,mEAAmE;AACnE,SAAS,UAAU,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAe;IAC9D,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,OAAO;QAAE,OAAO,QAAQ,CAAC;IACxD,OAAO,cAAc,CAAwB,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/F,CAAC;AAED,uBAAuB;AACvB,MAAM,UAAU,MAAM,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAe;IACjE,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACvC,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,4FAA4F;QAC5F,KAAK;YACH,UAAU,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC,KAAK,SAAS;gBAC7C,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,qBAAqB,EAAE,EAAE,EAAE;gBAC9D,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC5B,CAAC;IACD,wEAAwE;IACxE,OAAO,oBAAC,OAAO,uBAAiB,EAAE,EAAC,wBAAwB,WAAK,KAAK,KAAM,KAAK,GAAI,CAAC;AACvF,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { isValidElement } from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName } from './element.js';\n\n/*\n * Headless auto focus: the <govuk-react-auto-focus> element (./element.ts) moves focus to its\n * `Target` when it appears. The server renders the target with `autofocus`, so the browser focuses\n * it when the page loads, even without JavaScript.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** Don't move focus. */\n disabled?: boolean;\n};\n\nexport function Root({ disabled, ...props }: RootProps) {\n return React.createElement(tagName, { ...props, disabled: disabled ? '' : undefined });\n}\n\nexport type TargetProps = ComponentProps<'div'> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n /** Don't move focus. Pass the same as to `Root`. */\n disabled?: boolean;\n};\n\n/** The tabindex of the target, or with `asChild`, of its child. */\nfunction tabIndexOf({ asChild, tabIndex, children }: TargetProps) {\n if (tabIndex !== undefined || !asChild) return tabIndex;\n return isValidElement<{ tabIndex?: number }>(children) ? children.props.tabIndex : undefined;\n}\n\n/** What gets focus. */\nexport function Target({ asChild, disabled, ...props }: TargetProps) {\n const Element = asChild ? Slot : 'div';\n let focus = {};\n if (!disabled) {\n // `data-focus-tabindex` says the tabindex is only for this, so it goes when focus moves on.\n focus =\n tabIndexOf({ asChild, ...props }) === undefined\n ? { tabIndex: -1, autoFocus: true, 'data-focus-tabindex': '' }\n : { autoFocus: true };\n }\n // The element owns `tabindex` and `autofocus` after it has moved focus.\n return <Element data-auto-focus=\"\" suppressHydrationWarning {...focus} {...props} />;\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*! Based on accessible-autocomplete, Copyright (c) 2017 Crown Copyright (Government Digital Service), MIT licence: licences/accessible-autocomplete.txt */
|
|
2
|
-
declare const tagName = "govuk-react-autocomplete";
|
|
3
|
-
type Filter = 'contains' | 'starts-with' | 'none';
|
|
4
|
-
interface AutocompleteElement extends HTMLElement {
|
|
2
|
+
export declare const tagName = "govuk-react-autocomplete";
|
|
3
|
+
export type Filter = 'contains' | 'starts-with' | 'none';
|
|
4
|
+
export interface AutocompleteElement extends HTMLElement {
|
|
5
5
|
/** The query, once upgraded. */
|
|
6
6
|
readonly query: string | undefined;
|
|
7
7
|
}
|
|
@@ -16,7 +16,7 @@ declare global {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
/** accessible-autocomplete's English text. */
|
|
19
|
-
declare const defaults: {
|
|
19
|
+
export declare const defaults: {
|
|
20
20
|
'status-query-too-short': string;
|
|
21
21
|
'status-no-results': string;
|
|
22
22
|
'status-selected-option': string;
|
|
@@ -25,6 +25,3 @@ declare const defaults: {
|
|
|
25
25
|
other: string;
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
|
-
|
|
29
|
-
export { defaults, tagName };
|
|
30
|
-
export type { AutocompleteElement, Filter };
|