menhir 0.1.11 → 0.1.13

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 (60) hide show
  1. package/README.md +1 -1
  2. package/dist/checkbox/root/CheckboxRoot.js +10 -61
  3. package/dist/core/hiddenInput.js +94 -0
  4. package/dist/core/rovingFocus.js +1 -1
  5. package/dist/number-field/decrement/NumberFieldDecrement.js +11 -0
  6. package/dist/number-field/increment/NumberFieldIncrement.js +11 -0
  7. package/dist/number-field/index.js +2 -0
  8. package/dist/number-field/index.parts.js +5 -0
  9. package/dist/number-field/input/NumberFieldInput.js +87 -0
  10. package/dist/number-field/parse.js +28 -0
  11. package/dist/number-field/root/NumberFieldRoot.js +169 -0
  12. package/dist/number-field/root/NumberFieldRootContext.js +10 -0
  13. package/dist/number-field/stepButton.js +60 -0
  14. package/dist/radio/index.js +2 -0
  15. package/dist/radio/index.parts.js +4 -0
  16. package/dist/radio/indicator/RadioIndicator.js +20 -0
  17. package/dist/radio/item/RadioItem.js +94 -0
  18. package/dist/radio/item/RadioItemContext.js +10 -0
  19. package/dist/radio/root/RadioRoot.js +96 -0
  20. package/dist/radio/root/RadioRootContext.js +10 -0
  21. package/dist/source/checkbox/root/CheckboxRoot.jsx +5 -57
  22. package/dist/source/core/hiddenInput.d.ts +46 -0
  23. package/dist/source/core/hiddenInput.jsx +93 -0
  24. package/dist/source/core/rovingFocus.d.ts +1 -1
  25. package/dist/source/core/rovingFocus.js +1 -1
  26. package/dist/source/number-field/decrement/NumberFieldDecrement.d.ts +8 -0
  27. package/dist/source/number-field/decrement/NumberFieldDecrement.jsx +9 -0
  28. package/dist/source/number-field/increment/NumberFieldIncrement.d.ts +8 -0
  29. package/dist/source/number-field/increment/NumberFieldIncrement.jsx +9 -0
  30. package/dist/source/number-field/index.d.ts +1 -0
  31. package/dist/source/number-field/index.js +1 -0
  32. package/dist/source/number-field/index.parts.d.ts +4 -0
  33. package/dist/source/number-field/index.parts.js +4 -0
  34. package/dist/source/number-field/input/NumberFieldInput.d.ts +14 -0
  35. package/dist/source/number-field/input/NumberFieldInput.jsx +103 -0
  36. package/dist/source/number-field/parse.d.ts +11 -0
  37. package/dist/source/number-field/parse.js +46 -0
  38. package/dist/source/number-field/root/NumberFieldRoot.d.ts +49 -0
  39. package/dist/source/number-field/root/NumberFieldRoot.jsx +175 -0
  40. package/dist/source/number-field/root/NumberFieldRootContext.d.ts +35 -0
  41. package/dist/source/number-field/root/NumberFieldRootContext.js +13 -0
  42. package/dist/source/number-field/stepButton.d.ts +12 -0
  43. package/dist/source/number-field/stepButton.jsx +69 -0
  44. package/dist/source/radio/index.d.ts +1 -0
  45. package/dist/source/radio/index.js +1 -0
  46. package/dist/source/radio/index.parts.d.ts +3 -0
  47. package/dist/source/radio/index.parts.js +3 -0
  48. package/dist/source/radio/indicator/RadioIndicator.d.ts +10 -0
  49. package/dist/source/radio/indicator/RadioIndicator.jsx +12 -0
  50. package/dist/source/radio/item/RadioItem.d.ts +14 -0
  51. package/dist/source/radio/item/RadioItem.jsx +96 -0
  52. package/dist/source/radio/item/RadioItemContext.d.ts +12 -0
  53. package/dist/source/radio/item/RadioItemContext.js +12 -0
  54. package/dist/source/radio/root/RadioRoot.d.ts +28 -0
  55. package/dist/source/radio/root/RadioRoot.jsx +112 -0
  56. package/dist/source/radio/root/RadioRootContext.d.ts +40 -0
  57. package/dist/source/radio/root/RadioRootContext.js +13 -0
  58. package/dist/source/switch/root/SwitchRoot.jsx +5 -57
  59. package/dist/switch/root/SwitchRoot.js +10 -61
  60. package/package.json +11 -1
@@ -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, onFlush, onSettled, untrack } from "solid-js";
2
+ import { merge, omit } from "solid-js";
3
3
  import { warnUnlessButton } from "../../core/button.js";
4
4
  import { createControllableSignal } from "../../core/createControllableSignal.js";
5
+ import { createHiddenInput } from "../../core/hiddenInput.jsx";
5
6
  import { mergeElementProps } from "../../core/mergeElementProps.js";
6
7
  import { renderElement } from "../../core/renderElement.jsx";
7
8
  import { 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?.click(),
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
- let input;
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
- <input type="checkbox" aria-hidden="true" tabindex={-1} style={visuallyHidden} name={props.name} value={props.value} disabled={props.disabled} required={props.required} form={props.form} checked={checked()} onChange={onInputChange}
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;
@@ -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,8 @@
1
+ import type { StepButtonProps } from "../stepButton.tsx";
2
+ export type NumberFieldDecrementProps = StepButtonProps;
3
+ /**
4
+ * The button that decreases the number by `step`: a `<button type="button"
5
+ * tabindex="-1">`, repeating while held, disabled at `min`. Give it a
6
+ * name, such as `aria-label="Decrease"`. Place it inside `NumberField.Root`.
7
+ */
8
+ export declare function NumberFieldDecrement(props: NumberFieldDecrementProps): import("@solidjs/web").JSX.Element;
@@ -0,0 +1,9 @@
1
+ import { renderStepButton } from "../stepButton.jsx";
2
+ /**
3
+ * The button that decreases the number by `step`: a `<button type="button"
4
+ * tabindex="-1">`, repeating while held, disabled at `min`. Give it a
5
+ * name, such as `aria-label="Decrease"`. Place it inside `NumberField.Root`.
6
+ */
7
+ export function NumberFieldDecrement(props) {
8
+ return renderStepButton(props, -1, "NumberField.Decrement");
9
+ }
@@ -0,0 +1,8 @@
1
+ import type { StepButtonProps } from "../stepButton.tsx";
2
+ export type NumberFieldIncrementProps = StepButtonProps;
3
+ /**
4
+ * The button that increases the number by `step`: a `<button type="button"
5
+ * tabindex="-1">`, repeating while held, disabled at `max`. Give it a
6
+ * name, such as `aria-label="Increase"`. Place it inside `NumberField.Root`.
7
+ */
8
+ export declare function NumberFieldIncrement(props: NumberFieldIncrementProps): import("@solidjs/web").JSX.Element;
@@ -0,0 +1,9 @@
1
+ import { renderStepButton } from "../stepButton.jsx";
2
+ /**
3
+ * The button that increases the number by `step`: a `<button type="button"
4
+ * tabindex="-1">`, repeating while held, disabled at `max`. Give it a
5
+ * name, such as `aria-label="Increase"`. Place it inside `NumberField.Root`.
6
+ */
7
+ export function NumberFieldIncrement(props) {
8
+ return renderStepButton(props, 1, "NumberField.Increment");
9
+ }
@@ -0,0 +1 @@
1
+ export * as NumberField from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as NumberField from "./index.parts.js";
@@ -0,0 +1,4 @@
1
+ export { NumberFieldRoot as Root } from "./root/NumberFieldRoot.tsx";
2
+ export { NumberFieldInput as Input } from "./input/NumberFieldInput.tsx";
3
+ export { NumberFieldIncrement as Increment } from "./increment/NumberFieldIncrement.tsx";
4
+ export { NumberFieldDecrement as Decrement } from "./decrement/NumberFieldDecrement.tsx";
@@ -0,0 +1,4 @@
1
+ export { NumberFieldRoot as Root } from "./root/NumberFieldRoot.jsx";
2
+ export { NumberFieldInput as Input } from "./input/NumberFieldInput.jsx";
3
+ export { NumberFieldIncrement as Increment } from "./increment/NumberFieldIncrement.jsx";
4
+ export { NumberFieldDecrement as Decrement } from "./decrement/NumberFieldDecrement.jsx";
@@ -0,0 +1,14 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface NumberFieldInputProps extends Omit<JSX.InputHTMLAttributes<HTMLInputElement>, "ref" | "id" | "value" | "defaultValue" | "type"> {
4
+ ref?: JSX.Ref<HTMLElement>;
5
+ /** Renders the input with your own component, which must render an `<input>`. Read once. */
6
+ render?: RenderProp;
7
+ }
8
+ /**
9
+ * The spinbutton: an `<input type="text" role="spinbutton">` that shows the
10
+ * number formatted, keeps text as it is typed, and reads it back on blur
11
+ * and on Enter. Up, Down, Page Up, Page Down, Home and End step the number.
12
+ * Takes the Root's `id`; place it inside `NumberField.Root`.
13
+ */
14
+ export declare function NumberFieldInput(props: NumberFieldInputProps): JSX.Element;
@@ -0,0 +1,103 @@
1
+ import { createEffect, merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useNumberFieldRootContext } from "../root/NumberFieldRootContext.js";
5
+ /**
6
+ * The spinbutton: an `<input type="text" role="spinbutton">` that shows the
7
+ * number formatted, keeps text as it is typed, and reads it back on blur
8
+ * and on Enter. Up, Down, Page Up, Page Down, Home and End step the number.
9
+ * Takes the Root's `id`; place it inside `NumberField.Root`.
10
+ */
11
+ export function NumberFieldInput(props) {
12
+ const root = useNumberFieldRootContext();
13
+ const ownProps = merge(root.attributes, {
14
+ type: "text",
15
+ role: "spinbutton",
16
+ inputmode: "decimal",
17
+ autocomplete: "off",
18
+ get id() {
19
+ return root.inputId();
20
+ },
21
+ // The `value` attribute, not the property: a binding of `value` would
22
+ // write the text again over the user's when any other attribute of the
23
+ // element changes, such as a Field's `aria-invalid`. The attribute is
24
+ // the server's text, and the text a form reset puts back; it leaves an
25
+ // edited input alone.
26
+ get defaultValue() {
27
+ return root.text();
28
+ },
29
+ get disabled() {
30
+ return root.props.disabled;
31
+ },
32
+ get readonly() {
33
+ return root.props.readOnly;
34
+ },
35
+ get "aria-valuenow"() {
36
+ return root.value() ?? undefined;
37
+ },
38
+ get "aria-valuetext"() {
39
+ return root.value() === null ? undefined : root.text();
40
+ },
41
+ get "aria-valuemin"() {
42
+ return root.props.min;
43
+ },
44
+ get "aria-valuemax"() {
45
+ return root.props.max;
46
+ },
47
+ // Solid 2.0 removes an attribute set to false, so spell the value out.
48
+ get "aria-required"() {
49
+ return root.props.required ? "true" : undefined;
50
+ },
51
+ // From a Field around the Root, which is its Control.
52
+ get "aria-describedby"() {
53
+ return root.props["aria-describedby"];
54
+ },
55
+ get "aria-invalid"() {
56
+ return root.props["aria-invalid"];
57
+ },
58
+ onInput: () => {
59
+ root.edited = true;
60
+ },
61
+ onBlur: () => root.commit(),
62
+ onKeyDown: (event) => {
63
+ const { key } = event;
64
+ // Enter commits and goes on, so that a form submits the number. An
65
+ // input method uses Enter to commit its own text.
66
+ if (key === "Enter") {
67
+ if (!event.isComposing)
68
+ root.commit();
69
+ return;
70
+ }
71
+ if (!root.enabled() || event.ctrlKey || event.altKey || event.metaKey)
72
+ return;
73
+ const limit = key === "Home" ? "min" : key === "End" ? "max" : undefined;
74
+ if (key === "ArrowUp" || key === "ArrowDown") {
75
+ // Shift with an arrow selects text.
76
+ if (event.shiftKey)
77
+ return;
78
+ root.step(key === "ArrowUp" ? 1 : -1);
79
+ }
80
+ else if (key === "PageUp" || key === "PageDown") {
81
+ root.step(key === "PageUp" ? 1 : -1, true);
82
+ }
83
+ else if (limit && root.props[limit] !== undefined) {
84
+ root.setLimit(limit);
85
+ }
86
+ else {
87
+ // Every other key edits the text, as the browser does.
88
+ return;
89
+ }
90
+ event.preventDefault();
91
+ },
92
+ ref: (element) => {
93
+ root.input = element;
94
+ },
95
+ });
96
+ // The value changed, by the app or by a step: the input shows its text,
97
+ // over any text the user had typed.
98
+ createEffect(() => root.text(), () => root.sync());
99
+ // Typed for an input; the merge takes any element's attributes.
100
+ const consumerProps = omit(props, "render");
101
+ const elementProps = mergeElementProps(ownProps, consumerProps);
102
+ return renderElement(props, elementProps, (p) => <input {...p}/>);
103
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Reads text written in a formatter's locale and format back to a number,
3
+ * `NaN` when it is not one (src/number-field/README.md, "Decisions"). Reads
4
+ * the locale's digits, and ASCII digits, its decimal separator, its minus
5
+ * sign and `-`; drops its group separator, whitespace, apostrophes, and the
6
+ * currency, percent sign, unit and literal text the format writes. Any
7
+ * other character makes the text not a number. A percentage is read as the
8
+ * fraction the formatter writes as one: `25%` is `0.25`. Empty text is
9
+ * `NaN` too: the caller reads it as no value.
10
+ */
11
+ export declare function parseNumber(formatter: Intl.NumberFormat, text: string): number;