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.
Files changed (51) hide show
  1. package/README.md +1 -1
  2. package/dist/checkbox/root/CheckboxRoot.js +10 -61
  3. package/dist/checkbox-group/index.js +2 -0
  4. package/dist/checkbox-group/index.parts.js +5 -0
  5. package/dist/checkbox-group/item/CheckboxGroupItem.js +40 -0
  6. package/dist/checkbox-group/parent/CheckboxGroupParent.js +55 -0
  7. package/dist/checkbox-group/root/CheckboxGroupRoot.js +89 -0
  8. package/dist/checkbox-group/root/CheckboxGroupRootContext.js +10 -0
  9. package/dist/core/hiddenInput.js +94 -0
  10. package/dist/core/rovingFocus.js +1 -1
  11. package/dist/radio/index.js +2 -0
  12. package/dist/radio/index.parts.js +4 -0
  13. package/dist/radio/indicator/RadioIndicator.js +20 -0
  14. package/dist/radio/item/RadioItem.js +94 -0
  15. package/dist/radio/item/RadioItemContext.js +10 -0
  16. package/dist/radio/root/RadioRoot.js +96 -0
  17. package/dist/radio/root/RadioRootContext.js +10 -0
  18. package/dist/source/checkbox/root/CheckboxRoot.jsx +5 -57
  19. package/dist/source/checkbox-group/index.d.ts +1 -0
  20. package/dist/source/checkbox-group/index.js +1 -0
  21. package/dist/source/checkbox-group/index.parts.d.ts +4 -0
  22. package/dist/source/checkbox-group/index.parts.js +4 -0
  23. package/dist/source/checkbox-group/item/CheckboxGroupItem.d.ts +10 -0
  24. package/dist/source/checkbox-group/item/CheckboxGroupItem.jsx +47 -0
  25. package/dist/source/checkbox-group/parent/CheckboxGroupParent.d.ts +17 -0
  26. package/dist/source/checkbox-group/parent/CheckboxGroupParent.jsx +72 -0
  27. package/dist/source/checkbox-group/root/CheckboxGroupRoot.d.ts +28 -0
  28. package/dist/source/checkbox-group/root/CheckboxGroupRoot.jsx +75 -0
  29. package/dist/source/checkbox-group/root/CheckboxGroupRootContext.d.ts +20 -0
  30. package/dist/source/checkbox-group/root/CheckboxGroupRootContext.js +13 -0
  31. package/dist/source/core/hiddenInput.d.ts +46 -0
  32. package/dist/source/core/hiddenInput.jsx +93 -0
  33. package/dist/source/core/rovingFocus.d.ts +1 -1
  34. package/dist/source/core/rovingFocus.js +1 -1
  35. package/dist/source/radio/index.d.ts +1 -0
  36. package/dist/source/radio/index.js +1 -0
  37. package/dist/source/radio/index.parts.d.ts +3 -0
  38. package/dist/source/radio/index.parts.js +3 -0
  39. package/dist/source/radio/indicator/RadioIndicator.d.ts +10 -0
  40. package/dist/source/radio/indicator/RadioIndicator.jsx +12 -0
  41. package/dist/source/radio/item/RadioItem.d.ts +14 -0
  42. package/dist/source/radio/item/RadioItem.jsx +96 -0
  43. package/dist/source/radio/item/RadioItemContext.d.ts +12 -0
  44. package/dist/source/radio/item/RadioItemContext.js +12 -0
  45. package/dist/source/radio/root/RadioRoot.d.ts +28 -0
  46. package/dist/source/radio/root/RadioRoot.jsx +112 -0
  47. package/dist/source/radio/root/RadioRootContext.d.ts +40 -0
  48. package/dist/source/radio/root/RadioRootContext.js +13 -0
  49. package/dist/source/switch/root/SwitchRoot.jsx +5 -57
  50. package/dist/switch/root/SwitchRoot.js +10 -61
  51. package/package.json +11 -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, 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, effect, heldRead, isDev, memo, ref, setAttribute, setProperty, spread, template } from "@solidjs/web";
7
- import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
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(`<input type=checkbox 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>`);
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?.click(),
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
- let input;
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$2 = _tmpl$2();
100
- spread(_el$2, p, false);
101
- return _el$2;
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,2 @@
1
+ import * as CheckboxGroup from "./index.parts.js";
2
+ export { CheckboxGroup };
@@ -0,0 +1,5 @@
1
+ import { CheckboxGroupRoot } from "./root/CheckboxGroupRoot.js";
2
+ import { CheckboxGroupItem } from "./item/CheckboxGroupItem.js";
3
+ import { CheckboxGroupParent } from "./parent/CheckboxGroupParent.js";
4
+ import { CheckboxIndicator } from "../checkbox/indicator/CheckboxIndicator.js";
5
+ export { CheckboxIndicator as Indicator, CheckboxGroupItem as Item, CheckboxGroupParent as Parent, CheckboxGroupRoot as Root };
@@ -0,0 +1,40 @@
1
+ import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.js";
2
+ import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
3
+ import { createComponent } from "@solidjs/web";
4
+ import { createMemo, merge, omit } from "solid-js";
5
+ // src/checkbox-group/item/CheckboxGroupItem.tsx
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
+ function CheckboxGroupItem(props) {
11
+ const group = useCheckboxGroupRootContext();
12
+ const checked = createMemo(() => group.value().includes(props.value));
13
+ const checkboxProps = merge(omit(props, "value", "disabled"), {
14
+ get checked() {
15
+ return checked();
16
+ },
17
+ get defaultChecked() {
18
+ return checked();
19
+ },
20
+ onCheckedChange: (included) => {
21
+ const next = group.requested().filter((item) => item !== props.value);
22
+ if (included) next.push(props.value);
23
+ group.setValue(next);
24
+ },
25
+ get name() {
26
+ return group.name();
27
+ },
28
+ get value() {
29
+ return props.value;
30
+ },
31
+ get form() {
32
+ return group.form();
33
+ },
34
+ get disabled() {
35
+ return group.disabled() || props.disabled;
36
+ }
37
+ });
38
+ return createComponent(CheckboxRoot, checkboxProps);
39
+ }
40
+ export { CheckboxGroupItem };
@@ -0,0 +1,55 @@
1
+ import { CheckboxRoot } from "../../checkbox/root/CheckboxRoot.js";
2
+ import { useCheckboxGroupRootContext } from "../root/CheckboxGroupRootContext.js";
3
+ import { createComponent } from "@solidjs/web";
4
+ import { createMemo, merge, omit } from "solid-js";
5
+ // src/checkbox-group/parent/CheckboxGroupParent.tsx
6
+ /**
7
+ * A checkbox over the group's boxes: a `Checkbox.Root`, checked while every
8
+ * box of `values` is, mixed while some are. A click on a mixed parent checks
9
+ * all, on a checked one unchecks all, and on an unchecked one brings back
10
+ * the boxes checked when it was last mixed, or checks all. It submits
11
+ * nothing. Place it inside `CheckboxGroup.Root`.
12
+ */
13
+ function CheckboxGroupParent(props) {
14
+ const group = useCheckboxGroupRootContext();
15
+ let set;
16
+ const boxes = createMemo((previous) => {
17
+ const list = group.value();
18
+ const checked = props.values.filter((item) => list.includes(item));
19
+ return {
20
+ checked,
21
+ remembered: previous && list === set ? previous.remembered : checked
22
+ };
23
+ });
24
+ const allChecked = createMemo(() => props.values.length > 0 && boxes().checked.length === props.values.length);
25
+ const someChecked = createMemo(() => boxes().checked.length > 0);
26
+ const checkboxProps = merge(omit(props, "values", "disabled"), {
27
+ get checked() {
28
+ return allChecked();
29
+ },
30
+ get defaultChecked() {
31
+ return allChecked();
32
+ },
33
+ get indeterminate() {
34
+ return someChecked() && !allChecked();
35
+ },
36
+ onCheckedChange: (checked) => {
37
+ const values = props.values;
38
+ const current = group.requested();
39
+ const others = current.filter((item) => !values.includes(item));
40
+ const { remembered } = boxes();
41
+ let next = others;
42
+ if (checked) next = values.some((item) => current.includes(item)) || remembered.length === 0 ? [...current, ...values.filter((item) => !current.includes(item))] : [...others, ...remembered];
43
+ set = next;
44
+ group.setValue(next);
45
+ },
46
+ get form() {
47
+ return group.form();
48
+ },
49
+ get disabled() {
50
+ return group.disabled() || props.disabled;
51
+ }
52
+ });
53
+ return createComponent(CheckboxRoot, checkboxProps);
54
+ }
55
+ export { CheckboxGroupParent };
@@ -0,0 +1,89 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.js";
4
+ import { CheckboxGroupRootContext } from "./CheckboxGroupRootContext.js";
5
+ import { createComponent, effect, memo, ref, setAttribute, spread, template } from "@solidjs/web";
6
+ import { createUniqueId, omit, onSettled } from "solid-js";
7
+ // src/checkbox-group/root/CheckboxGroupRoot.tsx
8
+ var _tmpl$ = /* @__PURE__ */ template(`<input type=radio 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>`);
9
+ var _tmpl$2 = /* @__PURE__ */ template(`<fieldset>`);
10
+ /**
11
+ * The group: a `<fieldset>` around `CheckboxGroup.Item`s, named by a
12
+ * `<legend>`, that holds the values of the checked boxes.
13
+ */
14
+ function CheckboxGroupRoot(props) {
15
+ const [value, setValue, requested] = createControllableSignal({
16
+ get value() {
17
+ return props.value;
18
+ },
19
+ get defaultValue() {
20
+ return props.defaultValue ?? [];
21
+ },
22
+ onChange: (value) => props.onValueChange?.(value)
23
+ });
24
+ const context = {
25
+ value,
26
+ requested,
27
+ setValue: (next) => setValue(next),
28
+ name: () => props.name,
29
+ form: () => props.form,
30
+ disabled: () => props.disabled
31
+ };
32
+ const validityName = createUniqueId();
33
+ let root;
34
+ let input;
35
+ onSettled(() => {
36
+ const form = input?.form;
37
+ if (!form) return;
38
+ const onReset = () => setValue(props.defaultValue ?? []);
39
+ form.addEventListener("reset", onReset);
40
+ return () => form.removeEventListener("reset", onReset);
41
+ });
42
+ const elementProps = mergeElementProps({
43
+ get disabled() {
44
+ return props.disabled;
45
+ },
46
+ get "data-disabled"() {
47
+ return props.disabled ? "" : void 0;
48
+ },
49
+ get "data-required"() {
50
+ return props.required ? "" : void 0;
51
+ },
52
+ ref: (element) => {
53
+ root = element;
54
+ }
55
+ }, omit(props, "render", "value", "defaultValue", "onValueChange", "name", "disabled", "required", "form"));
56
+ return createComponent(CheckboxGroupRootContext, {
57
+ value: context,
58
+ get children() {
59
+ return [memo(() => {
60
+ return renderElement(props, elementProps, (p) => (() => {
61
+ var _el$2 = _tmpl$2();
62
+ spread(_el$2, p, false);
63
+ return _el$2;
64
+ })());
65
+ }), (() => {
66
+ var _el$ = _tmpl$();
67
+ _el$.addEventListener("focus", () => root?.querySelector("[role=checkbox]:enabled")?.focus());
68
+ setAttribute(_el$, "name", validityName);
69
+ var _ref$ = input;
70
+ typeof _ref$ === "function" || Array.isArray(_ref$) ? ref(() => {
71
+ return _ref$;
72
+ }, _el$) : input = _el$;
73
+ effect(() => {
74
+ return {
75
+ e: props.disabled,
76
+ t: props.required && value().length === 0,
77
+ a: props.form
78
+ };
79
+ }, ({ e, t, a }, _p$) => {
80
+ e !== _p$?.e && setAttribute(_el$, "disabled", e);
81
+ t !== _p$?.t && setAttribute(_el$, "required", t);
82
+ a !== _p$?.a && setAttribute(_el$, "form", a);
83
+ });
84
+ return _el$;
85
+ })()];
86
+ }
87
+ });
88
+ }
89
+ export { CheckboxGroupRoot };
@@ -0,0 +1,10 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // src/checkbox-group/root/CheckboxGroupRootContext.ts
4
+ var CheckboxGroupRootContext = createContext(null);
5
+ function useCheckboxGroupRootContext() {
6
+ const context = useContext(CheckboxGroupRootContext);
7
+ if (isDev && !context) throw new Error("[menhir] CheckboxGroup parts must be placed within <CheckboxGroup.Root>.");
8
+ return context;
9
+ }
10
+ export { CheckboxGroupRootContext, useCheckboxGroupRootContext };
@@ -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 };
@@ -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,2 @@
1
+ import * as Radio from "./index.parts.js";
2
+ export { Radio };
@@ -0,0 +1,4 @@
1
+ import { RadioRoot } from "./root/RadioRoot.js";
2
+ import { RadioItem } from "./item/RadioItem.js";
3
+ import { RadioIndicator } from "./indicator/RadioIndicator.js";
4
+ export { RadioIndicator as Indicator, RadioItem as Item, RadioRoot as Root };
@@ -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 };