@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.
Files changed (258) hide show
  1. package/dist/{accordion-element.d.ts → accordion/element.d.ts} +2 -5
  2. package/dist/accordion/element.js +302 -0
  3. package/dist/accordion/element.js.map +1 -0
  4. package/dist/accordion/index.d.ts +74 -0
  5. package/dist/accordion/index.js +138 -0
  6. package/dist/accordion/index.js.map +1 -0
  7. package/dist/auto-focus/element.d.ts +1 -0
  8. package/dist/auto-focus/element.js +75 -0
  9. package/dist/auto-focus/element.js.map +1 -0
  10. package/dist/{index-Bul0NJh_.d.ts → auto-focus/index.d.ts} +27 -40
  11. package/dist/auto-focus/index.js +29 -0
  12. package/dist/{chunk-6ZFU4VYW.js.map → auto-focus/index.js.map} +1 -1
  13. package/dist/{autocomplete-element.d.ts → autocomplete/element.d.ts} +4 -7
  14. package/dist/autocomplete/element.js +597 -0
  15. package/dist/autocomplete/element.js.map +1 -0
  16. package/dist/{index-D7lMYMUR.d.ts → autocomplete/index.d.ts} +34 -57
  17. package/dist/autocomplete/index.js +108 -0
  18. package/dist/autocomplete/index.js.map +1 -0
  19. package/dist/button/element.d.ts +1 -0
  20. package/dist/button/element.js +59 -0
  21. package/dist/button/element.js.map +1 -0
  22. package/dist/button/index.d.ts +10 -0
  23. package/dist/button/index.js +9 -0
  24. package/dist/button/index.js.map +1 -0
  25. package/dist/{character-count-element.d.ts → character-count/element.d.ts} +2 -5
  26. package/dist/character-count/element.js +287 -0
  27. package/dist/character-count/element.js.map +1 -0
  28. package/dist/character-count/index.d.ts +57 -0
  29. package/dist/character-count/index.js +79 -0
  30. package/dist/character-count/index.js.map +1 -0
  31. package/dist/character-count/messages.d.ts +20 -0
  32. package/dist/character-count/messages.js +54 -0
  33. package/dist/character-count/messages.js.map +1 -0
  34. package/dist/choices/element.d.ts +5 -0
  35. package/dist/choices/element.js +202 -0
  36. package/dist/choices/element.js.map +1 -0
  37. package/dist/{index-WuGvkTxu.d.ts → choices/index.d.ts} +30 -44
  38. package/dist/choices/index.js +37 -0
  39. package/dist/choices/index.js.map +1 -0
  40. package/dist/{disclosure-element.d.ts → disclosure/element.d.ts} +2 -5
  41. package/dist/disclosure/element.js +123 -0
  42. package/dist/disclosure/element.js.map +1 -0
  43. package/dist/{index-D4Gzyvfu.d.ts → disclosure/index.d.ts} +40 -54
  44. package/dist/disclosure/index.js +66 -0
  45. package/dist/disclosure/index.js.map +1 -0
  46. package/dist/exit-this-page/element.d.ts +19 -0
  47. package/dist/exit-this-page/element.js +201 -0
  48. package/dist/exit-this-page/element.js.map +1 -0
  49. package/dist/{index-qF0jIQCW.d.ts → exit-this-page/index.d.ts} +36 -60
  50. package/dist/exit-this-page/index.js +54 -0
  51. package/dist/exit-this-page/index.js.map +1 -0
  52. package/dist/{file-upload-element.d.ts → file-upload/element.d.ts} +5 -9
  53. package/dist/file-upload/element.js +270 -0
  54. package/dist/file-upload/element.js.map +1 -0
  55. package/dist/file-upload/index.d.ts +46 -0
  56. package/dist/file-upload/index.js +65 -0
  57. package/dist/file-upload/index.js.map +1 -0
  58. package/dist/focus-links/element.d.ts +1 -0
  59. package/dist/focus-links/element.js +90 -0
  60. package/dist/focus-links/element.js.map +1 -0
  61. package/dist/{index-BR3MAGF8.d.ts → focus-links/index.d.ts} +24 -35
  62. package/dist/focus-links/index.js +7 -0
  63. package/dist/focus-links/index.js.map +1 -0
  64. package/dist/index.d.ts +13 -17
  65. package/dist/index.js +13 -72
  66. package/dist/index.js.map +1 -1
  67. package/dist/inline/accordion-element.js +1 -1
  68. package/dist/inline/auto-focus-element.js +1 -1
  69. package/dist/inline/autocomplete-element.js +1 -1
  70. package/dist/inline/character-count-element.js +1 -1
  71. package/dist/inline/choices-element.js +1 -1
  72. package/dist/inline/disclosure-element.js +1 -1
  73. package/dist/inline/exit-this-page-element.js +1 -1
  74. package/dist/inline/file-upload-element.js +1 -1
  75. package/dist/inline/focus-links-element.js +1 -1
  76. package/dist/inline/modal-element.js +1 -1
  77. package/dist/inline/password-input-element.js +1 -1
  78. package/dist/inline/tabs-element.js +1 -1
  79. package/dist/inline-script/index.d.ts +26 -0
  80. package/dist/inline-script/index.js +31 -0
  81. package/dist/inline-script/index.js.map +1 -0
  82. package/dist/{modal-element.d.ts → modal/element.d.ts} +2 -4
  83. package/dist/modal/element.js +114 -0
  84. package/dist/modal/element.js.map +1 -0
  85. package/dist/modal/index.d.ts +36 -0
  86. package/dist/modal/index.js +109 -0
  87. package/dist/modal/index.js.map +1 -0
  88. package/dist/password-input/element.d.ts +1 -0
  89. package/dist/password-input/element.js +134 -0
  90. package/dist/password-input/element.js.map +1 -0
  91. package/dist/{index-sEHnx4-M.d.ts → password-input/index.d.ts} +32 -48
  92. package/dist/password-input/index.js +35 -0
  93. package/dist/password-input/index.js.map +1 -0
  94. package/dist/{tabs-element.d.ts → tabs/element.d.ts} +2 -5
  95. package/dist/tabs/element.js +416 -0
  96. package/dist/tabs/element.js.map +1 -0
  97. package/dist/tabs/index.d.ts +83 -0
  98. package/dist/tabs/index.js +138 -0
  99. package/dist/tabs/index.js.map +1 -0
  100. package/dist/utils/dom.d.ts +9 -0
  101. package/dist/utils/dom.js +27 -0
  102. package/dist/utils/dom.js.map +1 -0
  103. package/dist/utils/on-property-set.d.ts +9 -0
  104. package/dist/utils/on-property-set.js +38 -0
  105. package/dist/utils/on-property-set.js.map +1 -0
  106. package/dist/utils/set-focus.d.ts +11 -0
  107. package/dist/utils/set-focus.js +38 -0
  108. package/dist/utils/set-focus.js.map +1 -0
  109. package/dist/utils/set-up-when-ready.d.ts +6 -0
  110. package/dist/utils/set-up-when-ready.js +16 -0
  111. package/dist/utils/set-up-when-ready.js.map +1 -0
  112. package/dist/utils/slot.d.ts +13 -0
  113. package/dist/utils/slot.js +40 -0
  114. package/dist/utils/slot.js.map +1 -0
  115. package/dist/utils/translation.d.ts +13 -0
  116. package/dist/utils/translation.js +57 -0
  117. package/dist/utils/translation.js.map +1 -0
  118. package/package.json +11 -4
  119. package/dist/accordion-element.js +0 -9
  120. package/dist/accordion-element.js.map +0 -1
  121. package/dist/accordion.d.ts +0 -2
  122. package/dist/accordion.js +0 -34
  123. package/dist/accordion.js.map +0 -1
  124. package/dist/auto-focus-element.d.ts +0 -3
  125. package/dist/auto-focus-element.js +0 -10
  126. package/dist/auto-focus-element.js.map +0 -1
  127. package/dist/auto-focus.d.ts +0 -2
  128. package/dist/auto-focus.js +0 -15
  129. package/dist/auto-focus.js.map +0 -1
  130. package/dist/autocomplete-element.js +0 -12
  131. package/dist/autocomplete-element.js.map +0 -1
  132. package/dist/autocomplete.d.ts +0 -4
  133. package/dist/autocomplete.js +0 -25
  134. package/dist/autocomplete.js.map +0 -1
  135. package/dist/button-element.d.ts +0 -2
  136. package/dist/button-element.js +0 -3
  137. package/dist/button-element.js.map +0 -1
  138. package/dist/button.d.ts +0 -2
  139. package/dist/button.js +0 -12
  140. package/dist/button.js.map +0 -1
  141. package/dist/character-count-element.js +0 -10
  142. package/dist/character-count-element.js.map +0 -1
  143. package/dist/character-count.d.ts +0 -3
  144. package/dist/character-count.js +0 -26
  145. package/dist/character-count.js.map +0 -1
  146. package/dist/choices-element.d.ts +0 -8
  147. package/dist/choices-element.js +0 -9
  148. package/dist/choices-element.js.map +0 -1
  149. package/dist/choices.d.ts +0 -2
  150. package/dist/choices.js +0 -16
  151. package/dist/choices.js.map +0 -1
  152. package/dist/chunk-2IHAQAYF.js +0 -23
  153. package/dist/chunk-2IHAQAYF.js.map +0 -1
  154. package/dist/chunk-2ZJVSNC2.js +0 -31
  155. package/dist/chunk-2ZJVSNC2.js.map +0 -1
  156. package/dist/chunk-47NMY4SS.js +0 -108
  157. package/dist/chunk-47NMY4SS.js.map +0 -1
  158. package/dist/chunk-4XAR4FFG.js +0 -83
  159. package/dist/chunk-4XAR4FFG.js.map +0 -1
  160. package/dist/chunk-5HWRMQK2.js +0 -91
  161. package/dist/chunk-5HWRMQK2.js.map +0 -1
  162. package/dist/chunk-6ZFU4VYW.js +0 -41
  163. package/dist/chunk-B2I4J5NL.js +0 -264
  164. package/dist/chunk-B2I4J5NL.js.map +0 -1
  165. package/dist/chunk-DPERPCUH.js +0 -188
  166. package/dist/chunk-DPERPCUH.js.map +0 -1
  167. package/dist/chunk-ECYGKE4D.js +0 -168
  168. package/dist/chunk-ECYGKE4D.js.map +0 -1
  169. package/dist/chunk-ESBMTI25.js +0 -11
  170. package/dist/chunk-ESBMTI25.js.map +0 -1
  171. package/dist/chunk-ET7DCQM7.js +0 -133
  172. package/dist/chunk-ET7DCQM7.js.map +0 -1
  173. package/dist/chunk-F5XFC4QJ.js +0 -177
  174. package/dist/chunk-F5XFC4QJ.js.map +0 -1
  175. package/dist/chunk-GVGBDSDN.js +0 -78
  176. package/dist/chunk-GVGBDSDN.js.map +0 -1
  177. package/dist/chunk-IZQ2C4BC.js +0 -351
  178. package/dist/chunk-IZQ2C4BC.js.map +0 -1
  179. package/dist/chunk-L7E6GN2D.js +0 -87
  180. package/dist/chunk-L7E6GN2D.js.map +0 -1
  181. package/dist/chunk-LWIA644X.js +0 -34
  182. package/dist/chunk-LWIA644X.js.map +0 -1
  183. package/dist/chunk-NX4B3NKM.js +0 -35
  184. package/dist/chunk-NX4B3NKM.js.map +0 -1
  185. package/dist/chunk-PNWPGD7B.js +0 -68
  186. package/dist/chunk-PNWPGD7B.js.map +0 -1
  187. package/dist/chunk-QLQ6O7OW.js +0 -111
  188. package/dist/chunk-QLQ6O7OW.js.map +0 -1
  189. package/dist/chunk-RDUPELOK.js +0 -55
  190. package/dist/chunk-RDUPELOK.js.map +0 -1
  191. package/dist/chunk-RPMHI67T.js +0 -104
  192. package/dist/chunk-RPMHI67T.js.map +0 -1
  193. package/dist/chunk-TE4RUYVK.js +0 -97
  194. package/dist/chunk-TE4RUYVK.js.map +0 -1
  195. package/dist/chunk-TJVBFRIE.js +0 -60
  196. package/dist/chunk-TJVBFRIE.js.map +0 -1
  197. package/dist/chunk-U5OKNKYC.js +0 -510
  198. package/dist/chunk-U5OKNKYC.js.map +0 -1
  199. package/dist/chunk-UXJO4AJP.js +0 -290
  200. package/dist/chunk-UXJO4AJP.js.map +0 -1
  201. package/dist/chunk-VNXXENCU.js +0 -39
  202. package/dist/chunk-VNXXENCU.js.map +0 -1
  203. package/dist/chunk-XGK73UCD.js +0 -62
  204. package/dist/chunk-XGK73UCD.js.map +0 -1
  205. package/dist/chunk-XJYLFTYE.js +0 -157
  206. package/dist/chunk-XJYLFTYE.js.map +0 -1
  207. package/dist/chunk-YCESNTXQ.js +0 -241
  208. package/dist/chunk-YCESNTXQ.js.map +0 -1
  209. package/dist/chunk-ZTJLVNR4.js +0 -62
  210. package/dist/chunk-ZTJLVNR4.js.map +0 -1
  211. package/dist/disclosure-element.js +0 -9
  212. package/dist/disclosure-element.js.map +0 -1
  213. package/dist/disclosure.d.ts +0 -2
  214. package/dist/disclosure.js +0 -16
  215. package/dist/disclosure.js.map +0 -1
  216. package/dist/exit-this-page-element.d.ts +0 -22
  217. package/dist/exit-this-page-element.js +0 -13
  218. package/dist/exit-this-page-element.js.map +0 -1
  219. package/dist/exit-this-page.d.ts +0 -3
  220. package/dist/exit-this-page.js +0 -25
  221. package/dist/exit-this-page.js.map +0 -1
  222. package/dist/file-upload-element.js +0 -14
  223. package/dist/file-upload-element.js.map +0 -1
  224. package/dist/file-upload.d.ts +0 -3
  225. package/dist/file-upload.js +0 -25
  226. package/dist/file-upload.js.map +0 -1
  227. package/dist/focus-links-element.d.ts +0 -3
  228. package/dist/focus-links-element.js +0 -10
  229. package/dist/focus-links-element.js.map +0 -1
  230. package/dist/focus-links.d.ts +0 -2
  231. package/dist/focus-links.js +0 -12
  232. package/dist/focus-links.js.map +0 -1
  233. package/dist/index--FxJgSOS.d.ts +0 -108
  234. package/dist/index-CikOyFud.d.ts +0 -54
  235. package/dist/index-D8N5vxTL.d.ts +0 -90
  236. package/dist/index-DMWpkC_d.d.ts +0 -105
  237. package/dist/index-DN7fGMc3.d.ts +0 -20
  238. package/dist/index-DPXR1Yua.d.ts +0 -69
  239. package/dist/inline-script.d.ts +0 -29
  240. package/dist/inline-script.js +0 -63
  241. package/dist/inline-script.js.map +0 -1
  242. package/dist/modal-element.js +0 -11
  243. package/dist/modal-element.js.map +0 -1
  244. package/dist/modal.d.ts +0 -2
  245. package/dist/modal.js +0 -20
  246. package/dist/modal.js.map +0 -1
  247. package/dist/password-input-element.d.ts +0 -3
  248. package/dist/password-input-element.js +0 -9
  249. package/dist/password-input-element.js.map +0 -1
  250. package/dist/password-input.d.ts +0 -2
  251. package/dist/password-input.js +0 -18
  252. package/dist/password-input.js.map +0 -1
  253. package/dist/tabs-element.js +0 -9
  254. package/dist/tabs-element.js.map +0 -1
  255. package/dist/tabs.d.ts +0 -2
  256. package/dist/tabs.js +0 -28
  257. package/dist/tabs.js.map +0 -1
  258. package/dist/translation-BnpO2mN2.d.ts +0 -4
@@ -0,0 +1,114 @@
1
+ /*
2
+ * <govuk-react-modal>: a headless, light-DOM custom element around a modal `dialog` and the
3
+ * buttons that open and close it.
4
+ *
5
+ * The buttons use invoker commands (`command="show-modal"` or `command="close"`, with
6
+ * `commandfor` naming the dialog), so a browser that supports them opens a true modal dialog
7
+ * with no JavaScript: the page behind is inert, focus moves into the dialog and Escape closes
8
+ * it. The `dialog` element is the state.
9
+ *
10
+ * The element only adds what HTML can't do on its own:
11
+ * - In a browser without invoker commands, it runs the commands of its own buttons.
12
+ * - `open` (when present) controls the dialog: "true" opens it with `showModal()`, anything
13
+ * else closes it. Changing `open` fires no change event.
14
+ * - It fires a bubbling `govuk-react-modal:change` event with `{ open }` when the dialog opens
15
+ * or closes, for code that doesn't use React.
16
+ *
17
+ * Markup inside the element:
18
+ * - a `dialog` with an id
19
+ * - `button`s with `commandfor` set to the dialog's id, and `command` set to "show-modal",
20
+ * "close" or "request-close"
21
+ */
22
+ import { setUpWhenReady } from '../utils/set-up-when-ready.js';
23
+ export const tagName = 'govuk-react-modal';
24
+ /** Whether the browser runs invoker commands itself. */
25
+ export const supportsCommands = () => typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype;
26
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
27
+ class GovukReactModal extends HTMLElement {
28
+ constructor() {
29
+ super(...arguments);
30
+ this.wasOpen = false;
31
+ // Runs the commands of the element's own buttons when the browser can't.
32
+ this.onClick = (event) => {
33
+ var _a;
34
+ const button = event.target instanceof Element ? event.target.closest('button[commandfor]') : null;
35
+ if (!(button instanceof HTMLButtonElement) || button.closest(tagName) !== this)
36
+ return;
37
+ const dialog = document.getElementById((_a = button.getAttribute('commandfor')) !== null && _a !== void 0 ? _a : '');
38
+ if (!(dialog instanceof HTMLDialogElement))
39
+ return;
40
+ const command = button.getAttribute('command');
41
+ if (command === 'show-modal' && !dialog.open)
42
+ dialog.showModal();
43
+ else if (command === 'close' && dialog.open)
44
+ dialog.close();
45
+ else if (command === 'request-close' && dialog.open) {
46
+ // Newer than this package's DOM types.
47
+ const requestClose = Reflect.get(dialog, 'requestClose');
48
+ if (typeof requestClose === 'function')
49
+ requestClose.call(dialog);
50
+ else
51
+ dialog.close();
52
+ }
53
+ };
54
+ this.onToggle = () => {
55
+ const { dialog } = this;
56
+ if (!dialog || dialog.open === this.wasOpen)
57
+ return;
58
+ this.wasOpen = dialog.open;
59
+ this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open: dialog.open }, bubbles: true }));
60
+ };
61
+ }
62
+ static get observedAttributes() {
63
+ return ['open'];
64
+ }
65
+ connectedCallback() {
66
+ if (!supportsCommands())
67
+ this.addEventListener('click', this.onClick);
68
+ this.observer = setUpWhenReady(this, () => this.setup());
69
+ }
70
+ disconnectedCallback() {
71
+ var _a, _b, _c;
72
+ this.removeEventListener('click', this.onClick);
73
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
74
+ (_b = this.dialog) === null || _b === void 0 ? void 0 : _b.removeEventListener('close', this.onToggle);
75
+ (_c = this.dialog) === null || _c === void 0 ? void 0 : _c.removeEventListener('toggle', this.onToggle);
76
+ this.dialog = undefined;
77
+ }
78
+ attributeChangedCallback() {
79
+ this.sync();
80
+ }
81
+ setup() {
82
+ if (this.dialog)
83
+ return true;
84
+ const dialog = Array.from(this.querySelectorAll('dialog')).find((found) => found.closest(tagName) === this);
85
+ if (!dialog)
86
+ return false;
87
+ this.dialog = dialog;
88
+ this.wasOpen = dialog.open;
89
+ // `toggle` fires on open and close where supported, and `close` everywhere.
90
+ dialog.addEventListener('toggle', this.onToggle);
91
+ dialog.addEventListener('close', this.onToggle);
92
+ this.sync();
93
+ return true;
94
+ }
95
+ /** Opens or closes the dialog to match `open`, without reporting it. */
96
+ sync() {
97
+ const { dialog } = this;
98
+ const controlled = this.getAttribute('open');
99
+ if (!dialog || controlled === null)
100
+ return;
101
+ const open = controlled === 'true';
102
+ if (open === dialog.open)
103
+ return;
104
+ this.wasOpen = open;
105
+ if (open && dialog.isConnected)
106
+ dialog.showModal();
107
+ else if (!open)
108
+ dialog.close();
109
+ }
110
+ }
111
+ if (!customElements.get(tagName))
112
+ customElements.define(tagName, GovukReactModal);
113
+ }
114
+ //# sourceMappingURL=element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/modal/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAQ/D,MAAM,CAAC,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAE3C,wDAAwD;AACxD,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE,CACnC,OAAO,iBAAiB,KAAK,WAAW,IAAI,SAAS,IAAI,iBAAiB,CAAC,SAAS,CAAC;AAEvF,IAAI,OAAO,WAAW,KAAK,WAAW,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE,CAAC;IAChF,MAAM,eAAgB,SAAQ,WAAW;Q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and close it.\n *\n * The buttons use invoker commands (`command=\"show-modal\"` or `command=\"close\"`, with\n * `commandfor` naming the dialog), so a browser that supports them opens a true modal dialog\n * with no JavaScript: the page behind is inert, focus moves into the dialog and Escape closes\n * it. The `dialog` element is the state.\n *\n * The element only adds what HTML can't do on its own:\n * - In a browser without invoker commands, it runs the commands of its own buttons.\n * - `open` (when present) controls the dialog: \"true\" opens it with `showModal()`, anything\n * else closes it. Changing `open` fires no change event.\n * - It fires a bubbling `govuk-react-modal:change` event with `{ open }` when the dialog opens\n * or closes, for code that doesn't use React.\n *\n * Markup inside the element:\n * - a `dialog` with an id\n * - `button`s with `commandfor` set to the dialog's id, and `command` set to \"show-modal\",\n * \"close\" or \"request-close\"\n */\n\nimport { setUpWhenReady } from '../utils/set-up-when-ready.js';\n\ndeclare global {\n interface HTMLElementEventMap {\n 'govuk-react-modal:change': CustomEvent<{ open: boolean }>;\n }\n}\n\nexport const tagName = 'govuk-react-modal';\n\n/** Whether the browser runs invoker commands itself. */\nexport const supportsCommands = () =>\n typeof HTMLButtonElement !== 'undefined' && 'command' in HTMLButtonElement.prototype;\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactModal extends HTMLElement {\n static get observedAttributes() {\n return ['open'];\n }\n\n private dialog?: HTMLDialogElement;\n\n private observer?: MutationObserver;\n\n private wasOpen = false;\n\n // Runs the commands of the element's own buttons when the browser can't.\n private readonly onClick = (event: MouseEvent) => {\n const button = event.target instanceof Element ? event.target.closest('button[commandfor]') : null;\n if (!(button instanceof HTMLButtonElement) || button.closest(tagName) !== this) return;\n const dialog = document.getElementById(button.getAttribute('commandfor') ?? '');\n if (!(dialog instanceof HTMLDialogElement)) return;\n const command = button.getAttribute('command');\n if (command === 'show-modal' && !dialog.open) dialog.showModal();\n else if (command === 'close' && dialog.open) dialog.close();\n else if (command === 'request-close' && dialog.open) {\n // Newer than this package's DOM types.\n const requestClose: unknown = Reflect.get(dialog, 'requestClose');\n if (typeof requestClose === 'function') requestClose.call(dialog);\n else dialog.close();\n }\n };\n\n private readonly onToggle = () => {\n const { dialog } = this;\n if (!dialog || dialog.open === this.wasOpen) return;\n this.wasOpen = dialog.open;\n this.dispatchEvent(new CustomEvent('govuk-react-modal:change', { detail: { open: dialog.open }, bubbles: true }));\n };\n\n connectedCallback() {\n if (!supportsCommands()) this.addEventListener('click', this.onClick);\n this.observer = setUpWhenReady(this, () => this.setup());\n }\n\n disconnectedCallback() {\n this.removeEventListener('click', this.onClick);\n this.observer?.disconnect();\n this.dialog?.removeEventListener('close', this.onToggle);\n this.dialog?.removeEventListener('toggle', this.onToggle);\n this.dialog = undefined;\n }\n\n attributeChangedCallback() {\n this.sync();\n }\n\n private setup() {\n if (this.dialog) return true;\n const dialog = Array.from(this.querySelectorAll('dialog')).find((found) => found.closest(tagName) === this);\n if (!dialog) return false;\n this.dialog = dialog;\n this.wasOpen = dialog.open;\n // `toggle` fires on open and close where supported, and `close` everywhere.\n dialog.addEventListener('toggle', this.onToggle);\n dialog.addEventListener('close', this.onToggle);\n this.sync();\n return true;\n }\n\n /** Opens or closes the dialog to match `open`, without reporting it. */\n private sync() {\n const { dialog } = this;\n const controlled = this.getAttribute('open');\n if (!dialog || controlled === null) return;\n const open = controlled === 'true';\n if (open === dialog.open) return;\n this.wasOpen = open;\n if (open && dialog.isConnected) dialog.showModal();\n else if (!open) dialog.close();\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactModal);\n}\n"]}
@@ -0,0 +1,36 @@
1
+ import * as React from 'react';
2
+ import type { ComponentProps, ReactNode } from 'react';
3
+ export type RootProps = Omit<React.HTMLAttributes<HTMLElement>, 'onChange'> & {
4
+ children?: ReactNode;
5
+ /** Whether the dialog is open, to control it. Without it, the buttons alone open and close it. */
6
+ open?: boolean;
7
+ /** Called when the dialog opens or closes. */
8
+ onOpenChange?: (open: boolean) => void;
9
+ /** The dialog's id, which the buttons' `commandfor` names. Defaults to a generated one. */
10
+ dialogId?: string;
11
+ };
12
+ export declare function Root({ children, open, onOpenChange, dialogId: dialogIdProp, ...props }: RootProps): React.JSX.Element;
13
+ type PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {
14
+ /** Render the only child instead, with this part's props merged onto it. */
15
+ asChild?: boolean;
16
+ };
17
+ type CommandProps = PartProps<'button'> & {
18
+ /** The dialog's id. Set by Root. */
19
+ dialogId?: string;
20
+ };
21
+ export type TriggerProps = CommandProps;
22
+ /** A button that opens the dialog as a modal. */
23
+ export declare function Trigger(props: TriggerProps): React.JSX.Element;
24
+ export type CloseProps = CommandProps;
25
+ /** A button that closes the dialog. */
26
+ export declare function Close(props: CloseProps): React.JSX.Element;
27
+ export type ContentProps = PartProps<'dialog'>;
28
+ /**
29
+ * The dialog. It's named by Title, unless you pass `aria-labelledby` or `aria-label`. Pass
30
+ * `closedby="any"` to also close it when the user clicks outside it.
31
+ */
32
+ export declare function Content({ asChild, id, onClose, ref, ...props }: ContentProps): React.JSX.Element;
33
+ export type TitleProps = PartProps<'h2'>;
34
+ /** The dialog's heading, which names it. */
35
+ export declare function Title({ asChild, ...props }: TitleProps): React.JSX.Element;
36
+ export {};
@@ -0,0 +1,109 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { createContext, useContext, useEffect, useId, useRef } from 'react';
4
+ import { Slot } from '../utils/slot.js';
5
+ import { tagName } from './element.js';
6
+ /*
7
+ * Headless modal dialog: unstyled parts over a native `dialog` and the <govuk-react-modal>
8
+ * element (./element.ts). Every part except Root takes `asChild`.
9
+ *
10
+ * Trigger and Close are buttons with invoker commands (`command` and `commandfor`), so where
11
+ * the browser supports them the dialog opens as a true modal with no JavaScript: the page behind
12
+ * is inert, focus moves into the dialog, and Escape closes it. The element runs the commands in
13
+ * browsers without them, and applies `open` when controlled.
14
+ *
15
+ * Context only fills in ids: in an island without a Root, pass `dialogId` to Trigger and Close,
16
+ * and `id` to Content.
17
+ */
18
+ const ModalContext = createContext({});
19
+ export function Root({ children, open, onOpenChange, dialogId: dialogIdProp, ...props }) {
20
+ const generatedId = useId();
21
+ const dialogId = dialogIdProp !== null && dialogIdProp !== void 0 ? dialogIdProp : generatedId;
22
+ return (React.createElement(ModalContext.Provider, { value: { dialogId, titleId: `${dialogId}-title`, onOpenChange } }, React.createElement(tagName, {
23
+ ...props,
24
+ open: open === undefined ? undefined : String(open),
25
+ suppressHydrationWarning: true,
26
+ }, children)));
27
+ }
28
+ function Command({ command, asChild, dialogId, ...props }) {
29
+ const context = useContext(ModalContext);
30
+ // `command` and `commandfor` aren't in React's types yet, so they go in as a spread.
31
+ const invoker = { command, commandfor: dialogId !== null && dialogId !== void 0 ? dialogId : context.dialogId };
32
+ if (asChild)
33
+ return React.createElement(Slot, { ...invoker, ...props });
34
+ return React.createElement("button", { type: "button", ...invoker, ...props });
35
+ }
36
+ /** A button that opens the dialog as a modal. */
37
+ export function Trigger(props) {
38
+ return React.createElement(Command, { command: "show-modal", ...props });
39
+ }
40
+ /** A button that closes the dialog. */
41
+ export function Close(props) {
42
+ return React.createElement(Command, { command: "close", ...props });
43
+ }
44
+ /**
45
+ * The dialog. It's named by Title, unless you pass `aria-labelledby` or `aria-label`. Pass
46
+ * `closedby="any"` to also close it when the user clicks outside it.
47
+ */
48
+ export function Content({ asChild, id, onClose, ref, ...props }) {
49
+ const context = useContext(ModalContext);
50
+ const reported = useRef(undefined);
51
+ const onOpenChange = useRef(context.onOpenChange);
52
+ useEffect(() => {
53
+ onOpenChange.current = context.onOpenChange;
54
+ });
55
+ // Report each change once: `toggle` fires on opening and closing where the browser fires it on
56
+ // dialogs, and `close` on closing everywhere.
57
+ const report = (dialog) => {
58
+ var _a;
59
+ if (reported.current === dialog.open)
60
+ return;
61
+ reported.current = dialog.open;
62
+ (_a = onOpenChange.current) === null || _a === void 0 ? void 0 : _a.call(onOpenChange, dialog.open);
63
+ };
64
+ // A dialog the user opened before hydration, through its invoker command or the element, fired
65
+ // nothing React heard, so report it once.
66
+ const dialogRef = useRef(null);
67
+ useEffect(() => {
68
+ var _a;
69
+ const dialog = dialogRef.current;
70
+ if ((dialog === null || dialog === void 0 ? void 0 : dialog.open) && reported.current === undefined) {
71
+ reported.current = true;
72
+ (_a = onOpenChange.current) === null || _a === void 0 ? void 0 : _a.call(onOpenChange, true);
73
+ }
74
+ }, []);
75
+ // React doesn't listen for `toggle` on a dialog, so listen natively.
76
+ const listen = (dialog) => {
77
+ dialogRef.current = dialog;
78
+ if (typeof ref === 'function')
79
+ ref(dialog);
80
+ else if (ref)
81
+ ref.current = dialog;
82
+ if (!dialog)
83
+ return undefined;
84
+ const onToggle = () => report(dialog);
85
+ dialog.addEventListener('toggle', onToggle);
86
+ return () => dialog.removeEventListener('toggle', onToggle);
87
+ };
88
+ const shared = {
89
+ id: id !== null && id !== void 0 ? id : context.dialogId,
90
+ 'aria-labelledby': props['aria-label'] ? undefined : context.titleId,
91
+ ref: listen,
92
+ onClose: (event) => {
93
+ onClose === null || onClose === void 0 ? void 0 : onClose(event);
94
+ report(event.currentTarget);
95
+ },
96
+ // The dialog's open state belongs to the browser.
97
+ suppressHydrationWarning: true,
98
+ };
99
+ if (asChild)
100
+ return React.createElement(Slot, { ...shared, ...props });
101
+ return React.createElement("dialog", { ...shared, ...props });
102
+ }
103
+ /** The dialog's heading, which names it. */
104
+ export function Title({ asChild, ...props }) {
105
+ const context = useContext(ModalContext);
106
+ const Element = asChild ? Slot : 'h2';
107
+ return React.createElement(Element, { id: context.titleId, ...props });
108
+ }
109
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/modal/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE5E,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACx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AA4C;AAC5C,MAAM,UAAU,KAAK,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAc;IACrD,MAAM,OAAO,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;IACzC,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACtC,OAAO,oBAAC,OAAO,IAAC,EAAE,EAAE,OAAO,CAAC,OAAO,KAAM,KAAK,GAAI,CAAC;AACrD,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createContext, useContext, useEffect, useId, useRef } from 'react';\nimport type { ComponentProps, ReactNode, SyntheticEvent } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName } from './element.js';\n\n/*\n * Headless modal dialog: unstyled parts over a native `dialog` and the <govuk-react-modal>\n * element (./element.ts). Every part except Root takes `asChild`.\n *\n * Trigger and Close are buttons with invoker commands (`command` and `commandfor`), so where\n * the browser supports them the dialog opens as a true modal with no JavaScript: the page behind\n * is inert, focus moves into the dialog, and Escape closes it. The element runs the commands in\n * browsers without them, and applies `open` when controlled.\n *\n * Context only fills in ids: in an island without a Root, pass `dialogId` to Trigger and Close,\n * and `id` to Content.\n */\n\nconst ModalContext = createContext<{\n dialogId?: string;\n titleId?: string;\n onOpenChange?: (open: boolean) => void;\n}>({});\n\nexport type RootProps = Omit<React.HTMLAttributes<HTMLElement>, 'onChange'> & {\n children?: ReactNode;\n /** Whether the dialog is open, to control it. Without it, the buttons alone open and close it. */\n open?: boolean;\n /** Called when the dialog opens or closes. */\n onOpenChange?: (open: boolean) => void;\n /** The dialog's id, which the buttons' `commandfor` names. Defaults to a generated one. */\n dialogId?: string;\n};\n\nexport function Root({ children, open, onOpenChange, dialogId: dialogIdProp, ...props }: RootProps) {\n const generatedId = useId();\n const dialogId = dialogIdProp ?? generatedId;\n return (\n <ModalContext.Provider value={{ dialogId, titleId: `${dialogId}-title`, onOpenChange }}>\n {React.createElement(\n tagName,\n {\n ...props,\n open: open === undefined ? undefined : String(open),\n suppressHydrationWarning: true,\n },\n children\n )}\n </ModalContext.Provider>\n );\n}\n\ntype PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n};\n\ntype CommandProps = PartProps<'button'> & {\n /** The dialog's id. Set by Root. */\n dialogId?: string;\n};\n\nfunction Command({ command, asChild, dialogId, ...props }: CommandProps & { command: string }) {\n const context = useContext(ModalContext);\n // `command` and `commandfor` aren't in React's types yet, so they go in as a spread.\n const invoker = { command, commandfor: dialogId ?? context.dialogId };\n if (asChild) return <Slot {...invoker} {...props} />;\n return <button type=\"button\" {...invoker} {...props} />;\n}\n\nexport type TriggerProps = CommandProps;\n\n/** A button that opens the dialog as a modal. */\nexport function Trigger(props: TriggerProps) {\n return <Command command=\"show-modal\" {...props} />;\n}\n\nexport type CloseProps = CommandProps;\n\n/** A button that closes the dialog. */\nexport function Close(props: CloseProps) {\n return <Command command=\"close\" {...props} />;\n}\n\nexport type ContentProps = PartProps<'dialog'>;\n\n/**\n * The dialog. It's named by Title, unless you pass `aria-labelledby` or `aria-label`. Pass\n * `closedby=\"any\"` to also close it when the user clicks outside it.\n */\nexport function Content({ asChild, id, onClose, ref, ...props }: ContentProps) {\n const context = useContext(ModalContext);\n const reported = useRef<boolean | undefined>(undefined);\n const onOpenChange = useRef(context.onOpenChange);\n useEffect(() => {\n onOpenChange.current = context.onOpenChange;\n });\n // Report each change once: `toggle` fires on opening and closing where the browser fires it on\n // dialogs, and `close` on closing everywhere.\n const report = (dialog: HTMLDialogElement) => {\n if (reported.current === dialog.open) return;\n reported.current = dialog.open;\n onOpenChange.current?.(dialog.open);\n };\n // A dialog the user opened before hydration, through its invoker command or the element, fired\n // nothing React heard, so report it once.\n const dialogRef = useRef<HTMLDialogElement | null>(null);\n useEffect(() => {\n const dialog = dialogRef.current;\n if (dialog?.open && reported.current === undefined) {\n reported.current = true;\n onOpenChange.current?.(true);\n }\n }, []);\n // React doesn't listen for `toggle` on a dialog, so listen natively.\n const listen = (dialog: HTMLDialogElement | null) => {\n dialogRef.current = dialog;\n if (typeof ref === 'function') ref(dialog);\n else if (ref) ref.current = dialog;\n if (!dialog) return undefined;\n const onToggle = () => report(dialog);\n dialog.addEventListener('toggle', onToggle);\n return () => dialog.removeEventListener('toggle', onToggle);\n };\n const shared = {\n id: id ?? context.dialogId,\n 'aria-labelledby': props['aria-label'] ? undefined : context.titleId,\n ref: listen,\n onClose: (event: SyntheticEvent<HTMLDialogElement>) => {\n onClose?.(event);\n report(event.currentTarget);\n },\n // The dialog's open state belongs to the browser.\n suppressHydrationWarning: true,\n };\n if (asChild) return <Slot {...shared} {...props} />;\n return <dialog {...shared} {...props} />;\n}\n\nexport type TitleProps = PartProps<'h2'>;\n\n/** The dialog's heading, which names it. */\nexport function Title({ asChild, ...props }: TitleProps) {\n const context = useContext(ModalContext);\n const Element = asChild ? Slot : 'h2';\n return <Element id={context.titleId} {...props} />;\n}\n"]}
@@ -0,0 +1 @@
1
+ export declare const tagName = "govuk-react-password-input";
@@ -0,0 +1,134 @@
1
+ /*
2
+ * <govuk-react-password-input>: a headless, light-DOM custom element that adds a show and hide
3
+ * button to a password field, with or without React.
4
+ *
5
+ * The input's `type` is the state, kept when React sets it again. Without JavaScript the field
6
+ * stays a password field and the button stays hidden. On upgrade the element shows the button, which switches the input between
7
+ * "password" and "text", updates its own text and label, and announces the change.
8
+ *
9
+ * Markup inside the element:
10
+ * - `[data-password-input]`: an `input` of type "password".
11
+ * - `[data-password-toggle]`: a `button` of type "button", rendered `hidden`.
12
+ * - `[data-password-status]` (optional): a visually hidden live region for the announcements.
13
+ * Without one, the element adds one after the input.
14
+ *
15
+ * Attributes, for the text (English by default):
16
+ * - `show-text`, `hide-text`: the button's text.
17
+ * - `show-label`, `hide-label`: the button's accessible name.
18
+ * - `shown-announcement`, `hidden-announcement`: announced when the password is shown or hidden.
19
+ *
20
+ * As in govuk-frontend, the password is hidden again when its form is submitted, unless by a hidden
21
+ * submit button such as a form library's, and when the page is restored from the back-forward cache.
22
+ */
23
+ import { onPropertySet } from '../utils/on-property-set.js';
24
+ import { setUpWhenReady } from '../utils/set-up-when-ready.js';
25
+ export const tagName = 'govuk-react-password-input';
26
+ const defaults = {
27
+ 'show-text': 'Show',
28
+ 'hide-text': 'Hide',
29
+ 'show-label': 'Show password',
30
+ 'hide-label': 'Hide password',
31
+ 'shown-announcement': 'Your password is visible',
32
+ 'hidden-announcement': 'Your password is hidden',
33
+ };
34
+ // The same visually hidden styles as govuk-frontend's, for a live region the element adds itself.
35
+ const visuallyHidden = 'position:absolute;width:1px;height:1px;margin:0;padding:0;overflow:hidden;clip:rect(0 0 0 0);clip-path:inset(50%);border:0;white-space:nowrap';
36
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
37
+ class GovukReactPasswordInput extends HTMLElement {
38
+ constructor() {
39
+ super(...arguments);
40
+ this.visible = false;
41
+ // React sets `type` again whenever it updates a controlled input.
42
+ this.onTypeSet = () => {
43
+ const type = this.visible ? 'text' : 'password';
44
+ if (this.input && this.input.type !== type)
45
+ this.input.type = type;
46
+ };
47
+ this.onToggle = (event) => {
48
+ event.preventDefault();
49
+ this.setVisible(!this.visible);
50
+ };
51
+ // A hidden submitter isn't the user submitting, but a script, such as Conform validating as the
52
+ // user types.
53
+ this.onSubmit = (event) => {
54
+ var _a;
55
+ const submitter = event instanceof SubmitEvent ? event.submitter : null;
56
+ if (event.target === ((_a = this.input) === null || _a === void 0 ? void 0 : _a.form) && !(submitter instanceof HTMLElement && submitter.hidden)) {
57
+ this.setVisible(false);
58
+ }
59
+ };
60
+ this.onPageShow = (event) => {
61
+ if (event.persisted)
62
+ this.setVisible(false);
63
+ };
64
+ }
65
+ connectedCallback() {
66
+ this.observer = setUpWhenReady(this, () => this.setup());
67
+ }
68
+ disconnectedCallback() {
69
+ var _a, _b, _c;
70
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
71
+ (_b = this.toggle) === null || _b === void 0 ? void 0 : _b.removeEventListener('click', this.onToggle);
72
+ document.removeEventListener('submit', this.onSubmit);
73
+ window.removeEventListener('pageshow', this.onPageShow);
74
+ (_c = this.stopWatchingType) === null || _c === void 0 ? void 0 : _c.call(this);
75
+ this.input = undefined;
76
+ this.toggle = undefined;
77
+ }
78
+ text(name) {
79
+ var _a;
80
+ return (_a = this.getAttribute(name)) !== null && _a !== void 0 ? _a : defaults[name];
81
+ }
82
+ own(selector, type) {
83
+ const element = this.querySelector(selector);
84
+ return element instanceof type && element.closest(tagName) === this ? element : undefined;
85
+ }
86
+ setup() {
87
+ if (this.input)
88
+ return true;
89
+ const input = this.own('[data-password-input]', HTMLInputElement);
90
+ const toggle = this.own('[data-password-toggle]', HTMLButtonElement);
91
+ if (!input || !toggle)
92
+ return false;
93
+ this.input = input;
94
+ this.toggle = toggle;
95
+ this.status = this.own('[data-password-status]', HTMLElement);
96
+ if (!this.status) {
97
+ this.status = document.createElement('div');
98
+ this.status.dataset.passwordStatus = '';
99
+ this.status.setAttribute('aria-live', 'polite');
100
+ this.status.setAttribute('style', visuallyHidden);
101
+ input.insertAdjacentElement('afterend', this.status);
102
+ }
103
+ toggle.hidden = false;
104
+ toggle.addEventListener('click', this.onToggle);
105
+ document.addEventListener('submit', this.onSubmit);
106
+ window.addEventListener('pageshow', this.onPageShow);
107
+ // Start hidden, with the text set but nothing announced.
108
+ this.visible = false;
109
+ input.type = 'password';
110
+ this.stopWatchingType = onPropertySet(input, 'type', this.onTypeSet);
111
+ this.label(false);
112
+ return true;
113
+ }
114
+ setVisible(visible) {
115
+ const { input } = this;
116
+ if (!input || visible === this.visible)
117
+ return;
118
+ this.visible = visible;
119
+ input.type = visible ? 'text' : 'password';
120
+ this.label(visible);
121
+ if (this.status)
122
+ this.status.textContent = this.text(visible ? 'shown-announcement' : 'hidden-announcement');
123
+ }
124
+ label(visible) {
125
+ if (!this.toggle)
126
+ return;
127
+ this.toggle.textContent = this.text(visible ? 'hide-text' : 'show-text');
128
+ this.toggle.setAttribute('aria-label', this.text(visible ? 'hide-label' : 'show-label'));
129
+ }
130
+ }
131
+ if (!customElements.get(tagName))
132
+ customElements.define(tagName, GovukReactPasswordInput);
133
+ }
134
+ //# sourceMappingURL=element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/password-input/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAE/D,MAAM,CAAC,MAAM,OAAO,GAAG,4BAA4B,CAAC;AAEpD,MAAM,QAAQ,GAAG;IACf,WAAW,EAAE,MAAM;IACnB,WAAW,EAAE,MAAM;IACnB,YAAY,EAAE,eAAe;IAC7B,YAAY,EAAE,eAAe;IAC7B,oBAAoB,EAAE,0BAA0B;IAChD,qBAAqB,EAAE,yBAAyB;CACjD,CAAC;AAEF,kGAAkG;AAClG,MAAM,cAAc,GAClB,+IAA+I,CAAC;AAElJ,IAAI,OAAO,WAAW,KAAK,WAAW,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE,CAAC;IAChF,MAAM,uBAAwB,SAAQ,WAAW;QAAjD;;YASU,YAAO,GAAG,KAAK,CAAC;YAIxB,kEAAkE;YACjD,cAAS,GAAG,GAAG,EAAE;gBAChC,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC;gBAChD,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI;oBAAE,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC;YACrE,CAAC,CAAC;YAEe,aAAQ,GAAG,CAAC,KAAiB,EAAE,EAAE;gBAChD,KAAK,CAAC,cAAc,EAAE,CAA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ACzB,IAAI,CAAC,MAAM,CAAC,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;YACzE,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,YAAY,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC;QAC3F,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAAC;AAC5F,CAAC","sourcesContent":["/*\n * <govuk-react-password-input>: a headless, light-DOM custom element that adds a show and hide\n * button to a password field, with or without React.\n *\n * The input's `type` is the state, kept when React sets it again. Without JavaScript the field\n * stays a password field and the button stays hidden. On upgrade the element shows the button, which switches the input between\n * \"password\" and \"text\", updates its own text and label, and announces the change.\n *\n * Markup inside the element:\n * - `[data-password-input]`: an `input` of type \"password\".\n * - `[data-password-toggle]`: a `button` of type \"button\", rendered `hidden`.\n * - `[data-password-status]` (optional): a visually hidden live region for the announcements.\n * Without one, the element adds one after the input.\n *\n * Attributes, for the text (English by default):\n * - `show-text`, `hide-text`: the button's text.\n * - `show-label`, `hide-label`: the button's accessible name.\n * - `shown-announcement`, `hidden-announcement`: announced when the password is shown or hidden.\n *\n * As in govuk-frontend, the password is hidden again when its form is submitted, unless by a hidden\n * submit button such as a form library's, and when the page is restored from the back-forward cache.\n */\n\nimport { onPropertySet } from '../utils/on-property-set.js';\nimport { setUpWhenReady } from '../utils/set-up-when-ready.js';\n\nexport const tagName = 'govuk-react-password-input';\n\nconst defaults = {\n 'show-text': 'Show',\n 'hide-text': 'Hide',\n 'show-label': 'Show password',\n 'hide-label': 'Hide password',\n 'shown-announcement': 'Your password is visible',\n 'hidden-announcement': 'Your password is hidden',\n};\n\n// The same visually hidden styles as govuk-frontend's, for a live region the element adds itself.\nconst visuallyHidden =\n 'position:absolute;width:1px;height:1px;margin:0;padding:0;overflow:hidden;clip:rect(0 0 0 0);clip-path:inset(50%);border:0;white-space:nowrap';\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactPasswordInput extends HTMLElement {\n private input?: HTMLInputElement;\n\n private toggle?: HTMLButtonElement;\n\n private status?: HTMLElement;\n\n private observer?: MutationObserver;\n\n private visible = false;\n\n private stopWatchingType?: () => void;\n\n // React sets `type` again whenever it updates a controlled input.\n private readonly onTypeSet = () => {\n const type = this.visible ? 'text' : 'password';\n if (this.input && this.input.type !== type) this.input.type = type;\n };\n\n private readonly onToggle = (event: MouseEvent) => {\n event.preventDefault();\n this.setVisible(!this.visible);\n };\n\n // A hidden submitter isn't the user submitting, but a script, such as Conform validating as the\n // user types.\n private readonly onSubmit = (event: Event) => {\n const submitter = event instanceof SubmitEvent ? event.submitter : null;\n if (event.target === this.input?.form && !(submitter instanceof HTMLElement && submitter.hidden)) {\n this.setVisible(false);\n }\n };\n\n private readonly onPageShow = (event: PageTransitionEvent) => {\n if (event.persisted) this.setVisible(false);\n };\n\n connectedCallback() {\n this.observer = setUpWhenReady(this, () => this.setup());\n }\n\n disconnectedCallback() {\n this.observer?.disconnect();\n this.toggle?.removeEventListener('click', this.onToggle);\n document.removeEventListener('submit', this.onSubmit);\n window.removeEventListener('pageshow', this.onPageShow);\n this.stopWatchingType?.();\n this.input = undefined;\n this.toggle = undefined;\n }\n\n private text(name: keyof typeof defaults) {\n return this.getAttribute(name) ?? defaults[name];\n }\n\n private own<E extends Element>(selector: string, type: new () => E): E | undefined {\n const element = this.querySelector(selector);\n return element instanceof type && element.closest(tagName) === this ? element : undefined;\n }\n\n private setup() {\n if (this.input) return true;\n const input = this.own('[data-password-input]', HTMLInputElement);\n const toggle = this.own('[data-password-toggle]', HTMLButtonElement);\n if (!input || !toggle) return false;\n this.input = input;\n this.toggle = toggle;\n this.status = this.own('[data-password-status]', HTMLElement);\n if (!this.status) {\n this.status = document.createElement('div');\n this.status.dataset.passwordStatus = '';\n this.status.setAttribute('aria-live', 'polite');\n this.status.setAttribute('style', visuallyHidden);\n input.insertAdjacentElement('afterend', this.status);\n }\n toggle.hidden = false;\n toggle.addEventListener('click', this.onToggle);\n document.addEventListener('submit', this.onSubmit);\n window.addEventListener('pageshow', this.onPageShow);\n // Start hidden, with the text set but nothing announced.\n this.visible = false;\n input.type = 'password';\n this.stopWatchingType = onPropertySet(input, 'type', this.onTypeSet);\n this.label(false);\n return true;\n }\n\n private setVisible(visible: boolean) {\n const { input } = this;\n if (!input || visible === this.visible) return;\n this.visible = visible;\n input.type = visible ? 'text' : 'password';\n this.label(visible);\n if (this.status) this.status.textContent = this.text(visible ? 'shown-announcement' : 'hidden-announcement');\n }\n\n private label(visible: boolean) {\n if (!this.toggle) return;\n this.toggle.textContent = this.text(visible ? 'hide-text' : 'show-text');\n this.toggle.setAttribute('aria-label', this.text(visible ? 'hide-label' : 'show-label'));\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactPasswordInput);\n}\n"]}
@@ -1,7 +1,6 @@
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
  /** The button's text when the password is hidden. Defaults to "Show". */
6
5
  showText?: string;
7
6
  /** The button's text when the password is visible. Defaults to "Hide". */
@@ -15,7 +14,7 @@ type RootProps = React.HTMLAttributes<HTMLElement> & {
15
14
  /** Announced when the password is hidden. Defaults to "Your password is hidden". */
16
15
  hiddenAnnouncement?: string;
17
16
  };
18
- declare function Root({ showText, hideText, showLabel, hideLabel, shownAnnouncement, hiddenAnnouncement, ...props }: RootProps): React.DOMElement<{
17
+ export declare function Root({ showText, hideText, showLabel, hideLabel, shownAnnouncement, hiddenAnnouncement, ...props }: RootProps): React.DOMElement<{
19
18
  'show-text': string;
20
19
  'hide-text': string;
21
20
  'show-label': string;
@@ -30,17 +29,17 @@ declare function Root({ showText, hideText, showLabel, hideLabel, shownAnnouncem
30
29
  autoCapitalize?: "off" | "none" | "on" | "sentences" | "words" | "characters" | (string & {});
31
30
  autoFocus?: boolean;
32
31
  className?: string;
33
- contentEditable?: (boolean | "true" | "false") | "inherit" | "plaintext-only";
32
+ contentEditable?: (boolean | "false" | "true") | "inherit" | "plaintext-only";
34
33
  contextMenu?: string;
35
34
  dir?: string;
36
- draggable?: boolean | "true" | "false";
35
+ draggable?: boolean | "false" | "true";
37
36
  enterKeyHint?: "search" | "enter" | "done" | "go" | "next" | "previous" | "send";
38
37
  hidden?: boolean;
39
38
  id?: string;
40
39
  lang?: string;
41
40
  nonce?: string;
42
41
  slot?: string;
43
- spellCheck?: boolean | "true" | "false";
42
+ spellCheck?: boolean | "false" | "true";
44
43
  style?: React.CSSProperties;
45
44
  tabIndex?: number;
46
45
  title?: string;
@@ -73,57 +72,57 @@ declare function Root({ showText, hideText, showLabel, hideLabel, shownAnnouncem
73
72
  popoverTargetAction?: "toggle" | "show" | "hide";
74
73
  popoverTarget?: string;
75
74
  inert?: boolean;
76
- inputMode?: "search" | "none" | "text" | "tel" | "url" | "email" | "numeric" | "decimal";
75
+ inputMode?: "search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal";
77
76
  is?: string;
78
77
  exportparts?: string;
79
78
  part?: string;
80
79
  "aria-activedescendant"?: string;
81
- "aria-atomic"?: boolean | "true" | "false";
80
+ "aria-atomic"?: boolean | "false" | "true";
82
81
  "aria-autocomplete"?: "none" | "list" | "inline" | "both";
83
82
  "aria-braillelabel"?: string;
84
83
  "aria-brailleroledescription"?: string;
85
- "aria-busy"?: boolean | "true" | "false";
86
- "aria-checked"?: boolean | "true" | "false" | "mixed";
84
+ "aria-busy"?: boolean | "false" | "true";
85
+ "aria-checked"?: boolean | "false" | "true" | "mixed";
87
86
  "aria-colcount"?: number;
88
87
  "aria-colindex"?: number;
89
88
  "aria-colindextext"?: string;
90
89
  "aria-colspan"?: number;
91
90
  "aria-controls"?: string;
92
- "aria-current"?: boolean | "time" | "true" | "false" | "page" | "step" | "location" | "date";
91
+ "aria-current"?: boolean | "time" | "false" | "true" | "page" | "step" | "location" | "date";
93
92
  "aria-describedby"?: string;
94
93
  "aria-description"?: string;
95
94
  "aria-details"?: string;
96
- "aria-disabled"?: boolean | "true" | "false";
97
- "aria-dropeffect"?: "link" | "none" | "copy" | "execute" | "move" | "popup";
95
+ "aria-disabled"?: boolean | "false" | "true";
96
+ "aria-dropeffect"?: "link" | "copy" | "none" | "execute" | "move" | "popup";
98
97
  "aria-errormessage"?: string;
99
- "aria-expanded"?: boolean | "true" | "false";
98
+ "aria-expanded"?: boolean | "false" | "true";
100
99
  "aria-flowto"?: string;
101
- "aria-grabbed"?: boolean | "true" | "false";
102
- "aria-haspopup"?: boolean | "dialog" | "menu" | "true" | "false" | "grid" | "listbox" | "tree";
103
- "aria-hidden"?: boolean | "true" | "false";
104
- "aria-invalid"?: boolean | "true" | "false" | "grammar" | "spelling";
100
+ "aria-grabbed"?: boolean | "false" | "true";
101
+ "aria-haspopup"?: boolean | "dialog" | "menu" | "false" | "true" | "grid" | "listbox" | "tree";
102
+ "aria-hidden"?: boolean | "false" | "true";
103
+ "aria-invalid"?: boolean | "false" | "true" | "grammar" | "spelling";
105
104
  "aria-keyshortcuts"?: string;
106
105
  "aria-label"?: string;
107
106
  "aria-labelledby"?: string;
108
107
  "aria-level"?: number;
109
108
  "aria-live"?: "off" | "assertive" | "polite";
110
- "aria-modal"?: boolean | "true" | "false";
111
- "aria-multiline"?: boolean | "true" | "false";
112
- "aria-multiselectable"?: boolean | "true" | "false";
109
+ "aria-modal"?: boolean | "false" | "true";
110
+ "aria-multiline"?: boolean | "false" | "true";
111
+ "aria-multiselectable"?: boolean | "false" | "true";
113
112
  "aria-orientation"?: "horizontal" | "vertical";
114
113
  "aria-owns"?: string;
115
114
  "aria-placeholder"?: string;
116
115
  "aria-posinset"?: number;
117
- "aria-pressed"?: boolean | "true" | "false" | "mixed";
118
- "aria-readonly"?: boolean | "true" | "false";
116
+ "aria-pressed"?: boolean | "false" | "true" | "mixed";
117
+ "aria-readonly"?: boolean | "false" | "true";
119
118
  "aria-relevant"?: "text" | "additions" | "additions removals" | "additions text" | "all" | "removals" | "removals additions" | "removals text" | "text additions" | "text removals";
120
- "aria-required"?: boolean | "true" | "false";
119
+ "aria-required"?: boolean | "false" | "true";
121
120
  "aria-roledescription"?: string;
122
121
  "aria-rowcount"?: number;
123
122
  "aria-rowindex"?: number;
124
123
  "aria-rowindextext"?: string;
125
124
  "aria-rowspan"?: number;
126
- "aria-selected"?: boolean | "true" | "false";
125
+ "aria-selected"?: boolean | "false" | "true";
127
126
  "aria-setsize"?: number;
128
127
  "aria-sort"?: "none" | "ascending" | "descending" | "other";
129
128
  "aria-valuemax"?: number;
@@ -307,28 +306,13 @@ type PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E>
307
306
  /** Render the only child instead, with this part's props merged onto it. */
308
307
  asChild?: boolean;
309
308
  };
310
- type InputProps = PartProps<'input'>;
309
+ export type InputProps = PartProps<'input'>;
311
310
  /** The password field. Its `type` starts as "password" and the element switches it. */
312
- declare function Input({ asChild, ...props }: InputProps): React.JSX.Element;
313
- type ToggleProps = PartProps<'button'>;
311
+ export declare function Input({ asChild, ...props }: InputProps): React.JSX.Element;
312
+ export type ToggleProps = PartProps<'button'>;
314
313
  /** The show and hide button. Hidden until the element upgrades, which then sets its text and label. */
315
- declare function Toggle({ asChild, children, ...props }: ToggleProps): React.JSX.Element;
316
- type StatusProps = PartProps<'div'>;
314
+ export declare function Toggle({ asChild, children, ...props }: ToggleProps): React.JSX.Element;
315
+ export type StatusProps = PartProps<'div'>;
317
316
  /** A live region for the announcements. Make it visually hidden. */
318
- declare function Status({ asChild, ...props }: StatusProps): React.JSX.Element;
319
-
320
- declare const index_Input: typeof Input;
321
- type index_InputProps = InputProps;
322
- declare const index_Root: typeof Root;
323
- type index_RootProps = RootProps;
324
- declare const index_Status: typeof Status;
325
- type index_StatusProps = StatusProps;
326
- declare const index_Toggle: typeof Toggle;
327
- type index_ToggleProps = ToggleProps;
328
- declare namespace index {
329
- export { index_Input as Input, index_Root as Root, index_Status as Status, index_Toggle as Toggle };
330
- export type { index_InputProps as InputProps, index_RootProps as RootProps, index_StatusProps as StatusProps, index_ToggleProps as ToggleProps };
331
- }
332
-
333
- export { Input as I, Root as R, Status as S, Toggle as T, index as i };
334
- export type { InputProps as a, RootProps as b, StatusProps as c, ToggleProps as d };
317
+ export declare function Status({ asChild, ...props }: StatusProps): React.JSX.Element;
318
+ export {};