menhir 0.1.11 → 0.1.12

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 (33) hide show
  1. package/README.md +1 -1
  2. package/dist/checkbox/root/CheckboxRoot.js +10 -61
  3. package/dist/core/hiddenInput.js +94 -0
  4. package/dist/core/rovingFocus.js +1 -1
  5. package/dist/radio/index.js +2 -0
  6. package/dist/radio/index.parts.js +4 -0
  7. package/dist/radio/indicator/RadioIndicator.js +20 -0
  8. package/dist/radio/item/RadioItem.js +94 -0
  9. package/dist/radio/item/RadioItemContext.js +10 -0
  10. package/dist/radio/root/RadioRoot.js +96 -0
  11. package/dist/radio/root/RadioRootContext.js +10 -0
  12. package/dist/source/checkbox/root/CheckboxRoot.jsx +5 -57
  13. package/dist/source/core/hiddenInput.d.ts +46 -0
  14. package/dist/source/core/hiddenInput.jsx +93 -0
  15. package/dist/source/core/rovingFocus.d.ts +1 -1
  16. package/dist/source/core/rovingFocus.js +1 -1
  17. package/dist/source/radio/index.d.ts +1 -0
  18. package/dist/source/radio/index.js +1 -0
  19. package/dist/source/radio/index.parts.d.ts +3 -0
  20. package/dist/source/radio/index.parts.js +3 -0
  21. package/dist/source/radio/indicator/RadioIndicator.d.ts +10 -0
  22. package/dist/source/radio/indicator/RadioIndicator.jsx +12 -0
  23. package/dist/source/radio/item/RadioItem.d.ts +14 -0
  24. package/dist/source/radio/item/RadioItem.jsx +96 -0
  25. package/dist/source/radio/item/RadioItemContext.d.ts +12 -0
  26. package/dist/source/radio/item/RadioItemContext.js +12 -0
  27. package/dist/source/radio/root/RadioRoot.d.ts +28 -0
  28. package/dist/source/radio/root/RadioRoot.jsx +112 -0
  29. package/dist/source/radio/root/RadioRootContext.d.ts +40 -0
  30. package/dist/source/radio/root/RadioRootContext.js +13 -0
  31. package/dist/source/switch/root/SwitchRoot.jsx +5 -57
  32. package/dist/switch/root/SwitchRoot.js +10 -61
  33. package/package.json +6 -1
@@ -0,0 +1,93 @@
1
+ import { onFlush, onSettled, untrack } from "solid-js";
2
+ /**
3
+ * The form's view of a control whose element is a native button (PLAN.md
4
+ * section 8, "The hidden input is the form's view of the control"): a
5
+ * visually hidden `<input type="checkbox">` or `<input type="radio">`,
6
+ * rendered after the button, for Switch, Checkbox and a Radio's Items.
7
+ * Returns the input, which the button clicks, so the form sees the input's
8
+ * native `input` and `change` events:
9
+ *
10
+ * ```tsx
11
+ * const input = createHiddenInput(
12
+ * "checkbox",
13
+ * props,
14
+ * checked,
15
+ * setChecked,
16
+ * () => button,
17
+ * () => setChecked(props.defaultChecked ?? false),
18
+ * );
19
+ * // The button's own props: onClick: () => input.click()
20
+ * ```
21
+ *
22
+ * - `props` are the control's own, read where they are used.
23
+ * - `checked` is the control's state, which the input follows, and
24
+ * `setChecked` asks for the input's checkedness once the user changed it.
25
+ * - `button` is where focus goes when the browser focuses the input, as it
26
+ * does when it blocks a submission.
27
+ * - `reset` asks for the control's default on a form reset, through the
28
+ * setter, so that a controlled control asks. A Radio's Items each ask
29
+ * the group, which asks once.
30
+ *
31
+ * A plain function with positional parameters, not a component with props:
32
+ * a component would wrap each prop in a getter, and an options object
33
+ * would spell out each name. Measured on 2026-11-24, Switch and Checkbox
34
+ * are 30 B larger each than with a copy of their own (src/radio/README.md,
35
+ * "Size").
36
+ */
37
+ export function createHiddenInput(type, props, checked, setChecked, button, reset) {
38
+ let input;
39
+ // Puts the input back to the control's value, once that has settled. The
40
+ // `checked` binding writes the DOM only when the value changes, so it does
41
+ // not undo a change of the input that the control did not take.
42
+ const sync = () => {
43
+ if (input)
44
+ input.checked = untrack(checked);
45
+ };
46
+ const onClick = (event) => {
47
+ // The click the button sends the input is an implementation detail:
48
+ // ancestors already received the user's click.
49
+ event.stopPropagation();
50
+ // Canceling the click keeps the input as it was and fires no `input` or
51
+ // `change` event, whoever clicked it.
52
+ if (props.readOnly)
53
+ event.preventDefault();
54
+ };
55
+ // A form reset puts a native control back to its default, and so it does
56
+ // this one: through the setter, so that a controlled control asks.
57
+ onSettled(() => {
58
+ const form = input?.form;
59
+ if (!form)
60
+ return;
61
+ const onReset = () => {
62
+ reset();
63
+ // The browser resets the input after this event, to its `checked`
64
+ // attribute, which the binding does not set. `onFlush` runs after
65
+ // that, as the event has finished by then.
66
+ onFlush(sync);
67
+ };
68
+ form.addEventListener("reset", onReset);
69
+ return () => form.removeEventListener("reset", onReset);
70
+ });
71
+ return (
72
+ // The element, for the button to click. On the server it is the HTML,
73
+ // which nothing clicks.
74
+ <input type={type} aria-hidden="true" tabindex={-1} style={visuallyHidden} name={props.name} value={props.value} disabled={props.disabled} required={props.required} form={props.form} checked={checked()} onChange={() => {
75
+ setChecked(input.checked);
76
+ // The browser has already changed the input, so the form's own
77
+ // listeners, which run after this one, see the new value. The two
78
+ // differ afterwards only when a controlled control's consumer
79
+ // declined the change.
80
+ onFlush(sync);
81
+ }}
82
+ // The browser focuses an invalid input when it blocks a submission.
83
+ // The user knows the control, not the hidden input.
84
+ onFocus={() => button()?.focus()} ref={(element) => {
85
+ input = element;
86
+ // Solid delegates click handlers to the document, so this one is a
87
+ // native listener, to act before the click bubbles.
88
+ element.addEventListener("click", onClick);
89
+ }}/>);
90
+ }
91
+ // Hidden from sight but not from the form. Not `display: none`, so the
92
+ // browser can still anchor a validation message to it.
93
+ const visuallyHidden = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none";
@@ -18,7 +18,7 @@ export interface RovingFocus<T extends RovingItem> {
18
18
  /**
19
19
  * One tab stop among a group's items, with arrow keys between them (roving
20
20
  * tabindex, PLAN.md section 8, "A group of items is one tab stop"): Toggle
21
- * Group's items, Tabs' tabs. Pass the options as getters and functions, so
21
+ * Group's items, Tabs' tabs, a Radio's radios. Pass the options as getters and functions, so
22
22
  * they are read where they are used:
23
23
  *
24
24
  * ```ts
@@ -7,7 +7,7 @@ const byPosition = (a, b) => a.element.compareDocumentPosition(b.element) &
7
7
  /**
8
8
  * One tab stop among a group's items, with arrow keys between them (roving
9
9
  * tabindex, PLAN.md section 8, "A group of items is one tab stop"): Toggle
10
- * Group's items, Tabs' tabs. Pass the options as getters and functions, so
10
+ * Group's items, Tabs' tabs, a Radio's radios. Pass the options as getters and functions, so
11
11
  * they are read where they are used:
12
12
  *
13
13
  * ```ts
@@ -0,0 +1 @@
1
+ export * as Radio from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Radio from "./index.parts.js";
@@ -0,0 +1,3 @@
1
+ export { RadioRoot as Root } from "./root/RadioRoot.tsx";
2
+ export { RadioItem as Item } from "./item/RadioItem.tsx";
3
+ export { RadioIndicator as Indicator } from "./indicator/RadioIndicator.tsx";
@@ -0,0 +1,3 @@
1
+ export { RadioRoot as Root } from "./root/RadioRoot.jsx";
2
+ export { RadioItem as Item } from "./item/RadioItem.jsx";
3
+ export { RadioIndicator as Indicator } from "./indicator/RadioIndicator.jsx";
@@ -0,0 +1,10 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface RadioIndicatorProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The dot: a `<span>` with its Item's `data-*` attributes, always rendered,
8
+ * so that CSS shows it by state. Place it inside `Radio.Item`.
9
+ */
10
+ export declare function RadioIndicator(props: RadioIndicatorProps): JSX.Element;
@@ -0,0 +1,12 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useRadioItemContext } from "../item/RadioItemContext.js";
5
+ /**
6
+ * The dot: a `<span>` with its Item's `data-*` attributes, always rendered,
7
+ * so that CSS shows it by state. Place it inside `Radio.Item`.
8
+ */
9
+ export function RadioIndicator(props) {
10
+ const elementProps = mergeElementProps(useRadioItemContext(), omit(props, "render"));
11
+ return renderElement(props, elementProps, (p) => <span {...p}/>);
12
+ }
@@ -0,0 +1,14 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface RadioItemProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** Identifies the radio in the group's value, and is submitted while it is checked. */
5
+ value: string;
6
+ /** Whether the radio ignores the user. It cannot be focused either. */
7
+ disabled?: boolean;
8
+ render?: RenderProp;
9
+ }
10
+ /**
11
+ * One radio of the group: a `<button role="radio">`, checked while the
12
+ * group's value is its `value`. Place it inside `Radio.Root`.
13
+ */
14
+ export declare function RadioItem(props: RadioItemProps): JSX.Element;
@@ -0,0 +1,96 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createMemo, merge, omit, onSettled, untrack } from "solid-js";
3
+ import { warnUnlessButton } from "../../core/button.js";
4
+ import { createHiddenInput } from "../../core/hiddenInput.jsx";
5
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
6
+ import { renderElement } from "../../core/renderElement.jsx";
7
+ import { useRadioRootContext } from "../root/RadioRootContext.js";
8
+ import { RadioItemContext } from "./RadioItemContext.js";
9
+ /**
10
+ * One radio of the group: a `<button role="radio">`, checked while the
11
+ * group's value is its `value`. Place it inside `Radio.Root`.
12
+ */
13
+ export function RadioItem(props) {
14
+ const group = useRadioRootContext();
15
+ // A memo, so that a change of the value that leaves this radio as it was
16
+ // does not run its bindings again.
17
+ const checked = createMemo(() => group.value() === props.value);
18
+ const disabled = () => !!(group.disabled() || props.disabled);
19
+ let element;
20
+ const stateAttributes = {
21
+ get "data-checked"() {
22
+ return checked() ? "" : undefined;
23
+ },
24
+ get "data-unchecked"() {
25
+ return checked() ? undefined : "";
26
+ },
27
+ get "data-disabled"() {
28
+ return disabled() ? "" : undefined;
29
+ },
30
+ };
31
+ const inputProps = {
32
+ get name() {
33
+ return group.name();
34
+ },
35
+ get value() {
36
+ return props.value;
37
+ },
38
+ get disabled() {
39
+ return disabled();
40
+ },
41
+ get required() {
42
+ return group.required();
43
+ },
44
+ get form() {
45
+ return group.form();
46
+ },
47
+ };
48
+ // The input's change only ever checks it: the user cannot uncheck a radio.
49
+ const input = createHiddenInput("radio", inputProps, checked, () => group.select(props.value), () => element, group.reset);
50
+ // Read once: the tab stop of the server HTML, and of the first render.
51
+ const first = group.claimTabStop(untrack(checked), untrack(disabled));
52
+ const buttonProps = {
53
+ role: "radio",
54
+ type: "button",
55
+ get disabled() {
56
+ return disabled();
57
+ },
58
+ // Solid 2.0 removes an attribute set to false, so spell the value out.
59
+ get "aria-checked"() {
60
+ return checked() ? "true" : "false";
61
+ },
62
+ get tabindex() {
63
+ return group.isTabStop(element, first) ? 0 : -1;
64
+ },
65
+ // The change goes through the hidden input, so that the form sees the
66
+ // native `input` and `change` events a radio fires.
67
+ onClick: () => input.click(),
68
+ onKeyDown: (event) => {
69
+ // A button clicks on Enter, a radio does not: the pattern names only
70
+ // Space. Canceling the key down keeps the browser from clicking.
71
+ if (event.key === "Enter")
72
+ event.preventDefault();
73
+ else
74
+ group.navigate(event, element);
75
+ },
76
+ // An arrow key checks the radio it moves to; Tab and a click focus it
77
+ // without checking it, though a click checks it then.
78
+ onFocus: () => {
79
+ if (group.moving())
80
+ input.click();
81
+ },
82
+ ref: (el) => {
83
+ element = el;
84
+ // Production builds drop the check with `isDev`.
85
+ if (isDev)
86
+ warnUnlessButton(el, "Radio.Item");
87
+ },
88
+ };
89
+ // Mounted, so its place in the document is known.
90
+ onSettled(() => group.register({ element: element, input, disabled, checked }));
91
+ const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "value", "disabled"));
92
+ return (<RadioItemContext value={stateAttributes}>
93
+ {renderElement(props, elementProps, (p) => (<button {...p}/>))}
94
+ {input}
95
+ </RadioItemContext>);
96
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The radio's state as `data-*` attributes, each `""` when it applies and
3
+ * absent otherwise. Item and Indicator both spread the same object, so its
4
+ * getters are read where each element uses them.
5
+ */
6
+ export type RadioStateAttributes = {
7
+ readonly "data-checked": "" | undefined;
8
+ readonly "data-unchecked": "" | undefined;
9
+ readonly "data-disabled": "" | undefined;
10
+ };
11
+ export declare const RadioItemContext: import("solid-js").Context<RadioStateAttributes | null>;
12
+ export declare function useRadioItemContext(): RadioStateAttributes;
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside an Item, so that the part can name what is missing.
4
+ export const RadioItemContext = createContext(null);
5
+ export function useRadioItemContext() {
6
+ const context = useContext(RadioItemContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context) {
9
+ throw new Error("[menhir] Radio.Indicator must be placed within <Radio.Item>.");
10
+ }
11
+ return context;
12
+ }
@@ -0,0 +1,28 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface RadioRootProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** The checked radio's value, when controlled. `null` for none. */
5
+ value?: string | null;
6
+ /** The radio checked at first, when uncontrolled. */
7
+ defaultValue?: string | null;
8
+ /**
9
+ * Called with the new value when the user checks a radio, or with
10
+ * `defaultValue` when the form is reset. When controlled, the group
11
+ * changes only if `value` does.
12
+ */
13
+ onValueChange?: (value: string | null) => void;
14
+ /** The name the checked radio's value is submitted under. */
15
+ name?: string;
16
+ /** Whether every radio ignores the user. The fieldset is disabled too. */
17
+ disabled?: boolean;
18
+ /** Whether the form can be submitted only while a radio is checked. */
19
+ required?: boolean;
20
+ /** The `id` of the form the radios belong to, when they are not inside it. */
21
+ form?: string;
22
+ render?: RenderProp;
23
+ }
24
+ /**
25
+ * The group: a `<fieldset role="radiogroup">` around `Radio.Item`s, named
26
+ * by a `<legend>`, that holds the checked radio's value.
27
+ */
28
+ export declare function RadioRoot(props: RadioRootProps): JSX.Element;
@@ -0,0 +1,112 @@
1
+ import { createUniqueId, omit, onFlush, untrack } from "solid-js";
2
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
3
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
4
+ import { createRovingFocus } from "../../core/rovingFocus.js";
5
+ import { renderElement } from "../../core/renderElement.jsx";
6
+ import { RadioRootContext } from "./RadioRootContext.js";
7
+ /**
8
+ * The group: a `<fieldset role="radiogroup">` around `Radio.Item`s, named
9
+ * by a `<legend>`, that holds the checked radio's value.
10
+ */
11
+ export function RadioRoot(props) {
12
+ const [value, setValue] = createControllableSignal({
13
+ get value() {
14
+ return props.value;
15
+ },
16
+ get defaultValue() {
17
+ return props.defaultValue ?? null;
18
+ },
19
+ onChange: (value) => props.onValueChange?.(value),
20
+ });
21
+ // The pattern moves on every arrow key, whatever the layout. Roving focus
22
+ // reads one axis, so `navigate` sets it from each key: Up and Down as a
23
+ // vertical group's, Left and Right as a horizontal one's, which roving
24
+ // focus mirrors right to left.
25
+ let vertical = false;
26
+ // Whether focus is moving by an arrow key, so the Item it reaches checks.
27
+ let moving = false;
28
+ // Whether an Item has claimed the tab stop of the server HTML.
29
+ let claimed = false;
30
+ const items = new Set();
31
+ const roving = createRovingFocus({
32
+ get vertical() {
33
+ return vertical;
34
+ },
35
+ focusable: (item) => !item.disabled(),
36
+ preferred: (item) => item.checked(),
37
+ });
38
+ // After a declined change. The browser checked the clicked radio's input
39
+ // and unchecked the checked one's, which no binding changed: this checks
40
+ // it again, once the value has settled. The clicked one unchecks itself.
41
+ const restore = () => {
42
+ for (const item of items)
43
+ if (untrack(item.checked))
44
+ item.input.checked = true;
45
+ };
46
+ // The hidden inputs' name, which the server render and hydration agree
47
+ // on. Browsers find a radio missing only with a name, so a group with no
48
+ // `name` of its own gives them this one while none is checked, when they
49
+ // submit nothing; once one is checked, they have none again.
50
+ const validityName = createUniqueId();
51
+ const empty = () => value() === null;
52
+ const context = {
53
+ value,
54
+ select: (next) => {
55
+ setValue(next);
56
+ onFlush(restore);
57
+ },
58
+ // Each Item's hidden input asks on a form reset. A set equal to the one
59
+ // before it in the turn does nothing, so a controlled group asks once.
60
+ reset: () => setValue(props.defaultValue ?? null),
61
+ name: () => props.name ?? (empty() ? validityName : undefined),
62
+ form: () => props.form,
63
+ disabled: () => props.disabled,
64
+ // Only while none is checked: once one is, the group is not missing,
65
+ // and an unnamed, unchecked, required radio would be missing on its own
66
+ // by the HTML standard. It follows the value, not the inputs'
67
+ // checkedness, which a form reset changes behind the bindings' back.
68
+ required: () => props.required && empty(),
69
+ register: (item) => {
70
+ items.add(item);
71
+ const unregister = roving.register(item);
72
+ return () => {
73
+ items.delete(item);
74
+ unregister();
75
+ };
76
+ },
77
+ isTabStop: roving.isTabStop,
78
+ // Before any Item has registered, as on the server, the checked Item
79
+ // has the tab stop, or the first enabled one while none is checked.
80
+ claimTabStop: (checked, disabled) => !disabled &&
81
+ (checked || (untrack(value) === null && !claimed && (claimed = true))),
82
+ navigate: (event, from) => {
83
+ // The pattern names no Home or End, and native radios have none.
84
+ if (!event.key.startsWith("Arrow"))
85
+ return;
86
+ vertical = event.key === "ArrowUp" || event.key === "ArrowDown";
87
+ moving = true;
88
+ roving.navigate(event, from);
89
+ moving = false;
90
+ },
91
+ moving: () => moving,
92
+ };
93
+ const ownProps = {
94
+ role: "radiogroup",
95
+ get disabled() {
96
+ return props.disabled;
97
+ },
98
+ get "aria-required"() {
99
+ return props.required ? "true" : undefined;
100
+ },
101
+ get "data-disabled"() {
102
+ return props.disabled ? "" : undefined;
103
+ },
104
+ get "data-required"() {
105
+ return props.required ? "" : undefined;
106
+ },
107
+ };
108
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "defaultValue", "onValueChange", "name", "disabled", "required", "form"));
109
+ return (<RadioRootContext value={context}>
110
+ {renderElement(props, elementProps, (p) => (<fieldset {...p}/>))}
111
+ </RadioRootContext>);
112
+ }
@@ -0,0 +1,40 @@
1
+ import type { RovingItem } from "../../core/rovingFocus.ts";
2
+ /** A mounted Item, as the group knows it. */
3
+ export interface RadioItemEntry extends RovingItem {
4
+ /** The Item's hidden input, which the group checks again after a declined change. */
5
+ readonly input: HTMLInputElement;
6
+ disabled(): boolean;
7
+ checked(): boolean;
8
+ }
9
+ /** What the group shares with its Items. */
10
+ export interface RadioRootContextValue {
11
+ /** The checked Item's value, `null` while none is checked. */
12
+ value(): string | null;
13
+ /** Asks to check the Item with this value. */
14
+ select(value: string): void;
15
+ /** Asks for `defaultValue`, on a form reset. Asked once however many Items ask. */
16
+ reset(): void;
17
+ /** The `name` each Item's hidden input takes. */
18
+ name(): string | undefined;
19
+ /** The `id` of the form the Items belong to, when they are not inside it. */
20
+ form(): string | undefined;
21
+ /** Whether the whole group is disabled. */
22
+ disabled(): boolean | undefined;
23
+ /** Whether each Item's hidden input is `required`: while none is checked in a required group. */
24
+ required(): boolean | undefined;
25
+ /** Adds a mounted Item; returns what removes it. */
26
+ register(item: RadioItemEntry): () => void;
27
+ /** Whether this Item has the tab stop; `fallback` until Items register. */
28
+ isTabStop(element: HTMLElement | undefined, fallback: boolean): boolean;
29
+ /**
30
+ * Whether an Item, created checked or not and enabled or not, has the tab
31
+ * stop of the server HTML and of the first render.
32
+ */
33
+ claimTabStop(checked: boolean, disabled: boolean): boolean;
34
+ /** Moves focus, and checks, for an arrow key pressed on an Item. */
35
+ navigate(event: KeyboardEvent, from: HTMLElement): void;
36
+ /** Whether focus is moving by an arrow key, so the Item it reaches checks. */
37
+ moving(): boolean;
38
+ }
39
+ export declare const RadioRootContext: import("solid-js").Context<RadioRootContextValue | null>;
40
+ export declare function useRadioRootContext(): RadioRootContextValue;
@@ -0,0 +1,13 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside a Root, so that the part can name what is missing. Solid's
4
+ // own error for a context with no provider and no default does not.
5
+ export const RadioRootContext = createContext(null);
6
+ export function useRadioRootContext() {
7
+ const context = useContext(RadioRootContext);
8
+ // Production builds drop the check, and its message, with `isDev`.
9
+ if (isDev && !context) {
10
+ throw new Error("[menhir] Radio.Item must be placed within <Radio.Root>.");
11
+ }
12
+ return context;
13
+ }
@@ -1,7 +1,8 @@
1
1
  import { isDev } from "@solidjs/web";
2
- import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
2
+ import { merge, omit } from "solid-js";
3
3
  import { warnUnlessButton } from "../../core/button.js";
4
4
  import { createControllableSignal } from "../../core/createControllableSignal.js";
5
+ import { createHiddenInput } from "../../core/hiddenInput.jsx";
5
6
  import { mergeElementProps } from "../../core/mergeElementProps.js";
6
7
  import { renderElement } from "../../core/renderElement.jsx";
7
8
  import { SwitchRootContext } from "./SwitchRootContext.js";
@@ -53,7 +54,7 @@ export function SwitchRoot(props) {
53
54
  },
54
55
  // The change goes through the hidden input, so that the form sees the
55
56
  // native `input` and `change` events a checkbox fires.
56
- onClick: () => input?.click(),
57
+ onClick: () => input.click(),
57
58
  ref: (element) => {
58
59
  button = element;
59
60
  // Production builds drop the check with `isDev`.
@@ -62,63 +63,10 @@ export function SwitchRoot(props) {
62
63
  },
63
64
  };
64
65
  let button;
65
- let input;
66
- // Puts the input back to the switch's value, once that has settled. The
67
- // `checked` binding writes the DOM only when the value changes, so it
68
- // does not undo a change of the input that the switch did not take.
69
- const syncInput = () => {
70
- if (input)
71
- input.checked = untrack(checked);
72
- };
73
- const onInputChange = () => {
74
- if (!input)
75
- return;
76
- setChecked(input.checked);
77
- // The browser has already changed the input, so the form's own
78
- // listeners, which run after this one, see the new value. The two
79
- // differ afterwards only when a controlled switch's consumer declined
80
- // the change.
81
- onFlush(syncInput);
82
- };
83
- const onInputClick = (event) => {
84
- // The click the button sends the input is an implementation detail:
85
- // ancestors already received the user's click.
86
- event.stopPropagation();
87
- // Canceling the click keeps the checkbox as it was and fires no
88
- // `input` or `change` event, whoever clicked it.
89
- if (props.readOnly)
90
- event.preventDefault();
91
- };
92
- // A form reset puts a checkbox back to its default, and so it does the
93
- // switch: through the setter, so that a controlled switch asks.
94
- onSettled(() => {
95
- const form = input?.form;
96
- if (!form)
97
- return;
98
- const onReset = () => {
99
- setChecked(props.defaultChecked ?? false);
100
- // The browser resets the input after this event, to its `checked`
101
- // attribute, which the binding does not set. `onFlush` runs after
102
- // that, as the event has finished by then.
103
- onFlush(syncInput);
104
- };
105
- form.addEventListener("reset", onReset);
106
- return () => form.removeEventListener("reset", onReset);
107
- });
66
+ const input = createHiddenInput("checkbox", props, checked, setChecked, () => button, () => setChecked(props.defaultChecked ?? false));
108
67
  const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "checked", "defaultChecked", "onCheckedChange", "name", "value", "disabled", "readOnly", "required", "form"));
109
68
  return (<SwitchRootContext value={stateAttributes}>
110
69
  {renderElement(props, elementProps, (p) => (<button {...p}/>))}
111
- <input type="checkbox" aria-hidden="true" tabindex={-1} style={visuallyHidden} name={props.name} value={props.value} disabled={props.disabled} required={props.required} form={props.form} checked={checked()} onChange={onInputChange}
112
- // The browser focuses an invalid input when it blocks a submission.
113
- // The user knows the switch, not the hidden input.
114
- onFocus={() => button?.focus()} ref={(element) => {
115
- input = element;
116
- // Solid delegates click handlers to the document, so this one is
117
- // a native listener, to act before the click bubbles.
118
- element.addEventListener("click", onInputClick);
119
- }}/>
70
+ {input}
120
71
  </SwitchRootContext>);
121
72
  }
122
- // Hidden from sight but not from the form. Not `display: none`, so the
123
- // browser can still anchor a validation message to it.
124
- const visuallyHidden = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none";