menhir 0.1.10 → 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.
- package/README.md +1 -1
- package/dist/checkbox/root/CheckboxRoot.js +10 -61
- package/dist/checkbox-group/index.js +2 -0
- package/dist/checkbox-group/index.parts.js +5 -0
- package/dist/checkbox-group/item/CheckboxGroupItem.js +40 -0
- package/dist/checkbox-group/parent/CheckboxGroupParent.js +55 -0
- package/dist/checkbox-group/root/CheckboxGroupRoot.js +89 -0
- package/dist/checkbox-group/root/CheckboxGroupRootContext.js +10 -0
- package/dist/core/hiddenInput.js +94 -0
- package/dist/core/rovingFocus.js +1 -1
- package/dist/radio/index.js +2 -0
- package/dist/radio/index.parts.js +4 -0
- package/dist/radio/indicator/RadioIndicator.js +20 -0
- package/dist/radio/item/RadioItem.js +94 -0
- package/dist/radio/item/RadioItemContext.js +10 -0
- package/dist/radio/root/RadioRoot.js +96 -0
- package/dist/radio/root/RadioRootContext.js +10 -0
- package/dist/source/checkbox/root/CheckboxRoot.jsx +5 -57
- package/dist/source/checkbox-group/index.d.ts +1 -0
- package/dist/source/checkbox-group/index.js +1 -0
- package/dist/source/checkbox-group/index.parts.d.ts +4 -0
- package/dist/source/checkbox-group/index.parts.js +4 -0
- package/dist/source/checkbox-group/item/CheckboxGroupItem.d.ts +10 -0
- package/dist/source/checkbox-group/item/CheckboxGroupItem.jsx +47 -0
- package/dist/source/checkbox-group/parent/CheckboxGroupParent.d.ts +17 -0
- package/dist/source/checkbox-group/parent/CheckboxGroupParent.jsx +72 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRoot.d.ts +28 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRoot.jsx +75 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRootContext.d.ts +20 -0
- package/dist/source/checkbox-group/root/CheckboxGroupRootContext.js +13 -0
- package/dist/source/core/hiddenInput.d.ts +46 -0
- package/dist/source/core/hiddenInput.jsx +93 -0
- package/dist/source/core/rovingFocus.d.ts +1 -1
- package/dist/source/core/rovingFocus.js +1 -1
- package/dist/source/radio/index.d.ts +1 -0
- package/dist/source/radio/index.js +1 -0
- package/dist/source/radio/index.parts.d.ts +3 -0
- package/dist/source/radio/index.parts.js +3 -0
- package/dist/source/radio/indicator/RadioIndicator.d.ts +10 -0
- package/dist/source/radio/indicator/RadioIndicator.jsx +12 -0
- package/dist/source/radio/item/RadioItem.d.ts +14 -0
- package/dist/source/radio/item/RadioItem.jsx +96 -0
- package/dist/source/radio/item/RadioItemContext.d.ts +12 -0
- package/dist/source/radio/item/RadioItemContext.js +12 -0
- package/dist/source/radio/root/RadioRoot.d.ts +28 -0
- package/dist/source/radio/root/RadioRoot.jsx +112 -0
- package/dist/source/radio/root/RadioRootContext.d.ts +40 -0
- package/dist/source/radio/root/RadioRootContext.js +13 -0
- package/dist/source/switch/root/SwitchRoot.jsx +5 -57
- package/dist/switch/root/SwitchRoot.js +10 -61
- package/package.json +11 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { isDev } from "@solidjs/web";
|
|
2
|
-
import { merge, omit
|
|
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 { CheckboxRootContext } from "./CheckboxRootContext.js";
|
|
@@ -58,7 +59,7 @@ export function CheckboxRoot(props) {
|
|
|
58
59
|
},
|
|
59
60
|
// The change goes through the hidden input, so that the form sees the
|
|
60
61
|
// native `input` and `change` events a checkbox fires.
|
|
61
|
-
onClick: () => input
|
|
62
|
+
onClick: () => input.click(),
|
|
62
63
|
// A button clicks on Enter, a checkbox does not: the pattern names only
|
|
63
64
|
// Space. Canceling the key down keeps the browser from clicking.
|
|
64
65
|
onKeyDown: (event) => {
|
|
@@ -73,63 +74,10 @@ export function CheckboxRoot(props) {
|
|
|
73
74
|
},
|
|
74
75
|
};
|
|
75
76
|
let button;
|
|
76
|
-
|
|
77
|
-
// Puts the input back to the checkbox's value, once that has settled.
|
|
78
|
-
// The `checked` binding writes the DOM only when the value changes, so it
|
|
79
|
-
// does not undo a change of the input that the checkbox did not take.
|
|
80
|
-
const syncInput = () => {
|
|
81
|
-
if (input)
|
|
82
|
-
input.checked = untrack(checked);
|
|
83
|
-
};
|
|
84
|
-
const onInputChange = () => {
|
|
85
|
-
if (!input)
|
|
86
|
-
return;
|
|
87
|
-
setChecked(input.checked);
|
|
88
|
-
// The browser has already changed the input, so the form's own
|
|
89
|
-
// listeners, which run after this one, see the new value. The two
|
|
90
|
-
// differ afterwards only when a controlled checkbox's consumer declined
|
|
91
|
-
// the change.
|
|
92
|
-
onFlush(syncInput);
|
|
93
|
-
};
|
|
94
|
-
const onInputClick = (event) => {
|
|
95
|
-
// The click the button sends the input is an implementation detail:
|
|
96
|
-
// ancestors already received the user's click.
|
|
97
|
-
event.stopPropagation();
|
|
98
|
-
// Canceling the click keeps the checkbox as it was and fires no
|
|
99
|
-
// `input` or `change` event, whoever clicked it.
|
|
100
|
-
if (props.readOnly)
|
|
101
|
-
event.preventDefault();
|
|
102
|
-
};
|
|
103
|
-
// A form reset puts a checkbox back to its default, and so it does this
|
|
104
|
-
// one: through the setter, so that a controlled checkbox asks.
|
|
105
|
-
onSettled(() => {
|
|
106
|
-
const form = input?.form;
|
|
107
|
-
if (!form)
|
|
108
|
-
return;
|
|
109
|
-
const onReset = () => {
|
|
110
|
-
setChecked(props.defaultChecked ?? false);
|
|
111
|
-
// The browser resets the input after this event, to its `checked`
|
|
112
|
-
// attribute, which the binding does not set. `onFlush` runs after
|
|
113
|
-
// that, as the event has finished by then.
|
|
114
|
-
onFlush(syncInput);
|
|
115
|
-
};
|
|
116
|
-
form.addEventListener("reset", onReset);
|
|
117
|
-
return () => form.removeEventListener("reset", onReset);
|
|
118
|
-
});
|
|
77
|
+
const input = createHiddenInput("checkbox", props, checked, setChecked, () => button, () => setChecked(props.defaultChecked ?? false));
|
|
119
78
|
const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "checked", "defaultChecked", "onCheckedChange", "indeterminate", "name", "value", "disabled", "readOnly", "required", "form"));
|
|
120
79
|
return (<CheckboxRootContext value={stateAttributes}>
|
|
121
80
|
{renderElement(props, elementProps, (p) => (<button {...p}/>))}
|
|
122
|
-
|
|
123
|
-
// The browser focuses an invalid input when it blocks a submission.
|
|
124
|
-
// The user knows the checkbox, not the hidden input.
|
|
125
|
-
onFocus={() => button?.focus()} ref={(element) => {
|
|
126
|
-
input = element;
|
|
127
|
-
// Solid delegates click handlers to the document, so this one is
|
|
128
|
-
// a native listener, to act before the click bubbles.
|
|
129
|
-
element.addEventListener("click", onInputClick);
|
|
130
|
-
}}/>
|
|
81
|
+
{input}
|
|
131
82
|
</CheckboxRootContext>);
|
|
132
83
|
}
|
|
133
|
-
// Hidden from sight but not from the form. Not `display: none`, so the
|
|
134
|
-
// browser can still anchor a validation message to it.
|
|
135
|
-
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";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as CheckboxGroup from "./index.parts.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as CheckboxGroup from "./index.parts.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CheckboxGroupRoot as Root } from "./root/CheckboxGroupRoot.tsx";
|
|
2
|
+
export { CheckboxGroupItem as Item } from "./item/CheckboxGroupItem.tsx";
|
|
3
|
+
export { CheckboxGroupParent as Parent } from "./parent/CheckboxGroupParent.tsx";
|
|
4
|
+
export { CheckboxIndicator as Indicator } from "../checkbox/indicator/CheckboxIndicator.tsx";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CheckboxGroupRoot as Root } from "./root/CheckboxGroupRoot.jsx";
|
|
2
|
+
export { CheckboxGroupItem as Item } from "./item/CheckboxGroupItem.jsx";
|
|
3
|
+
export { CheckboxGroupParent as Parent } from "./parent/CheckboxGroupParent.jsx";
|
|
4
|
+
export { CheckboxIndicator as Indicator } from "../checkbox/indicator/CheckboxIndicator.jsx";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CheckboxRootProps } from "../../checkbox/root/CheckboxRoot.tsx";
|
|
2
|
+
export interface CheckboxGroupItemProps extends Omit<CheckboxRootProps, "checked" | "defaultChecked" | "onCheckedChange" | "indeterminate" | "name" | "value" | "required" | "form"> {
|
|
3
|
+
/** Identifies the box in the group's value, and is submitted while it is checked. */
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* One box of the group: a `Checkbox.Root`, checked while the group's value
|
|
8
|
+
* has its `value`. Place it inside `CheckboxGroup.Root`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function CheckboxGroupItem(props: CheckboxGroupItemProps): import("@solidjs/web").JSX.Element;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createMemo, merge, omit } from "solid-js";
|
|
2
|
+
import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.jsx";
|
|
3
|
+
import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* One box of the group: a `Checkbox.Root`, checked while the group's value
|
|
6
|
+
* has its `value`. Place it inside `CheckboxGroup.Root`.
|
|
7
|
+
*/
|
|
8
|
+
export function CheckboxGroupItem(props) {
|
|
9
|
+
const group = useCheckboxGroupRootContext();
|
|
10
|
+
// A memo, so that a change to the list that leaves this box as it was
|
|
11
|
+
// does not run the box's bindings again: a hidden input the user changed
|
|
12
|
+
// in the same turn, not yet in the list, would be written back over.
|
|
13
|
+
const checked = createMemo(() => group.value().includes(props.value));
|
|
14
|
+
const own = {
|
|
15
|
+
get checked() {
|
|
16
|
+
return checked();
|
|
17
|
+
},
|
|
18
|
+
// The group puts the list back on a form reset, once. Checkbox's own
|
|
19
|
+
// reset asks for `defaultChecked`, so passing the box's state makes it
|
|
20
|
+
// ask for nothing, and put its hidden input back to that state.
|
|
21
|
+
get defaultChecked() {
|
|
22
|
+
return checked();
|
|
23
|
+
},
|
|
24
|
+
onCheckedChange: (included) => {
|
|
25
|
+
// Reads lag writes, so a second change before the flush builds on
|
|
26
|
+
// the first, not on what `value` still reads.
|
|
27
|
+
const next = group.requested().filter((item) => item !== props.value);
|
|
28
|
+
if (included)
|
|
29
|
+
next.push(props.value);
|
|
30
|
+
group.setValue(next);
|
|
31
|
+
},
|
|
32
|
+
get name() {
|
|
33
|
+
return group.name();
|
|
34
|
+
},
|
|
35
|
+
get value() {
|
|
36
|
+
return props.value;
|
|
37
|
+
},
|
|
38
|
+
get form() {
|
|
39
|
+
return group.form();
|
|
40
|
+
},
|
|
41
|
+
get disabled() {
|
|
42
|
+
return group.disabled() || props.disabled;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const checkboxProps = merge(omit(props, "value", "disabled"), own);
|
|
46
|
+
return <CheckboxRoot {...checkboxProps}/>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CheckboxRootProps } from "../../checkbox/root/CheckboxRoot.tsx";
|
|
2
|
+
export interface CheckboxGroupParentProps extends Omit<CheckboxRootProps, "checked" | "defaultChecked" | "onCheckedChange" | "indeterminate" | "name" | "value" | "required" | "form"> {
|
|
3
|
+
/**
|
|
4
|
+
* The values of the boxes the parent checks and unchecks, usually every
|
|
5
|
+
* Item's. It is checked while the group's value has all of them, mixed
|
|
6
|
+
* while it has some.
|
|
7
|
+
*/
|
|
8
|
+
values: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A checkbox over the group's boxes: a `Checkbox.Root`, checked while every
|
|
12
|
+
* box of `values` is, mixed while some are. A click on a mixed parent checks
|
|
13
|
+
* all, on a checked one unchecks all, and on an unchecked one brings back
|
|
14
|
+
* the boxes checked when it was last mixed, or checks all. It submits
|
|
15
|
+
* nothing. Place it inside `CheckboxGroup.Root`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function CheckboxGroupParent(props: CheckboxGroupParentProps): import("@solidjs/web").JSX.Element;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createMemo, merge, omit } from "solid-js";
|
|
2
|
+
import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.jsx";
|
|
3
|
+
import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* A checkbox over the group's boxes: a `Checkbox.Root`, checked while every
|
|
6
|
+
* box of `values` is, mixed while some are. A click on a mixed parent checks
|
|
7
|
+
* all, on a checked one unchecks all, and on an unchecked one brings back
|
|
8
|
+
* the boxes checked when it was last mixed, or checks all. It submits
|
|
9
|
+
* nothing. Place it inside `CheckboxGroup.Root`.
|
|
10
|
+
*/
|
|
11
|
+
export function CheckboxGroupParent(props) {
|
|
12
|
+
const group = useCheckboxGroupRootContext();
|
|
13
|
+
// The list the parent set last, by reference: a list it did not set is
|
|
14
|
+
// a change the user made, through a box or a reset.
|
|
15
|
+
let set;
|
|
16
|
+
// The boxes of `values` that are checked, and the combination a click on
|
|
17
|
+
// the unchecked parent brings back: the one the user made last, which a
|
|
18
|
+
// change by the parent leaves as it was, as the WAI-ARIA mixed-state
|
|
19
|
+
// example does. Read by the bindings below, so it sees every change.
|
|
20
|
+
const boxes = createMemo((previous) => {
|
|
21
|
+
const list = group.value();
|
|
22
|
+
const checked = props.values.filter((item) => list.includes(item));
|
|
23
|
+
return {
|
|
24
|
+
checked,
|
|
25
|
+
remembered: previous && list === set ? previous.remembered : checked,
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
// Memos, so that a change to the list that leaves the parent as it was
|
|
29
|
+
// does not run its bindings again (CheckboxGroupItem).
|
|
30
|
+
const allChecked = createMemo(() => props.values.length > 0 && boxes().checked.length === props.values.length);
|
|
31
|
+
const someChecked = createMemo(() => boxes().checked.length > 0);
|
|
32
|
+
const own = {
|
|
33
|
+
get checked() {
|
|
34
|
+
return allChecked();
|
|
35
|
+
},
|
|
36
|
+
// The group puts the list back on a form reset, so Checkbox's own reset
|
|
37
|
+
// must ask for nothing (CheckboxGroupItem).
|
|
38
|
+
get defaultChecked() {
|
|
39
|
+
return allChecked();
|
|
40
|
+
},
|
|
41
|
+
get indeterminate() {
|
|
42
|
+
return someChecked() && !allChecked();
|
|
43
|
+
},
|
|
44
|
+
// Checkbox asks for the opposite of `checked`: `false` from a checked
|
|
45
|
+
// parent, `true` from a mixed or an unchecked one.
|
|
46
|
+
onCheckedChange: (checked) => {
|
|
47
|
+
const values = props.values;
|
|
48
|
+
const current = group.requested();
|
|
49
|
+
const others = current.filter((item) => !values.includes(item));
|
|
50
|
+
const { remembered } = boxes();
|
|
51
|
+
let next = others;
|
|
52
|
+
if (checked) {
|
|
53
|
+
// Mixed, or unchecked with nothing to bring back: check all.
|
|
54
|
+
next =
|
|
55
|
+
values.some((item) => current.includes(item)) ||
|
|
56
|
+
remembered.length === 0
|
|
57
|
+
? [...current, ...values.filter((item) => !current.includes(item))]
|
|
58
|
+
: [...others, ...remembered];
|
|
59
|
+
}
|
|
60
|
+
set = next;
|
|
61
|
+
group.setValue(next);
|
|
62
|
+
},
|
|
63
|
+
get form() {
|
|
64
|
+
return group.form();
|
|
65
|
+
},
|
|
66
|
+
get disabled() {
|
|
67
|
+
return group.disabled() || props.disabled;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
const checkboxProps = merge(omit(props, "values", "disabled"), own);
|
|
71
|
+
return <CheckboxRoot {...checkboxProps}/>;
|
|
72
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface CheckboxGroupRootProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** The values of the checked boxes, when controlled. `[]` for none. */
|
|
5
|
+
value?: string[];
|
|
6
|
+
/** The values of the boxes checked at first, when uncontrolled. */
|
|
7
|
+
defaultValue?: string[];
|
|
8
|
+
/**
|
|
9
|
+
* Called with the new list when the user checks or unchecks a box, or
|
|
10
|
+
* the form is reset. When controlled, the group changes only if `value`
|
|
11
|
+
* does.
|
|
12
|
+
*/
|
|
13
|
+
onValueChange?: (value: string[]) => void;
|
|
14
|
+
/** The name each checked box is submitted under, one entry per box. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** Whether every box ignores the user. The fieldset is disabled too. */
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
/** Whether the form can be submitted only while a box is checked. */
|
|
19
|
+
required?: boolean;
|
|
20
|
+
/** The `id` of the form the boxes belong to, when they are not inside it. */
|
|
21
|
+
form?: string;
|
|
22
|
+
render?: RenderProp;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The group: a `<fieldset>` around `CheckboxGroup.Item`s, named by a
|
|
26
|
+
* `<legend>`, that holds the values of the checked boxes.
|
|
27
|
+
*/
|
|
28
|
+
export declare function CheckboxGroupRoot(props: CheckboxGroupRootProps): JSX.Element;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createUniqueId, omit, onSettled } from "solid-js";
|
|
2
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
3
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
4
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
5
|
+
import { CheckboxGroupRootContext } from "./CheckboxGroupRootContext.js";
|
|
6
|
+
/**
|
|
7
|
+
* The group: a `<fieldset>` around `CheckboxGroup.Item`s, named by a
|
|
8
|
+
* `<legend>`, that holds the values of the checked boxes.
|
|
9
|
+
*/
|
|
10
|
+
export function CheckboxGroupRoot(props) {
|
|
11
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
12
|
+
get value() {
|
|
13
|
+
return props.value;
|
|
14
|
+
},
|
|
15
|
+
get defaultValue() {
|
|
16
|
+
return props.defaultValue ?? [];
|
|
17
|
+
},
|
|
18
|
+
onChange: (value) => props.onValueChange?.(value),
|
|
19
|
+
});
|
|
20
|
+
const context = {
|
|
21
|
+
value,
|
|
22
|
+
requested,
|
|
23
|
+
setValue: (next) => setValue(next),
|
|
24
|
+
name: () => props.name,
|
|
25
|
+
form: () => props.form,
|
|
26
|
+
disabled: () => props.disabled,
|
|
27
|
+
};
|
|
28
|
+
// The hidden radio's name, which the server render and hydration agree on.
|
|
29
|
+
const validityName = createUniqueId();
|
|
30
|
+
let root;
|
|
31
|
+
let input;
|
|
32
|
+
// A form reset puts the list back to its default, once, through the
|
|
33
|
+
// setter, so that a controlled group asks once. Each Item's Checkbox asks
|
|
34
|
+
// for nothing itself (CheckboxGroupItem).
|
|
35
|
+
onSettled(() => {
|
|
36
|
+
const form = input?.form;
|
|
37
|
+
if (!form)
|
|
38
|
+
return;
|
|
39
|
+
const onReset = () => setValue(props.defaultValue ?? []);
|
|
40
|
+
form.addEventListener("reset", onReset);
|
|
41
|
+
return () => form.removeEventListener("reset", onReset);
|
|
42
|
+
});
|
|
43
|
+
const ownProps = {
|
|
44
|
+
get disabled() {
|
|
45
|
+
return props.disabled;
|
|
46
|
+
},
|
|
47
|
+
get "data-disabled"() {
|
|
48
|
+
return props.disabled ? "" : undefined;
|
|
49
|
+
},
|
|
50
|
+
get "data-required"() {
|
|
51
|
+
return props.required ? "" : undefined;
|
|
52
|
+
},
|
|
53
|
+
ref: (element) => {
|
|
54
|
+
root = element;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "defaultValue", "onValueChange", "name", "disabled", "required", "form"));
|
|
58
|
+
return (<CheckboxGroupRootContext value={context}>
|
|
59
|
+
{renderElement(props, elementProps, (p) => (<fieldset {...p}/>))}
|
|
60
|
+
{/* "At least one" has no constraint of its own. An unchecked radio
|
|
61
|
+
that is required while the list is empty blocks a submission, and
|
|
62
|
+
the browser's message for it asks to select an option. Browsers
|
|
63
|
+
find a radio missing only with a name, unique so that no other
|
|
64
|
+
group's radio joins its radio group; unchecked, it submits nothing.
|
|
65
|
+
Its `required` follows the list, not its checkedness, which a form
|
|
66
|
+
reset would change behind the binding's back. */}
|
|
67
|
+
<input type="radio" name={validityName} aria-hidden="true" tabindex={-1} style={visuallyHidden} disabled={props.disabled} required={props.required && value().length === 0} form={props.form}
|
|
68
|
+
// The browser focuses an invalid input when it blocks a submission.
|
|
69
|
+
// The user knows the boxes, not the hidden input.
|
|
70
|
+
onFocus={() => root?.querySelector("[role=checkbox]:enabled")?.focus()} ref={input}/>
|
|
71
|
+
</CheckboxGroupRootContext>);
|
|
72
|
+
}
|
|
73
|
+
// Hidden from sight but not from the form. Not `display: none`, so the
|
|
74
|
+
// browser can still anchor a validation message to it.
|
|
75
|
+
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";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** What the group shares with its Items and its Parent. */
|
|
2
|
+
export interface CheckboxGroupRootContextValue {
|
|
3
|
+
/** The values of the checked boxes. */
|
|
4
|
+
value(): string[];
|
|
5
|
+
/**
|
|
6
|
+
* The list set last in this turn, or `value`, untracked: for a change
|
|
7
|
+
* that builds on one made earlier in the same turn.
|
|
8
|
+
*/
|
|
9
|
+
requested(): string[];
|
|
10
|
+
/** Asks to set the whole list. */
|
|
11
|
+
setValue(next: string[]): void;
|
|
12
|
+
/** The `name` each Item submits under. */
|
|
13
|
+
name(): string | undefined;
|
|
14
|
+
/** The `id` of the form the Items belong to, when they are not inside it. */
|
|
15
|
+
form(): string | undefined;
|
|
16
|
+
/** Whether the whole group is disabled. */
|
|
17
|
+
disabled(): boolean | undefined;
|
|
18
|
+
}
|
|
19
|
+
export declare const CheckboxGroupRootContext: import("solid-js").Context<CheckboxGroupRootContextValue | null>;
|
|
20
|
+
export declare function useCheckboxGroupRootContext(): CheckboxGroupRootContextValue;
|
|
@@ -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 CheckboxGroupRootContext = createContext(null);
|
|
6
|
+
export function useCheckboxGroupRootContext() {
|
|
7
|
+
const context = useContext(CheckboxGroupRootContext);
|
|
8
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
9
|
+
if (isDev && !context) {
|
|
10
|
+
throw new Error("[menhir] CheckboxGroup parts must be placed within <CheckboxGroup.Root>.");
|
|
11
|
+
}
|
|
12
|
+
return context;
|
|
13
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** The props of a control that its hidden input takes, read where used. */
|
|
2
|
+
export interface HiddenInputProps {
|
|
3
|
+
readonly name?: string;
|
|
4
|
+
readonly value?: string;
|
|
5
|
+
readonly disabled?: boolean;
|
|
6
|
+
readonly required?: boolean;
|
|
7
|
+
readonly form?: string;
|
|
8
|
+
/** Whether a click is canceled, so the input keeps its checkedness. */
|
|
9
|
+
readonly readOnly?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The form's view of a control whose element is a native button (PLAN.md
|
|
13
|
+
* section 8, "The hidden input is the form's view of the control"): a
|
|
14
|
+
* visually hidden `<input type="checkbox">` or `<input type="radio">`,
|
|
15
|
+
* rendered after the button, for Switch, Checkbox and a Radio's Items.
|
|
16
|
+
* Returns the input, which the button clicks, so the form sees the input's
|
|
17
|
+
* native `input` and `change` events:
|
|
18
|
+
*
|
|
19
|
+
* ```tsx
|
|
20
|
+
* const input = createHiddenInput(
|
|
21
|
+
* "checkbox",
|
|
22
|
+
* props,
|
|
23
|
+
* checked,
|
|
24
|
+
* setChecked,
|
|
25
|
+
* () => button,
|
|
26
|
+
* () => setChecked(props.defaultChecked ?? false),
|
|
27
|
+
* );
|
|
28
|
+
* // The button's own props: onClick: () => input.click()
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* - `props` are the control's own, read where they are used.
|
|
32
|
+
* - `checked` is the control's state, which the input follows, and
|
|
33
|
+
* `setChecked` asks for the input's checkedness once the user changed it.
|
|
34
|
+
* - `button` is where focus goes when the browser focuses the input, as it
|
|
35
|
+
* does when it blocks a submission.
|
|
36
|
+
* - `reset` asks for the control's default on a form reset, through the
|
|
37
|
+
* setter, so that a controlled control asks. A Radio's Items each ask
|
|
38
|
+
* the group, which asks once.
|
|
39
|
+
*
|
|
40
|
+
* A plain function with positional parameters, not a component with props:
|
|
41
|
+
* a component would wrap each prop in a getter, and an options object
|
|
42
|
+
* would spell out each name. Measured on 2026-11-24, Switch and Checkbox
|
|
43
|
+
* are 30 B larger each than with a copy of their own (src/radio/README.md,
|
|
44
|
+
* "Size").
|
|
45
|
+
*/
|
|
46
|
+
export declare function createHiddenInput(type: "checkbox" | "radio", props: HiddenInputProps, checked: () => boolean, setChecked: (checked: boolean) => void, button: () => HTMLElement | undefined, reset: () => void): HTMLInputElement;
|
|
@@ -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,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;
|