@govuk-react/primitives 0.0.0-stage → 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 (121) hide show
  1. package/README.md +271 -2
  2. package/dist/accordion/element.d.ts +14 -0
  3. package/dist/accordion/element.js +302 -0
  4. package/dist/accordion/element.js.map +1 -0
  5. package/dist/accordion/index.d.ts +74 -0
  6. package/dist/accordion/index.js +138 -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 +297 -0
  12. package/dist/auto-focus/index.js +29 -0
  13. package/dist/auto-focus/index.js.map +1 -0
  14. package/dist/autocomplete/element.d.ts +27 -0
  15. package/dist/autocomplete/element.js +597 -0
  16. package/dist/autocomplete/element.js.map +1 -0
  17. package/dist/autocomplete/index.d.ts +350 -0
  18. package/dist/autocomplete/index.js +108 -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 +7 -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 +5 -0
  36. package/dist/choices/element.js +202 -0
  37. package/dist/choices/element.js.map +1 -0
  38. package/dist/choices/index.d.ts +309 -0
  39. package/dist/choices/index.js +37 -0
  40. package/dist/choices/index.js.map +1 -0
  41. package/dist/disclosure/element.d.ts +14 -0
  42. package/dist/disclosure/element.js +123 -0
  43. package/dist/disclosure/element.js.map +1 -0
  44. package/dist/disclosure/index.d.ts +307 -0
  45. package/dist/disclosure/index.js +66 -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 +201 -0
  49. package/dist/exit-this-page/element.js.map +1 -0
  50. package/dist/exit-this-page/index.d.ts +330 -0
  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 +23 -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/focus-links/index.d.ts +288 -0
  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 -0
  66. package/dist/index.js +14 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/inline/accordion-element.js +1 -0
  69. package/dist/inline/auto-focus-element.js +1 -0
  70. package/dist/inline/autocomplete-element.js +2 -0
  71. package/dist/inline/button-element.js +1 -0
  72. package/dist/inline/character-count-element.js +1 -0
  73. package/dist/inline/choices-element.js +1 -0
  74. package/dist/inline/disclosure-element.js +1 -0
  75. package/dist/inline/exit-this-page-element.js +1 -0
  76. package/dist/inline/file-upload-element.js +1 -0
  77. package/dist/inline/focus-links-element.js +1 -0
  78. package/dist/inline/modal-element.js +1 -0
  79. package/dist/inline/password-input-element.js +1 -0
  80. package/dist/inline/tabs-element.js +1 -0
  81. package/dist/inline-script/index.d.ts +26 -0
  82. package/dist/inline-script/index.js +31 -0
  83. package/dist/inline-script/index.js.map +1 -0
  84. package/dist/modal/element.d.ts +10 -0
  85. package/dist/modal/element.js +114 -0
  86. package/dist/modal/element.js.map +1 -0
  87. package/dist/modal/index.d.ts +36 -0
  88. package/dist/modal/index.js +109 -0
  89. package/dist/modal/index.js.map +1 -0
  90. package/dist/password-input/element.d.ts +1 -0
  91. package/dist/password-input/element.js +134 -0
  92. package/dist/password-input/element.js.map +1 -0
  93. package/dist/password-input/index.d.ts +318 -0
  94. package/dist/password-input/index.js +35 -0
  95. package/dist/password-input/index.js.map +1 -0
  96. package/dist/tabs/element.d.ts +19 -0
  97. package/dist/tabs/element.js +416 -0
  98. package/dist/tabs/element.js.map +1 -0
  99. package/dist/tabs/index.d.ts +83 -0
  100. package/dist/tabs/index.js +138 -0
  101. package/dist/tabs/index.js.map +1 -0
  102. package/dist/utils/dom.d.ts +9 -0
  103. package/dist/utils/dom.js +27 -0
  104. package/dist/utils/dom.js.map +1 -0
  105. package/dist/utils/on-property-set.d.ts +9 -0
  106. package/dist/utils/on-property-set.js +38 -0
  107. package/dist/utils/on-property-set.js.map +1 -0
  108. package/dist/utils/set-focus.d.ts +11 -0
  109. package/dist/utils/set-focus.js +38 -0
  110. package/dist/utils/set-focus.js.map +1 -0
  111. package/dist/utils/set-up-when-ready.d.ts +6 -0
  112. package/dist/utils/set-up-when-ready.js +16 -0
  113. package/dist/utils/set-up-when-ready.js.map +1 -0
  114. package/dist/utils/slot.d.ts +13 -0
  115. package/dist/utils/slot.js +40 -0
  116. package/dist/utils/slot.js.map +1 -0
  117. package/dist/utils/translation.d.ts +13 -0
  118. package/dist/utils/translation.js +57 -0
  119. package/dist/utils/translation.js.map +1 -0
  120. package/licences/accessible-autocomplete.txt +20 -0
  121. package/package.json +49 -4
@@ -0,0 +1,54 @@
1
+ /*
2
+ * Counting and count messages, shared by the <govuk-react-character-count> element and its React
3
+ * parts, so the server can render the message the element starts with.
4
+ */
5
+ import { translate, translationAttributes, translationFromAttributes, withLocale, } from '../utils/translation.js';
6
+ export const messageNames = [
7
+ 'characters-under-limit',
8
+ 'characters-at-limit',
9
+ 'characters-over-limit',
10
+ 'words-under-limit',
11
+ 'words-at-limit',
12
+ 'words-over-limit',
13
+ ];
14
+ /** govuk-frontend's English messages. `%{count}` is replaced with the number. */
15
+ export const defaultMessages = {
16
+ 'characters-under-limit': {
17
+ one: 'You have %{count} character remaining',
18
+ other: 'You have %{count} characters remaining',
19
+ },
20
+ 'characters-at-limit': 'You have 0 characters remaining',
21
+ 'characters-over-limit': {
22
+ one: 'You have %{count} character too many',
23
+ other: 'You have %{count} characters too many',
24
+ },
25
+ 'words-under-limit': { one: 'You have %{count} word remaining', other: 'You have %{count} words remaining' },
26
+ 'words-at-limit': 'You have 0 words remaining',
27
+ 'words-over-limit': { one: 'You have %{count} word too many', other: 'You have %{count} words too many' },
28
+ };
29
+ /** The attributes for a message: the message itself, or one attribute for each plural form. */
30
+ export const messageAttributes = (name, message) => translationAttributes(name, message);
31
+ /** Reads a message back from attributes written by `messageAttributes`. */
32
+ export const messageFromAttributes = (element, name) => translationFromAttributes(element, name);
33
+ /** Counts the text as govuk-frontend does: UTF-16 code units, characters as users see them, or words. */
34
+ export function countOf(text, countType, locale) {
35
+ var _a, _b;
36
+ if (countType === 'length')
37
+ return text.length;
38
+ if (typeof Intl.Segmenter !== 'function') {
39
+ // Older browsers: close enough, rather than no count at all.
40
+ return countType === 'words' ? ((_b = (_a = text.match(/\S+/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) : Array.from(text).length;
41
+ }
42
+ const segmenter = withLocale(locale, (l) => new Intl.Segmenter(l, { granularity: countType === 'words' ? 'word' : 'grapheme' }));
43
+ const segments = Array.from(segmenter.segment(text));
44
+ return countType === 'words' ? segments.filter((segment) => segment.isWordLike).length : segments.length;
45
+ }
46
+ /** The message for the number of characters or words left, with its state. */
47
+ export function countMessage(remaining, countType, messages, locale) {
48
+ var _a;
49
+ const state = remaining < 0 ? 'over' : remaining === 0 ? 'at' : 'under';
50
+ const name = `${countType === 'words' ? 'words' : 'characters'}-${state}-limit`;
51
+ const message = (_a = messages[name]) !== null && _a !== void 0 ? _a : defaultMessages[name];
52
+ return { state, text: translate(message, Math.abs(remaining), locale) };
53
+ }
54
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/character-count/messages.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,SAAS,EACT,qBAAqB,EACrB,yBAAyB,EACzB,UAAU,GAEX,MAAM,yBAAyB,CAAC;AAMjC,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,wBAAwB;IACxB,qBAAqB;IACrB,uBAAuB;IACvB,mBAAmB;IACnB,gBAAgB;IAChB,kBAAkB;CACV,CAAC;AAIX,iFAAiF;AACjF,MAAM,CAAC,MAAM,eAAe,GAAqC;IAC/D,wBAAwB,EAAE;QACxB,GAAG,EAAE,uCAAuC;QAC5C,KAAK,EAAE,wCAAwC;KAChD;IACD,qBAAqB,EAAE,iCAAiC;IACxD,uBAAuB,EAAE;QACvB,GAAG,EAAE,sCAAsC;QAC3C,KAAK,EAAE,uCAAuC;KAC/C;IACD,mBAAmB,EAAE,EAAE,GAAG,EAAE,kCAAkC,EAAE,KAAK,EAAE,mCAAmC,EAAE;IAC5G,gBAAgB,EAAE,4BAA4B;IAC9C,kBAAkB,EAAE,EAAE,GAAG,EAAE,iCAAiC,EAAE,KAAK,EAAE,kCAAkC,EAAE;CAC1G,CAAC;AAEF,+FAA+F;AAC/F,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,IAAiB,EAAE,OAAgC,EAAE,EAAE,CACvF,qBAAqB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAEvC,2EAA2E;AAC3E,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,OAAgB,EAAE,IAAiB,EAAE,EAAE,CAAC,yBAAyB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAEvH,yGAAyG;AACzG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AK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,QAAQ,CAAC;IAC7F,MAAM,OAAO,GAAG,MAAA,QAAQ,CAAC,IAAI,CAAC,mCAAI,eAAe,CAAC,IAAI,CAAC,CAAC;IACxD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC;AAC1E,CAAC","sourcesContent":["/*\n * Counting and count messages, shared by the <govuk-react-character-count> element and its React\n * parts, so the server can render the message the element starts with.\n */\n\nimport {\n translate,\n translationAttributes,\n translationFromAttributes,\n withLocale,\n type Translation,\n} from '../utils/translation.js';\n\nexport type { Translation };\n\nexport type CountType = 'length' | 'characters' | 'words';\n\nexport const messageNames = [\n 'characters-under-limit',\n 'characters-at-limit',\n 'characters-over-limit',\n 'words-under-limit',\n 'words-at-limit',\n 'words-over-limit',\n] as const;\n\nexport type MessageName = (typeof messageNames)[number];\n\n/** govuk-frontend's English messages. `%{count}` is replaced with the number. */\nexport const defaultMessages: Record<MessageName, Translation> = {\n 'characters-under-limit': {\n one: 'You have %{count} character remaining',\n other: 'You have %{count} characters remaining',\n },\n 'characters-at-limit': 'You have 0 characters remaining',\n 'characters-over-limit': {\n one: 'You have %{count} character too many',\n other: 'You have %{count} characters too many',\n },\n 'words-under-limit': { one: 'You have %{count} word remaining', other: 'You have %{count} words remaining' },\n 'words-at-limit': 'You have 0 words remaining',\n 'words-over-limit': { one: 'You have %{count} word too many', other: 'You have %{count} words too many' },\n};\n\n/** The attributes for a message: the message itself, or one attribute for each plural form. */\nexport const messageAttributes = (name: MessageName, message: Translation | undefined) =>\n translationAttributes(name, message);\n\n/** Reads a message back from attributes written by `messageAttributes`. */\nexport const messageFromAttributes = (element: Element, name: MessageName) => translationFromAttributes(element, name);\n\n/** Counts the text as govuk-frontend does: UTF-16 code units, characters as users see them, or words. */\nexport function countOf(text: string, countType: CountType, locale?: string) {\n if (countType === 'length') return text.length;\n if (typeof Intl.Segmenter !== 'function') {\n // Older browsers: close enough, rather than no count at all.\n return countType === 'words' ? (text.match(/\\S+/g)?.length ?? 0) : Array.from(text).length;\n }\n const segmenter = withLocale(\n locale,\n (l) => new Intl.Segmenter(l, { granularity: countType === 'words' ? 'word' : 'grapheme' })\n );\n const segments = Array.from(segmenter.segment(text));\n return countType === 'words' ? segments.filter((segment) => segment.isWordLike).length : segments.length;\n}\n\n/** The message for the number of characters or words left, with its state. */\nexport function countMessage(\n remaining: number,\n countType: CountType,\n messages: Partial<Record<MessageName, Translation>>,\n locale?: string\n) {\n const state = remaining < 0 ? 'over' : remaining === 0 ? 'at' : 'under';\n const name: MessageName = `${countType === 'words' ? 'words' : 'characters'}-${state}-limit`;\n const message = messages[name] ?? defaultMessages[name];\n return { state, text: translate(message, Math.abs(remaining), locale) };\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export declare const tagName = "govuk-react-choices";
2
+ export interface ChoicesElement extends HTMLElement {
3
+ /** Shows and hides the reveals to match the choices, such as after setting `checked`. */
4
+ sync(): void;
5
+ }
@@ -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, GovukReactChoices);
201
+ }
202
+ //# sourceMappingURL=element.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element.js","sourceRoot":"","sources":["../../src/choices/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,MAAM,CAAC,MAAM,OAAO,GAAG,qBAAqB,CAAC;AAO7C,MAAM,QAAQ,GAAG,CAAC,OAA2B,EAA+B,EAAE,CAC5E,OAAO,YAAY,gBAAgB,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;AAEnG,IAAI,OAAO,WAAW,KAAK,WAAW,IAAI,OAAO,cAAc,KAAK,WAAW,EAAE,CAAC;IAChF,MAAM,iBAAkB,SAAQ,WAAW;QAA3C;;YACU,aAAQ,GAAG,KAAK,CAAC;YAIzB,gDAAgD;YAC/B,YAAO,GAAG,IAAI,GAAG,EAAgC,CAAC;YAE3D,eAAU,GAAG,KAAK,CAAC;YAE3B,8FAA8F;YAC9F,kCAAkC;YACjB,iBAAY,GAAG,GAAG,EAAE;gBACnC,IAAI,IAAI,CAAC,UAAU;oBAAE,OAAO;gBAC5B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;gBACvB,cAAc,CAAC,GAAG,EAAE;oBAClB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;oBACxB,IAAI,CAAC,OAAO,EAAE,CAAC;gBACjB,CAAC,CAAC,CAAC;YACL,CAAC,CAAC;YAEe,YAAO,GAAG,CAAC,KAAY,EAAE,EAAE;gBAC1C,MAAM,KAAK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AC;QAED,IAAI;YACF,IAAI,CAAC,IAAI,CAAC,QAAQ;gBAAE,OAAO;YAC3B,IAAI,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,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"]}
@@ -0,0 +1,309 @@
1
+ import * as React from 'react';
2
+ import type { ComponentProps } from 'react';
3
+ export type RootProps = React.HTMLAttributes<HTMLElement> & {
4
+ /** Classes for the element once upgraded. */
5
+ upgradedClass?: string;
6
+ /** Classes that hide a reveal. Without them, `hidden` does. */
7
+ hiddenConditionalClass?: string;
8
+ };
9
+ export declare function Root({ upgradedClass, hiddenConditionalClass, ...props }: RootProps): React.DOMElement<{
10
+ ref: React.RefObject<HTMLElement>;
11
+ suppressHydrationWarning: boolean;
12
+ 'upgraded-class': string;
13
+ 'hidden-conditional-class': string;
14
+ defaultChecked?: boolean;
15
+ defaultValue?: string | number | readonly string[];
16
+ suppressContentEditableWarning?: boolean;
17
+ accessKey?: string;
18
+ autoCapitalize?: "off" | "none" | "on" | "sentences" | "words" | "characters" | (string & {});
19
+ autoFocus?: boolean;
20
+ className?: string;
21
+ contentEditable?: (boolean | "false" | "true") | "inherit" | "plaintext-only";
22
+ contextMenu?: string;
23
+ dir?: string;
24
+ draggable?: boolean | "false" | "true";
25
+ enterKeyHint?: "search" | "enter" | "done" | "go" | "next" | "previous" | "send";
26
+ hidden?: boolean;
27
+ id?: string;
28
+ lang?: string;
29
+ nonce?: string;
30
+ slot?: string;
31
+ spellCheck?: boolean | "false" | "true";
32
+ style?: React.CSSProperties;
33
+ tabIndex?: number;
34
+ title?: string;
35
+ translate?: "yes" | "no";
36
+ radioGroup?: string;
37
+ role?: React.AriaRole;
38
+ about?: string;
39
+ content?: string;
40
+ datatype?: string;
41
+ inlist?: any;
42
+ prefix?: string;
43
+ property?: string;
44
+ rel?: string;
45
+ resource?: string;
46
+ rev?: string;
47
+ typeof?: string;
48
+ vocab?: string;
49
+ autoCorrect?: string;
50
+ autoSave?: string;
51
+ color?: string;
52
+ itemProp?: string;
53
+ itemScope?: boolean;
54
+ itemType?: string;
55
+ itemID?: string;
56
+ itemRef?: string;
57
+ results?: number;
58
+ security?: string;
59
+ unselectable?: "off" | "on";
60
+ popover?: "" | "auto" | "manual" | "hint";
61
+ popoverTargetAction?: "toggle" | "show" | "hide";
62
+ popoverTarget?: string;
63
+ inert?: boolean;
64
+ inputMode?: "search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal";
65
+ is?: string;
66
+ exportparts?: string;
67
+ part?: string;
68
+ "aria-activedescendant"?: string;
69
+ "aria-atomic"?: boolean | "false" | "true";
70
+ "aria-autocomplete"?: "none" | "list" | "inline" | "both";
71
+ "aria-braillelabel"?: string;
72
+ "aria-brailleroledescription"?: string;
73
+ "aria-busy"?: boolean | "false" | "true";
74
+ "aria-checked"?: boolean | "false" | "true" | "mixed";
75
+ "aria-colcount"?: number;
76
+ "aria-colindex"?: number;
77
+ "aria-colindextext"?: string;
78
+ "aria-colspan"?: number;
79
+ "aria-controls"?: string;
80
+ "aria-current"?: boolean | "time" | "false" | "true" | "page" | "step" | "location" | "date";
81
+ "aria-describedby"?: string;
82
+ "aria-description"?: string;
83
+ "aria-details"?: string;
84
+ "aria-disabled"?: boolean | "false" | "true";
85
+ "aria-dropeffect"?: "link" | "copy" | "none" | "execute" | "move" | "popup";
86
+ "aria-errormessage"?: string;
87
+ "aria-expanded"?: boolean | "false" | "true";
88
+ "aria-flowto"?: string;
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";
93
+ "aria-keyshortcuts"?: string;
94
+ "aria-label"?: string;
95
+ "aria-labelledby"?: string;
96
+ "aria-level"?: number;
97
+ "aria-live"?: "off" | "assertive" | "polite";
98
+ "aria-modal"?: boolean | "false" | "true";
99
+ "aria-multiline"?: boolean | "false" | "true";
100
+ "aria-multiselectable"?: boolean | "false" | "true";
101
+ "aria-orientation"?: "horizontal" | "vertical";
102
+ "aria-owns"?: string;
103
+ "aria-placeholder"?: string;
104
+ "aria-posinset"?: number;
105
+ "aria-pressed"?: boolean | "false" | "true" | "mixed";
106
+ "aria-readonly"?: boolean | "false" | "true";
107
+ "aria-relevant"?: "text" | "additions" | "additions removals" | "additions text" | "all" | "removals" | "removals additions" | "removals text" | "text additions" | "text removals";
108
+ "aria-required"?: boolean | "false" | "true";
109
+ "aria-roledescription"?: string;
110
+ "aria-rowcount"?: number;
111
+ "aria-rowindex"?: number;
112
+ "aria-rowindextext"?: string;
113
+ "aria-rowspan"?: number;
114
+ "aria-selected"?: boolean | "false" | "true";
115
+ "aria-setsize"?: number;
116
+ "aria-sort"?: "none" | "ascending" | "descending" | "other";
117
+ "aria-valuemax"?: number;
118
+ "aria-valuemin"?: number;
119
+ "aria-valuenow"?: number;
120
+ "aria-valuetext"?: string;
121
+ children?: React.ReactNode;
122
+ dangerouslySetInnerHTML?: {
123
+ __html: string | TrustedHTML;
124
+ };
125
+ onCopy?: React.ClipboardEventHandler<HTMLElement>;
126
+ onCopyCapture?: React.ClipboardEventHandler<HTMLElement>;
127
+ onCut?: React.ClipboardEventHandler<HTMLElement>;
128
+ onCutCapture?: React.ClipboardEventHandler<HTMLElement>;
129
+ onPaste?: React.ClipboardEventHandler<HTMLElement>;
130
+ onPasteCapture?: React.ClipboardEventHandler<HTMLElement>;
131
+ onCompositionEnd?: React.CompositionEventHandler<HTMLElement>;
132
+ onCompositionEndCapture?: React.CompositionEventHandler<HTMLElement>;
133
+ onCompositionStart?: React.CompositionEventHandler<HTMLElement>;
134
+ onCompositionStartCapture?: React.CompositionEventHandler<HTMLElement>;
135
+ onCompositionUpdate?: React.CompositionEventHandler<HTMLElement>;
136
+ onCompositionUpdateCapture?: React.CompositionEventHandler<HTMLElement>;
137
+ onFocus?: React.FocusEventHandler<HTMLElement>;
138
+ onFocusCapture?: React.FocusEventHandler<HTMLElement>;
139
+ onBlur?: React.FocusEventHandler<HTMLElement>;
140
+ onBlurCapture?: React.FocusEventHandler<HTMLElement>;
141
+ onChange?: React.ChangeEventHandler<HTMLElement, Element>;
142
+ onChangeCapture?: React.ChangeEventHandler<HTMLElement, Element>;
143
+ onBeforeInput?: React.InputEventHandler<HTMLElement>;
144
+ onBeforeInputCapture?: React.InputEventHandler<HTMLElement>;
145
+ onInput?: React.InputEventHandler<HTMLElement>;
146
+ onInputCapture?: React.InputEventHandler<HTMLElement>;
147
+ onReset?: React.ReactEventHandler<HTMLElement>;
148
+ onResetCapture?: React.ReactEventHandler<HTMLElement>;
149
+ onSubmit?: React.SubmitEventHandler<HTMLElement>;
150
+ onSubmitCapture?: React.SubmitEventHandler<HTMLElement>;
151
+ onInvalid?: React.ReactEventHandler<HTMLElement>;
152
+ onInvalidCapture?: React.ReactEventHandler<HTMLElement>;
153
+ onLoad?: React.ReactEventHandler<HTMLElement>;
154
+ onLoadCapture?: React.ReactEventHandler<HTMLElement>;
155
+ onError?: React.ReactEventHandler<HTMLElement>;
156
+ onErrorCapture?: React.ReactEventHandler<HTMLElement>;
157
+ onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
158
+ onKeyDownCapture?: React.KeyboardEventHandler<HTMLElement>;
159
+ onKeyPress?: React.KeyboardEventHandler<HTMLElement>;
160
+ onKeyPressCapture?: React.KeyboardEventHandler<HTMLElement>;
161
+ onKeyUp?: React.KeyboardEventHandler<HTMLElement>;
162
+ onKeyUpCapture?: React.KeyboardEventHandler<HTMLElement>;
163
+ onAbort?: React.ReactEventHandler<HTMLElement>;
164
+ onAbortCapture?: React.ReactEventHandler<HTMLElement>;
165
+ onCanPlay?: React.ReactEventHandler<HTMLElement>;
166
+ onCanPlayCapture?: React.ReactEventHandler<HTMLElement>;
167
+ onCanPlayThrough?: React.ReactEventHandler<HTMLElement>;
168
+ onCanPlayThroughCapture?: React.ReactEventHandler<HTMLElement>;
169
+ onDurationChange?: React.ReactEventHandler<HTMLElement>;
170
+ onDurationChangeCapture?: React.ReactEventHandler<HTMLElement>;
171
+ onEmptied?: React.ReactEventHandler<HTMLElement>;
172
+ onEmptiedCapture?: React.ReactEventHandler<HTMLElement>;
173
+ onEncrypted?: React.ReactEventHandler<HTMLElement>;
174
+ onEncryptedCapture?: React.ReactEventHandler<HTMLElement>;
175
+ onEnded?: React.ReactEventHandler<HTMLElement>;
176
+ onEndedCapture?: React.ReactEventHandler<HTMLElement>;
177
+ onLoadedData?: React.ReactEventHandler<HTMLElement>;
178
+ onLoadedDataCapture?: React.ReactEventHandler<HTMLElement>;
179
+ onLoadedMetadata?: React.ReactEventHandler<HTMLElement>;
180
+ onLoadedMetadataCapture?: React.ReactEventHandler<HTMLElement>;
181
+ onLoadStart?: React.ReactEventHandler<HTMLElement>;
182
+ onLoadStartCapture?: React.ReactEventHandler<HTMLElement>;
183
+ onPause?: React.ReactEventHandler<HTMLElement>;
184
+ onPauseCapture?: React.ReactEventHandler<HTMLElement>;
185
+ onPlay?: React.ReactEventHandler<HTMLElement>;
186
+ onPlayCapture?: React.ReactEventHandler<HTMLElement>;
187
+ onPlaying?: React.ReactEventHandler<HTMLElement>;
188
+ onPlayingCapture?: React.ReactEventHandler<HTMLElement>;
189
+ onProgress?: React.ReactEventHandler<HTMLElement>;
190
+ onProgressCapture?: React.ReactEventHandler<HTMLElement>;
191
+ onRateChange?: React.ReactEventHandler<HTMLElement>;
192
+ onRateChangeCapture?: React.ReactEventHandler<HTMLElement>;
193
+ onSeeked?: React.ReactEventHandler<HTMLElement>;
194
+ onSeekedCapture?: React.ReactEventHandler<HTMLElement>;
195
+ onSeeking?: React.ReactEventHandler<HTMLElement>;
196
+ onSeekingCapture?: React.ReactEventHandler<HTMLElement>;
197
+ onStalled?: React.ReactEventHandler<HTMLElement>;
198
+ onStalledCapture?: React.ReactEventHandler<HTMLElement>;
199
+ onSuspend?: React.ReactEventHandler<HTMLElement>;
200
+ onSuspendCapture?: React.ReactEventHandler<HTMLElement>;
201
+ onTimeUpdate?: React.ReactEventHandler<HTMLElement>;
202
+ onTimeUpdateCapture?: React.ReactEventHandler<HTMLElement>;
203
+ onVolumeChange?: React.ReactEventHandler<HTMLElement>;
204
+ onVolumeChangeCapture?: React.ReactEventHandler<HTMLElement>;
205
+ onWaiting?: React.ReactEventHandler<HTMLElement>;
206
+ onWaitingCapture?: React.ReactEventHandler<HTMLElement>;
207
+ onAuxClick?: React.MouseEventHandler<HTMLElement>;
208
+ onAuxClickCapture?: React.MouseEventHandler<HTMLElement>;
209
+ onClick?: React.MouseEventHandler<HTMLElement>;
210
+ onClickCapture?: React.MouseEventHandler<HTMLElement>;
211
+ onContextMenu?: React.MouseEventHandler<HTMLElement>;
212
+ onContextMenuCapture?: React.MouseEventHandler<HTMLElement>;
213
+ onDoubleClick?: React.MouseEventHandler<HTMLElement>;
214
+ onDoubleClickCapture?: React.MouseEventHandler<HTMLElement>;
215
+ onDrag?: React.DragEventHandler<HTMLElement>;
216
+ onDragCapture?: React.DragEventHandler<HTMLElement>;
217
+ onDragEnd?: React.DragEventHandler<HTMLElement>;
218
+ onDragEndCapture?: React.DragEventHandler<HTMLElement>;
219
+ onDragEnter?: React.DragEventHandler<HTMLElement>;
220
+ onDragEnterCapture?: React.DragEventHandler<HTMLElement>;
221
+ onDragExit?: React.DragEventHandler<HTMLElement>;
222
+ onDragExitCapture?: React.DragEventHandler<HTMLElement>;
223
+ onDragLeave?: React.DragEventHandler<HTMLElement>;
224
+ onDragLeaveCapture?: React.DragEventHandler<HTMLElement>;
225
+ onDragOver?: React.DragEventHandler<HTMLElement>;
226
+ onDragOverCapture?: React.DragEventHandler<HTMLElement>;
227
+ onDragStart?: React.DragEventHandler<HTMLElement>;
228
+ onDragStartCapture?: React.DragEventHandler<HTMLElement>;
229
+ onDrop?: React.DragEventHandler<HTMLElement>;
230
+ onDropCapture?: React.DragEventHandler<HTMLElement>;
231
+ onMouseDown?: React.MouseEventHandler<HTMLElement>;
232
+ onMouseDownCapture?: React.MouseEventHandler<HTMLElement>;
233
+ onMouseEnter?: React.MouseEventHandler<HTMLElement>;
234
+ onMouseLeave?: React.MouseEventHandler<HTMLElement>;
235
+ onMouseMove?: React.MouseEventHandler<HTMLElement>;
236
+ onMouseMoveCapture?: React.MouseEventHandler<HTMLElement>;
237
+ onMouseOut?: React.MouseEventHandler<HTMLElement>;
238
+ onMouseOutCapture?: React.MouseEventHandler<HTMLElement>;
239
+ onMouseOver?: React.MouseEventHandler<HTMLElement>;
240
+ onMouseOverCapture?: React.MouseEventHandler<HTMLElement>;
241
+ onMouseUp?: React.MouseEventHandler<HTMLElement>;
242
+ onMouseUpCapture?: React.MouseEventHandler<HTMLElement>;
243
+ onSelect?: React.ReactEventHandler<HTMLElement>;
244
+ onSelectCapture?: React.ReactEventHandler<HTMLElement>;
245
+ onTouchCancel?: React.TouchEventHandler<HTMLElement>;
246
+ onTouchCancelCapture?: React.TouchEventHandler<HTMLElement>;
247
+ onTouchEnd?: React.TouchEventHandler<HTMLElement>;
248
+ onTouchEndCapture?: React.TouchEventHandler<HTMLElement>;
249
+ onTouchMove?: React.TouchEventHandler<HTMLElement>;
250
+ onTouchMoveCapture?: React.TouchEventHandler<HTMLElement>;
251
+ onTouchStart?: React.TouchEventHandler<HTMLElement>;
252
+ onTouchStartCapture?: React.TouchEventHandler<HTMLElement>;
253
+ onPointerDown?: React.PointerEventHandler<HTMLElement>;
254
+ onPointerDownCapture?: React.PointerEventHandler<HTMLElement>;
255
+ onPointerMove?: React.PointerEventHandler<HTMLElement>;
256
+ onPointerMoveCapture?: React.PointerEventHandler<HTMLElement>;
257
+ onPointerUp?: React.PointerEventHandler<HTMLElement>;
258
+ onPointerUpCapture?: React.PointerEventHandler<HTMLElement>;
259
+ onPointerCancel?: React.PointerEventHandler<HTMLElement>;
260
+ onPointerCancelCapture?: React.PointerEventHandler<HTMLElement>;
261
+ onPointerEnter?: React.PointerEventHandler<HTMLElement>;
262
+ onPointerLeave?: React.PointerEventHandler<HTMLElement>;
263
+ onPointerOver?: React.PointerEventHandler<HTMLElement>;
264
+ onPointerOverCapture?: React.PointerEventHandler<HTMLElement>;
265
+ onPointerOut?: React.PointerEventHandler<HTMLElement>;
266
+ onPointerOutCapture?: React.PointerEventHandler<HTMLElement>;
267
+ onGotPointerCapture?: React.PointerEventHandler<HTMLElement>;
268
+ onGotPointerCaptureCapture?: React.PointerEventHandler<HTMLElement>;
269
+ onLostPointerCapture?: React.PointerEventHandler<HTMLElement>;
270
+ onLostPointerCaptureCapture?: React.PointerEventHandler<HTMLElement>;
271
+ onScroll?: React.UIEventHandler<HTMLElement>;
272
+ onScrollCapture?: React.UIEventHandler<HTMLElement>;
273
+ onScrollEnd?: React.UIEventHandler<HTMLElement>;
274
+ onScrollEndCapture?: React.UIEventHandler<HTMLElement>;
275
+ onWheel?: React.WheelEventHandler<HTMLElement>;
276
+ onWheelCapture?: React.WheelEventHandler<HTMLElement>;
277
+ onAnimationStart?: React.AnimationEventHandler<HTMLElement>;
278
+ onAnimationStartCapture?: React.AnimationEventHandler<HTMLElement>;
279
+ onAnimationEnd?: React.AnimationEventHandler<HTMLElement>;
280
+ onAnimationEndCapture?: React.AnimationEventHandler<HTMLElement>;
281
+ onAnimationIteration?: React.AnimationEventHandler<HTMLElement>;
282
+ onAnimationIterationCapture?: React.AnimationEventHandler<HTMLElement>;
283
+ onToggle?: React.ToggleEventHandler<HTMLElement>;
284
+ onBeforeToggle?: React.ToggleEventHandler<HTMLElement>;
285
+ onTransitionCancel?: React.TransitionEventHandler<HTMLElement>;
286
+ onTransitionCancelCapture?: React.TransitionEventHandler<HTMLElement>;
287
+ onTransitionEnd?: React.TransitionEventHandler<HTMLElement>;
288
+ onTransitionEndCapture?: React.TransitionEventHandler<HTMLElement>;
289
+ onTransitionRun?: React.TransitionEventHandler<HTMLElement>;
290
+ onTransitionRunCapture?: React.TransitionEventHandler<HTMLElement>;
291
+ onTransitionStart?: React.TransitionEventHandler<HTMLElement>;
292
+ onTransitionStartCapture?: React.TransitionEventHandler<HTMLElement>;
293
+ }, HTMLElement>;
294
+ type PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {
295
+ /** Render the only child instead, with this part's props merged onto it. */
296
+ asChild?: boolean;
297
+ };
298
+ export type InputProps = PartProps<'input'> & {
299
+ /** The id of the `Conditional` this choice reveals. */
300
+ controls?: string;
301
+ /** For a checkbox such as "None of these": checking it unchecks the others with its name. */
302
+ exclusive?: boolean;
303
+ };
304
+ /** A radio or checkbox. */
305
+ export declare function Input({ asChild, controls, exclusive, ...props }: InputProps): React.JSX.Element;
306
+ export type ConditionalProps = PartProps<'div'>;
307
+ /** Content revealed while its choice is checked. Give it the id the choice `controls`. */
308
+ export declare function Conditional({ asChild, ...props }: ConditionalProps): React.JSX.Element;
309
+ export {};