@govuk-react/primitives 0.2.0 → 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.
Files changed (267) hide show
  1. package/README.md +100 -25
  2. package/dist/{accordion-element.d.ts → accordion/element.d.ts} +5 -5
  3. package/dist/accordion/element.js +302 -0
  4. package/dist/accordion/element.js.map +1 -0
  5. package/dist/accordion/index.d.ts +70 -0
  6. package/dist/accordion/index.js +140 -0
  7. package/dist/accordion/index.js.map +1 -0
  8. package/dist/auto-focus/element.d.ts +1 -0
  9. package/dist/auto-focus/element.js +75 -0
  10. package/dist/auto-focus/element.js.map +1 -0
  11. package/dist/auto-focus/index.d.ts +15 -0
  12. package/dist/auto-focus/index.js +39 -0
  13. package/dist/auto-focus/index.js.map +1 -0
  14. package/dist/{autocomplete-element.d.ts → autocomplete/element.d.ts} +11 -11
  15. package/dist/autocomplete/element.js +597 -0
  16. package/dist/autocomplete/element.js.map +1 -0
  17. package/dist/{index-D7lMYMUR.d.ts → autocomplete/index.d.ts} +39 -62
  18. package/dist/autocomplete/index.js +100 -0
  19. package/dist/autocomplete/index.js.map +1 -0
  20. package/dist/button/element.d.ts +1 -0
  21. package/dist/button/element.js +59 -0
  22. package/dist/button/element.js.map +1 -0
  23. package/dist/button/index.d.ts +10 -0
  24. package/dist/button/index.js +9 -0
  25. package/dist/button/index.js.map +1 -0
  26. package/dist/character-count/element.d.ts +12 -0
  27. package/dist/character-count/element.js +287 -0
  28. package/dist/character-count/element.js.map +1 -0
  29. package/dist/character-count/index.d.ts +57 -0
  30. package/dist/character-count/index.js +79 -0
  31. package/dist/character-count/index.js.map +1 -0
  32. package/dist/character-count/messages.d.ts +20 -0
  33. package/dist/character-count/messages.js +54 -0
  34. package/dist/character-count/messages.js.map +1 -0
  35. package/dist/choices/element.d.ts +10 -0
  36. package/dist/choices/element.js +177 -0
  37. package/dist/choices/element.js.map +1 -0
  38. package/dist/{index-WuGvkTxu.d.ts → choices/index.d.ts} +30 -44
  39. package/dist/choices/index.js +37 -0
  40. package/dist/choices/index.js.map +1 -0
  41. package/dist/{disclosure-element.d.ts → disclosure/element.d.ts} +5 -5
  42. package/dist/disclosure/element.js +132 -0
  43. package/dist/disclosure/element.js.map +1 -0
  44. package/dist/{index-D4Gzyvfu.d.ts → disclosure/index.d.ts} +43 -56
  45. package/dist/disclosure/index.js +76 -0
  46. package/dist/disclosure/index.js.map +1 -0
  47. package/dist/exit-this-page/element.d.ts +19 -0
  48. package/dist/exit-this-page/element.js +200 -0
  49. package/dist/exit-this-page/element.js.map +1 -0
  50. package/dist/{index-qF0jIQCW.d.ts → exit-this-page/index.d.ts} +44 -68
  51. package/dist/exit-this-page/index.js +54 -0
  52. package/dist/exit-this-page/index.js.map +1 -0
  53. package/dist/file-upload/element.d.ts +28 -0
  54. package/dist/file-upload/element.js +270 -0
  55. package/dist/file-upload/element.js.map +1 -0
  56. package/dist/file-upload/index.d.ts +46 -0
  57. package/dist/file-upload/index.js +65 -0
  58. package/dist/file-upload/index.js.map +1 -0
  59. package/dist/focus-links/element.d.ts +1 -0
  60. package/dist/focus-links/element.js +90 -0
  61. package/dist/focus-links/element.js.map +1 -0
  62. package/dist/{index-BR3MAGF8.d.ts → focus-links/index.d.ts} +24 -35
  63. package/dist/focus-links/index.js +7 -0
  64. package/dist/focus-links/index.js.map +1 -0
  65. package/dist/index.d.ts +13 -17
  66. package/dist/index.js +13 -72
  67. package/dist/index.js.map +1 -1
  68. package/dist/inline/accordion-element.js +1 -1
  69. package/dist/inline/auto-focus-element.js +1 -1
  70. package/dist/inline/autocomplete-element.js +1 -1
  71. package/dist/inline/character-count-element.js +1 -1
  72. package/dist/inline/choices-element.js +1 -1
  73. package/dist/inline/disclosure-element.js +1 -1
  74. package/dist/inline/exit-this-page-element.js +1 -1
  75. package/dist/inline/file-upload-element.js +1 -1
  76. package/dist/inline/focus-links-element.js +1 -1
  77. package/dist/inline/modal-element.js +1 -1
  78. package/dist/inline/password-input-element.js +1 -1
  79. package/dist/inline/tabs-element.js +1 -1
  80. package/dist/inline-script/index.d.ts +26 -0
  81. package/dist/inline-script/index.js +31 -0
  82. package/dist/inline-script/index.js.map +1 -0
  83. package/dist/{modal-element.d.ts → modal/element.d.ts} +2 -4
  84. package/dist/modal/element.js +229 -0
  85. package/dist/modal/element.js.map +1 -0
  86. package/dist/modal/index.d.ts +41 -0
  87. package/dist/modal/index.js +145 -0
  88. package/dist/modal/index.js.map +1 -0
  89. package/dist/password-input/element.d.ts +1 -0
  90. package/dist/password-input/element.js +150 -0
  91. package/dist/password-input/element.js.map +1 -0
  92. package/dist/{index-sEHnx4-M.d.ts → password-input/index.d.ts} +33 -49
  93. package/dist/password-input/index.js +37 -0
  94. package/dist/password-input/index.js.map +1 -0
  95. package/dist/styles.css +11 -0
  96. package/dist/{tabs-element.d.ts → tabs/element.d.ts} +5 -5
  97. package/dist/tabs/element.js +423 -0
  98. package/dist/tabs/element.js.map +1 -0
  99. package/dist/tabs/index.d.ts +74 -0
  100. package/dist/tabs/index.js +142 -0
  101. package/dist/tabs/index.js.map +1 -0
  102. package/dist/utils/change-details.d.ts +11 -0
  103. package/dist/utils/change-details.js +3 -0
  104. package/dist/utils/change-details.js.map +1 -0
  105. package/dist/utils/dom.d.ts +9 -0
  106. package/dist/utils/dom.js +27 -0
  107. package/dist/utils/dom.js.map +1 -0
  108. package/dist/utils/on-property-change.d.ts +7 -0
  109. package/dist/utils/on-property-change.js +19 -0
  110. package/dist/utils/on-property-change.js.map +1 -0
  111. package/dist/utils/set-focus.d.ts +11 -0
  112. package/dist/utils/set-focus.js +38 -0
  113. package/dist/utils/set-focus.js.map +1 -0
  114. package/dist/utils/set-up-when-ready.d.ts +6 -0
  115. package/dist/utils/set-up-when-ready.js +16 -0
  116. package/dist/utils/set-up-when-ready.js.map +1 -0
  117. package/dist/utils/slot.d.ts +15 -0
  118. package/dist/utils/slot.js +62 -0
  119. package/dist/utils/slot.js.map +1 -0
  120. package/dist/utils/translation.d.ts +13 -0
  121. package/dist/utils/translation.js +57 -0
  122. package/dist/utils/translation.js.map +1 -0
  123. package/package.json +17 -4
  124. package/dist/accordion-element.js +0 -9
  125. package/dist/accordion-element.js.map +0 -1
  126. package/dist/accordion.d.ts +0 -2
  127. package/dist/accordion.js +0 -34
  128. package/dist/accordion.js.map +0 -1
  129. package/dist/auto-focus-element.d.ts +0 -3
  130. package/dist/auto-focus-element.js +0 -10
  131. package/dist/auto-focus-element.js.map +0 -1
  132. package/dist/auto-focus.d.ts +0 -2
  133. package/dist/auto-focus.js +0 -15
  134. package/dist/auto-focus.js.map +0 -1
  135. package/dist/autocomplete-element.js +0 -12
  136. package/dist/autocomplete-element.js.map +0 -1
  137. package/dist/autocomplete.d.ts +0 -4
  138. package/dist/autocomplete.js +0 -25
  139. package/dist/autocomplete.js.map +0 -1
  140. package/dist/button-element.d.ts +0 -2
  141. package/dist/button-element.js +0 -3
  142. package/dist/button-element.js.map +0 -1
  143. package/dist/button.d.ts +0 -2
  144. package/dist/button.js +0 -12
  145. package/dist/button.js.map +0 -1
  146. package/dist/character-count-element.d.ts +0 -10
  147. package/dist/character-count-element.js +0 -10
  148. package/dist/character-count-element.js.map +0 -1
  149. package/dist/character-count.d.ts +0 -3
  150. package/dist/character-count.js +0 -26
  151. package/dist/character-count.js.map +0 -1
  152. package/dist/choices-element.d.ts +0 -8
  153. package/dist/choices-element.js +0 -9
  154. package/dist/choices-element.js.map +0 -1
  155. package/dist/choices.d.ts +0 -2
  156. package/dist/choices.js +0 -16
  157. package/dist/choices.js.map +0 -1
  158. package/dist/chunk-2IHAQAYF.js +0 -23
  159. package/dist/chunk-2IHAQAYF.js.map +0 -1
  160. package/dist/chunk-2ZJVSNC2.js +0 -31
  161. package/dist/chunk-2ZJVSNC2.js.map +0 -1
  162. package/dist/chunk-47NMY4SS.js +0 -108
  163. package/dist/chunk-47NMY4SS.js.map +0 -1
  164. package/dist/chunk-4XAR4FFG.js +0 -83
  165. package/dist/chunk-4XAR4FFG.js.map +0 -1
  166. package/dist/chunk-5HWRMQK2.js +0 -91
  167. package/dist/chunk-5HWRMQK2.js.map +0 -1
  168. package/dist/chunk-6ZFU4VYW.js +0 -41
  169. package/dist/chunk-6ZFU4VYW.js.map +0 -1
  170. package/dist/chunk-B2I4J5NL.js +0 -264
  171. package/dist/chunk-B2I4J5NL.js.map +0 -1
  172. package/dist/chunk-DPERPCUH.js +0 -188
  173. package/dist/chunk-DPERPCUH.js.map +0 -1
  174. package/dist/chunk-ECYGKE4D.js +0 -168
  175. package/dist/chunk-ECYGKE4D.js.map +0 -1
  176. package/dist/chunk-ESBMTI25.js +0 -11
  177. package/dist/chunk-ESBMTI25.js.map +0 -1
  178. package/dist/chunk-ET7DCQM7.js +0 -133
  179. package/dist/chunk-ET7DCQM7.js.map +0 -1
  180. package/dist/chunk-F5XFC4QJ.js +0 -177
  181. package/dist/chunk-F5XFC4QJ.js.map +0 -1
  182. package/dist/chunk-GVGBDSDN.js +0 -78
  183. package/dist/chunk-GVGBDSDN.js.map +0 -1
  184. package/dist/chunk-IZQ2C4BC.js +0 -351
  185. package/dist/chunk-IZQ2C4BC.js.map +0 -1
  186. package/dist/chunk-L7E6GN2D.js +0 -87
  187. package/dist/chunk-L7E6GN2D.js.map +0 -1
  188. package/dist/chunk-LWIA644X.js +0 -34
  189. package/dist/chunk-LWIA644X.js.map +0 -1
  190. package/dist/chunk-NX4B3NKM.js +0 -35
  191. package/dist/chunk-NX4B3NKM.js.map +0 -1
  192. package/dist/chunk-PNWPGD7B.js +0 -68
  193. package/dist/chunk-PNWPGD7B.js.map +0 -1
  194. package/dist/chunk-QLQ6O7OW.js +0 -111
  195. package/dist/chunk-QLQ6O7OW.js.map +0 -1
  196. package/dist/chunk-RDUPELOK.js +0 -55
  197. package/dist/chunk-RDUPELOK.js.map +0 -1
  198. package/dist/chunk-RPMHI67T.js +0 -104
  199. package/dist/chunk-RPMHI67T.js.map +0 -1
  200. package/dist/chunk-TE4RUYVK.js +0 -97
  201. package/dist/chunk-TE4RUYVK.js.map +0 -1
  202. package/dist/chunk-TJVBFRIE.js +0 -60
  203. package/dist/chunk-TJVBFRIE.js.map +0 -1
  204. package/dist/chunk-U5OKNKYC.js +0 -510
  205. package/dist/chunk-U5OKNKYC.js.map +0 -1
  206. package/dist/chunk-UXJO4AJP.js +0 -290
  207. package/dist/chunk-UXJO4AJP.js.map +0 -1
  208. package/dist/chunk-VNXXENCU.js +0 -39
  209. package/dist/chunk-VNXXENCU.js.map +0 -1
  210. package/dist/chunk-XGK73UCD.js +0 -62
  211. package/dist/chunk-XGK73UCD.js.map +0 -1
  212. package/dist/chunk-XJYLFTYE.js +0 -157
  213. package/dist/chunk-XJYLFTYE.js.map +0 -1
  214. package/dist/chunk-YCESNTXQ.js +0 -241
  215. package/dist/chunk-YCESNTXQ.js.map +0 -1
  216. package/dist/chunk-ZTJLVNR4.js +0 -62
  217. package/dist/chunk-ZTJLVNR4.js.map +0 -1
  218. package/dist/disclosure-element.js +0 -9
  219. package/dist/disclosure-element.js.map +0 -1
  220. package/dist/disclosure.d.ts +0 -2
  221. package/dist/disclosure.js +0 -16
  222. package/dist/disclosure.js.map +0 -1
  223. package/dist/exit-this-page-element.d.ts +0 -22
  224. package/dist/exit-this-page-element.js +0 -13
  225. package/dist/exit-this-page-element.js.map +0 -1
  226. package/dist/exit-this-page.d.ts +0 -3
  227. package/dist/exit-this-page.js +0 -25
  228. package/dist/exit-this-page.js.map +0 -1
  229. package/dist/file-upload-element.d.ts +0 -27
  230. package/dist/file-upload-element.js +0 -14
  231. package/dist/file-upload-element.js.map +0 -1
  232. package/dist/file-upload.d.ts +0 -3
  233. package/dist/file-upload.js +0 -25
  234. package/dist/file-upload.js.map +0 -1
  235. package/dist/focus-links-element.d.ts +0 -3
  236. package/dist/focus-links-element.js +0 -10
  237. package/dist/focus-links-element.js.map +0 -1
  238. package/dist/focus-links.d.ts +0 -2
  239. package/dist/focus-links.js +0 -12
  240. package/dist/focus-links.js.map +0 -1
  241. package/dist/index--FxJgSOS.d.ts +0 -108
  242. package/dist/index-Bul0NJh_.d.ts +0 -310
  243. package/dist/index-CikOyFud.d.ts +0 -54
  244. package/dist/index-D8N5vxTL.d.ts +0 -90
  245. package/dist/index-DMWpkC_d.d.ts +0 -105
  246. package/dist/index-DN7fGMc3.d.ts +0 -20
  247. package/dist/index-DPXR1Yua.d.ts +0 -69
  248. package/dist/inline-script.d.ts +0 -29
  249. package/dist/inline-script.js +0 -63
  250. package/dist/inline-script.js.map +0 -1
  251. package/dist/modal-element.js +0 -11
  252. package/dist/modal-element.js.map +0 -1
  253. package/dist/modal.d.ts +0 -2
  254. package/dist/modal.js +0 -20
  255. package/dist/modal.js.map +0 -1
  256. package/dist/password-input-element.d.ts +0 -3
  257. package/dist/password-input-element.js +0 -9
  258. package/dist/password-input-element.js.map +0 -1
  259. package/dist/password-input.d.ts +0 -2
  260. package/dist/password-input.js +0 -18
  261. package/dist/password-input.js.map +0 -1
  262. package/dist/tabs-element.js +0 -9
  263. package/dist/tabs-element.js.map +0 -1
  264. package/dist/tabs.d.ts +0 -2
  265. package/dist/tabs.js +0 -28
  266. package/dist/tabs.js.map +0 -1
  267. package/dist/translation-BnpO2mN2.d.ts +0 -4
@@ -0,0 +1,597 @@
1
+ /*! Based on accessible-autocomplete, Copyright (c) 2017 Crown Copyright (Government Digital Service), MIT licence: licences/accessible-autocomplete.txt */
2
+ var _a;
3
+ /*
4
+ * <govuk-react-autocomplete>: a headless, light-DOM custom element that suggests options as the
5
+ * user types into a text input, with or without React. A port of GOV.UK's accessible-autocomplete
6
+ * (https://github.com/alphagov/accessible-autocomplete), which enhances an existing input rather
7
+ * than rendering its own.
8
+ *
9
+ * Without JavaScript the input is a plain input and the options stay hidden. On upgrade the input
10
+ * becomes a combobox, as in accessible-autocomplete: options matching the query show in the
11
+ * listbox, Up and Down move focus through them, Enter, Space or a click confirms one, and Escape
12
+ * closes the menu. Screen readers hear how many options there are once the user pauses.
13
+ *
14
+ * The options are whatever the listbox holds, so they can be rendered on the server, or replaced
15
+ * as the user types, such as from a `govuk-react-autocomplete:query` listener. The rendered
16
+ * markup is never moved, so React can keep rendering it.
17
+ *
18
+ * Markup inside the element:
19
+ * - `input[data-autocomplete-input]`: the input, with an `id`.
20
+ * - `[data-autocomplete-listbox]`: the options, rendered `hidden`.
21
+ * - `[data-autocomplete-option]`: an option. Its value is `data-value`, or its text.
22
+ * - `[data-autocomplete-no-results]` (optional): shown when no option matches, rendered `hidden`.
23
+ * - `[data-autocomplete-status]` (optional): visually hidden, holding two
24
+ * `[data-autocomplete-status-region]`s, each with `role="status"`. They're updated alternately,
25
+ * so a repeated message is still announced.
26
+ * - `[data-autocomplete-hint]` (optional): how to use the options, rendered `hidden`. It
27
+ * describes the input while the query is empty.
28
+ *
29
+ * The element gets `data-focused` while focus is in it, and the option in focus, or highlighted
30
+ * while focus is on the input, gets `data-focused`.
31
+ *
32
+ * Attributes:
33
+ * - `min-length`: the length the query needs before options show. 0 by default.
34
+ * - `filter`: which options match the query: "contains" (the default), "starts-with", or "none"
35
+ * for options that already match, such as results from a server.
36
+ * - `confirm-on-blur`: "false" to leave the query alone when focus leaves an option. By default,
37
+ * as in accessible-autocomplete, the highlighted option is confirmed.
38
+ * - `submit-on-confirm`: submit the input's form when the user confirms an option, and let Enter
39
+ * submit it while the menu is open with no option highlighted.
40
+ * - `query-too-short-announcement`, `no-results-announcement`, `selected-option-announcement`, `results-announcement`:
41
+ * what screen readers hear, English by default, with `%{count}` for the number and, in
42
+ * `selected-option-announcement`, `%{option}` and `%{position}`. Add a plural form to the name, such
43
+ * as `results-announcement-one`, for a message for each form.
44
+ *
45
+ * Events, which bubble:
46
+ * - `govuk-react-autocomplete:query`, when the user changes the query and options are due.
47
+ * - `govuk-react-autocomplete:confirm`, when an option is confirmed. Cancel it to stop
48
+ * `submit-on-confirm` submitting the form.
49
+ */
50
+ import { translate, translationAttributeNames, translationFromAttributes, withLocale, } from '../utils/translation.js';
51
+ import { setText, setAttribute } from '../utils/dom.js';
52
+ export const tagName = 'govuk-react-autocomplete';
53
+ /** accessible-autocomplete's English text. */
54
+ export const defaults = {
55
+ 'query-too-short-announcement': 'Type in %{count} or more characters for results',
56
+ 'no-results-announcement': 'No search results',
57
+ 'selected-option-announcement': '%{option} %{position} of %{count} is highlighted',
58
+ 'results-announcement': { one: '%{count} result is available.', other: '%{count} results are available.' },
59
+ };
60
+ /** As accessible-autocomplete, which waits this long after the last change before announcing. */
61
+ const statusDelay = 1400;
62
+ /** As accessible-autocomplete, which polls for speech recognition software changing the value. */
63
+ const pollInterval = 100;
64
+ const isIosDevice = () => typeof navigator !== 'undefined' &&
65
+ /(iPod|iPhone|iPad)/.test(navigator.userAgent) &&
66
+ /AppleWebKit/.test(navigator.userAgent);
67
+ const valueSetter = typeof HTMLInputElement === 'undefined'
68
+ ? undefined
69
+ : (_a = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')) === null || _a === void 0 ? void 0 : _a.set;
70
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
71
+ class GovukReactAutocomplete extends HTMLElement {
72
+ constructor() {
73
+ super(...arguments);
74
+ // accessible-autocomplete's state. `focused` and `selected` are -1 for the input.
75
+ this.focused = null;
76
+ this.hovered = null;
77
+ this.menuOpen = false;
78
+ this.options = [];
79
+ this.currentQuery = '';
80
+ this.validChoiceMade = false;
81
+ this.selected = null;
82
+ this.ariaHint = true;
83
+ this.lastFocused = null;
84
+ this.settingValue = false;
85
+ this.bump = false;
86
+ this.generatedIds = 0;
87
+ this.onPoll = () => {
88
+ // Speech recognition software, such as Dragon NaturallySpeaking, can set the value directly.
89
+ if (this.input && this.input.value !== this.currentQuery)
90
+ this.handleInputChange(this.input.value);
91
+ };
92
+ this.onInput = () => {
93
+ if (this.input && !this.settingValue)
94
+ this.handleInputChange(this.input.value);
95
+ };
96
+ this.onInputFocus = () => {
97
+ const reopen = !this.validChoiceMade && this.queryLongEnough && this.options.length > 0;
98
+ this.focused = -1;
99
+ if (reopen) {
100
+ this.menuOpen = true;
101
+ this.selected = -1;
102
+ }
103
+ this.render();
104
+ };
105
+ this.onFocusOut = (event) => {
106
+ const { target, relatedTarget } = event;
107
+ if (target === this.input) {
108
+ // Focus moving to an option has already set `focused`.
109
+ if (this.focused !== -1)
110
+ return;
111
+ const ios = isIosDevice();
112
+ this.handleComponentBlur({
113
+ menuOpen: this.menuOpen && ios,
114
+ query: ios ? this.currentQuery : this.valueOf(this.selectedOption),
115
+ });
116
+ return;
117
+ }
118
+ // An option React removed while focused: the new options move focus back to the input.
119
+ if (!(target instanceof HTMLElement) || !target.isConnected)
120
+ return;
121
+ const index = this.options.indexOf(target);
122
+ if (index === -1)
123
+ return;
124
+ const focusingOutsideComponent = relatedTarget === null;
125
+ const focusingInput = relatedTarget === this.input;
126
+ const focusingAnotherOption = this.focused !== index && this.focused !== -1;
127
+ if ((!focusingAnotherOption && focusingOutsideComponent) || !(focusingAnotherOption || focusingInput)) {
128
+ this.handleComponentBlur({
129
+ menuOpen: this.menuOpen && isIosDevice(),
130
+ query: this.valueOf(this.selectedOption),
131
+ });
132
+ }
133
+ };
134
+ this.onKeyDown = (event) => {
135
+ var _a, _b, _c;
136
+ const { target } = event;
137
+ const onInput = target === this.input;
138
+ const onOption = target instanceof HTMLElement && this.options.includes(target);
139
+ if (!onInput && !onOption)
140
+ return;
141
+ const selected = (_a = this.selected) !== null && _a !== void 0 ? _a : -1;
142
+ switch (event.key) {
143
+ case 'ArrowUp':
144
+ event.preventDefault();
145
+ if (this.menuOpen && selected !== -1)
146
+ this.focusOption(selected - 1);
147
+ break;
148
+ case 'ArrowDown':
149
+ event.preventDefault();
150
+ if (this.menuOpen && selected !== this.options.length - 1)
151
+ this.focusOption(selected + 1);
152
+ break;
153
+ case ' ':
154
+ if (this.focused !== null && this.focused !== -1) {
155
+ event.preventDefault();
156
+ this.confirm(this.focused);
157
+ }
158
+ break;
159
+ case 'Enter':
160
+ if (!this.menuOpen)
161
+ break;
162
+ if (selected >= 0) {
163
+ event.preventDefault();
164
+ this.confirm(selected);
165
+ }
166
+ else if (!this.submitOnConfirm) {
167
+ event.preventDefault();
168
+ }
169
+ break;
170
+ case 'Escape':
171
+ // Rather than leave focus on a hidden option.
172
+ if (onOption)
173
+ (_b = this.input) === null || _b === void 0 ? void 0 : _b.focus();
174
+ this.handleComponentBlur({ menuOpen: false, query: this.currentQuery });
175
+ break;
176
+ default:
177
+ if (onOption &&
178
+ !event.ctrlKey &&
179
+ !event.metaKey &&
180
+ !event.altKey &&
181
+ (event.key.length === 1 || event.key === 'Backspace')) {
182
+ (_c = this.input) === null || _c === void 0 ? void 0 : _c.focus();
183
+ }
184
+ }
185
+ };
186
+ // Stops the input blurring, so a click confirms the option instead of closing the menu.
187
+ this.onOptionMouseDown = (event) => {
188
+ if (this.optionOf(event.target) !== -1)
189
+ event.preventDefault();
190
+ };
191
+ this.onOptionClick = (event) => {
192
+ const index = this.optionOf(event.target);
193
+ if (index !== -1)
194
+ this.confirm(index);
195
+ };
196
+ this.onOptionMouseOver = (event) => {
197
+ const index = this.optionOf(event.target);
198
+ // As accessible-autocomplete: iOS Safari drops the click if hovering changes the option.
199
+ if (index === -1 || index === this.hovered || isIosDevice())
200
+ return;
201
+ this.hovered = index;
202
+ this.render();
203
+ };
204
+ this.onListMouseLeave = () => {
205
+ if (this.hovered === null)
206
+ return;
207
+ this.hovered = null;
208
+ this.render();
209
+ };
210
+ }
211
+ static get observedAttributes() {
212
+ return [
213
+ 'min-length',
214
+ 'filter',
215
+ 'confirm-on-blur',
216
+ 'submit-on-confirm',
217
+ ...Object.keys(defaults).flatMap(translationAttributeNames),
218
+ ];
219
+ }
220
+ get query() {
221
+ return this.input ? this.currentQuery : undefined;
222
+ }
223
+ connectedCallback() {
224
+ // The parts may be parsed or rendered later, and React can replace the options at any time.
225
+ this.observer = new MutationObserver((records) => this.onMutation(records));
226
+ this.observer.observe(this, {
227
+ childList: true,
228
+ subtree: true,
229
+ characterData: true,
230
+ attributes: true,
231
+ attributeFilter: ['aria-describedby', 'data-value'],
232
+ });
233
+ this.setup();
234
+ }
235
+ disconnectedCallback() {
236
+ var _a;
237
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
238
+ this.unbind();
239
+ }
240
+ attributeChangedCallback() {
241
+ // Wait for every attribute: React sets them one by one before connecting the element.
242
+ if (!this.isConnected)
243
+ return;
244
+ if (this.input)
245
+ this.render();
246
+ else
247
+ this.setup();
248
+ }
249
+ own(selector) {
250
+ return this.ownAll(selector)[0];
251
+ }
252
+ ownAll(selector) {
253
+ return Array.from(this.querySelectorAll(selector)).filter((element) => element.closest(tagName) === this);
254
+ }
255
+ get allOptions() {
256
+ return this.ownAll('[data-autocomplete-option]');
257
+ }
258
+ get minLength() {
259
+ const value = Number(this.getAttribute('min-length'));
260
+ return Number.isFinite(value) && value > 0 ? value : 0;
261
+ }
262
+ get filterType() {
263
+ const filter = this.getAttribute('filter');
264
+ return filter === 'starts-with' || filter === 'none' ? filter : 'contains';
265
+ }
266
+ get confirmOnBlur() {
267
+ return this.getAttribute('confirm-on-blur') !== 'false';
268
+ }
269
+ get submitOnConfirm() {
270
+ return this.hasAttribute('submit-on-confirm');
271
+ }
272
+ get locale() {
273
+ var _a, _b;
274
+ return ((_b = ((_a = this.input) !== null && _a !== void 0 ? _a : this).closest('[lang]')) === null || _b === void 0 ? void 0 : _b.getAttribute('lang')) || 'en';
275
+ }
276
+ setup() {
277
+ const input = this.own('[data-autocomplete-input]');
278
+ const listbox = this.own('[data-autocomplete-listbox]');
279
+ if (!(input instanceof HTMLInputElement) || !input.id || !listbox) {
280
+ this.unbind();
281
+ delete this.dataset.upgraded;
282
+ return;
283
+ }
284
+ if (input === this.input && listbox === this.listbox) {
285
+ this.render();
286
+ return;
287
+ }
288
+ this.unbind();
289
+ this.input = input;
290
+ this.listbox = listbox;
291
+ input.addEventListener('input', this.onInput);
292
+ input.addEventListener('click', this.onInput);
293
+ input.addEventListener('focus', this.onInputFocus);
294
+ this.addEventListener('keydown', this.onKeyDown);
295
+ this.addEventListener('focusout', this.onFocusOut);
296
+ listbox.addEventListener('mousedown', this.onOptionMouseDown);
297
+ listbox.addEventListener('click', this.onOptionClick);
298
+ listbox.addEventListener('mouseover', this.onOptionMouseOver);
299
+ listbox.addEventListener('mouseleave', this.onListMouseLeave);
300
+ this.poll = window.setInterval(this.onPoll, pollInterval);
301
+ this.currentQuery = input.value;
302
+ this.ariaHint = input.value === '';
303
+ this.options = this.queryLongEnough ? this.matchingOptions() : [];
304
+ this.focused = document.activeElement === input ? -1 : null;
305
+ this.lastFocused = this.focused;
306
+ this.selected = null;
307
+ this.menuOpen = false;
308
+ this.validChoiceMade = false;
309
+ this.dataset.upgraded = '';
310
+ this.render();
311
+ }
312
+ unbind() {
313
+ const { input, listbox } = this;
314
+ window.clearInterval(this.poll);
315
+ window.clearTimeout(this.statusTimer);
316
+ if (!input || !listbox)
317
+ return;
318
+ input.removeEventListener('input', this.onInput);
319
+ input.removeEventListener('click', this.onInput);
320
+ input.removeEventListener('focus', this.onInputFocus);
321
+ this.removeEventListener('keydown', this.onKeyDown);
322
+ this.removeEventListener('focusout', this.onFocusOut);
323
+ listbox.removeEventListener('mousedown', this.onOptionMouseDown);
324
+ listbox.removeEventListener('click', this.onOptionClick);
325
+ listbox.removeEventListener('mouseover', this.onOptionMouseOver);
326
+ listbox.removeEventListener('mouseleave', this.onListMouseLeave);
327
+ this.input = undefined;
328
+ this.listbox = undefined;
329
+ }
330
+ onMutation(records) {
331
+ var _a, _b;
332
+ const status = this.own('[data-autocomplete-status]');
333
+ const relevant = records.filter((record) => {
334
+ var _a;
335
+ return !((_a = status === null || status === void 0 ? void 0 : status.contains(record.target)) !== null && _a !== void 0 ? _a : false) &&
336
+ !(record.type === 'attributes' && record.target === this.input && this.describedByIsCurrent());
337
+ });
338
+ if (!relevant.length)
339
+ return;
340
+ if (!((_a = this.input) === null || _a === void 0 ? void 0 : _a.isConnected) || !((_b = this.listbox) === null || _b === void 0 ? void 0 : _b.isConnected) || !this.contains(this.input)) {
341
+ this.setup();
342
+ return;
343
+ }
344
+ if (relevant.some((record) => { var _a; return (_a = this.listbox) === null || _a === void 0 ? void 0 : _a.contains(record.target); }))
345
+ this.updateOptions();
346
+ this.render();
347
+ }
348
+ /** New options from the page, as accessible-autocomplete's `source` callback gives them. */
349
+ updateOptions() {
350
+ const highlighted = this.selectedOption;
351
+ if (!this.queryLongEnough || this.currentQuery === '') {
352
+ this.options = [];
353
+ return;
354
+ }
355
+ this.options = this.matchingOptions();
356
+ if (this.focused === null || this.validChoiceMade)
357
+ return;
358
+ this.menuOpen = this.options.length > 0;
359
+ // Keep the highlighted option if it's still there, and otherwise go back to the input.
360
+ const index = highlighted ? this.options.indexOf(highlighted) : -1;
361
+ this.selected = index;
362
+ if (this.focused >= 0)
363
+ this.focused = index;
364
+ }
365
+ get queryLongEnough() {
366
+ return this.currentQuery.length >= this.minLength;
367
+ }
368
+ valueOf(option) {
369
+ var _a, _b, _c;
370
+ if (!option)
371
+ return undefined;
372
+ return (_c = (_a = option.dataset.value) !== null && _a !== void 0 ? _a : (_b = option.textContent) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : '';
373
+ }
374
+ matchingOptions() {
375
+ const query = this.currentQuery.toLowerCase();
376
+ const filter = this.filterType;
377
+ return this.allOptions.filter((option) => {
378
+ var _a;
379
+ if (filter === 'none')
380
+ return true;
381
+ const value = ((_a = this.valueOf(option)) !== null && _a !== void 0 ? _a : '').toLowerCase();
382
+ return filter === 'starts-with' ? value.startsWith(query) : value.includes(query);
383
+ });
384
+ }
385
+ isQueryAnOption(query) {
386
+ return this.options.some((option) => { var _a; return ((_a = this.valueOf(option)) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === query.toLowerCase(); });
387
+ }
388
+ handleInputChange(query) {
389
+ const queryChanged = this.currentQuery !== query;
390
+ this.currentQuery = query;
391
+ this.ariaHint = query === '';
392
+ if (query !== '' && queryChanged && this.queryLongEnough) {
393
+ this.dispatchEvent(new CustomEvent('govuk-react-autocomplete:query', { bubbles: true, detail: { query } }));
394
+ this.options = this.matchingOptions();
395
+ this.menuOpen = this.options.length > 0;
396
+ this.selected = -1;
397
+ this.validChoiceMade = false;
398
+ }
399
+ else if (query === '' || !this.queryLongEnough) {
400
+ this.menuOpen = false;
401
+ this.options = [];
402
+ }
403
+ this.render();
404
+ }
405
+ get selectedOption() {
406
+ return this.selected === null || this.selected < 0 ? undefined : this.options[this.selected];
407
+ }
408
+ handleComponentBlur({ menuOpen, query }) {
409
+ var _a;
410
+ let newQuery = this.currentQuery;
411
+ const option = this.selectedOption;
412
+ if (this.confirmOnBlur) {
413
+ newQuery = query || this.currentQuery;
414
+ if (option)
415
+ this.confirmEvent((_a = this.valueOf(option)) !== null && _a !== void 0 ? _a : '');
416
+ }
417
+ this.focused = null;
418
+ this.menuOpen = menuOpen;
419
+ this.selected = null;
420
+ this.validChoiceMade = this.isQueryAnOption(newQuery);
421
+ this.setQuery(newQuery);
422
+ this.render();
423
+ }
424
+ /** Sets the input's value as the user would, so React's `onChange` hears it. */
425
+ setQuery(query) {
426
+ this.currentQuery = query;
427
+ const { input } = this;
428
+ if (!input || input.value === query)
429
+ return;
430
+ this.settingValue = true;
431
+ if (valueSetter)
432
+ valueSetter.call(input, query);
433
+ else
434
+ input.value = query;
435
+ input.dispatchEvent(new Event('input', { bubbles: true }));
436
+ this.settingValue = false;
437
+ }
438
+ confirmEvent(value) {
439
+ return this.dispatchEvent(new CustomEvent('govuk-react-autocomplete:confirm', { bubbles: true, cancelable: true, detail: { value } }));
440
+ }
441
+ confirm(index) {
442
+ var _a, _b;
443
+ const option = this.options[index];
444
+ if (!option)
445
+ return;
446
+ const value = (_a = this.valueOf(option)) !== null && _a !== void 0 ? _a : '';
447
+ this.focused = -1;
448
+ this.hovered = null;
449
+ this.menuOpen = false;
450
+ this.selected = -1;
451
+ this.validChoiceMade = true;
452
+ this.setQuery(value);
453
+ this.render();
454
+ const proceed = this.confirmEvent(value);
455
+ // After React has updated a controlled input's state, so an `onSubmit` sees the option.
456
+ const form = (_b = this.input) === null || _b === void 0 ? void 0 : _b.form;
457
+ if (proceed && this.submitOnConfirm && form)
458
+ window.setTimeout(() => form.requestSubmit());
459
+ }
460
+ focusOption(index) {
461
+ this.focused = index;
462
+ this.hovered = null;
463
+ this.selected = index;
464
+ this.render();
465
+ }
466
+ optionOf(target) {
467
+ if (!(target instanceof Element))
468
+ return -1;
469
+ const option = target.closest('[data-autocomplete-option]');
470
+ return option ? this.options.indexOf(option) : -1;
471
+ }
472
+ ensureId(element, suffix) {
473
+ var _a;
474
+ if (!element.id) {
475
+ this.generatedIds += 1;
476
+ element.id = `${(_a = this.input) === null || _a === void 0 ? void 0 : _a.id}__${suffix}${suffix.endsWith('--') ? this.generatedIds : ''}`;
477
+ }
478
+ return element.id;
479
+ }
480
+ get hint() {
481
+ return this.own('[data-autocomplete-hint]');
482
+ }
483
+ describedBy() {
484
+ var _a;
485
+ const { input } = this;
486
+ if (!input)
487
+ return null;
488
+ const hint = this.hint;
489
+ const hintId = hint ? this.ensureId(hint, 'assistiveHint') : undefined;
490
+ const tokens = ((_a = input.getAttribute('aria-describedby')) !== null && _a !== void 0 ? _a : '')
491
+ .split(/\s+/)
492
+ .filter((token) => token && token !== hintId);
493
+ if (this.ariaHint && hintId)
494
+ tokens.push(hintId);
495
+ return tokens.length ? tokens.join(' ') : null;
496
+ }
497
+ describedByIsCurrent() {
498
+ var _a, _b;
499
+ return ((_b = (_a = this.input) === null || _a === void 0 ? void 0 : _a.getAttribute('aria-describedby')) !== null && _b !== void 0 ? _b : null) === this.describedBy();
500
+ }
501
+ /** Brings the markup up to date with the state, as accessible-autocomplete's render does. */
502
+ render() {
503
+ var _a, _b;
504
+ const { input, listbox } = this;
505
+ if (!input || !listbox)
506
+ return;
507
+ const { focused, hovered, menuOpen, options, selected } = this;
508
+ const inputFocused = focused === -1;
509
+ const showNoOptionsFound = inputFocused && options.length === 0 && this.currentQuery !== '' && this.queryLongEnough;
510
+ const optionFocused = focused !== null && focused !== -1;
511
+ const listboxId = this.ensureId(listbox, 'listbox');
512
+ setAttribute(listbox, 'role', 'listbox');
513
+ if (!listbox.hasAttribute('aria-labelledby'))
514
+ listbox.setAttribute('aria-labelledby', input.id);
515
+ listbox.hidden = !(menuOpen || showNoOptionsFound);
516
+ const all = this.allOptions;
517
+ all.forEach((option) => {
518
+ const index = options.indexOf(option);
519
+ this.ensureId(option, 'option--');
520
+ option.hidden = index === -1;
521
+ setAttribute(option, 'role', 'option');
522
+ setAttribute(option, 'tabindex', '-1');
523
+ setAttribute(option, 'aria-selected', focused === index && index !== -1 ? 'true' : 'false');
524
+ setAttribute(option, 'aria-posinset', index === -1 ? null : String(index + 1));
525
+ setAttribute(option, 'aria-setsize', index === -1 ? null : String(options.length));
526
+ const showFocused = index !== -1 && (focused === -1 ? selected === index : focused === index);
527
+ setAttribute(option, 'data-focused', showFocused && hovered === null ? '' : null);
528
+ });
529
+ const noResults = this.own('[data-autocomplete-no-results]');
530
+ if (noResults) {
531
+ noResults.hidden = !showNoOptionsFound;
532
+ setAttribute(noResults, 'role', 'option');
533
+ setAttribute(noResults, 'aria-disabled', 'true');
534
+ }
535
+ setAttribute(input, 'role', 'combobox');
536
+ setAttribute(input, 'autocomplete', 'off');
537
+ setAttribute(input, 'aria-autocomplete', 'list');
538
+ setAttribute(input, 'aria-controls', listboxId);
539
+ setAttribute(input, 'aria-expanded', menuOpen ? 'true' : 'false');
540
+ setAttribute(input, 'aria-activedescendant', optionFocused ? ((_b = (_a = options[focused]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null) : null);
541
+ setAttribute(input, 'aria-describedby', this.describedBy());
542
+ setAttribute(this, 'data-focused', focused === null ? null : '');
543
+ // As accessible-autocomplete's componentDidUpdate: focus follows `focused`, and the query is
544
+ // selected when focus comes into the input.
545
+ if (focused !== this.lastFocused && focused !== null) {
546
+ const element = focused === -1 ? input : options[focused];
547
+ if (element && document.activeElement !== element)
548
+ element.focus();
549
+ // Some input types, such as email, have no selection.
550
+ if (focused === -1 && this.lastFocused === null && input.selectionStart !== null) {
551
+ input.setSelectionRange(0, input.value.length);
552
+ }
553
+ }
554
+ this.lastFocused = focused;
555
+ this.scheduleStatus();
556
+ }
557
+ message(name) {
558
+ var _a;
559
+ return (_a = translationFromAttributes(this, name)) !== null && _a !== void 0 ? _a : defaults[name];
560
+ }
561
+ statusText() {
562
+ const { locale, options } = this;
563
+ if (!this.queryLongEnough)
564
+ return translate(this.message('query-too-short-announcement'), this.minLength, locale);
565
+ if (options.length === 0)
566
+ return translate(this.message('no-results-announcement'), 0, locale);
567
+ const results = translate(this.message('results-announcement'), options.length, locale);
568
+ const option = this.valueOf(this.selectedOption);
569
+ if (option === undefined || this.selected === null)
570
+ return results;
571
+ const position = withLocale(locale, (l) => new Intl.NumberFormat(l)).format(this.selected + 1);
572
+ const highlighted = translate(this.message('selected-option-announcement'), options.length, locale)
573
+ .replaceAll('%{option}', () => option)
574
+ .replaceAll('%{position}', () => position);
575
+ return `${results} ${highlighted}`;
576
+ }
577
+ // As accessible-autocomplete: every change clears the status, which is set again once there
578
+ // have been no changes for a while, in the other region, so the same text is announced again.
579
+ // It stays silent once focus has left or the user has made a choice.
580
+ scheduleStatus() {
581
+ const regions = this.ownAll('[data-autocomplete-status-region]');
582
+ if (!regions.length)
583
+ return;
584
+ regions.forEach((region) => setText(region, ''));
585
+ window.clearTimeout(this.statusTimer);
586
+ this.statusTimer = window.setTimeout(() => {
587
+ this.bump = !this.bump;
588
+ const silenced = this.focused === null || this.validChoiceMade;
589
+ const text = silenced ? '' : this.statusText();
590
+ regions.forEach((region, index) => setText(region, (index === 0) === this.bump ? text : ''));
591
+ }, statusDelay);
592
+ }
593
+ }
594
+ if (!customElements.get(tagName))
595
+ customElements.define(tagName, GovukReactAutocomplete);
596
+ }
597
+ //# sourceMappingURL=element.js.map