menhir 0.1.13 → 0.1.15

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 (48) hide show
  1. package/README.md +1 -1
  2. package/dist/autocomplete/input/AutocompleteInput.js +5 -2
  3. package/dist/checkbox-group/root/CheckboxGroupRoot.js +3 -8
  4. package/dist/combobox/input/ComboboxInput.js +6 -2
  5. package/dist/combobox/input/createComboboxInput.js +2 -17
  6. package/dist/core/formReset.js +70 -0
  7. package/dist/core/hiddenInput.js +9 -12
  8. package/dist/field/root/FieldRoot.js +4 -6
  9. package/dist/form/Form.js +5 -7
  10. package/dist/input/root/InputRoot.js +14 -23
  11. package/dist/number-field/root/NumberFieldRoot.js +7 -17
  12. package/dist/select/trigger/SelectTrigger.js +7 -10
  13. package/dist/slider/index.js +2 -0
  14. package/dist/slider/index.parts.js +5 -0
  15. package/dist/slider/root/SliderRoot.js +113 -0
  16. package/dist/slider/root/SliderRootContext.js +10 -0
  17. package/dist/slider/thumb/SliderThumb.js +119 -0
  18. package/dist/slider/track/SliderTrack.js +65 -0
  19. package/dist/slider/value/SliderValue.js +24 -0
  20. package/dist/source/autocomplete/input/AutocompleteInput.jsx +9 -2
  21. package/dist/source/checkbox-group/root/CheckboxGroupRoot.jsx +3 -9
  22. package/dist/source/combobox/input/ComboboxInput.jsx +10 -2
  23. package/dist/source/combobox/input/createComboboxInput.d.ts +0 -8
  24. package/dist/source/combobox/input/createComboboxInput.js +1 -17
  25. package/dist/source/core/formReset.d.ts +42 -0
  26. package/dist/source/core/formReset.js +74 -0
  27. package/dist/source/core/hiddenInput.d.ts +2 -2
  28. package/dist/source/core/hiddenInput.jsx +11 -16
  29. package/dist/source/field/root/FieldRoot.jsx +10 -12
  30. package/dist/source/form/Form.jsx +9 -10
  31. package/dist/source/input/root/InputRoot.jsx +19 -33
  32. package/dist/source/number-field/root/NumberFieldRoot.jsx +9 -25
  33. package/dist/source/select/trigger/SelectTrigger.jsx +9 -13
  34. package/dist/source/slider/index.d.ts +1 -0
  35. package/dist/source/slider/index.js +1 -0
  36. package/dist/source/slider/index.parts.d.ts +4 -0
  37. package/dist/source/slider/index.parts.js +4 -0
  38. package/dist/source/slider/root/SliderRoot.d.ts +55 -0
  39. package/dist/source/slider/root/SliderRoot.jsx +136 -0
  40. package/dist/source/slider/root/SliderRootContext.d.ts +51 -0
  41. package/dist/source/slider/root/SliderRootContext.js +13 -0
  42. package/dist/source/slider/thumb/SliderThumb.d.ts +13 -0
  43. package/dist/source/slider/thumb/SliderThumb.jsx +91 -0
  44. package/dist/source/slider/track/SliderTrack.d.ts +11 -0
  45. package/dist/source/slider/track/SliderTrack.jsx +79 -0
  46. package/dist/source/slider/value/SliderValue.d.ts +11 -0
  47. package/dist/source/slider/value/SliderValue.jsx +18 -0
  48. package/package.json +6 -1
@@ -1,4 +1,5 @@
1
- import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
1
+ import { merge, omit, onFlush, untrack } from "solid-js";
2
+ import { onFormReset } from "../../core/formReset.js";
2
3
  import { mergeElementProps } from "../../core/mergeElementProps.js";
3
4
  import { renderElement } from "../../core/renderElement.jsx";
4
5
  /**
@@ -19,39 +20,24 @@ export function InputRoot(props) {
19
20
  }
20
21
  };
21
22
  // A form reset puts a control back to its default, and so it does an
22
- // Input: through `onValueChange`, so that a controlled one asks.
23
- onSettled(() => {
24
- const form = element?.form;
25
- if (!form)
23
+ // Input: through `onValueChange`, so that a controlled one asks. An
24
+ // uncontrolled Input lets the browser reset it, to its `defaultValue`; a
25
+ // controlled one keeps its text through the reset, and its consumer
26
+ // decides.
27
+ onFormReset(() => element?.form, (event, text) => {
28
+ const defaultText = untrack(() => props.defaultValue) ?? "";
29
+ if (text === defaultText)
26
30
  return;
27
- // An Input the reset removes, such as a row of a list going back to
28
- // its default length, asks for nothing.
29
- let disposed = false;
30
- const onReset = (event) => {
31
- const text = element.value;
32
- const defaultText = untrack(() => props.defaultValue) ?? "";
33
- // The browser resets the element to its `value` attribute after this
34
- // event. A controlled Input keeps its text through it, by making that
35
- // its attribute, so that the element never shows a text its consumer
36
- // does not hold; its consumer then decides.
37
- if (untrack(() => props.value) !== undefined) {
38
- element.defaultValue = text;
39
- }
40
- // In a task of its own, not `onFlush`: when the user presses a reset
41
- // button, the browser runs microtasks as each listener returns,
42
- // before later listeners could cancel the reset.
43
- setTimeout(() => {
44
- if (disposed || event.defaultPrevented || text === defaultText)
45
- return;
46
- props.onValueChange?.(defaultText, { reason: "reset", event });
47
- onFlush(restoreValue);
48
- });
49
- };
50
- form.addEventListener("reset", onReset);
51
- return () => {
52
- disposed = true;
53
- form.removeEventListener("reset", onReset);
54
- };
31
+ props.onValueChange?.(defaultText, { reason: "reset", event });
32
+ onFlush(restoreValue);
33
+ },
34
+ // The text before the reset, held through it while controlled
35
+ // (`onFormReset`).
36
+ () => {
37
+ const text = element.value;
38
+ if (untrack(() => props.value) !== undefined)
39
+ element.defaultValue = text;
40
+ return text;
55
41
  });
56
42
  const ownProps = {
57
43
  onInput: (event) => {
@@ -1,5 +1,6 @@
1
- import { createMemo, createUniqueId, omit, onFlush, onSettled, untrack, } from "solid-js";
1
+ import { createMemo, createUniqueId, omit, onFlush, untrack } from "solid-js";
2
2
  import { createControllableSignal } from "../../core/createControllableSignal.js";
3
+ import { onFormReset } from "../../core/formReset.js";
3
4
  import { mergeElementProps } from "../../core/mergeElementProps.js";
4
5
  import { renderElement } from "../../core/renderElement.jsx";
5
6
  import { parseNumber } from "../parse.js";
@@ -130,30 +131,13 @@ export function NumberFieldRoot(props) {
130
131
  };
131
132
  // A form reset puts a control back to its default, and so it does this
132
133
  // one: through the setter, so that a controlled field asks.
133
- onSettled(() => {
134
- const form = hidden?.form;
135
- if (!form)
136
- return;
137
- // A field the reset removes, such as a row of a list going back to its
138
- // default length, asks for nothing.
139
- let disposed = false;
140
- const onReset = (event) => {
141
- // In a task of its own, not `onFlush`: when the user presses a reset
142
- // button, the browser runs microtasks as each listener returns,
143
- // before it resets the controls and before later listeners could
144
- // cancel the reset (src/input/README.md).
145
- setTimeout(() => {
146
- if (disposed || event.defaultPrevented)
147
- return;
148
- setValue(props.defaultValue ?? null);
149
- onFlush(sync);
150
- });
151
- };
152
- form.addEventListener("reset", onReset);
153
- return () => {
154
- disposed = true;
155
- form.removeEventListener("reset", onReset);
156
- };
134
+ // The Input's text is bound as its `defaultValue`, which the browser
135
+ // resets it to, so only the hidden input is held.
136
+ onFormReset(() => hidden?.form, () => {
137
+ setValue(props.defaultValue ?? null);
138
+ onFlush(sync);
139
+ }, () => {
140
+ hidden.defaultValue = hidden.value;
157
141
  });
158
142
  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"));
159
143
  return (<NumberFieldRootContext value={context}>
@@ -1,6 +1,7 @@
1
1
  import { isDev } from "@solidjs/web";
2
- import { createUniqueId, merge, omit, onFlush, onSettled, untrack, } from "solid-js";
2
+ import { createUniqueId, merge, omit, onFlush, untrack } from "solid-js";
3
3
  import { warnUnlessButton } from "../../core/button.js";
4
+ import { onFormReset } from "../../core/formReset.js";
4
5
  import { mergeElementProps } from "../../core/mergeElementProps.js";
5
6
  import { renderElement } from "../../core/renderElement.jsx";
6
7
  import { useSelectContext } from "../root/SelectContext.js";
@@ -151,18 +152,13 @@ export function SelectTrigger(props) {
151
152
  };
152
153
  // A form reset puts a control back to its default, and so it does the
153
154
  // Select: through the setter, so that a controlled Select asks.
154
- onSettled(() => {
155
- const form = input?.form;
156
- if (!form)
157
- return;
158
- const onReset = () => {
159
- select.setValue(select.props.defaultValue ?? null);
160
- // The browser resets the input after this event, to its `value`
161
- // attribute; `onFlush` runs after that.
162
- onFlush(syncInput);
163
- };
164
- form.addEventListener("reset", onReset);
165
- return () => form.removeEventListener("reset", onReset);
155
+ onFormReset(() => input?.form, () => {
156
+ select.setValue(select.props.defaultValue ?? null);
157
+ onFlush(syncInput);
158
+ },
159
+ // Held through the browser's reset (`onFormReset`).
160
+ () => {
161
+ input.defaultValue = input.value;
166
162
  });
167
163
  const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
168
164
  return (<>
@@ -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,4 @@
1
+ export { SliderRoot as Root } from "./root/SliderRoot.tsx";
2
+ export { SliderTrack as Track } from "./track/SliderTrack.tsx";
3
+ export { SliderThumb as Thumb } from "./thumb/SliderThumb.tsx";
4
+ export { SliderValue as Value } from "./value/SliderValue.tsx";
@@ -0,0 +1,4 @@
1
+ export { SliderRoot as Root } from "./root/SliderRoot.jsx";
2
+ export { SliderTrack as Track } from "./track/SliderTrack.jsx";
3
+ export { SliderThumb as Thumb } from "./thumb/SliderThumb.jsx";
4
+ export { SliderValue as Value } from "./value/SliderValue.jsx";
@@ -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,136 @@
1
+ import { createMemo, merge, omit, onFlush, untrack } from "solid-js";
2
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
3
+ import { onFormReset } from "../../core/formReset.js";
4
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
5
+ import { renderElement } from "../../core/renderElement.jsx";
6
+ import { SliderRootContext } from "./SliderRootContext.js";
7
+ // Decimal places of a number, to round a step's float error away.
8
+ const places = (number) => (String(number).split(".")[1] ?? "").length;
9
+ /**
10
+ * The slider: a `<div>` around its parts, which holds the value and sets
11
+ * `--slider` to its percentage of the range. Its `id`, `aria-describedby`
12
+ * and `aria-invalid` go on the Thumb's input, so it joins a Field as
13
+ * `Field.Control`'s `render`.
14
+ */
15
+ export function SliderRoot(props) {
16
+ const min = () => props.min ?? 0;
17
+ const max = () => props.max ?? 100;
18
+ const stepSize = () => props.step ?? 1;
19
+ // The value on the step grid from `min` nearest `number` by `round`,
20
+ // clamped to the range: the last value is the grid's last at or under
21
+ // `max`, as a range input's is. Rounded to the decimals of `step` and
22
+ // `min`, so that `0.1` steps add up.
23
+ const snap = (number, round) => {
24
+ const step = stepSize();
25
+ const steps = Math.min(Math.max(round((number - min()) / step), 0), Math.floor((max() - min()) / step + 1e-9));
26
+ return Number((min() + steps * step).toFixed(Math.max(places(step), places(min()))));
27
+ };
28
+ // As a range input's: the midpoint, on the grid.
29
+ const defaultValue = () => props.defaultValue ?? snap((min() + max()) / 2, Math.round);
30
+ const [value, setValue, requested] = createControllableSignal({
31
+ get value() {
32
+ return props.value;
33
+ },
34
+ get defaultValue() {
35
+ return defaultValue();
36
+ },
37
+ onChange: (next) => props.onValueChange?.(next),
38
+ });
39
+ const formatText = createMemo(() => {
40
+ const format = props.format;
41
+ if (typeof format === "function")
42
+ return format;
43
+ const formatter = new Intl.NumberFormat(props.locale, format);
44
+ return (number) => formatter.format(number);
45
+ });
46
+ // Puts the input back to the value, once it has settled: a controlled
47
+ // slider's consumer may have declined the change, and a form reset set
48
+ // the input to its `value` attribute.
49
+ const sync = () => {
50
+ if (context.input)
51
+ context.input.value = String(untrack(value));
52
+ };
53
+ const fire = (type) => context.input?.dispatchEvent(new Event(type, { bubbles: true }));
54
+ // The value when the first of the changes under way began, and how many
55
+ // are under way: a drag, a key held during it. The last to end commits.
56
+ let start = 0;
57
+ let holds = 0;
58
+ const context = {
59
+ props,
60
+ value,
61
+ requested,
62
+ min,
63
+ max,
64
+ stepSize,
65
+ text: () => formatText()(value()),
66
+ attributes: {
67
+ get "data-disabled"() {
68
+ return props.disabled ? "" : undefined;
69
+ },
70
+ get "data-horizontal"() {
71
+ return props.orientation === "vertical" ? undefined : "";
72
+ },
73
+ get "data-vertical"() {
74
+ return props.orientation === "vertical" ? "" : undefined;
75
+ },
76
+ },
77
+ change: (number, round = Math.round) => {
78
+ const next = snap(number, round);
79
+ if (next === requested())
80
+ return;
81
+ // The input changes first and fires its native `input`, as a range
82
+ // input does under a drag, so the form's listeners, a Field and a
83
+ // Form see the new value.
84
+ if (context.input)
85
+ context.input.value = String(next);
86
+ setValue(next);
87
+ fire("input");
88
+ onFlush(sync);
89
+ },
90
+ // From a value off the grid, the grid's next one that way, as
91
+ // `stepUp()` does.
92
+ step: (amount) => context.change(requested() + amount, amount > 0
93
+ ? (steps) => Math.floor(steps + 1e-9)
94
+ : (steps) => Math.ceil(steps - 1e-9)),
95
+ begin: () => {
96
+ if (!holds++)
97
+ start = requested();
98
+ },
99
+ end: () => {
100
+ if (--holds)
101
+ return;
102
+ const current = requested();
103
+ if (current === start)
104
+ return;
105
+ fire("change");
106
+ props.onValueCommit?.(current);
107
+ },
108
+ };
109
+ // A form reset puts a control back to its default, and so it does this
110
+ // one: through the setter, so that a controlled slider asks.
111
+ onFormReset(() => context.input?.form, () => {
112
+ const before = requested();
113
+ setValue(defaultValue());
114
+ onFlush(() => {
115
+ sync();
116
+ // Once taken: a controlled slider may decline it.
117
+ const current = untrack(value);
118
+ if (current !== before)
119
+ props.onValueCommit?.(current);
120
+ });
121
+ },
122
+ // Held through the browser's reset (`onFormReset`).
123
+ () => {
124
+ context.input.defaultValue = context.input.value;
125
+ });
126
+ const ownProps = merge(context.attributes, {
127
+ get style() {
128
+ const fraction = (Math.min(Math.max(value(), min()), max()) - min()) / (max() - min());
129
+ return { "--slider": `${fraction * 100}%` };
130
+ },
131
+ });
132
+ 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"));
133
+ return (<SliderRootContext value={context}>
134
+ {renderElement(props, elementProps, (p) => (<div {...p}/>))}
135
+ </SliderRootContext>);
136
+ }
@@ -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
+ }