menhir 0.1.13 → 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/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/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 +6 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ For everything else, go to the docs:
|
|
|
62
62
|
|
|
63
63
|
Early 0.x versions: the API may change between minor versions. The form components come next; the plan and roadmap are in the [repository](https://github.com/j13n/menhir). Built so far:
|
|
64
64
|
|
|
65
|
-
- Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Checkbox Group, Collapsible, Combobox, Context Menu, Dialog, Field, Form, Input, Menu, Menubar, Meter, Navigation Menu, Number Field, 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,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 };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useSliderRootContext } from "../root/SliderRootContext.js";
|
|
4
|
+
import { spread, template } from "@solidjs/web";
|
|
5
|
+
import { merge, omit } from "solid-js";
|
|
6
|
+
// src/slider/track/SliderTrack.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<div>`);
|
|
8
|
+
/**
|
|
9
|
+
* The track: a `<div>`, the area where a press moves the thumb and a drag
|
|
10
|
+
* follows the pointer. Place the Thumb inside it, inside `Slider.Root`.
|
|
11
|
+
*/
|
|
12
|
+
function SliderTrack(props) {
|
|
13
|
+
const root = useSliderRootContext();
|
|
14
|
+
let track;
|
|
15
|
+
let pointer;
|
|
16
|
+
let offset = 0;
|
|
17
|
+
const along = (x, y) => {
|
|
18
|
+
const rect = track.getBoundingClientRect();
|
|
19
|
+
return root.props.orientation === "vertical" ? rect.bottom - y : getComputedStyle(track).direction === "rtl" ? rect.right - x : x - rect.left;
|
|
20
|
+
};
|
|
21
|
+
const move = (event) => {
|
|
22
|
+
const rect = track.getBoundingClientRect();
|
|
23
|
+
const length = root.props.orientation === "vertical" ? rect.height : rect.width;
|
|
24
|
+
root.change(root.min() + (along(event.clientX, event.clientY) - offset) / length * (root.max() - root.min()));
|
|
25
|
+
};
|
|
26
|
+
const end = (event) => {
|
|
27
|
+
if (event.pointerId !== pointer) return;
|
|
28
|
+
pointer = void 0;
|
|
29
|
+
root.end();
|
|
30
|
+
};
|
|
31
|
+
const ownProps = merge(root.attributes, {
|
|
32
|
+
style: { "touch-action": "none" },
|
|
33
|
+
onPointerDown: (event) => {
|
|
34
|
+
if (event.button !== 0 || pointer !== void 0 || root.props.disabled) return;
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
root.input?.focus({ focusVisible: false });
|
|
37
|
+
pointer = event.pointerId;
|
|
38
|
+
track.setPointerCapture(pointer);
|
|
39
|
+
const thumb = root.thumb;
|
|
40
|
+
offset = 0;
|
|
41
|
+
if (thumb?.contains(event.target)) {
|
|
42
|
+
const { left, top, width, height } = thumb.getBoundingClientRect();
|
|
43
|
+
offset = along(event.clientX, event.clientY) - along(left + width / 2, top + height / 2);
|
|
44
|
+
}
|
|
45
|
+
root.begin();
|
|
46
|
+
move(event);
|
|
47
|
+
},
|
|
48
|
+
onPointerMove: (event) => {
|
|
49
|
+
if (event.pointerId === pointer) move(event);
|
|
50
|
+
},
|
|
51
|
+
onPointerUp: end,
|
|
52
|
+
onPointerCancel: end,
|
|
53
|
+
onLostPointerCapture: end,
|
|
54
|
+
ref: (element) => {
|
|
55
|
+
track = element;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
59
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
60
|
+
var _el$ = _tmpl$();
|
|
61
|
+
spread(_el$, p, false);
|
|
62
|
+
return _el$;
|
|
63
|
+
})());
|
|
64
|
+
}
|
|
65
|
+
export { SliderTrack };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
2
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
3
|
+
import { useSliderRootContext } from "../root/SliderRootContext.js";
|
|
4
|
+
import { spread, template } from "@solidjs/web";
|
|
5
|
+
import { merge, omit } from "solid-js";
|
|
6
|
+
// src/slider/value/SliderValue.tsx
|
|
7
|
+
var _tmpl$ = /* @__PURE__ */ template(`<span>`);
|
|
8
|
+
/**
|
|
9
|
+
* The value as text: a `<span>` with the Root's `aria-valuetext`, the value
|
|
10
|
+
* in its `format` and `locale`. Place it inside `Slider.Root`.
|
|
11
|
+
*/
|
|
12
|
+
function SliderValue(props) {
|
|
13
|
+
const root = useSliderRootContext();
|
|
14
|
+
const ownProps = merge(root.attributes, { get children() {
|
|
15
|
+
return root.text();
|
|
16
|
+
} });
|
|
17
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
18
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
19
|
+
var _el$ = _tmpl$();
|
|
20
|
+
spread(_el$, p, false);
|
|
21
|
+
return _el$;
|
|
22
|
+
})());
|
|
23
|
+
}
|
|
24
|
+
export { SliderValue };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Slider from "./index.parts.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Slider from "./index.parts.js";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface SliderRootProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** The value, when controlled. */
|
|
5
|
+
value?: number;
|
|
6
|
+
/**
|
|
7
|
+
* The value to start with when uncontrolled, and the one a form reset
|
|
8
|
+
* puts back. Default the midpoint of `min` and `max`, as a range input's.
|
|
9
|
+
*/
|
|
10
|
+
defaultValue?: number;
|
|
11
|
+
/** Called with the new value on each change while the thumb moves. */
|
|
12
|
+
onValueChange?: (value: number) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Called with the value once the user has finished changing it: a drag
|
|
15
|
+
* released or canceled, a key released or focus leaving first, the
|
|
16
|
+
* input's own `change`, a form reset once taken.
|
|
17
|
+
*/
|
|
18
|
+
onValueCommit?: (value: number) => void;
|
|
19
|
+
/** The lowest value. Default 0. */
|
|
20
|
+
min?: number;
|
|
21
|
+
/** The highest value. Default 100. */
|
|
22
|
+
max?: number;
|
|
23
|
+
/** The distance between the values the thumb can take. Default 1. */
|
|
24
|
+
step?: number;
|
|
25
|
+
/** How much Page Up and Page Down change the value. Default ten steps. */
|
|
26
|
+
largeStep?: number;
|
|
27
|
+
/** The direction of the track. Default `"horizontal"`. */
|
|
28
|
+
orientation?: "horizontal" | "vertical";
|
|
29
|
+
/**
|
|
30
|
+
* How the value reads as text, in `aria-valuetext` and `Slider.Value`:
|
|
31
|
+
* `Intl.NumberFormat` options, such as `{ useGrouping: false }` for a
|
|
32
|
+
* year, or a function from the value to its text, such as a month's name.
|
|
33
|
+
*/
|
|
34
|
+
format?: Intl.NumberFormatOptions | ((value: number) => string);
|
|
35
|
+
/**
|
|
36
|
+
* The language to format in, else the runtime's default. Pass it when
|
|
37
|
+
* rendering on the server, so that the server and the browser agree.
|
|
38
|
+
*/
|
|
39
|
+
locale?: Intl.LocalesArgument;
|
|
40
|
+
/** The name the value is submitted under. */
|
|
41
|
+
name?: string;
|
|
42
|
+
/** The `id` of the form the value belongs to, when the slider is not inside it. */
|
|
43
|
+
form?: string;
|
|
44
|
+
/** Whether every part ignores the user, and the form submits nothing. */
|
|
45
|
+
disabled?: boolean;
|
|
46
|
+
/** Renders the Root as another element or component. Read once. */
|
|
47
|
+
render?: RenderProp;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The slider: a `<div>` around its parts, which holds the value and sets
|
|
51
|
+
* `--slider` to its percentage of the range. Its `id`, `aria-describedby`
|
|
52
|
+
* and `aria-invalid` go on the Thumb's input, so it joins a Field as
|
|
53
|
+
* `Field.Control`'s `render`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function SliderRoot(props: SliderRootProps): JSX.Element;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { createMemo, merge, omit, onFlush, onSettled, untrack } from "solid-js";
|
|
2
|
+
import { createControllableSignal } from "../../core/createControllableSignal.js";
|
|
3
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
4
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
5
|
+
import { SliderRootContext } from "./SliderRootContext.js";
|
|
6
|
+
// Decimal places of a number, to round a step's float error away.
|
|
7
|
+
const places = (number) => (String(number).split(".")[1] ?? "").length;
|
|
8
|
+
/**
|
|
9
|
+
* The slider: a `<div>` around its parts, which holds the value and sets
|
|
10
|
+
* `--slider` to its percentage of the range. Its `id`, `aria-describedby`
|
|
11
|
+
* and `aria-invalid` go on the Thumb's input, so it joins a Field as
|
|
12
|
+
* `Field.Control`'s `render`.
|
|
13
|
+
*/
|
|
14
|
+
export function SliderRoot(props) {
|
|
15
|
+
const min = () => props.min ?? 0;
|
|
16
|
+
const max = () => props.max ?? 100;
|
|
17
|
+
const stepSize = () => props.step ?? 1;
|
|
18
|
+
// The value on the step grid from `min` nearest `number` by `round`,
|
|
19
|
+
// clamped to the range: the last value is the grid's last at or under
|
|
20
|
+
// `max`, as a range input's is. Rounded to the decimals of `step` and
|
|
21
|
+
// `min`, so that `0.1` steps add up.
|
|
22
|
+
const snap = (number, round) => {
|
|
23
|
+
const step = stepSize();
|
|
24
|
+
const steps = Math.min(Math.max(round((number - min()) / step), 0), Math.floor((max() - min()) / step + 1e-9));
|
|
25
|
+
return Number((min() + steps * step).toFixed(Math.max(places(step), places(min()))));
|
|
26
|
+
};
|
|
27
|
+
// As a range input's: the midpoint, on the grid.
|
|
28
|
+
const defaultValue = () => props.defaultValue ?? snap((min() + max()) / 2, Math.round);
|
|
29
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
30
|
+
get value() {
|
|
31
|
+
return props.value;
|
|
32
|
+
},
|
|
33
|
+
get defaultValue() {
|
|
34
|
+
return defaultValue();
|
|
35
|
+
},
|
|
36
|
+
onChange: (next) => props.onValueChange?.(next),
|
|
37
|
+
});
|
|
38
|
+
const formatText = createMemo(() => {
|
|
39
|
+
const format = props.format;
|
|
40
|
+
if (typeof format === "function")
|
|
41
|
+
return format;
|
|
42
|
+
const formatter = new Intl.NumberFormat(props.locale, format);
|
|
43
|
+
return (number) => formatter.format(number);
|
|
44
|
+
});
|
|
45
|
+
// Puts the input back to the value, once it has settled: a controlled
|
|
46
|
+
// slider's consumer may have declined the change, and a form reset set
|
|
47
|
+
// the input to its `value` attribute.
|
|
48
|
+
const sync = () => {
|
|
49
|
+
if (context.input)
|
|
50
|
+
context.input.value = String(untrack(value));
|
|
51
|
+
};
|
|
52
|
+
const fire = (type) => context.input?.dispatchEvent(new Event(type, { bubbles: true }));
|
|
53
|
+
// The value when the first of the changes under way began, and how many
|
|
54
|
+
// are under way: a drag, a key held during it. The last to end commits.
|
|
55
|
+
let start = 0;
|
|
56
|
+
let holds = 0;
|
|
57
|
+
const context = {
|
|
58
|
+
props,
|
|
59
|
+
value,
|
|
60
|
+
requested,
|
|
61
|
+
min,
|
|
62
|
+
max,
|
|
63
|
+
stepSize,
|
|
64
|
+
text: () => formatText()(value()),
|
|
65
|
+
attributes: {
|
|
66
|
+
get "data-disabled"() {
|
|
67
|
+
return props.disabled ? "" : undefined;
|
|
68
|
+
},
|
|
69
|
+
get "data-horizontal"() {
|
|
70
|
+
return props.orientation === "vertical" ? undefined : "";
|
|
71
|
+
},
|
|
72
|
+
get "data-vertical"() {
|
|
73
|
+
return props.orientation === "vertical" ? "" : undefined;
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
change: (number, round = Math.round) => {
|
|
77
|
+
const next = snap(number, round);
|
|
78
|
+
if (next === requested())
|
|
79
|
+
return;
|
|
80
|
+
// The input changes first and fires its native `input`, as a range
|
|
81
|
+
// input does under a drag, so the form's listeners, a Field and a
|
|
82
|
+
// Form see the new value.
|
|
83
|
+
if (context.input)
|
|
84
|
+
context.input.value = String(next);
|
|
85
|
+
setValue(next);
|
|
86
|
+
fire("input");
|
|
87
|
+
onFlush(sync);
|
|
88
|
+
},
|
|
89
|
+
// From a value off the grid, the grid's next one that way, as
|
|
90
|
+
// `stepUp()` does.
|
|
91
|
+
step: (amount) => context.change(requested() + amount, amount > 0
|
|
92
|
+
? (steps) => Math.floor(steps + 1e-9)
|
|
93
|
+
: (steps) => Math.ceil(steps - 1e-9)),
|
|
94
|
+
begin: () => {
|
|
95
|
+
if (!holds++)
|
|
96
|
+
start = requested();
|
|
97
|
+
},
|
|
98
|
+
end: () => {
|
|
99
|
+
if (--holds)
|
|
100
|
+
return;
|
|
101
|
+
const current = requested();
|
|
102
|
+
if (current === start)
|
|
103
|
+
return;
|
|
104
|
+
fire("change");
|
|
105
|
+
props.onValueCommit?.(current);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
// A form reset puts a control back to its default, and so it does this
|
|
109
|
+
// one: through the setter, so that a controlled slider asks.
|
|
110
|
+
onSettled(() => {
|
|
111
|
+
const form = context.input?.form;
|
|
112
|
+
if (!form)
|
|
113
|
+
return;
|
|
114
|
+
// A slider the reset removes, such as a row of a list going back to its
|
|
115
|
+
// default length, asks for nothing.
|
|
116
|
+
let disposed = false;
|
|
117
|
+
const onReset = (event) => {
|
|
118
|
+
// In a task of its own, not `onFlush`: when the user presses a reset
|
|
119
|
+
// button, the browser runs microtasks as each listener returns,
|
|
120
|
+
// before it resets the controls and before later listeners could
|
|
121
|
+
// cancel the reset (src/input/README.md).
|
|
122
|
+
setTimeout(() => {
|
|
123
|
+
if (disposed || event.defaultPrevented)
|
|
124
|
+
return;
|
|
125
|
+
const before = requested();
|
|
126
|
+
setValue(defaultValue());
|
|
127
|
+
onFlush(() => {
|
|
128
|
+
sync();
|
|
129
|
+
// Once taken: a controlled slider may decline it.
|
|
130
|
+
const current = untrack(value);
|
|
131
|
+
if (current !== before)
|
|
132
|
+
props.onValueCommit?.(current);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
form.addEventListener("reset", onReset);
|
|
137
|
+
return () => {
|
|
138
|
+
disposed = true;
|
|
139
|
+
form.removeEventListener("reset", onReset);
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
const ownProps = merge(context.attributes, {
|
|
143
|
+
get style() {
|
|
144
|
+
const fraction = (Math.min(Math.max(value(), min()), max()) - min()) / (max() - min());
|
|
145
|
+
return { "--slider": `${fraction * 100}%` };
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
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"));
|
|
149
|
+
return (<SliderRootContext value={context}>
|
|
150
|
+
{renderElement(props, elementProps, (p) => (<div {...p}/>))}
|
|
151
|
+
</SliderRootContext>);
|
|
152
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ElementProps } from "../../core/renderElement.tsx";
|
|
2
|
+
import type { SliderRootProps } from "./SliderRoot.tsx";
|
|
3
|
+
/** What the Root shares with its parts. */
|
|
4
|
+
export interface SliderRootContextValue {
|
|
5
|
+
readonly props: SliderRootProps;
|
|
6
|
+
/** The value. */
|
|
7
|
+
value(): number;
|
|
8
|
+
/** The value set last in this turn, else the value; untracked. */
|
|
9
|
+
requested(): number;
|
|
10
|
+
/** `min`, default 0. */
|
|
11
|
+
min(): number;
|
|
12
|
+
/** `max`, default 100. */
|
|
13
|
+
max(): number;
|
|
14
|
+
/** `step`, default 1. */
|
|
15
|
+
stepSize(): number;
|
|
16
|
+
/** The value as text: in `format` and `locale`, or `format`'s own. */
|
|
17
|
+
text(): string;
|
|
18
|
+
/**
|
|
19
|
+
* `data-disabled` and `data-horizontal` or `data-vertical`, for every
|
|
20
|
+
* part.
|
|
21
|
+
*/
|
|
22
|
+
readonly attributes: ElementProps;
|
|
23
|
+
/** The Thumb's range input, once rendered. */
|
|
24
|
+
input?: HTMLInputElement;
|
|
25
|
+
/** The Thumb's element, once rendered. */
|
|
26
|
+
thumb?: HTMLElement;
|
|
27
|
+
/**
|
|
28
|
+
* Sets the value by the user's hand, rounded to the step grid with
|
|
29
|
+
* `round`, by default to the nearest value, and clamped to the range.
|
|
30
|
+
*/
|
|
31
|
+
change(value: number, round?: (steps: number) => number): void;
|
|
32
|
+
/**
|
|
33
|
+
* Moves the value by `amount`, to the step grid's next value that way,
|
|
34
|
+
* clamped to the range.
|
|
35
|
+
*/
|
|
36
|
+
step(amount: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Marks the start of a change under way, a drag or a key held, which
|
|
39
|
+
* ends in a commit. Several may overlap, such as a key during a drag;
|
|
40
|
+
* each calls `end` once.
|
|
41
|
+
*/
|
|
42
|
+
begin(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Ends a change under way. Once the last has ended, if the value is not
|
|
45
|
+
* what it was when the first began, fires the input's `change` and calls
|
|
46
|
+
* `onValueCommit`.
|
|
47
|
+
*/
|
|
48
|
+
end(): void;
|
|
49
|
+
}
|
|
50
|
+
export declare const SliderRootContext: import("solid-js").Context<SliderRootContextValue | null>;
|
|
51
|
+
export declare function useSliderRootContext(): SliderRootContextValue;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createContext, useContext } from "solid-js";
|
|
2
|
+
import { isDev } from "@solidjs/web";
|
|
3
|
+
// `null` outside a Root, so that the part can name what is missing. Solid's
|
|
4
|
+
// own error for a context with no provider and no default does not.
|
|
5
|
+
export const SliderRootContext = createContext(null);
|
|
6
|
+
export function useSliderRootContext() {
|
|
7
|
+
const context = useContext(SliderRootContext);
|
|
8
|
+
// Production builds drop the check, and its message, with `isDev`.
|
|
9
|
+
if (isDev && !context) {
|
|
10
|
+
throw new Error("[menhir] Slider parts must be placed within <Slider.Root>.");
|
|
11
|
+
}
|
|
12
|
+
return context;
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface SliderThumbProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "id"> {
|
|
4
|
+
/** Renders the thumb's element as another element or component. Read once. */
|
|
5
|
+
render?: RenderProp;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The thumb: a `<div>` the consumer places with `--slider`, with the
|
|
9
|
+
* slider's control inside it, a visually hidden `<input type="range">`.
|
|
10
|
+
* Its `aria-label`, `aria-labelledby` and `aria-describedby` go on the
|
|
11
|
+
* input. Place it inside `Slider.Track`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function SliderThumb(props: SliderThumbProps): JSX.Element;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { merge, omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useSliderRootContext } from "../root/SliderRootContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The thumb: a `<div>` the consumer places with `--slider`, with the
|
|
7
|
+
* slider's control inside it, a visually hidden `<input type="range">`.
|
|
8
|
+
* Its `aria-label`, `aria-labelledby` and `aria-describedby` go on the
|
|
9
|
+
* input. Place it inside `Slider.Track`.
|
|
10
|
+
*/
|
|
11
|
+
export function SliderThumb(props) {
|
|
12
|
+
const root = useSliderRootContext();
|
|
13
|
+
// Whether a key, or the input itself, holds a change under way, which
|
|
14
|
+
// ends once the key is released, focus leaves, or the input fires
|
|
15
|
+
// `change`.
|
|
16
|
+
let held = false;
|
|
17
|
+
const hold = () => {
|
|
18
|
+
if (!held)
|
|
19
|
+
root.begin();
|
|
20
|
+
held = true;
|
|
21
|
+
};
|
|
22
|
+
const release = () => {
|
|
23
|
+
// Cleared first: the commit fires the input's `change`, which calls this
|
|
24
|
+
// again.
|
|
25
|
+
if (!held)
|
|
26
|
+
return;
|
|
27
|
+
held = false;
|
|
28
|
+
root.end();
|
|
29
|
+
};
|
|
30
|
+
// The keys of the slider pattern. Each is canceled, so the input does not
|
|
31
|
+
// move itself as well, which it would by its own rules, nor the page
|
|
32
|
+
// scroll.
|
|
33
|
+
const onKeyDown = (event) => {
|
|
34
|
+
// Alt+Left goes back, and the rest are the browser's too.
|
|
35
|
+
if (event.ctrlKey || event.altKey || event.metaKey)
|
|
36
|
+
return;
|
|
37
|
+
const step = root.stepSize();
|
|
38
|
+
const large = root.props.largeStep ?? step * 10;
|
|
39
|
+
// Right to left, the track is drawn mirrored, and so are Left and Right.
|
|
40
|
+
const right = getComputedStyle(event.currentTarget).direction === "rtl"
|
|
41
|
+
? -step
|
|
42
|
+
: step;
|
|
43
|
+
// Home and End step past the ends, which the step clamps to.
|
|
44
|
+
const amount = {
|
|
45
|
+
__proto__: null,
|
|
46
|
+
ArrowRight: right,
|
|
47
|
+
ArrowLeft: -right,
|
|
48
|
+
ArrowUp: step,
|
|
49
|
+
ArrowDown: -step,
|
|
50
|
+
PageUp: large,
|
|
51
|
+
PageDown: -large,
|
|
52
|
+
Home: -Infinity,
|
|
53
|
+
End: Infinity,
|
|
54
|
+
}[event.key];
|
|
55
|
+
if (!amount)
|
|
56
|
+
return;
|
|
57
|
+
hold();
|
|
58
|
+
root.step(amount);
|
|
59
|
+
event.preventDefault();
|
|
60
|
+
};
|
|
61
|
+
const input = (<input type="range" style={visuallyHidden} id={root.props.id} name={root.props.name} form={root.props.form} min={root.props.min} max={root.props.max} step={root.props.step} value={String(root.value())} disabled={root.props.disabled} aria-valuetext={root.text()} aria-orientation={root.props.orientation === "vertical" ? "vertical" : undefined} aria-label={props["aria-label"]} aria-labelledby={props["aria-labelledby"]}
|
|
62
|
+
// The Thumb's own, else a Field's around the Root.
|
|
63
|
+
aria-describedby={props["aria-describedby"] ?? root.props["aria-describedby"]} aria-invalid={root.props["aria-invalid"]} onKeyDown={onKeyDown}
|
|
64
|
+
// A held key repeats, and commits once, when released.
|
|
65
|
+
onKeyUp={release} onBlur={release}
|
|
66
|
+
// A change the input made itself, as assistive technology may make
|
|
67
|
+
// one, or a key with a modifier the browser moves it by. The slider's
|
|
68
|
+
// own changes set the input's value first, so they match.
|
|
69
|
+
onInput={(event) => {
|
|
70
|
+
const number = Number(event.currentTarget.value);
|
|
71
|
+
if (number === root.requested())
|
|
72
|
+
return;
|
|
73
|
+
hold();
|
|
74
|
+
root.change(number);
|
|
75
|
+
}} onChange={release} ref={(element) => {
|
|
76
|
+
root.input = element;
|
|
77
|
+
}}/>);
|
|
78
|
+
const ownProps = merge(root.attributes, {
|
|
79
|
+
get children() {
|
|
80
|
+
return [input, props.children];
|
|
81
|
+
},
|
|
82
|
+
ref: (element) => {
|
|
83
|
+
root.thumb = element;
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render", "children", "aria-label", "aria-labelledby", "aria-describedby"));
|
|
87
|
+
return renderElement(props, elementProps, (p) => <div {...p}/>);
|
|
88
|
+
}
|
|
89
|
+
// Hidden from sight but not from the keyboard or assistive technology. Not
|
|
90
|
+
// `display: none`, so it can still take focus.
|
|
91
|
+
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,11 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface SliderTrackProps extends JSX.HTMLAttributes<HTMLElement> {
|
|
4
|
+
/** Renders the track as another element or component. Read once. */
|
|
5
|
+
render?: RenderProp;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The track: a `<div>`, the area where a press moves the thumb and a drag
|
|
9
|
+
* follows the pointer. Place the Thumb inside it, inside `Slider.Root`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function SliderTrack(props: SliderTrackProps): JSX.Element;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { merge, omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useSliderRootContext } from "../root/SliderRootContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The track: a `<div>`, the area where a press moves the thumb and a drag
|
|
7
|
+
* follows the pointer. Place the Thumb inside it, inside `Slider.Root`.
|
|
8
|
+
*/
|
|
9
|
+
export function SliderTrack(props) {
|
|
10
|
+
const root = useSliderRootContext();
|
|
11
|
+
let track;
|
|
12
|
+
// The pointer that drags, and how far from the thumb's centre it pressed,
|
|
13
|
+
// along the track, so the thumb does not jump by a press off its centre.
|
|
14
|
+
let pointer;
|
|
15
|
+
let offset = 0;
|
|
16
|
+
// How far along the track a point is, in pixels from where the range
|
|
17
|
+
// starts: the left, the right when right to left, the bottom when
|
|
18
|
+
// vertical. Read on each event, so the track may move or change size.
|
|
19
|
+
const along = (x, y) => {
|
|
20
|
+
const rect = track.getBoundingClientRect();
|
|
21
|
+
return root.props.orientation === "vertical"
|
|
22
|
+
? rect.bottom - y
|
|
23
|
+
: getComputedStyle(track).direction === "rtl"
|
|
24
|
+
? rect.right - x
|
|
25
|
+
: x - rect.left;
|
|
26
|
+
};
|
|
27
|
+
const move = (event) => {
|
|
28
|
+
const rect = track.getBoundingClientRect();
|
|
29
|
+
const length = root.props.orientation === "vertical" ? rect.height : rect.width;
|
|
30
|
+
root.change(root.min() +
|
|
31
|
+
((along(event.clientX, event.clientY) - offset) / length) *
|
|
32
|
+
(root.max() - root.min()));
|
|
33
|
+
};
|
|
34
|
+
const end = (event) => {
|
|
35
|
+
if (event.pointerId !== pointer)
|
|
36
|
+
return;
|
|
37
|
+
pointer = undefined;
|
|
38
|
+
root.end();
|
|
39
|
+
};
|
|
40
|
+
const ownProps = merge(root.attributes, {
|
|
41
|
+
// A touch drag moves the thumb, and neither scrolls nor zooms the page.
|
|
42
|
+
style: { "touch-action": "none" },
|
|
43
|
+
onPointerDown: (event) => {
|
|
44
|
+
if (event.button !== 0 || pointer !== undefined || root.props.disabled)
|
|
45
|
+
return;
|
|
46
|
+
// No text selection, and no focus on whatever was pressed: the
|
|
47
|
+
// input takes it, so the keys go on from there.
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
// Without a focus ring, as after a press on a native control. A
|
|
50
|
+
// browser without the option shows one (PLAN.md section 5).
|
|
51
|
+
root.input?.focus({ focusVisible: false });
|
|
52
|
+
pointer = event.pointerId;
|
|
53
|
+
// Captured, so the drag goes on outside the track.
|
|
54
|
+
track.setPointerCapture(pointer);
|
|
55
|
+
const thumb = root.thumb;
|
|
56
|
+
offset = 0;
|
|
57
|
+
if (thumb?.contains(event.target)) {
|
|
58
|
+
const { left, top, width, height } = thumb.getBoundingClientRect();
|
|
59
|
+
offset =
|
|
60
|
+
along(event.clientX, event.clientY) -
|
|
61
|
+
along(left + width / 2, top + height / 2);
|
|
62
|
+
}
|
|
63
|
+
root.begin();
|
|
64
|
+
move(event);
|
|
65
|
+
},
|
|
66
|
+
onPointerMove: (event) => {
|
|
67
|
+
if (event.pointerId === pointer)
|
|
68
|
+
move(event);
|
|
69
|
+
},
|
|
70
|
+
onPointerUp: end,
|
|
71
|
+
onPointerCancel: end,
|
|
72
|
+
onLostPointerCapture: end,
|
|
73
|
+
ref: (element) => {
|
|
74
|
+
track = element;
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
78
|
+
return renderElement(props, elementProps, (p) => <div {...p}/>);
|
|
79
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RenderProp } from "../../core/renderElement.tsx";
|
|
3
|
+
export interface SliderValueProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "children"> {
|
|
4
|
+
/** Renders the value as another element or component. Read once. */
|
|
5
|
+
render?: RenderProp;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The value as text: a `<span>` with the Root's `aria-valuetext`, the value
|
|
9
|
+
* in its `format` and `locale`. Place it inside `Slider.Root`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function SliderValue(props: SliderValueProps): JSX.Element;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { merge, omit } from "solid-js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.jsx";
|
|
4
|
+
import { useSliderRootContext } from "../root/SliderRootContext.js";
|
|
5
|
+
/**
|
|
6
|
+
* The value as text: a `<span>` with the Root's `aria-valuetext`, the value
|
|
7
|
+
* in its `format` and `locale`. Place it inside `Slider.Root`.
|
|
8
|
+
*/
|
|
9
|
+
export function SliderValue(props) {
|
|
10
|
+
const root = useSliderRootContext();
|
|
11
|
+
const ownProps = merge(root.attributes, {
|
|
12
|
+
get children() {
|
|
13
|
+
return root.text();
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
17
|
+
return renderElement(props, elementProps, (p) => <span {...p}/>);
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "menhir",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"solid",
|
|
@@ -149,6 +149,11 @@
|
|
|
149
149
|
"solid": "./dist/source/separator/index.js",
|
|
150
150
|
"import": "./dist/separator/index.js"
|
|
151
151
|
},
|
|
152
|
+
"./slider": {
|
|
153
|
+
"types": "./dist/source/slider/index.d.ts",
|
|
154
|
+
"solid": "./dist/source/slider/index.js",
|
|
155
|
+
"import": "./dist/slider/index.js"
|
|
156
|
+
},
|
|
152
157
|
"./switch": {
|
|
153
158
|
"types": "./dist/source/switch/index.d.ts",
|
|
154
159
|
"solid": "./dist/source/switch/index.js",
|