@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,202 @@
1
+ /*
2
+ * <govuk-react-choices>: a headless, light-DOM custom element for a group of radios or checkboxes,
3
+ * with or without React. It reveals content when a choice is checked, and makes a checkbox such
4
+ * as "None of these" exclusive, as govuk-frontend's radios and checkboxes do.
5
+ *
6
+ * Without JavaScript every conditional reveal is shown, and an exclusive checkbox is an ordinary
7
+ * one. On upgrade each reveal shows only while its choice is checked, and the choice gets
8
+ * `aria-controls` and `aria-expanded`.
9
+ *
10
+ * Markup inside the element:
11
+ * - `input[type=radio]` or `input[type=checkbox]`: the choices. `data-choices-controls` names the
12
+ * id of the content it reveals. A checkbox with `data-choices-exclusive` unchecks the other
13
+ * checkboxes with its name when checked, and is unchecked when one of them is.
14
+ * - `[data-choices-conditional]`: the content a choice reveals, with an id.
15
+ *
16
+ * As in govuk-frontend, that covers checkboxes with the same name and form anywhere on the page,
17
+ * and checking a radio updates the reveals of its whole group, even across elements. Choices are
18
+ * unchecked with `click()`, so `change` events fire and React's `onChange` sees them.
19
+ *
20
+ * Attributes:
21
+ * - `upgraded-class`: classes for the element once upgraded.
22
+ * - `hidden-conditional-class`: classes that hide a reveal. Without them, the element sets
23
+ * `hidden` on it.
24
+ */
25
+ import { classesOf } from '../utils/dom.js';
26
+ import { onPropertySet } from '../utils/on-property-set.js';
27
+ export const tagName = 'govuk-react-choices';
28
+ const isChoice = (element) => element instanceof HTMLInputElement && (element.type === 'radio' || element.type === 'checkbox');
29
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
30
+ class GovukReactChoices extends HTMLElement {
31
+ constructor() {
32
+ super(...arguments);
33
+ this.upgraded = false;
34
+ /** Puts back each choice's `checked` setter. */
35
+ this.watched = new Map();
36
+ this.syncQueued = false;
37
+ // A `checked` set by a script, such as a form library's, fires no event. Checking a radio can
38
+ // uncheck one in another element.
39
+ this.onCheckedSet = () => {
40
+ if (this.syncQueued)
41
+ return;
42
+ this.syncQueued = true;
43
+ queueMicrotask(() => {
44
+ this.syncQueued = false;
45
+ this.syncAll();
46
+ });
47
+ };
48
+ this.onClick = (event) => {
49
+ const input = event.target;
50
+ if (!isChoice(input) || !this.owns(input))
51
+ return;
52
+ if (input.type === 'radio') {
53
+ // Checking a radio unchecks another, with no event for it, maybe in another group.
54
+ this.syncAll();
55
+ return;
56
+ }
57
+ this.syncInput(input);
58
+ if (!input.checked)
59
+ return;
60
+ const exclusive = input.hasAttribute('data-choices-exclusive');
61
+ // As govuk-frontend: every checkbox with the name in the form, even in another group.
62
+ const others = this.inputsNamed(input).filter((other) => other !== input &&
63
+ other.type === 'checkbox' &&
64
+ other.checked &&
65
+ (exclusive || other.hasAttribute('data-choices-exclusive')));
66
+ // Unchecked once this click, and React's updates for it, are done, so each is its own event
67
+ // that React sees.
68
+ window.setTimeout(() => {
69
+ others
70
+ .filter((other) => other.checked)
71
+ .forEach((other) => {
72
+ // A disabled checkbox ignores `click()`.
73
+ if (other.disabled)
74
+ other.checked = false;
75
+ else
76
+ other.click();
77
+ });
78
+ this.syncAll();
79
+ });
80
+ };
81
+ // `reset` changes the choices with no event for each, after it fires.
82
+ this.onReset = (event) => {
83
+ if (event.target instanceof HTMLFormElement && this.choices().some((input) => input.form === event.target)) {
84
+ window.setTimeout(() => this.sync());
85
+ }
86
+ };
87
+ this.onChange = (event) => {
88
+ if (isChoice(event.target) && this.owns(event.target))
89
+ this.sync();
90
+ };
91
+ // A page restored from the back-forward cache keeps its choices.
92
+ this.onPageShow = () => this.sync();
93
+ }
94
+ connectedCallback() {
95
+ this.observer = new MutationObserver((records) => {
96
+ if (records.some((record) => record.type === 'childList'))
97
+ this.setup();
98
+ else {
99
+ // React can rewrite the element's own class.
100
+ if (this.upgraded)
101
+ this.addUpgradedClasses();
102
+ this.sync();
103
+ }
104
+ });
105
+ // Choices rendered later, and a class React rewrites.
106
+ this.observer.observe(this, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] });
107
+ this.addEventListener('click', this.onClick);
108
+ this.addEventListener('change', this.onChange);
109
+ window.addEventListener('pageshow', this.onPageShow);
110
+ document.addEventListener('reset', this.onReset, true);
111
+ this.setup();
112
+ }
113
+ disconnectedCallback() {
114
+ var _a;
115
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
116
+ this.removeEventListener('click', this.onClick);
117
+ this.removeEventListener('change', this.onChange);
118
+ window.removeEventListener('pageshow', this.onPageShow);
119
+ document.removeEventListener('reset', this.onReset, true);
120
+ this.watched.forEach((stop) => stop());
121
+ this.watched.clear();
122
+ }
123
+ get root() {
124
+ const root = this.getRootNode();
125
+ return root instanceof Document || root instanceof ShadowRoot ? root : document;
126
+ }
127
+ /** The inputs with the same name and form, anywhere on the page. */
128
+ inputsNamed(input) {
129
+ return Array.from(this.root.querySelectorAll('input')).filter((other) => other.type === input.type && other.name === input.name && other.form === input.form);
130
+ }
131
+ /** Syncs every element on the page, for choices that changed in another group. */
132
+ syncAll() {
133
+ Array.from(this.root.querySelectorAll(tagName)).forEach((element) => {
134
+ if (element instanceof GovukReactChoices)
135
+ element.sync();
136
+ });
137
+ }
138
+ owns(element) {
139
+ return element.closest(tagName) === this;
140
+ }
141
+ choices() {
142
+ return Array.from(this.querySelectorAll('input')).filter((input) => isChoice(input) && this.owns(input));
143
+ }
144
+ target(input) {
145
+ const id = input.getAttribute('data-choices-controls');
146
+ if (!id)
147
+ return undefined;
148
+ const root = this.getRootNode();
149
+ const target = root instanceof Document || root instanceof ShadowRoot ? root.getElementById(id) : null;
150
+ return target instanceof HTMLElement && target.hasAttribute('data-choices-conditional') ? target : undefined;
151
+ }
152
+ watch(choices) {
153
+ this.watched.forEach((stop, input) => {
154
+ if (choices.includes(input))
155
+ return;
156
+ stop();
157
+ this.watched.delete(input);
158
+ });
159
+ choices.forEach((input) => {
160
+ if (!this.watched.has(input))
161
+ this.watched.set(input, onPropertySet(input, 'checked', this.onCheckedSet));
162
+ });
163
+ }
164
+ setup() {
165
+ const choices = this.choices();
166
+ this.watch(choices);
167
+ if (!choices.length)
168
+ return;
169
+ if (!this.upgraded) {
170
+ this.upgraded = true;
171
+ this.dataset.upgraded = '';
172
+ this.addUpgradedClasses();
173
+ }
174
+ this.sync();
175
+ }
176
+ addUpgradedClasses() {
177
+ // `toggle` with force leaves a class that's already there alone, so the observer isn't woken.
178
+ classesOf(this.getAttribute('upgraded-class')).forEach((name) => this.classList.toggle(name, true));
179
+ }
180
+ sync() {
181
+ if (!this.upgraded)
182
+ return;
183
+ this.choices().forEach((input) => this.syncInput(input));
184
+ }
185
+ syncInput(input) {
186
+ const target = this.target(input);
187
+ if (!target)
188
+ return;
189
+ input.setAttribute('aria-controls', target.id);
190
+ input.setAttribute('aria-expanded', String(input.checked));
191
+ const hiddenClasses = classesOf(this.getAttribute('hidden-conditional-class'));
192
+ // `toggle` with force leaves a class that's already there alone, so the observer isn't woken.
193
+ if (hiddenClasses.length)
194
+ hiddenClasses.forEach((name) => target.classList.toggle(name, !input.checked));
195
+ else
196
+ target.hidden = !input.checked;
197
+ }
198
+ }
199
+ if (!customElements.get(tagName))
200
+ customElements.define(tagName, GovukReactCh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CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QAC3D,CAAC;QAEO,SAAS,CAAC,KAAuB;YACvC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAClC,IAAI,CAAC,MAAM;gBAAE,OAAO;YACpB,KAAK,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;YAC/C,KAAK,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;YAC3D,MAAM,aAAa,GAAG,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,0BAA0B,CAAC,CAAC,CAAC;YAC/E,8FAA8F;YAC9F,IAAI,aAAa,CAAC,MAAM;gBAAE,aAAa,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;;gBACpG,MAAM,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC;QACtC,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;AACtF,CAAC","sourcesContent":["/*\n * <govuk-react-choices>: a headless, light-DOM custom element for a group of radios or checkboxes,\n * with or without React. It reveals content when a choice is checked, and makes a checkbox such\n * as \"None of these\" exclusive, as govuk-frontend's radios and checkboxes do.\n *\n * Without JavaScript every conditional reveal is shown, and an exclusive checkbox is an ordinary\n * one. On upgrade each reveal shows only while its choice is checked, and the choice gets\n * `aria-controls` and `aria-expanded`.\n *\n * Markup inside the element:\n * - `input[type=radio]` or `input[type=checkbox]`: the choices. `data-choices-controls` names the\n * id of the content it reveals. A checkbox with `data-choices-exclusive` unchecks the other\n * checkboxes with its name when checked, and is unchecked when one of them is.\n * - `[data-choices-conditional]`: the content a choice reveals, with an id.\n *\n * As in govuk-frontend, that covers checkboxes with the same name and form anywhere on the page,\n * and checking a radio updates the reveals of its whole group, even across elements. Choices are\n * unchecked with `click()`, so `change` events fire and React's `onChange` sees them.\n *\n * Attributes:\n * - `upgraded-class`: classes for the element once upgraded.\n * - `hidden-conditional-class`: classes that hide a reveal. Without them, the element sets\n * `hidden` on it.\n */\n\nimport { classesOf } from '../utils/dom.js';\nimport { onPropertySet } from '../utils/on-property-set.js';\n\nexport const tagName = 'govuk-react-choices';\n\nexport interface ChoicesElement extends HTMLElement {\n /** Shows and hides the reveals to match the choices, such as after setting `checked`. */\n sync(): void;\n}\n\nconst isChoice = (element: EventTarget | null): element is HTMLInputElement =>\n element instanceof HTMLInputElement && (element.type === 'radio' || element.type === 'checkbox');\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactChoices extends HTMLElement implements ChoicesElement {\n private upgraded = false;\n\n private observer?: MutationObserver;\n\n /** Puts back each choice's `checked` setter. */\n private readonly watched = new Map<HTMLInputElement, () => void>();\n\n private syncQueued = false;\n\n // A `checked` set by a script, such as a form library's, fires no event. Checking a radio can\n // uncheck one in another element.\n private readonly onCheckedSet = () => {\n if (this.syncQueued) return;\n this.syncQueued = true;\n queueMicrotask(() => {\n this.syncQueued = false;\n this.syncAll();\n });\n };\n\n private readonly onClick = (event: Event) => {\n const input = event.target;\n if (!isChoice(input) || !this.owns(input)) return;\n if (input.type === 'radio') {\n // Checking a radio unchecks another, with no event for it, maybe in another group.\n this.syncAll();\n return;\n }\n this.syncInput(input);\n if (!input.checked) return;\n const exclusive = input.hasAttribute('data-choices-exclusive');\n // As govuk-frontend: every checkbox with the name in the form, even in another group.\n const others = this.inputsNamed(input).filter(\n (other) =>\n other !== input &&\n other.type === 'checkbox' &&\n other.checked &&\n (exclusive || other.hasAttribute('data-choices-exclusive'))\n );\n // Unchecked once this click, and React's updates for it, are done, so each is its own event\n // that React sees.\n window.setTimeout(() => {\n others\n .filter((other) => other.checked)\n .forEach((other) => {\n // A disabled checkbox ignores `click()`.\n if (other.disabled) other.checked = false;\n else other.click();\n });\n this.syncAll();\n });\n };\n\n // `reset` changes the choices with no event for each, after it fires.\n private readonly onReset = (event: Event) => {\n if (event.target instanceof HTMLFormElement && this.choices().some((input) => input.form === event.target)) {\n window.setTimeout(() => this.sync());\n }\n };\n\n private readonly onChange = (event: Event) => {\n if (isChoice(event.target) && this.owns(event.target)) this.sync();\n };\n\n // A page restored from the back-forward cache keeps its choices.\n private readonly onPageShow = () => this.sync();\n\n connectedCallback() {\n this.observer = new MutationObserver((records) => {\n if (records.some((record) => record.type === 'childList')) this.setup();\n else {\n // React can rewrite the element's own class.\n if (this.upgraded) this.addUpgradedClasses();\n this.sync();\n }\n });\n // Choices rendered later, and a class React rewrites.\n this.observer.observe(this, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] });\n this.addEventListener('click', this.onClick);\n this.addEventListener('change', this.onChange);\n window.addEventListener('pageshow', this.onPageShow);\n document.addEventListener('reset', this.onReset, true);\n this.setup();\n }\n\n disconnectedCallback() {\n this.observer?.disconnect();\n this.removeEventListener('click', this.onClick);\n this.removeEventListener('change', this.onChange);\n window.removeEventListener('pageshow', this.onPageShow);\n document.removeEventListener('reset', this.onReset, true);\n this.watched.forEach((stop) => stop());\n this.watched.clear();\n }\n\n private get root() {\n const root = this.getRootNode();\n return root instanceof Document || root instanceof ShadowRoot ? root : document;\n }\n\n /** The inputs with the same name and form, anywhere on the page. */\n private inputsNamed(input: HTMLInputElement) {\n return Array.from(this.root.querySelectorAll('input')).filter(\n (other) => other.type === input.type && other.name === input.name && other.form === input.form\n );\n }\n\n /** Syncs every element on the page, for choices that changed in another group. */\n private syncAll() {\n Array.from(this.root.querySelectorAll(tagName)).forEach((element) => {\n if (element instanceof GovukReactChoices) element.sync();\n });\n }\n\n private owns(element: Element) {\n return element.closest(tagName) === this;\n }\n\n private choices() {\n return Array.from(this.querySelectorAll('input')).filter((input) => isChoice(input) && this.owns(input));\n }\n\n private target(input: HTMLInputElement) {\n const id = input.getAttribute('data-choices-controls');\n if (!id) return undefined;\n const root = this.getRootNode();\n const target = root instanceof Document || root instanceof ShadowRoot ? root.getElementById(id) : null;\n return target instanceof HTMLElement && target.hasAttribute('data-choices-conditional') ? target : undefined;\n }\n\n private watch(choices: HTMLInputElement[]) {\n this.watched.forEach((stop, input) => {\n if (choices.includes(input)) return;\n stop();\n this.watched.delete(input);\n });\n choices.forEach((input) => {\n if (!this.watched.has(input)) this.watched.set(input, onPropertySet(input, 'checked', this.onCheckedSet));\n });\n }\n\n private setup() {\n const choices = this.choices();\n this.watch(choices);\n if (!choices.length) return;\n if (!this.upgraded) {\n this.upgraded = true;\n this.dataset.upgraded = '';\n this.addUpgradedClasses();\n }\n this.sync();\n }\n\n private addUpgradedClasses() {\n // `toggle` with force leaves a class that's already there alone, so the observer isn't woken.\n classesOf(this.getAttribute('upgraded-class')).forEach((name) => this.classList.toggle(name, true));\n }\n\n sync() {\n if (!this.upgraded) return;\n this.choices().forEach((input) => this.syncInput(input));\n }\n\n private syncInput(input: HTMLInputElement) {\n const target = this.target(input);\n if (!target) return;\n input.setAttribute('aria-controls', target.id);\n input.setAttribute('aria-expanded', String(input.checked));\n const hiddenClasses = classesOf(this.getAttribute('hidden-conditional-class'));\n // `toggle` with force leaves a class that's already there alone, so the observer isn't woken.\n if (hiddenClasses.length) hiddenClasses.forEach((name) => target.classList.toggle(name, !input.checked));\n else target.hidden = !input.checked;\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactChoices);\n}\n"]}
@@ -1,13 +1,12 @@
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
  /** Classes for the element once upgraded. */
6
5
  upgradedClass?: string;
7
6
  /** Classes that hide a reveal. Without them, `hidden` does. */
8
7
  hiddenConditionalClass?: string;
9
8
  };
10
- declare function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootProps): React.DOMElement<{
9
+ export declare function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootProps): React.DOMElement<{
11
10
  ref: React.RefObject<HTMLElement>;
12
11
  suppressHydrationWarning: boolean;
13
12
  'upgraded-class': string;
@@ -19,17 +18,17 @@ declare function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootP
19
18
  autoCapitalize?: "off" | "none" | "on" | "sentences" | "words" | "characters" | (string & {});
20
19
  autoFocus?: boolean;
21
20
  className?: string;
22
- contentEditable?: (boolean | "true" | "false") | "inherit" | "plaintext-only";
21
+ contentEditable?: (boolean | "false" | "true") | "inherit" | "plaintext-only";
23
22
  contextMenu?: string;
24
23
  dir?: string;
25
- draggable?: boolean | "true" | "false";
24
+ draggable?: boolean | "false" | "true";
26
25
  enterKeyHint?: "search" | "enter" | "done" | "go" | "next" | "previous" | "send";
27
26
  hidden?: boolean;
28
27
  id?: string;
29
28
  lang?: string;
30
29
  nonce?: string;
31
30
  slot?: string;
32
- spellCheck?: boolean | "true" | "false";
31
+ spellCheck?: boolean | "false" | "true";
33
32
  style?: React.CSSProperties;
34
33
  tabIndex?: number;
35
34
  title?: string;
@@ -58,7 +57,7 @@ declare function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootP
58
57
  results?: number;
59
58
  security?: string;
60
59
  unselectable?: "off" | "on";
61
- popover?: "" | "manual" | "auto" | "hint";
60
+ popover?: "" | "auto" | "manual" | "hint";
62
61
  popoverTargetAction?: "toggle" | "show" | "hide";
63
62
  popoverTarget?: string;
64
63
  inert?: boolean;
@@ -67,52 +66,52 @@ declare function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootP
67
66
  exportparts?: string;
68
67
  part?: string;
69
68
  "aria-activedescendant"?: string;
70
- "aria-atomic"?: boolean | "true" | "false";
69
+ "aria-atomic"?: boolean | "false" | "true";
71
70
  "aria-autocomplete"?: "none" | "list" | "inline" | "both";
72
71
  "aria-braillelabel"?: string;
73
72
  "aria-brailleroledescription"?: string;
74
- "aria-busy"?: boolean | "true" | "false";
75
- "aria-checked"?: boolean | "true" | "false" | "mixed";
73
+ "aria-busy"?: boolean | "false" | "true";
74
+ "aria-checked"?: boolean | "false" | "true" | "mixed";
76
75
  "aria-colcount"?: number;
77
76
  "aria-colindex"?: number;
78
77
  "aria-colindextext"?: string;
79
78
  "aria-colspan"?: number;
80
79
  "aria-controls"?: string;
81
- "aria-current"?: boolean | "time" | "true" | "false" | "page" | "step" | "location" | "date";
80
+ "aria-current"?: boolean | "time" | "false" | "true" | "page" | "step" | "location" | "date";
82
81
  "aria-describedby"?: string;
83
82
  "aria-description"?: string;
84
83
  "aria-details"?: string;
85
- "aria-disabled"?: boolean | "true" | "false";
86
- "aria-dropeffect"?: "copy" | "link" | "none" | "execute" | "move" | "popup";
84
+ "aria-disabled"?: boolean | "false" | "true";
85
+ "aria-dropeffect"?: "link" | "copy" | "none" | "execute" | "move" | "popup";
87
86
  "aria-errormessage"?: string;
88
- "aria-expanded"?: boolean | "true" | "false";
87
+ "aria-expanded"?: boolean | "false" | "true";
89
88
  "aria-flowto"?: string;
90
- "aria-grabbed"?: boolean | "true" | "false";
91
- "aria-haspopup"?: boolean | "dialog" | "menu" | "true" | "false" | "grid" | "listbox" | "tree";
92
- "aria-hidden"?: boolean | "true" | "false";
93
- "aria-invalid"?: boolean | "true" | "false" | "grammar" | "spelling";
89
+ "aria-grabbed"?: boolean | "false" | "true";
90
+ "aria-haspopup"?: boolean | "dialog" | "menu" | "false" | "true" | "grid" | "listbox" | "tree";
91
+ "aria-hidden"?: boolean | "false" | "true";
92
+ "aria-invalid"?: boolean | "false" | "true" | "grammar" | "spelling";
94
93
  "aria-keyshortcuts"?: string;
95
94
  "aria-label"?: string;
96
95
  "aria-labelledby"?: string;
97
96
  "aria-level"?: number;
98
97
  "aria-live"?: "off" | "assertive" | "polite";
99
- "aria-modal"?: boolean | "true" | "false";
100
- "aria-multiline"?: boolean | "true" | "false";
101
- "aria-multiselectable"?: boolean | "true" | "false";
98
+ "aria-modal"?: boolean | "false" | "true";
99
+ "aria-multiline"?: boolean | "false" | "true";
100
+ "aria-multiselectable"?: boolean | "false" | "true";
102
101
  "aria-orientation"?: "horizontal" | "vertical";
103
102
  "aria-owns"?: string;
104
103
  "aria-placeholder"?: string;
105
104
  "aria-posinset"?: number;
106
- "aria-pressed"?: boolean | "true" | "false" | "mixed";
107
- "aria-readonly"?: boolean | "true" | "false";
105
+ "aria-pressed"?: boolean | "false" | "true" | "mixed";
106
+ "aria-readonly"?: boolean | "false" | "true";
108
107
  "aria-relevant"?: "text" | "additions" | "additions removals" | "additions text" | "all" | "removals" | "removals additions" | "removals text" | "text additions" | "text removals";
109
- "aria-required"?: boolean | "true" | "false";
108
+ "aria-required"?: boolean | "false" | "true";
110
109
  "aria-roledescription"?: string;
111
110
  "aria-rowcount"?: number;
112
111
  "aria-rowindex"?: number;
113
112
  "aria-rowindextext"?: string;
114
113
  "aria-rowspan"?: number;
115
- "aria-selected"?: boolean | "true" | "false";
114
+ "aria-selected"?: boolean | "false" | "true";
116
115
  "aria-setsize"?: number;
117
116
  "aria-sort"?: "none" | "ascending" | "descending" | "other";
118
117
  "aria-valuemax"?: number;
@@ -296,28 +295,15 @@ type PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E>
296
295
  /** Render the only child instead, with this part's props merged onto it. */
297
296
  asChild?: boolean;
298
297
  };
299
- type InputProps = PartProps<'input'> & {
298
+ export type InputProps = PartProps<'input'> & {
300
299
  /** The id of the `Conditional` this choice reveals. */
301
300
  controls?: string;
302
301
  /** For a checkbox such as "None of these": checking it unchecks the others with its name. */
303
302
  exclusive?: boolean;
304
303
  };
305
304
  /** A radio or checkbox. */
306
- declare function Input({ asChild, controls, exclusive, ...props }: InputProps): React.JSX.Element;
307
- type ConditionalProps = PartProps<'div'>;
305
+ export declare function Input({ asChild, controls, exclusive, ...props }: InputProps): React.JSX.Element;
306
+ export type ConditionalProps = PartProps<'div'>;
308
307
  /** Content revealed while its choice is checked. Give it the id the choice `controls`. */
309
- declare function Conditional({ asChild, ...props }: ConditionalProps): React.JSX.Element;
310
-
311
- declare const index_Conditional: typeof Conditional;
312
- type index_ConditionalProps = ConditionalProps;
313
- declare const index_Input: typeof Input;
314
- type index_InputProps = InputProps;
315
- declare const index_Root: typeof Root;
316
- type index_RootProps = RootProps;
317
- declare namespace index {
318
- export { index_Conditional as Conditional, index_Input as Input, index_Root as Root };
319
- export type { index_ConditionalProps as ConditionalProps, index_InputProps as InputProps, index_RootProps as RootProps };
320
- }
321
-
322
- export { Conditional as C, Input as I, Root as R, index as i };
323
- export type { ConditionalProps as a, InputProps as b, RootProps as c };
308
+ export declare function Conditional({ asChild, ...props }: ConditionalProps): React.JSX.Element;
309
+ export {};
@@ -0,0 +1,37 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { useEffect, useRef } from 'react';
4
+ import { Slot } from '../utils/slot.js';
5
+ import { tagName } from './element.js';
6
+ const isChoices = (element) => 'sync' in element;
7
+ export function Root({ upgradedClass, hiddenConditionalClass, ...props }) {
8
+ const element = useRef(null);
9
+ // A `checked` set by React fires no event. A change that re-renders only an `Input`, not Root,
10
+ // shows once Root re-renders or the user next picks.
11
+ useEffect(() => {
12
+ const node = element.current;
13
+ if (node && isChoices(node))
14
+ node.sync();
15
+ });
16
+ return React.createElement(tagName, {
17
+ ...props,
18
+ ref: element,
19
+ // The element owns `data-upgraded` and its upgraded classes.
20
+ suppressHydrationWarning: true,
21
+ 'upgraded-class': upgradedClass,
22
+ 'hidden-conditional-class': hiddenConditionalClass,
23
+ });
24
+ }
25
+ /** A radio or checkbox. */
26
+ export function Input({ asChild, controls, exclusive, ...props }) {
27
+ const Element = asChild ? Slot : 'input';
28
+ // The element owns `aria-controls` and `aria-expanded` after upgrade.
29
+ return (React.createElement(Element, { "data-choices-controls": controls, "data-choices-exclusive": exclusive ? '' : undefined, suppressHydrationWarning: true, ...props }));
30
+ }
31
+ /** Content revealed while its choice is checked. Give it the id the choice `controls`. */
32
+ export function Conditional({ asChild, ...props }) {
33
+ const Element = asChild ? Slot : 'div';
34
+ // The element owns whether it's hidden after upgrade.
35
+ return React.createElement(Element, { "data-choices-conditional": "", suppressHydrationWarning: true, ...props });
36
+ }
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/choices/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAuB,MAAM,cAAc,CAAC;AAiB5D,MAAM,SAAS,GAAG,CAAC,OAAoB,EAA6B,EAAE,CAAC,MAAM,IAAI,OAAO,CAAC;AAEzF,MAAM,UAAU,IAAI,CAAC,EAAE,aAAa,EAAE,sBAAsB,EAAE,GAAG,KAAK,EAAa;IACjF,MAAM,OAAO,GAAG,MAAM,CAAc,IAAI,CAAC,CAAC;IAE1C,+FAA+F;IAC/F,qDAAqD;IACrD,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC;QAC7B,IAAI,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC;YAAE,IAAI,CAAC,IAAI,EAAE,CAAC;IAC3C,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE;QAClC,GAAG,KAAK;QACR,GAAG,EAAE,OAAO;QACZ,6DAA6D;QAC7D,wBAAwB,EAAE,IAAI;QAC9B,gBAAgB,EAAE,aAAa;QAC/B,0BAA0B,EAAE,sBAAsB;KACnD,CAAC,CAAC;AACL,CAAC;AAcD,2BAA2B;AAC3B,MAAM,UAAU,KAAK,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,KAAK,EAAc;IAC1E,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC;IACzC,sEAAsE;IACtE,OAAO,CACL,oBAAC,OAAO,6BACiB,QAAQ,4BACP,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAClD,wBAAwB,WACpB,KAAK,GACT,CACH,CAAC;AACJ,CAAC;AAID,0FAA0F;AAC1F,MAAM,UAAU,WAAW,CAAC,EAAE,OAAO,EAAE,GAAG,KAAK,EAAoB;IACjE,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACvC,sDAAsD;IACtD,OAAO,oBAAC,OAAO,gCAA0B,EAAE,EAAC,wBAAwB,WAAK,KAAK,GAAI,CAAC;AACrF,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useEffect, useRef } from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName, type ChoicesElement } from './element.js';\n\n/*\n * Headless radios and checkboxes behaviour: unstyled parts over the <govuk-react-choices> element\n * (./element.ts), which reveals content when a choice is checked, and makes a checkbox exclusive.\n * Every part except Root takes `asChild`.\n *\n * The inputs' `checked` is the state, so control them as usual, with `checked` and `onChange`.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** Classes for the element once upgraded. */\n upgradedClass?: string;\n /** Classes that hide a reveal. Without them, `hidden` does. */\n hiddenConditionalClass?: string;\n};\n\nconst isChoices = (element: HTMLElement): element is ChoicesElement => 'sync' in element;\n\nexport function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootProps) {\n const element = useRef<HTMLElement>(null);\n\n // A `checked` set by React fires no event. A change that re-renders only an `Input`, not Root,\n // shows once Root re-renders or the user next picks.\n useEffect(() => {\n const node = element.current;\n if (node && isChoices(node)) node.sync();\n });\n\n return React.createElement(tagName, {\n ...props,\n ref: element,\n // The element owns `data-upgraded` and its upgraded classes.\n suppressHydrationWarning: true,\n 'upgraded-class': upgradedClass,\n 'hidden-conditional-class': hiddenConditionalClass,\n });\n}\n\ntype PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n};\n\nexport type InputProps = PartProps<'input'> & {\n /** The id of the `Conditional` this choice reveals. */\n controls?: string;\n /** For a checkbox such as \"None of these\": checking it unchecks the others with its name. */\n exclusive?: boolean;\n};\n\n/** A radio or checkbox. */\nexport function Input({ asChild, controls, exclusive, ...props }: InputProps) {\n const Element = asChild ? Slot : 'input';\n // The element owns `aria-controls` and `aria-expanded` after upgrade.\n return (\n <Element\n data-choices-controls={controls}\n data-choices-exclusive={exclusive ? '' : undefined}\n suppressHydrationWarning\n {...props}\n />\n );\n}\n\nexport type ConditionalProps = PartProps<'div'>;\n\n/** Content revealed while its choice is checked. Give it the id the choice `controls`. */\nexport function Conditional({ asChild, ...props }: ConditionalProps) {\n const Element = asChild ? Slot : 'div';\n // The element owns whether it's hidden after upgrade.\n return <Element data-choices-conditional=\"\" suppressHydrationWarning {...props} />;\n}\n"]}
@@ -5,13 +5,10 @@ declare global {
5
5
  }>;
6
6
  }
7
7
  }
8
- declare const tagName = "govuk-react-disclosure";
9
- interface DisclosureElement extends HTMLElement {
8
+ export declare const tagName = "govuk-react-disclosure";
9
+ export interface DisclosureElement extends HTMLElement {
10
10
  /** Whether the content is open, once upgraded, even while `media` shows it anyway. */
11
11
  readonly expanded: boolean | undefined;
12
12
  /** The user's last choice before `open` was first set, such as before React hydrated. */
13
13
  readonly pickedOpen: boolean | undefined;
14
14
  }
15
-
16
- export { tagName };
17
- export type { DisclosureElement };
@@ -0,0 +1,123 @@
1
+ /*
2
+ * <govuk-react-disclosure>: a headless, light-DOM custom element that adds a show and hide
3
+ * button to some content, such as a navigation menu on small screens, with or without React.
4
+ *
5
+ * Without JavaScript the content is shown and the button stays hidden. On upgrade the element
6
+ * shows the button and hides the content, and the button shows and hides it. The button's
7
+ * `aria-expanded` is the state.
8
+ *
9
+ * Markup inside the element:
10
+ * - `[data-disclosure-toggle]`: a `button` of type "button", rendered `hidden`.
11
+ * - `[data-disclosure-content]`: the content, with an id for the button's `aria-controls`.
12
+ *
13
+ * Attributes:
14
+ * - `media`: while this media query matches, the content is always shown and the button hidden,
15
+ * with `aria-hidden`, as in govuk-frontend's service navigation.
16
+ * - `open`: controls the content. "true" shows it, anything else hides it. A user's click only
17
+ * fires the change event, and the content changes when `open` does.
18
+ * - `default-open`: start with the content shown.
19
+ *
20
+ * A user's click fires a bubbling `govuk-react-disclosure:change` event with `{ open }`, for
21
+ * code that doesn't use React.
22
+ */
23
+ import { setUpWhenReady } from '../utils/set-up-when-ready.js';
24
+ export const tagName = 'govuk-react-disclosure';
25
+ if (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {
26
+ class GovukReactDisclosure extends HTMLElement {
27
+ constructor() {
28
+ super(...arguments);
29
+ this.isOpen = false;
30
+ this.controlled = false;
31
+ this.onClick = () => {
32
+ const open = !this.isOpen;
33
+ if (!this.hasAttribute('open')) {
34
+ this.apply(open);
35
+ if (!this.controlled)
36
+ this.picked = open;
37
+ }
38
+ this.dispatchEvent(new CustomEvent('govuk-react-disclosure:change', { detail: { open }, bubbles: true }));
39
+ };
40
+ this.onModeChange = () => this.render();
41
+ }
42
+ static get observedAttributes() {
43
+ return ['open'];
44
+ }
45
+ get expanded() {
46
+ return this.toggle ? this.isOpen : undefined;
47
+ }
48
+ get pickedOpen() {
49
+ return this.picked;
50
+ }
51
+ connectedCallback() {
52
+ this.observer = setUpWhenReady(this, () => this.setup());
53
+ }
54
+ disconnectedCallback() {
55
+ var _a, _b, _c;
56
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
57
+ (_b = this.toggle) === null || _b === void 0 ? void 0 : _b.removeEventListener('click', this.onClick);
58
+ (_c = this.mql) === null || _c === void 0 ? void 0 : _c.removeEventListener('change', this.onModeChange);
59
+ this.toggle = undefined;
60
+ this.content = undefined;
61
+ this.mql = undefined;
62
+ }
63
+ attributeChangedCallback() {
64
+ const open = this.getAttribute('open');
65
+ if (open === null)
66
+ return;
67
+ this.controlled = true;
68
+ this.apply(open === 'true');
69
+ }
70
+ own(selector, type) {
71
+ const element = Array.from(this.querySelectorAll(selector)).find((found) => found.closest(tagName) === this);
72
+ return element instanceof type ? element : undefined;
73
+ }
74
+ setup() {
75
+ if (this.toggle)
76
+ return true;
77
+ const toggle = this.own('[data-disclosure-toggle]', HTMLButtonElement);
78
+ const content = this.own('[data-disclosure-content]', HTMLElement);
79
+ if (!toggle || !content)
80
+ return false;
81
+ this.toggle = toggle;
82
+ this.content = content;
83
+ const open = this.getAttribute('open');
84
+ this.isOpen = open === null ? this.hasAttribute('default-open') : open === 'true';
85
+ const media = this.getAttribute('media');
86
+ if (media) {
87
+ this.mql = window.matchMedia(media);
88
+ this.mql.addEventListener('change', this.onModeChange);
89
+ }
90
+ toggle.addEventListener('click', this.onClick);
91
+ this.render();
92
+ return true;
93
+ }
94
+ apply(open) {
95
+ if (open === this.isOpen)
96
+ return;
97
+ this.isOpen = open;
98
+ this.render();
99
+ }
100
+ render() {
101
+ var _a;
102
+ const { toggle, content } = this;
103
+ if (!toggle || !content)
104
+ return;
105
+ if ((_a = this.mql) === null || _a === void 0 ? void 0 : _a.matches) {
106
+ content.hidden = false;
107
+ toggle.hidden = true;
108
+ toggle.setAttribute('aria-hidden', 'true');
109
+ toggle.removeAttribute('aria-expanded');
110
+ return;
111
+ }
112
+ toggle.hidden = false;
113
+ toggle.removeAttribute('aria-hidden');
114
+ toggle.setAttribute('aria-expanded', String(this.isOpen));
115
+ if (content.id)
116
+ toggle.setAttribute('aria-controls', content.id);
117
+ content.hidden = !this.isOpen;
118
+ }
119
+ }
120
+ if (!customElements.get(tagName))
121
+ customElements.define(tagName, GovukReactDisclosure);
122
+ }
123
+ //# sourceMappingURL=element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/disclosure/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAQ/D,MAAM,CAAC,MAAM,OAAO,GAAG,wBAAwB,CAAC;AAShD,IAAI,OAAO,WAAW,KAAK,WAAW,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE,CAAC;IAChF,MAAM,oBAAqB,SAAQ,WAAW;QAA9C;;YAaU,WAAM,GAAG,KAAK,CAAC;YAIf,eAAU,GAAG,KAAK,CAAC;YAEV,YAAO,GAAG,GAAG,EAAE;gBAC9B,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC;gBAC1B,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;oBAC/B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBACjB,IAAI,CAAC,IAAI,CAAC,UAAU;wBAAE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;gBAC3C,CAAC;gBACD,IAAI,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,+BAA+B,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;YAC5G,CAAC,CAAC;YAEe,iBAAY,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;QA4EtD,CAAC;QAvGC,MAAM,KAAK,kBAAkB;YAC3B,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC;QA2BD,IAAI,QAAQ;YACV,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;QAC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AAI,CAAC,OAAO;gBAAE,OAAO;YAChC,IAAI,MAAA,IAAI,CAAC,GAAG,0CAAE,OAAO,EAAE,CAAC;gBACtB,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC;gBACvB,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC;gBACrB,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;gBAC3C,MAAM,CAAC,eAAe,CAAC,eAAe,CAAC,CAAC;gBACxC,OAAO;YACT,CAAC;YACD,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC;YACtB,MAAM,CAAC,eAAe,CAAC,aAAa,CAAC,CAAC;YACtC,MAAM,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;YAC1D,IAAI,OAAO,CAAC,EAAE;gBAAE,MAAM,CAAC,YAAY,CAAC,eAAe,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;YACjE,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC;QAChC,CAAC;KACF;IAED,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,cAAc,CAAC,MAAM,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;AACzF,CAAC","sourcesContent":["/*\n * <govuk-react-disclosure>: a headless, light-DOM custom element that adds a show and hide\n * button to some content, such as a navigation menu on small screens, with or without React.\n *\n * Without JavaScript the content is shown and the button stays hidden. On upgrade the element\n * shows the button and hides the content, and the button shows and hides it. The button's\n * `aria-expanded` is the state.\n *\n * Markup inside the element:\n * - `[data-disclosure-toggle]`: a `button` of type \"button\", rendered `hidden`.\n * - `[data-disclosure-content]`: the content, with an id for the button's `aria-controls`.\n *\n * Attributes:\n * - `media`: while this media query matches, the content is always shown and the button hidden,\n * with `aria-hidden`, as in govuk-frontend's service navigation.\n * - `open`: controls the content. \"true\" shows it, anything else hides it. A user's click only\n * fires the change event, and the content changes when `open` does.\n * - `default-open`: start with the content shown.\n *\n * A user's click fires a bubbling `govuk-react-disclosure:change` event with `{ open }`, for\n * code that doesn't use React.\n */\n\nimport { setUpWhenReady } from '../utils/set-up-when-ready.js';\n\ndeclare global {\n interface HTMLElementEventMap {\n 'govuk-react-disclosure:change': CustomEvent<{ open: boolean }>;\n }\n}\n\nexport const tagName = 'govuk-react-disclosure';\n\nexport interface DisclosureElement extends HTMLElement {\n /** Whether the content is open, once upgraded, even while `media` shows it anyway. */\n readonly expanded: boolean | undefined;\n /** The user's last choice before `open` was first set, such as before React hydrated. */\n readonly pickedOpen: boolean | undefined;\n}\n\nif (typeof HTMLElement !== 'undefined' && typeof customElements !== 'undefined') {\n class GovukReactDisclosure extends HTMLElement implements DisclosureElement {\n static get observedAttributes() {\n return ['open'];\n }\n\n private toggle?: HTMLButtonElement;\n\n private content?: HTMLElement;\n\n private mql?: MediaQueryList;\n\n private observer?: MutationObserver;\n\n private isOpen = false;\n\n private picked?: boolean;\n\n private controlled = false;\n\n private readonly onClick = () => {\n const open = !this.isOpen;\n if (!this.hasAttribute('open')) {\n this.apply(open);\n if (!this.controlled) this.picked = open;\n }\n this.dispatchEvent(new CustomEvent('govuk-react-disclosure:change', { detail: { open }, bubbles: true }));\n };\n\n private readonly onModeChange = () => this.render();\n\n get expanded() {\n return this.toggle ? this.isOpen : undefined;\n }\n\n get pickedOpen() {\n return this.picked;\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.onClick);\n this.mql?.removeEventListener('change', this.onModeChange);\n this.toggle = undefined;\n this.content = undefined;\n this.mql = undefined;\n }\n\n attributeChangedCallback() {\n const open = this.getAttribute('open');\n if (open === null) return;\n this.controlled = true;\n this.apply(open === 'true');\n }\n\n private own<E extends Element>(selector: string, type: new () => E): E | undefined {\n const element = Array.from(this.querySelectorAll(selector)).find((found) => found.closest(tagName) === this);\n return element instanceof type ? element : undefined;\n }\n\n private setup() {\n if (this.toggle) return true;\n const toggle = this.own('[data-disclosure-toggle]', HTMLButtonElement);\n const content = this.own('[data-disclosure-content]', HTMLElement);\n if (!toggle || !content) return false;\n this.toggle = toggle;\n this.content = content;\n const open = this.getAttribute('open');\n this.isOpen = open === null ? this.hasAttribute('default-open') : open === 'true';\n const media = this.getAttribute('media');\n if (media) {\n this.mql = window.matchMedia(media);\n this.mql.addEventListener('change', this.onModeChange);\n }\n toggle.addEventListener('click', this.onClick);\n this.render();\n return true;\n }\n\n private apply(open: boolean) {\n if (open === this.isOpen) return;\n this.isOpen = open;\n this.render();\n }\n\n private render() {\n const { toggle, content } = this;\n if (!toggle || !content) return;\n if (this.mql?.matches) {\n content.hidden = false;\n toggle.hidden = true;\n toggle.setAttribute('aria-hidden', 'true');\n toggle.removeAttribute('aria-expanded');\n return;\n }\n toggle.hidden = false;\n toggle.removeAttribute('aria-hidden');\n toggle.setAttribute('aria-expanded', String(this.isOpen));\n if (content.id) toggle.setAttribute('aria-controls', content.id);\n content.hidden = !this.isOpen;\n }\n }\n\n if (!customElements.get(tagName)) customElements.define(tagName, GovukReactDisclosure);\n}\n"]}