menhir 0.1.12 → 0.1.14
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/number-field/decrement/NumberFieldDecrement.js +11 -0
- package/dist/number-field/increment/NumberFieldIncrement.js +11 -0
- package/dist/number-field/index.js +2 -0
- package/dist/number-field/index.parts.js +5 -0
- package/dist/number-field/input/NumberFieldInput.js +87 -0
- package/dist/number-field/parse.js +28 -0
- package/dist/number-field/root/NumberFieldRoot.js +169 -0
- package/dist/number-field/root/NumberFieldRootContext.js +10 -0
- package/dist/number-field/stepButton.js +60 -0
- package/dist/slider/index.js +2 -0
- package/dist/slider/index.parts.js +5 -0
- package/dist/slider/root/SliderRoot.js +123 -0
- package/dist/slider/root/SliderRootContext.js +10 -0
- package/dist/slider/thumb/SliderThumb.js +119 -0
- package/dist/slider/track/SliderTrack.js +65 -0
- package/dist/slider/value/SliderValue.js +24 -0
- package/dist/source/number-field/decrement/NumberFieldDecrement.d.ts +8 -0
- package/dist/source/number-field/decrement/NumberFieldDecrement.jsx +9 -0
- package/dist/source/number-field/increment/NumberFieldIncrement.d.ts +8 -0
- package/dist/source/number-field/increment/NumberFieldIncrement.jsx +9 -0
- package/dist/source/number-field/index.d.ts +1 -0
- package/dist/source/number-field/index.js +1 -0
- package/dist/source/number-field/index.parts.d.ts +4 -0
- package/dist/source/number-field/index.parts.js +4 -0
- package/dist/source/number-field/input/NumberFieldInput.d.ts +14 -0
- package/dist/source/number-field/input/NumberFieldInput.jsx +103 -0
- package/dist/source/number-field/parse.d.ts +11 -0
- package/dist/source/number-field/parse.js +46 -0
- package/dist/source/number-field/root/NumberFieldRoot.d.ts +49 -0
- package/dist/source/number-field/root/NumberFieldRoot.jsx +175 -0
- package/dist/source/number-field/root/NumberFieldRootContext.d.ts +35 -0
- package/dist/source/number-field/root/NumberFieldRootContext.js +13 -0
- package/dist/source/number-field/stepButton.d.ts +12 -0
- package/dist/source/number-field/stepButton.jsx +69 -0
- package/dist/source/slider/index.d.ts +1 -0
- package/dist/source/slider/index.js +1 -0
- package/dist/source/slider/index.parts.d.ts +4 -0
- package/dist/source/slider/index.parts.js +4 -0
- package/dist/source/slider/root/SliderRoot.d.ts +55 -0
- package/dist/source/slider/root/SliderRoot.jsx +152 -0
- package/dist/source/slider/root/SliderRootContext.d.ts +51 -0
- package/dist/source/slider/root/SliderRootContext.js +13 -0
- package/dist/source/slider/thumb/SliderThumb.d.ts +13 -0
- package/dist/source/slider/thumb/SliderThumb.jsx +91 -0
- package/dist/source/slider/track/SliderTrack.d.ts +11 -0
- package/dist/source/slider/track/SliderTrack.jsx +79 -0
- package/dist/source/slider/value/SliderValue.d.ts +11 -0
- package/dist/source/slider/value/SliderValue.jsx +18 -0
- 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, 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.
|
|
65
|
+
- Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Checkbox Group, Collapsible, Combobox, Context Menu, Dialog, Field, Form, Input, Menu, Menubar, Meter, Navigation Menu, Number Field, Popover, Progress, Radio, Select, Separator, Slider, 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
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { renderStepButton } from "../stepButton.js";
|
|
2
|
+
// src/number-field/decrement/NumberFieldDecrement.tsx
|
|
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
|
+
function NumberFieldDecrement(props) {
|
|
9
|
+
return renderStepButton(props, -1, "NumberField.Decrement");
|
|
10
|
+
}
|
|
11
|
+
export { NumberFieldDecrement };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { renderStepButton } from "../stepButton.js";
|
|
2
|
+
// src/number-field/increment/NumberFieldIncrement.tsx
|
|
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
|
+
function NumberFieldIncrement(props) {
|
|
9
|
+
return renderStepButton(props, 1, "NumberField.Increment");
|
|
10
|
+
}
|
|
11
|
+
export { NumberFieldIncrement };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { NumberFieldRoot } from "./root/NumberFieldRoot.js";
|
|
2
|
+
import { NumberFieldInput } from "./input/NumberFieldInput.js";
|
|
3
|
+
import { NumberFieldIncrement } from "./increment/NumberFieldIncrement.js";
|
|
4
|
+
import { NumberFieldDecrement } from "./decrement/NumberFieldDecrement.js";
|
|
5
|
+
export { NumberFieldDecrement as Decrement, NumberFieldIncrement as Increment, NumberFieldInput as Input, NumberFieldRoot as Root };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useNumberFieldRootContext } from "../root/NumberFieldRootContext.js";
|
|
4
|
+
import { spread, template } from "@solidjs/web";
|
|
5
|
+
import { createEffect, merge, omit } from "solid-js";
|
|
6
|
+
// src/number-field/input/NumberFieldInput.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input>`);
|
|
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
|
+
function NumberFieldInput(props) {
|
|
15
|
+
const root = useNumberFieldRootContext();
|
|
16
|
+
const ownProps = merge(root.attributes, {
|
|
17
|
+
type: "text",
|
|
18
|
+
role: "spinbutton",
|
|
19
|
+
inputmode: "decimal",
|
|
20
|
+
autocomplete: "off",
|
|
21
|
+
get id() {
|
|
22
|
+
return root.inputId();
|
|
23
|
+
},
|
|
24
|
+
get defaultValue() {
|
|
25
|
+
return root.text();
|
|
26
|
+
},
|
|
27
|
+
get disabled() {
|
|
28
|
+
return root.props.disabled;
|
|
29
|
+
},
|
|
30
|
+
get readonly() {
|
|
31
|
+
return root.props.readOnly;
|
|
32
|
+
},
|
|
33
|
+
get "aria-valuenow"() {
|
|
34
|
+
return root.value() ?? void 0;
|
|
35
|
+
},
|
|
36
|
+
get "aria-valuetext"() {
|
|
37
|
+
return root.value() === null ? void 0 : root.text();
|
|
38
|
+
},
|
|
39
|
+
get "aria-valuemin"() {
|
|
40
|
+
return root.props.min;
|
|
41
|
+
},
|
|
42
|
+
get "aria-valuemax"() {
|
|
43
|
+
return root.props.max;
|
|
44
|
+
},
|
|
45
|
+
get "aria-required"() {
|
|
46
|
+
return root.props.required ? "true" : void 0;
|
|
47
|
+
},
|
|
48
|
+
get "aria-describedby"() {
|
|
49
|
+
return root.props["aria-describedby"];
|
|
50
|
+
},
|
|
51
|
+
get "aria-invalid"() {
|
|
52
|
+
return root.props["aria-invalid"];
|
|
53
|
+
},
|
|
54
|
+
onInput: () => {
|
|
55
|
+
root.edited = true;
|
|
56
|
+
},
|
|
57
|
+
onBlur: () => root.commit(),
|
|
58
|
+
onKeyDown: (event) => {
|
|
59
|
+
const { key } = event;
|
|
60
|
+
if (key === "Enter") {
|
|
61
|
+
if (!event.isComposing) root.commit();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (!root.enabled() || event.ctrlKey || event.altKey || event.metaKey) return;
|
|
65
|
+
const limit = key === "Home" ? "min" : key === "End" ? "max" : void 0;
|
|
66
|
+
if (key === "ArrowUp" || key === "ArrowDown") {
|
|
67
|
+
if (event.shiftKey) return;
|
|
68
|
+
root.step(key === "ArrowUp" ? 1 : -1);
|
|
69
|
+
} else if (key === "PageUp" || key === "PageDown") root.step(key === "PageUp" ? 1 : -1, true);
|
|
70
|
+
else if (limit && root.props[limit] !== void 0) root.setLimit(limit);
|
|
71
|
+
else return;
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
},
|
|
74
|
+
ref: (element) => {
|
|
75
|
+
root.input = element;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
createEffect(() => root.text(), () => root.sync());
|
|
79
|
+
const consumerProps = omit(props, "render");
|
|
80
|
+
const elementProps = mergeElementProps(ownProps, consumerProps);
|
|
81
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
82
|
+
var _el$ = _tmpl$();
|
|
83
|
+
spread(_el$, p, false);
|
|
84
|
+
return _el$;
|
|
85
|
+
})());
|
|
86
|
+
}
|
|
87
|
+
export { NumberFieldInput };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/number-field/parse.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reads text written in a formatter's locale and format back to a number,
|
|
4
|
+
* `NaN` when it is not one (src/number-field/README.md, "Decisions"). Reads
|
|
5
|
+
* the locale's digits, and ASCII digits, its decimal separator, its minus
|
|
6
|
+
* sign and `-`; drops its group separator, whitespace, apostrophes, and the
|
|
7
|
+
* currency, percent sign, unit and literal text the format writes. Any
|
|
8
|
+
* other character makes the text not a number. A percentage is read as the
|
|
9
|
+
* fraction the formatter writes as one: `25%` is `0.25`. Empty text is
|
|
10
|
+
* `NaN` too: the caller reads it as no value.
|
|
11
|
+
*/
|
|
12
|
+
function parseNumber(formatter, text) {
|
|
13
|
+
const { locale, numberingSystem, style } = formatter.resolvedOptions();
|
|
14
|
+
const plain = new Intl.NumberFormat(locale, { numberingSystem });
|
|
15
|
+
const meaning = { "-": "-" };
|
|
16
|
+
for (const { type, value } of plain.formatToParts(-12345.6)) {
|
|
17
|
+
if (type === "decimal") meaning[value] = ".";
|
|
18
|
+
if (type === "minusSign") meaning[value] = "-";
|
|
19
|
+
if (type === "group") meaning[value] = "";
|
|
20
|
+
}
|
|
21
|
+
for (let digit = 0; digit < 10; digit++) meaning[plain.format(digit)] = String(digit);
|
|
22
|
+
for (const { type, value } of [...formatter.formatToParts(-1), ...formatter.formatToParts(1)]) if (/^(currency|percentSign|unit|literal|plusSign)$/.test(type)) text = text.split(value).join("");
|
|
23
|
+
let number = "";
|
|
24
|
+
for (const char of text.replace(/[\s'’]/g, "")) number += /\d/.test(char) ? char : meaning[char] ?? "!";
|
|
25
|
+
if (!/^-?(\d+\.?\d*|\.\d+)$/.test(number)) return NaN;
|
|
26
|
+
return style === "percent" ? Number(number) / 100 : Number(number);
|
|
27
|
+
}
|
|
28
|
+
export { parseNumber };
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
4
|
+
import { parseNumber } from "../parse.js";
|
|
5
|
+
import { NumberFieldRootContext } from "./NumberFieldRootContext.js";
|
|
6
|
+
import { createComponent, effect, heldRead, memo, ref, setAttribute, spread, template } from "@solidjs/web";
|
|
7
|
+
import { createMemo, createUniqueId, omit, onFlush, onSettled, untrack } from "solid-js";
|
|
8
|
+
// src/number-field/root/NumberFieldRoot.tsx
|
|
9
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input type=number 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(`<div>`);
|
|
11
|
+
/**
|
|
12
|
+
* The number field: a `<div>` around its Input and buttons, which holds the
|
|
13
|
+
* value, then a visually hidden `<input type="number">` with the plain
|
|
14
|
+
* number, the form's view of it. Its `id`, `aria-describedby` and
|
|
15
|
+
* `aria-invalid` go on the Input, so it joins a Field as `Field.Control`'s
|
|
16
|
+
* `render`.
|
|
17
|
+
*/
|
|
18
|
+
function NumberFieldRoot(props) {
|
|
19
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
20
|
+
get value() {
|
|
21
|
+
return props.value;
|
|
22
|
+
},
|
|
23
|
+
get defaultValue() {
|
|
24
|
+
return props.defaultValue ?? null;
|
|
25
|
+
},
|
|
26
|
+
onChange: (next) => props.onValueChange?.(next)
|
|
27
|
+
});
|
|
28
|
+
const formatter = createMemo(() => new Intl.NumberFormat(props.locale, props.format));
|
|
29
|
+
const format = (number) => number === null ? "" : formatter().format(number);
|
|
30
|
+
const generatedId = createUniqueId();
|
|
31
|
+
let hidden;
|
|
32
|
+
const roundAndClamp = (number, digits) => Math.min(Math.max(Number(number.toFixed(digits)), props.min ?? -Infinity), props.max ?? Infinity);
|
|
33
|
+
const formValue = (number) => number === null ? "" : String(number);
|
|
34
|
+
const sync = () => {
|
|
35
|
+
context.edited = false;
|
|
36
|
+
const number = untrack(value);
|
|
37
|
+
const text = untrack(() => format(number));
|
|
38
|
+
if (context.input && context.input.value !== text) context.input.value = text;
|
|
39
|
+
if (hidden) hidden.value = formValue(number);
|
|
40
|
+
};
|
|
41
|
+
const change = (next) => {
|
|
42
|
+
const before = hidden?.value;
|
|
43
|
+
if (hidden) hidden.value = formValue(next);
|
|
44
|
+
setValue(next);
|
|
45
|
+
if (hidden && hidden.value !== before) {
|
|
46
|
+
hidden.dispatchEvent(new Event("input", { bubbles: true }));
|
|
47
|
+
hidden.dispatchEvent(new Event("change", { bubbles: true }));
|
|
48
|
+
}
|
|
49
|
+
onFlush(sync);
|
|
50
|
+
};
|
|
51
|
+
const current = () => {
|
|
52
|
+
if (context.edited && context.input) {
|
|
53
|
+
const parsed = parseNumber(formatter(), context.input.value);
|
|
54
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
55
|
+
}
|
|
56
|
+
return requested();
|
|
57
|
+
};
|
|
58
|
+
const places = (number = 0) => (String(number).split(".")[1] ?? "").length;
|
|
59
|
+
const context = {
|
|
60
|
+
props,
|
|
61
|
+
value,
|
|
62
|
+
text: () => format(value()),
|
|
63
|
+
inputId: () => props.id || generatedId,
|
|
64
|
+
attributes: {
|
|
65
|
+
get "data-disabled"() {
|
|
66
|
+
return props.disabled ? "" : void 0;
|
|
67
|
+
},
|
|
68
|
+
get "data-readonly"() {
|
|
69
|
+
return props.readOnly ? "" : void 0;
|
|
70
|
+
},
|
|
71
|
+
get "data-required"() {
|
|
72
|
+
return props.required ? "" : void 0;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
edited: false,
|
|
76
|
+
sync,
|
|
77
|
+
enabled: () => !props.disabled && !props.readOnly,
|
|
78
|
+
canStep: (direction) => {
|
|
79
|
+
const number = value();
|
|
80
|
+
const limit = direction > 0 ? props.max : props.min;
|
|
81
|
+
return context.enabled() && (number === null || limit === void 0 || (direction > 0 ? number < limit : number > limit));
|
|
82
|
+
},
|
|
83
|
+
step: (direction, large) => {
|
|
84
|
+
const size = props.step ?? 1;
|
|
85
|
+
const amount = large ? props.largeStep ?? size * 10 : size;
|
|
86
|
+
const origin = props.min ?? 0;
|
|
87
|
+
const steps = ((current() ?? 0) - origin) / size;
|
|
88
|
+
const next = origin + (direction > 0 ? Math.floor(steps + 1e-9) : Math.ceil(steps - 1e-9)) * size + direction * amount;
|
|
89
|
+
change(roundAndClamp(next, Math.max(places(size), places(origin), places(amount))));
|
|
90
|
+
},
|
|
91
|
+
setLimit: (limit) => {
|
|
92
|
+
const number = props[limit];
|
|
93
|
+
if (number !== void 0) change(number);
|
|
94
|
+
},
|
|
95
|
+
commit: () => {
|
|
96
|
+
if (!context.edited || !context.input) return;
|
|
97
|
+
const text = context.input.value.trim();
|
|
98
|
+
const parsed = text ? parseNumber(formatter(), text) : null;
|
|
99
|
+
if (Number.isNaN(parsed)) return sync();
|
|
100
|
+
if (parsed === null) return change(null);
|
|
101
|
+
const options = formatter().resolvedOptions();
|
|
102
|
+
change(roundAndClamp(parsed, options.maximumFractionDigits + (options.style === "percent" ? 2 : 0)));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
onSettled(() => {
|
|
106
|
+
const form = hidden?.form;
|
|
107
|
+
if (!form) return;
|
|
108
|
+
let disposed = false;
|
|
109
|
+
const onReset = (event) => {
|
|
110
|
+
setTimeout(() => {
|
|
111
|
+
if (disposed || event.defaultPrevented) return;
|
|
112
|
+
setValue(props.defaultValue ?? null);
|
|
113
|
+
onFlush(sync);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
form.addEventListener("reset", onReset);
|
|
117
|
+
return () => {
|
|
118
|
+
disposed = true;
|
|
119
|
+
form.removeEventListener("reset", onReset);
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
const elementProps = mergeElementProps(context.attributes, omit(props, "render", "value", "defaultValue", "onValueChange", "min", "max", "step", "largeStep", "format", "locale", "name", "form", "required", "disabled", "readOnly", "id", "aria-describedby", "aria-invalid"));
|
|
123
|
+
return createComponent(NumberFieldRootContext, {
|
|
124
|
+
value: context,
|
|
125
|
+
get children() {
|
|
126
|
+
return [memo(() => {
|
|
127
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
128
|
+
var _el$2 = _tmpl$2();
|
|
129
|
+
spread(_el$2, p, false);
|
|
130
|
+
return _el$2;
|
|
131
|
+
})());
|
|
132
|
+
}), (() => {
|
|
133
|
+
var _el$ = _tmpl$();
|
|
134
|
+
_el$.addEventListener("focus", () => context.input?.focus());
|
|
135
|
+
ref(() => {
|
|
136
|
+
return (element) => {
|
|
137
|
+
hidden = element;
|
|
138
|
+
};
|
|
139
|
+
}, _el$);
|
|
140
|
+
effect(() => {
|
|
141
|
+
return {
|
|
142
|
+
e: props.name,
|
|
143
|
+
t: props.form,
|
|
144
|
+
a: props.min,
|
|
145
|
+
o: props.max,
|
|
146
|
+
i: props.step ?? "any",
|
|
147
|
+
n: props.required,
|
|
148
|
+
s: props.disabled,
|
|
149
|
+
h: props.readOnly,
|
|
150
|
+
r: formValue(value()),
|
|
151
|
+
d: heldRead()
|
|
152
|
+
};
|
|
153
|
+
}, ({ e, t, a, o, i, n, s, h, r, d }, _p$) => {
|
|
154
|
+
e !== _p$?.e && setAttribute(_el$, "name", e);
|
|
155
|
+
t !== _p$?.t && setAttribute(_el$, "form", t);
|
|
156
|
+
a !== _p$?.a && setAttribute(_el$, "min", a);
|
|
157
|
+
o !== _p$?.o && setAttribute(_el$, "max", o);
|
|
158
|
+
i !== _p$?.i && setAttribute(_el$, "step", i);
|
|
159
|
+
n !== _p$?.n && setAttribute(_el$, "required", n);
|
|
160
|
+
s !== _p$?.s && setAttribute(_el$, "disabled", s);
|
|
161
|
+
h !== _p$?.h && setAttribute(_el$, "readonly", h);
|
|
162
|
+
(r !== _p$?.r || !d) && (_el$.value = r ?? "");
|
|
163
|
+
});
|
|
164
|
+
return _el$;
|
|
165
|
+
})()];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
export { NumberFieldRoot };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, useContext } from "solid-js";
|
|
3
|
+
// src/number-field/root/NumberFieldRootContext.ts
|
|
4
|
+
var NumberFieldRootContext = createContext(null);
|
|
5
|
+
function useNumberFieldRootContext() {
|
|
6
|
+
const context = useContext(NumberFieldRootContext);
|
|
7
|
+
if (isDev && !context) throw new Error("[menhir] NumberField parts must be placed within <NumberField.Root>.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { NumberFieldRootContext, useNumberFieldRootContext };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { warnUnlessButton } from "../core/button.js";
|
|
2
|
+
import { mergeElementProps } from "../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../core/renderElement.js";
|
|
4
|
+
import { useNumberFieldRootContext } from "./root/NumberFieldRootContext.js";
|
|
5
|
+
import { isDev, spread, template } from "@solidjs/web";
|
|
6
|
+
import { merge, omit, onSettled } from "solid-js";
|
|
7
|
+
// src/number-field/stepButton.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<button>`);
|
|
9
|
+
var holdDelay = 400;
|
|
10
|
+
var repeatInterval = 60;
|
|
11
|
+
/**
|
|
12
|
+
* Increment or Decrement: a `<button type="button" tabindex="-1">` that
|
|
13
|
+
* steps the value that way on a click, and again and again while a pointer
|
|
14
|
+
* holds it. It never takes focus, and is disabled at its limit.
|
|
15
|
+
*/
|
|
16
|
+
function renderStepButton(props, direction, part) {
|
|
17
|
+
const root = useNumberFieldRootContext();
|
|
18
|
+
let timer;
|
|
19
|
+
const stop = () => clearTimeout(timer);
|
|
20
|
+
const repeat = (delay) => {
|
|
21
|
+
root.step(direction);
|
|
22
|
+
timer = setTimeout(() => {
|
|
23
|
+
if (root.canStep(direction)) repeat(repeatInterval);
|
|
24
|
+
}, delay);
|
|
25
|
+
};
|
|
26
|
+
onSettled(() => stop);
|
|
27
|
+
const ownProps = merge(root.attributes, {
|
|
28
|
+
type: "button",
|
|
29
|
+
tabindex: -1,
|
|
30
|
+
get disabled() {
|
|
31
|
+
return !root.canStep(direction);
|
|
32
|
+
},
|
|
33
|
+
get "aria-controls"() {
|
|
34
|
+
return root.inputId();
|
|
35
|
+
},
|
|
36
|
+
onPointerDown: (event) => {
|
|
37
|
+
if (event.button !== 0) return;
|
|
38
|
+
event.preventDefault();
|
|
39
|
+
if (event.pointerType === "mouse") root.input?.focus();
|
|
40
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
41
|
+
repeat(holdDelay);
|
|
42
|
+
},
|
|
43
|
+
onPointerUp: stop,
|
|
44
|
+
onPointerCancel: stop,
|
|
45
|
+
onLostPointerCapture: stop,
|
|
46
|
+
onClick: (event) => {
|
|
47
|
+
if (event.detail === 0) root.step(direction);
|
|
48
|
+
},
|
|
49
|
+
ref: (element) => {
|
|
50
|
+
if (isDev) warnUnlessButton(element, part);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
54
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
55
|
+
var _el$ = _tmpl$();
|
|
56
|
+
spread(_el$, p, false);
|
|
57
|
+
return _el$;
|
|
58
|
+
})());
|
|
59
|
+
}
|
|
60
|
+
export { renderStepButton };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SliderRoot } from "./root/SliderRoot.js";
|
|
2
|
+
import { SliderTrack } from "./track/SliderTrack.js";
|
|
3
|
+
import { SliderThumb } from "./thumb/SliderThumb.js";
|
|
4
|
+
import { SliderValue } from "./value/SliderValue.js";
|
|
5
|
+
export { SliderRoot as Root, SliderThumb as Thumb, SliderTrack as Track, SliderValue as Value };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
4
|
+
import { SliderRootContext } from "./SliderRootContext.js";
|
|
5
|
+
import { createComponent, spread, template } from "@solidjs/web";
|
|
6
|
+
import { createMemo, merge, omit, onFlush, onSettled, untrack } from "solid-js";
|
|
7
|
+
// src/slider/root/SliderRoot.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<div>`);
|
|
9
|
+
var places = (number) => (String(number).split(".")[1] ?? "").length;
|
|
10
|
+
/**
|
|
11
|
+
* The slider: a `<div>` around its parts, which holds the value and sets
|
|
12
|
+
* `--slider` to its percentage of the range. Its `id`, `aria-describedby`
|
|
13
|
+
* and `aria-invalid` go on the Thumb's input, so it joins a Field as
|
|
14
|
+
* `Field.Control`'s `render`.
|
|
15
|
+
*/
|
|
16
|
+
function SliderRoot(props) {
|
|
17
|
+
const min = () => props.min ?? 0;
|
|
18
|
+
const max = () => props.max ?? 100;
|
|
19
|
+
const stepSize = () => props.step ?? 1;
|
|
20
|
+
const snap = (number, round) => {
|
|
21
|
+
const step = stepSize();
|
|
22
|
+
const steps = Math.min(Math.max(round((number - min()) / step), 0), Math.floor((max() - min()) / step + 1e-9));
|
|
23
|
+
return Number((min() + steps * step).toFixed(Math.max(places(step), places(min()))));
|
|
24
|
+
};
|
|
25
|
+
const defaultValue = () => props.defaultValue ?? snap((min() + max()) / 2, Math.round);
|
|
26
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
27
|
+
get value() {
|
|
28
|
+
return props.value;
|
|
29
|
+
},
|
|
30
|
+
get defaultValue() {
|
|
31
|
+
return defaultValue();
|
|
32
|
+
},
|
|
33
|
+
onChange: (next) => props.onValueChange?.(next)
|
|
34
|
+
});
|
|
35
|
+
const formatText = createMemo(() => {
|
|
36
|
+
const format = props.format;
|
|
37
|
+
if (typeof format === "function") return format;
|
|
38
|
+
const formatter = new Intl.NumberFormat(props.locale, format);
|
|
39
|
+
return (number) => formatter.format(number);
|
|
40
|
+
});
|
|
41
|
+
const sync = () => {
|
|
42
|
+
if (context.input) context.input.value = String(untrack(value));
|
|
43
|
+
};
|
|
44
|
+
const fire = (type) => context.input?.dispatchEvent(new Event(type, { bubbles: true }));
|
|
45
|
+
let start = 0;
|
|
46
|
+
let holds = 0;
|
|
47
|
+
const context = {
|
|
48
|
+
props,
|
|
49
|
+
value,
|
|
50
|
+
requested,
|
|
51
|
+
min,
|
|
52
|
+
max,
|
|
53
|
+
stepSize,
|
|
54
|
+
text: () => formatText()(value()),
|
|
55
|
+
attributes: {
|
|
56
|
+
get "data-disabled"() {
|
|
57
|
+
return props.disabled ? "" : void 0;
|
|
58
|
+
},
|
|
59
|
+
get "data-horizontal"() {
|
|
60
|
+
return props.orientation === "vertical" ? void 0 : "";
|
|
61
|
+
},
|
|
62
|
+
get "data-vertical"() {
|
|
63
|
+
return props.orientation === "vertical" ? "" : void 0;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
change: (number, round = Math.round) => {
|
|
67
|
+
const next = snap(number, round);
|
|
68
|
+
if (next === requested()) return;
|
|
69
|
+
if (context.input) context.input.value = String(next);
|
|
70
|
+
setValue(next);
|
|
71
|
+
fire("input");
|
|
72
|
+
onFlush(sync);
|
|
73
|
+
},
|
|
74
|
+
step: (amount) => context.change(requested() + amount, amount > 0 ? (steps) => Math.floor(steps + 1e-9) : (steps) => Math.ceil(steps - 1e-9)),
|
|
75
|
+
begin: () => {
|
|
76
|
+
if (!holds++) start = requested();
|
|
77
|
+
},
|
|
78
|
+
end: () => {
|
|
79
|
+
if (--holds) return;
|
|
80
|
+
const current = requested();
|
|
81
|
+
if (current === start) return;
|
|
82
|
+
fire("change");
|
|
83
|
+
props.onValueCommit?.(current);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
onSettled(() => {
|
|
87
|
+
const form = context.input?.form;
|
|
88
|
+
if (!form) return;
|
|
89
|
+
let disposed = false;
|
|
90
|
+
const onReset = (event) => {
|
|
91
|
+
setTimeout(() => {
|
|
92
|
+
if (disposed || event.defaultPrevented) return;
|
|
93
|
+
const before = requested();
|
|
94
|
+
setValue(defaultValue());
|
|
95
|
+
onFlush(() => {
|
|
96
|
+
sync();
|
|
97
|
+
const current = untrack(value);
|
|
98
|
+
if (current !== before) props.onValueCommit?.(current);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
form.addEventListener("reset", onReset);
|
|
103
|
+
return () => {
|
|
104
|
+
disposed = true;
|
|
105
|
+
form.removeEventListener("reset", onReset);
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
const ownProps = merge(context.attributes, { get style() {
|
|
109
|
+
return { "--slider": `${(Math.min(Math.max(value(), min()), max()) - min()) / (max() - min()) * 100}%` };
|
|
110
|
+
} });
|
|
111
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "defaultValue", "onValueChange", "onValueCommit", "min", "max", "step", "largeStep", "orientation", "format", "locale", "name", "form", "disabled", "id", "aria-describedby", "aria-invalid"));
|
|
112
|
+
return createComponent(SliderRootContext, {
|
|
113
|
+
value: context,
|
|
114
|
+
get children() {
|
|
115
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
116
|
+
var _el$ = _tmpl$();
|
|
117
|
+
spread(_el$, p, false);
|
|
118
|
+
return _el$;
|
|
119
|
+
})());
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
export { SliderRoot };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isDev } from "@solidjs/web";
|
|
2
|
+
import { createContext, useContext } from "solid-js";
|
|
3
|
+
// src/slider/root/SliderRootContext.ts
|
|
4
|
+
var SliderRootContext = createContext(null);
|
|
5
|
+
function useSliderRootContext() {
|
|
6
|
+
const context = useContext(SliderRootContext);
|
|
7
|
+
if (isDev && !context) throw new Error("[menhir] Slider parts must be placed within <Slider.Root>.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { SliderRootContext, useSliderRootContext };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useSliderRootContext } from "../root/SliderRootContext.js";
|
|
4
|
+
import { delegateEvents, effect, heldRead, ref, setAttribute, spread, template } from "@solidjs/web";
|
|
5
|
+
import { merge, omit } from "solid-js";
|
|
6
|
+
// src/slider/thumb/SliderThumb.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<input type=range 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>`);
|
|
8
|
+
var _tmpl$2 = /* @__PURE__ */ template(`<div>`);
|
|
9
|
+
/**
|
|
10
|
+
* The thumb: a `<div>` the consumer places with `--slider`, with the
|
|
11
|
+
* slider's control inside it, a visually hidden `<input type="range">`.
|
|
12
|
+
* Its `aria-label`, `aria-labelledby` and `aria-describedby` go on the
|
|
13
|
+
* input. Place it inside `Slider.Track`.
|
|
14
|
+
*/
|
|
15
|
+
function SliderThumb(props) {
|
|
16
|
+
const root = useSliderRootContext();
|
|
17
|
+
let held = false;
|
|
18
|
+
const hold = () => {
|
|
19
|
+
if (!held) root.begin();
|
|
20
|
+
held = true;
|
|
21
|
+
};
|
|
22
|
+
const release = () => {
|
|
23
|
+
if (!held) return;
|
|
24
|
+
held = false;
|
|
25
|
+
root.end();
|
|
26
|
+
};
|
|
27
|
+
const onKeyDown = (event) => {
|
|
28
|
+
if (event.ctrlKey || event.altKey || event.metaKey) return;
|
|
29
|
+
const step = root.stepSize();
|
|
30
|
+
const large = root.props.largeStep ?? step * 10;
|
|
31
|
+
const right = getComputedStyle(event.currentTarget).direction === "rtl" ? -step : step;
|
|
32
|
+
const amount = {
|
|
33
|
+
__proto__: null,
|
|
34
|
+
ArrowRight: right,
|
|
35
|
+
ArrowLeft: -right,
|
|
36
|
+
ArrowUp: step,
|
|
37
|
+
ArrowDown: -step,
|
|
38
|
+
PageUp: large,
|
|
39
|
+
PageDown: -large,
|
|
40
|
+
Home: -Infinity,
|
|
41
|
+
End: Infinity
|
|
42
|
+
}[event.key];
|
|
43
|
+
if (!amount) return;
|
|
44
|
+
hold();
|
|
45
|
+
root.step(amount);
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
};
|
|
48
|
+
var _el$ = _tmpl$();
|
|
49
|
+
_el$.addEventListener("change", release);
|
|
50
|
+
_el$._$$input = (event) => {
|
|
51
|
+
const number = Number(event.currentTarget.value);
|
|
52
|
+
if (number === root.requested()) return;
|
|
53
|
+
hold();
|
|
54
|
+
root.change(number);
|
|
55
|
+
};
|
|
56
|
+
_el$.addEventListener("blur", release);
|
|
57
|
+
_el$._$$keyup = release;
|
|
58
|
+
_el$._$$keydown = onKeyDown;
|
|
59
|
+
ref(() => {
|
|
60
|
+
return (element) => {
|
|
61
|
+
root.input = element;
|
|
62
|
+
};
|
|
63
|
+
}, _el$);
|
|
64
|
+
effect(() => {
|
|
65
|
+
return {
|
|
66
|
+
e: root.props.id,
|
|
67
|
+
t: root.props.name,
|
|
68
|
+
a: root.props.form,
|
|
69
|
+
o: root.props.min,
|
|
70
|
+
i: root.props.max,
|
|
71
|
+
n: root.props.step,
|
|
72
|
+
s: String(root.value()),
|
|
73
|
+
h: root.props.disabled,
|
|
74
|
+
r: root.text(),
|
|
75
|
+
d: root.props.orientation === "vertical" ? "vertical" : void 0,
|
|
76
|
+
l: props["aria-label"],
|
|
77
|
+
u: props["aria-labelledby"],
|
|
78
|
+
c: props["aria-describedby"] ?? root.props["aria-describedby"],
|
|
79
|
+
w: root.props["aria-invalid"],
|
|
80
|
+
m: heldRead()
|
|
81
|
+
};
|
|
82
|
+
}, ({ e, t, a, o, i, n, s, h, r, d, l, u, c, w, m }, _p$) => {
|
|
83
|
+
e !== _p$?.e && setAttribute(_el$, "id", e);
|
|
84
|
+
t !== _p$?.t && setAttribute(_el$, "name", t);
|
|
85
|
+
a !== _p$?.a && setAttribute(_el$, "form", a);
|
|
86
|
+
o !== _p$?.o && setAttribute(_el$, "min", o);
|
|
87
|
+
i !== _p$?.i && setAttribute(_el$, "max", i);
|
|
88
|
+
n !== _p$?.n && setAttribute(_el$, "step", n);
|
|
89
|
+
(s !== _p$?.s || !m) && (_el$.value = s ?? "");
|
|
90
|
+
h !== _p$?.h && setAttribute(_el$, "disabled", h);
|
|
91
|
+
r !== _p$?.r && setAttribute(_el$, "aria-valuetext", r);
|
|
92
|
+
d !== _p$?.d && setAttribute(_el$, "aria-orientation", d);
|
|
93
|
+
l !== _p$?.l && setAttribute(_el$, "aria-label", l);
|
|
94
|
+
u !== _p$?.u && setAttribute(_el$, "aria-labelledby", u);
|
|
95
|
+
c !== _p$?.c && setAttribute(_el$, "aria-describedby", c);
|
|
96
|
+
w !== _p$?.w && setAttribute(_el$, "aria-invalid", w);
|
|
97
|
+
});
|
|
98
|
+
const input = _el$;
|
|
99
|
+
const ownProps = merge(root.attributes, {
|
|
100
|
+
get children() {
|
|
101
|
+
return [input, props.children];
|
|
102
|
+
},
|
|
103
|
+
ref: (element) => {
|
|
104
|
+
root.thumb = element;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "children", "aria-label", "aria-labelledby", "aria-describedby"));
|
|
108
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
109
|
+
var _el$2 = _tmpl$2();
|
|
110
|
+
spread(_el$2, p, false);
|
|
111
|
+
return _el$2;
|
|
112
|
+
})());
|
|
113
|
+
}
|
|
114
|
+
delegateEvents([
|
|
115
|
+
"keydown",
|
|
116
|
+
"keyup",
|
|
117
|
+
"input"
|
|
118
|
+
]);
|
|
119
|
+
export { SliderThumb };
|