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.
- package/README.md +1 -1
- package/dist/checkbox/root/CheckboxRoot.js +10 -61
- 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/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 +6 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ For everything else, go to the docs:
|
|
|
62
62
|
|
|
63
63
|
Early 0.x versions: the API may change between minor versions. The form components come next; the plan and roadmap are in the [repository](https://github.com/j13n/menhir). Built so far:
|
|
64
64
|
|
|
65
|
-
- Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Checkbox Group, Collapsible, Combobox, Context Menu, Dialog, Field, Form, Input, Menu, Menubar, Meter, Navigation Menu, Popover, Progress, Select, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
|
|
65
|
+
- Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Checkbox Group, Collapsible, Combobox, Context Menu, Dialog, Field, Form, Input, Menu, Menubar, Meter, Navigation Menu, Popover, Progress, Radio, Select, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
|
|
66
66
|
- Utilities: color scheme and theme, each with local-storage and cookie storage.
|
|
67
67
|
|
|
68
68
|
## Licence
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { warnUnlessButton } from "../../core/button.js";
|
|
2
2
|
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
3
|
+
import { createHiddenInput } from "../../core/hiddenInput.js";
|
|
3
4
|
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
4
5
|
import { renderElement } from "../../core/renderElement.js";
|
|
5
6
|
import { CheckboxRootContext } from "./CheckboxRootContext.js";
|
|
6
|
-
import { createComponent,
|
|
7
|
-
import { merge, omit
|
|
7
|
+
import { createComponent, isDev, memo, spread, template } from "@solidjs/web";
|
|
8
|
+
import { merge, omit } from "solid-js";
|
|
8
9
|
// src/checkbox/root/CheckboxRoot.tsx
|
|
9
|
-
var _tmpl$ = /* @__PURE__ */ template(`<
|
|
10
|
-
var _tmpl$2 = /* @__PURE__ */ template(`<button>`);
|
|
10
|
+
var _tmpl$ = /* @__PURE__ */ template(`<button>`);
|
|
11
11
|
/**
|
|
12
12
|
* The checkbox itself: a `<button role="checkbox">`.
|
|
13
13
|
*/
|
|
@@ -57,7 +57,7 @@ function CheckboxRoot(props) {
|
|
|
57
57
|
get "aria-required"() {
|
|
58
58
|
return props.required ? "true" : void 0;
|
|
59
59
|
},
|
|
60
|
-
onClick: () => input
|
|
60
|
+
onClick: () => input.click(),
|
|
61
61
|
onKeyDown: (event) => {
|
|
62
62
|
if (event.key === "Enter") event.preventDefault();
|
|
63
63
|
},
|
|
@@ -67,69 +67,18 @@ function CheckboxRoot(props) {
|
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
69
|
let button;
|
|
70
|
-
|
|
71
|
-
const syncInput = () => {
|
|
72
|
-
if (input) input.checked = untrack(checked);
|
|
73
|
-
};
|
|
74
|
-
const onInputChange = () => {
|
|
75
|
-
if (!input) return;
|
|
76
|
-
setChecked(input.checked);
|
|
77
|
-
onFlush(syncInput);
|
|
78
|
-
};
|
|
79
|
-
const onInputClick = (event) => {
|
|
80
|
-
event.stopPropagation();
|
|
81
|
-
if (props.readOnly) event.preventDefault();
|
|
82
|
-
};
|
|
83
|
-
onSettled(() => {
|
|
84
|
-
const form = input?.form;
|
|
85
|
-
if (!form) return;
|
|
86
|
-
const onReset = () => {
|
|
87
|
-
setChecked(props.defaultChecked ?? false);
|
|
88
|
-
onFlush(syncInput);
|
|
89
|
-
};
|
|
90
|
-
form.addEventListener("reset", onReset);
|
|
91
|
-
return () => form.removeEventListener("reset", onReset);
|
|
92
|
-
});
|
|
70
|
+
const input = createHiddenInput("checkbox", props, checked, setChecked, () => button, () => setChecked(props.defaultChecked ?? false));
|
|
93
71
|
const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "checked", "defaultChecked", "onCheckedChange", "indeterminate", "name", "value", "disabled", "readOnly", "required", "form"));
|
|
94
72
|
return createComponent(CheckboxRootContext, {
|
|
95
73
|
value: stateAttributes,
|
|
96
74
|
get children() {
|
|
97
75
|
return [memo(() => {
|
|
98
76
|
return renderElement(props, elementProps, (p) => (() => {
|
|
99
|
-
var _el$
|
|
100
|
-
spread(_el
|
|
101
|
-
return _el
|
|
77
|
+
var _el$ = _tmpl$();
|
|
78
|
+
spread(_el$, p, false);
|
|
79
|
+
return _el$;
|
|
102
80
|
})());
|
|
103
|
-
}),
|
|
104
|
-
var _el$ = _tmpl$();
|
|
105
|
-
_el$.addEventListener("focus", () => button?.focus());
|
|
106
|
-
_el$.addEventListener("change", onInputChange);
|
|
107
|
-
ref(() => {
|
|
108
|
-
return (element) => {
|
|
109
|
-
input = element;
|
|
110
|
-
element.addEventListener("click", onInputClick);
|
|
111
|
-
};
|
|
112
|
-
}, _el$);
|
|
113
|
-
effect(() => {
|
|
114
|
-
return {
|
|
115
|
-
e: props.name,
|
|
116
|
-
t: props.value,
|
|
117
|
-
a: props.disabled,
|
|
118
|
-
o: props.required,
|
|
119
|
-
i: props.form,
|
|
120
|
-
n: checked(),
|
|
121
|
-
s: heldRead()
|
|
122
|
-
};
|
|
123
|
-
}, ({ e, t, a, o, i, n, s }, _p$) => {
|
|
124
|
-
e !== _p$?.e && setAttribute(_el$, "name", e);
|
|
125
|
-
(t !== _p$?.t || !s) && setProperty(_el$, "value", t);
|
|
126
|
-
a !== _p$?.a && setAttribute(_el$, "disabled", a);
|
|
127
|
-
o !== _p$?.o && setAttribute(_el$, "required", o);
|
|
128
|
-
i !== _p$?.i && setAttribute(_el$, "form", i);
|
|
129
|
-
(n !== _p$?.n || !s) && (_el$.checked = n);
|
|
130
|
-
});
|
|
131
|
-
return _el$;
|
|
132
|
-
})()];
|
|
81
|
+
}), input];
|
|
133
82
|
}
|
|
134
83
|
});
|
|
135
84
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { effect, heldRead, ref, setAttribute, setProperty, template } from "@solidjs/web";
|
|
2
|
+
import { onFlush, onSettled, untrack } from "solid-js";
|
|
3
|
+
// src/core/hiddenInput.tsx
|
|
4
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input aria-hidden=true tabindex=-1 style=position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none>`);
|
|
5
|
+
/**
|
|
6
|
+
* The form's view of a control whose element is a native button (PLAN.md
|
|
7
|
+
* section 8, "The hidden input is the form's view of the control"): a
|
|
8
|
+
* visually hidden `<input type="checkbox">` or `<input type="radio">`,
|
|
9
|
+
* rendered after the button, for Switch, Checkbox and a Radio's Items.
|
|
10
|
+
* Returns the input, which the button clicks, so the form sees the input's
|
|
11
|
+
* native `input` and `change` events:
|
|
12
|
+
*
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const input = createHiddenInput(
|
|
15
|
+
* "checkbox",
|
|
16
|
+
* props,
|
|
17
|
+
* checked,
|
|
18
|
+
* setChecked,
|
|
19
|
+
* () => button,
|
|
20
|
+
* () => setChecked(props.defaultChecked ?? false),
|
|
21
|
+
* );
|
|
22
|
+
* // The button's own props: onClick: () => input.click()
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* - `props` are the control's own, read where they are used.
|
|
26
|
+
* - `checked` is the control's state, which the input follows, and
|
|
27
|
+
* `setChecked` asks for the input's checkedness once the user changed it.
|
|
28
|
+
* - `button` is where focus goes when the browser focuses the input, as it
|
|
29
|
+
* does when it blocks a submission.
|
|
30
|
+
* - `reset` asks for the control's default on a form reset, through the
|
|
31
|
+
* setter, so that a controlled control asks. A Radio's Items each ask
|
|
32
|
+
* the group, which asks once.
|
|
33
|
+
*
|
|
34
|
+
* A plain function with positional parameters, not a component with props:
|
|
35
|
+
* a component would wrap each prop in a getter, and an options object
|
|
36
|
+
* would spell out each name. Measured on 2026-11-24, Switch and Checkbox
|
|
37
|
+
* are 30 B larger each than with a copy of their own (src/radio/README.md,
|
|
38
|
+
* "Size").
|
|
39
|
+
*/
|
|
40
|
+
function createHiddenInput(type, props, checked, setChecked, button, reset) {
|
|
41
|
+
let input;
|
|
42
|
+
const sync = () => {
|
|
43
|
+
if (input) input.checked = untrack(checked);
|
|
44
|
+
};
|
|
45
|
+
const onClick = (event) => {
|
|
46
|
+
event.stopPropagation();
|
|
47
|
+
if (props.readOnly) event.preventDefault();
|
|
48
|
+
};
|
|
49
|
+
onSettled(() => {
|
|
50
|
+
const form = input?.form;
|
|
51
|
+
if (!form) return;
|
|
52
|
+
const onReset = () => {
|
|
53
|
+
reset();
|
|
54
|
+
onFlush(sync);
|
|
55
|
+
};
|
|
56
|
+
form.addEventListener("reset", onReset);
|
|
57
|
+
return () => form.removeEventListener("reset", onReset);
|
|
58
|
+
});
|
|
59
|
+
return (() => {
|
|
60
|
+
var _el$ = _tmpl$();
|
|
61
|
+
_el$.addEventListener("focus", () => button()?.focus());
|
|
62
|
+
_el$.addEventListener("change", () => {
|
|
63
|
+
setChecked(input.checked);
|
|
64
|
+
onFlush(sync);
|
|
65
|
+
});
|
|
66
|
+
setAttribute(_el$, "type", type);
|
|
67
|
+
ref(() => {
|
|
68
|
+
return (element) => {
|
|
69
|
+
input = element;
|
|
70
|
+
element.addEventListener("click", onClick);
|
|
71
|
+
};
|
|
72
|
+
}, _el$);
|
|
73
|
+
effect(() => {
|
|
74
|
+
return {
|
|
75
|
+
e: props.name,
|
|
76
|
+
t: props.value,
|
|
77
|
+
a: props.disabled,
|
|
78
|
+
o: props.required,
|
|
79
|
+
i: props.form,
|
|
80
|
+
n: checked(),
|
|
81
|
+
s: heldRead()
|
|
82
|
+
};
|
|
83
|
+
}, ({ e, t, a, o, i, n, s }, _p$) => {
|
|
84
|
+
e !== _p$?.e && setAttribute(_el$, "name", e);
|
|
85
|
+
(t !== _p$?.t || !s) && setProperty(_el$, "value", t);
|
|
86
|
+
a !== _p$?.a && setAttribute(_el$, "disabled", a);
|
|
87
|
+
o !== _p$?.o && setAttribute(_el$, "required", o);
|
|
88
|
+
i !== _p$?.i && setAttribute(_el$, "form", i);
|
|
89
|
+
(n !== _p$?.n || !s) && (_el$.checked = n);
|
|
90
|
+
});
|
|
91
|
+
return _el$;
|
|
92
|
+
})();
|
|
93
|
+
}
|
|
94
|
+
export { createHiddenInput };
|
package/dist/core/rovingFocus.js
CHANGED
|
@@ -4,7 +4,7 @@ var byPosition = (a, b) => a.element.compareDocumentPosition(b.element) & Node.D
|
|
|
4
4
|
/**
|
|
5
5
|
* One tab stop among a group's items, with arrow keys between them (roving
|
|
6
6
|
* tabindex, PLAN.md section 8, "A group of items is one tab stop"): Toggle
|
|
7
|
-
* Group's items, Tabs' tabs. Pass the options as getters and functions, so
|
|
7
|
+
* Group's items, Tabs' tabs, a Radio's radios. Pass the options as getters and functions, so
|
|
8
8
|
* they are read where they are used:
|
|
9
9
|
*
|
|
10
10
|
* ```ts
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useRadioItemContext } from "../item/RadioItemContext.js";
|
|
4
|
+
import { spread, template } from "@solidjs/web";
|
|
5
|
+
import { omit } from "solid-js";
|
|
6
|
+
// src/radio/indicator/RadioIndicator.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<span>`);
|
|
8
|
+
/**
|
|
9
|
+
* The dot: a `<span>` with its Item's `data-*` attributes, always rendered,
|
|
10
|
+
* so that CSS shows it by state. Place it inside `Radio.Item`.
|
|
11
|
+
*/
|
|
12
|
+
function RadioIndicator(props) {
|
|
13
|
+
const elementProps = mergeElementProps(useRadioItemContext(), omit(props, "render"));
|
|
14
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
15
|
+
var _el$ = _tmpl$();
|
|
16
|
+
spread(_el$, p, false);
|
|
17
|
+
return _el$;
|
|
18
|
+
})());
|
|
19
|
+
}
|
|
20
|
+
export { RadioIndicator };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { warnUnlessButton } from "../../core/button.js";
|
|
2
|
+
import { createHiddenInput } from "../../core/hiddenInput.js";
|
|
3
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
4
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
5
|
+
import { useRadioRootContext } from "../root/RadioRootContext.js";
|
|
6
|
+
import { RadioItemContext } from "./RadioItemContext.js";
|
|
7
|
+
import { createComponent, isDev, memo, spread, template } from "@solidjs/web";
|
|
8
|
+
import { createMemo, merge, omit, onSettled, untrack } from "solid-js";
|
|
9
|
+
// src/radio/item/RadioItem.tsx
|
|
10
|
+
var _tmpl$ = /* @__PURE__ */ template(`<button>`);
|
|
11
|
+
/**
|
|
12
|
+
* One radio of the group: a `<button role="radio">`, checked while the
|
|
13
|
+
* group's value is its `value`. Place it inside `Radio.Root`.
|
|
14
|
+
*/
|
|
15
|
+
function RadioItem(props) {
|
|
16
|
+
const group = useRadioRootContext();
|
|
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() ? "" : void 0;
|
|
23
|
+
},
|
|
24
|
+
get "data-unchecked"() {
|
|
25
|
+
return checked() ? void 0 : "";
|
|
26
|
+
},
|
|
27
|
+
get "data-disabled"() {
|
|
28
|
+
return disabled() ? "" : void 0;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const input = createHiddenInput("radio", {
|
|
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
|
+
}, checked, () => group.select(props.value), () => element, group.reset);
|
|
48
|
+
const first = group.claimTabStop(untrack(checked), untrack(disabled));
|
|
49
|
+
const buttonProps = {
|
|
50
|
+
role: "radio",
|
|
51
|
+
type: "button",
|
|
52
|
+
get disabled() {
|
|
53
|
+
return disabled();
|
|
54
|
+
},
|
|
55
|
+
get "aria-checked"() {
|
|
56
|
+
return checked() ? "true" : "false";
|
|
57
|
+
},
|
|
58
|
+
get tabindex() {
|
|
59
|
+
return group.isTabStop(element, first) ? 0 : -1;
|
|
60
|
+
},
|
|
61
|
+
onClick: () => input.click(),
|
|
62
|
+
onKeyDown: (event) => {
|
|
63
|
+
if (event.key === "Enter") event.preventDefault();
|
|
64
|
+
else group.navigate(event, element);
|
|
65
|
+
},
|
|
66
|
+
onFocus: () => {
|
|
67
|
+
if (group.moving()) input.click();
|
|
68
|
+
},
|
|
69
|
+
ref: (el) => {
|
|
70
|
+
element = el;
|
|
71
|
+
if (isDev) warnUnlessButton(el, "Radio.Item");
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
onSettled(() => group.register({
|
|
75
|
+
element,
|
|
76
|
+
input,
|
|
77
|
+
disabled,
|
|
78
|
+
checked
|
|
79
|
+
}));
|
|
80
|
+
const elementProps = mergeElementProps(merge(stateAttributes, buttonProps), omit(props, "render", "value", "disabled"));
|
|
81
|
+
return createComponent(RadioItemContext, {
|
|
82
|
+
value: stateAttributes,
|
|
83
|
+
get children() {
|
|
84
|
+
return [memo(() => {
|
|
85
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
86
|
+
var _el$ = _tmpl$();
|
|
87
|
+
spread(_el$, p, false);
|
|
88
|
+
return _el$;
|
|
89
|
+
})());
|
|
90
|
+
}), input];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export { RadioItem };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, useContext } from "solid-js";
|
|
3
|
+
// src/radio/item/RadioItemContext.ts
|
|
4
|
+
var RadioItemContext = createContext(null);
|
|
5
|
+
function useRadioItemContext() {
|
|
6
|
+
const context = useContext(RadioItemContext);
|
|
7
|
+
if (isDev && !context) throw new Error("[menhir] Radio.Indicator must be placed within <Radio.Item>.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { RadioItemContext, useRadioItemContext };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { createRovingFocus } from "../../core/rovingFocus.js";
|
|
4
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
5
|
+
import { RadioRootContext } from "./RadioRootContext.js";
|
|
6
|
+
import { createComponent, spread, template } from "@solidjs/web";
|
|
7
|
+
import { createUniqueId, omit, onFlush, untrack } from "solid-js";
|
|
8
|
+
// src/radio/root/RadioRoot.tsx
|
|
9
|
+
var _tmpl$ = /* @__PURE__ */ template(`<fieldset>`);
|
|
10
|
+
/**
|
|
11
|
+
* The group: a `<fieldset role="radiogroup">` around `Radio.Item`s, named
|
|
12
|
+
* by a `<legend>`, that holds the checked radio's value.
|
|
13
|
+
*/
|
|
14
|
+
function RadioRoot(props) {
|
|
15
|
+
const [value, setValue] = createControllableSignal({
|
|
16
|
+
get value() {
|
|
17
|
+
return props.value;
|
|
18
|
+
},
|
|
19
|
+
get defaultValue() {
|
|
20
|
+
return props.defaultValue ?? null;
|
|
21
|
+
},
|
|
22
|
+
onChange: (value) => props.onValueChange?.(value)
|
|
23
|
+
});
|
|
24
|
+
let vertical = false;
|
|
25
|
+
let moving = false;
|
|
26
|
+
let claimed = false;
|
|
27
|
+
const items = /* @__PURE__ */ new Set();
|
|
28
|
+
const roving = createRovingFocus({
|
|
29
|
+
get vertical() {
|
|
30
|
+
return vertical;
|
|
31
|
+
},
|
|
32
|
+
focusable: (item) => !item.disabled(),
|
|
33
|
+
preferred: (item) => item.checked()
|
|
34
|
+
});
|
|
35
|
+
const restore = () => {
|
|
36
|
+
for (const item of items) if (untrack(item.checked)) item.input.checked = true;
|
|
37
|
+
};
|
|
38
|
+
const validityName = createUniqueId();
|
|
39
|
+
const empty = () => value() === null;
|
|
40
|
+
const context = {
|
|
41
|
+
value,
|
|
42
|
+
select: (next) => {
|
|
43
|
+
setValue(next);
|
|
44
|
+
onFlush(restore);
|
|
45
|
+
},
|
|
46
|
+
reset: () => setValue(props.defaultValue ?? null),
|
|
47
|
+
name: () => props.name ?? (empty() ? validityName : void 0),
|
|
48
|
+
form: () => props.form,
|
|
49
|
+
disabled: () => props.disabled,
|
|
50
|
+
required: () => props.required && empty(),
|
|
51
|
+
register: (item) => {
|
|
52
|
+
items.add(item);
|
|
53
|
+
const unregister = roving.register(item);
|
|
54
|
+
return () => {
|
|
55
|
+
items.delete(item);
|
|
56
|
+
unregister();
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
isTabStop: roving.isTabStop,
|
|
60
|
+
claimTabStop: (checked, disabled) => !disabled && (checked || untrack(value) === null && !claimed && (claimed = true)),
|
|
61
|
+
navigate: (event, from) => {
|
|
62
|
+
if (!event.key.startsWith("Arrow")) return;
|
|
63
|
+
vertical = event.key === "ArrowUp" || event.key === "ArrowDown";
|
|
64
|
+
moving = true;
|
|
65
|
+
roving.navigate(event, from);
|
|
66
|
+
moving = false;
|
|
67
|
+
},
|
|
68
|
+
moving: () => moving
|
|
69
|
+
};
|
|
70
|
+
const elementProps = mergeElementProps({
|
|
71
|
+
role: "radiogroup",
|
|
72
|
+
get disabled() {
|
|
73
|
+
return props.disabled;
|
|
74
|
+
},
|
|
75
|
+
get "aria-required"() {
|
|
76
|
+
return props.required ? "true" : void 0;
|
|
77
|
+
},
|
|
78
|
+
get "data-disabled"() {
|
|
79
|
+
return props.disabled ? "" : void 0;
|
|
80
|
+
},
|
|
81
|
+
get "data-required"() {
|
|
82
|
+
return props.required ? "" : void 0;
|
|
83
|
+
}
|
|
84
|
+
}, omit(props, "render", "value", "defaultValue", "onValueChange", "name", "disabled", "required", "form"));
|
|
85
|
+
return createComponent(RadioRootContext, {
|
|
86
|
+
value: context,
|
|
87
|
+
get children() {
|
|
88
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
89
|
+
var _el$ = _tmpl$();
|
|
90
|
+
spread(_el$, p, false);
|
|
91
|
+
return _el$;
|
|
92
|
+
})());
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export { RadioRoot };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, useContext } from "solid-js";
|
|
3
|
+
// src/radio/root/RadioRootContext.ts
|
|
4
|
+
var RadioRootContext = createContext(null);
|
|
5
|
+
function useRadioRootContext() {
|
|
6
|
+
const context = useContext(RadioRootContext);
|
|
7
|
+
if (isDev && !context) throw new Error("[menhir] Radio.Item must be placed within <Radio.Root>.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { RadioRootContext, useRadioRootContext };
|
|
@@ -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,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;
|