@zag-js/slider 0.10.5 → 0.11.1

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.
@@ -1,203 +0,0 @@
1
- import { createMachine } from '@zag-js/core';
2
- import { trackPointerMove } from '@zag-js/dom-event';
3
- import { raf } from '@zag-js/dom-query';
4
- import { trackElementSize } from '@zag-js/element-size';
5
- import { trackFormControl } from '@zag-js/form-utils';
6
- import { getValuePercent, clampValue } from '@zag-js/numeric-range';
7
- import { compact } from '@zag-js/utils';
8
- import { dom } from './slider.dom.mjs';
9
- import { constrainValue, decrement, increment } from './slider.utils.mjs';
10
-
11
- function machine(userContext) {
12
- const ctx = compact(userContext);
13
- return createMachine(
14
- {
15
- id: "slider",
16
- initial: "idle",
17
- context: {
18
- thumbSize: null,
19
- thumbAlignment: "contain",
20
- disabled: false,
21
- threshold: 5,
22
- dir: "ltr",
23
- origin: "start",
24
- orientation: "horizontal",
25
- initialValue: null,
26
- value: 0,
27
- step: 1,
28
- min: 0,
29
- max: 100,
30
- ...ctx
31
- },
32
- computed: {
33
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
34
- isVertical: (ctx2) => ctx2.orientation === "vertical",
35
- isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
36
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly),
37
- hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null,
38
- valuePercent: (ctx2) => 100 * getValuePercent(ctx2.value, ctx2.min, ctx2.max)
39
- },
40
- watch: {
41
- value: ["invokeOnChange", "dispatchChangeEvent"]
42
- },
43
- activities: ["trackFormControlState", "trackThumbSize"],
44
- on: {
45
- SET_VALUE: {
46
- actions: "setValue"
47
- },
48
- INCREMENT: {
49
- actions: "increment"
50
- },
51
- DECREMENT: {
52
- actions: "decrement"
53
- }
54
- },
55
- entry: ["checkValue"],
56
- states: {
57
- idle: {
58
- on: {
59
- POINTER_DOWN: {
60
- target: "dragging",
61
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
62
- },
63
- FOCUS: "focus"
64
- }
65
- },
66
- focus: {
67
- entry: "focusThumb",
68
- on: {
69
- POINTER_DOWN: {
70
- target: "dragging",
71
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
72
- },
73
- ARROW_LEFT: {
74
- guard: "isHorizontal",
75
- actions: "decrement"
76
- },
77
- ARROW_RIGHT: {
78
- guard: "isHorizontal",
79
- actions: "increment"
80
- },
81
- ARROW_UP: {
82
- guard: "isVertical",
83
- actions: "increment"
84
- },
85
- ARROW_DOWN: {
86
- guard: "isVertical",
87
- actions: "decrement"
88
- },
89
- PAGE_UP: {
90
- actions: "increment"
91
- },
92
- PAGE_DOWN: {
93
- actions: "decrement"
94
- },
95
- HOME: {
96
- actions: "setToMin"
97
- },
98
- END: {
99
- actions: "setToMax"
100
- },
101
- BLUR: "idle"
102
- }
103
- },
104
- dragging: {
105
- entry: "focusThumb",
106
- activities: "trackPointerMove",
107
- on: {
108
- POINTER_UP: {
109
- target: "focus",
110
- actions: "invokeOnChangeEnd"
111
- },
112
- POINTER_MOVE: {
113
- actions: "setPointerValue"
114
- }
115
- }
116
- }
117
- }
118
- },
119
- {
120
- guards: {
121
- isHorizontal: (ctx2) => ctx2.isHorizontal,
122
- isVertical: (ctx2) => ctx2.isVertical
123
- },
124
- activities: {
125
- trackFormControlState(ctx2) {
126
- return trackFormControl(dom.getHiddenInputEl(ctx2), {
127
- onFieldsetDisabled() {
128
- ctx2.disabled = true;
129
- },
130
- onFormReset() {
131
- if (ctx2.initialValue != null) {
132
- ctx2.value = ctx2.initialValue;
133
- }
134
- }
135
- });
136
- },
137
- trackPointerMove(ctx2, _evt, { send }) {
138
- return trackPointerMove(dom.getDoc(ctx2), {
139
- onPointerMove(info) {
140
- send({ type: "POINTER_MOVE", point: info.point });
141
- },
142
- onPointerUp() {
143
- send("POINTER_UP");
144
- }
145
- });
146
- },
147
- trackThumbSize(ctx2, _evt) {
148
- if (ctx2.thumbAlignment !== "contain")
149
- return;
150
- return trackElementSize(dom.getThumbEl(ctx2), (size) => {
151
- if (size)
152
- ctx2.thumbSize = size;
153
- });
154
- }
155
- },
156
- actions: {
157
- checkValue(ctx2) {
158
- const value = constrainValue(ctx2, ctx2.value);
159
- ctx2.value = value;
160
- ctx2.initialValue = value;
161
- },
162
- invokeOnChangeStart(ctx2) {
163
- ctx2.onChangeStart?.({ value: ctx2.value });
164
- },
165
- invokeOnChangeEnd(ctx2) {
166
- ctx2.onChangeEnd?.({ value: ctx2.value });
167
- },
168
- invokeOnChange(ctx2) {
169
- ctx2.onChange?.({ value: ctx2.value });
170
- },
171
- dispatchChangeEvent(ctx2) {
172
- dom.dispatchChangeEvent(ctx2);
173
- },
174
- setPointerValue(ctx2, evt) {
175
- const value = dom.getValueFromPoint(ctx2, evt.point);
176
- if (value == null)
177
- return;
178
- ctx2.value = clampValue(value, ctx2.min, ctx2.max);
179
- },
180
- focusThumb(ctx2) {
181
- raf(() => dom.getThumbEl(ctx2)?.focus());
182
- },
183
- decrement(ctx2, evt) {
184
- ctx2.value = decrement(ctx2, evt.step);
185
- },
186
- increment(ctx2, evt) {
187
- ctx2.value = increment(ctx2, evt.step);
188
- },
189
- setToMin(ctx2) {
190
- ctx2.value = ctx2.min;
191
- },
192
- setToMax(ctx2) {
193
- ctx2.value = ctx2.max;
194
- },
195
- setValue(ctx2, evt) {
196
- ctx2.value = constrainValue(ctx2, evt.value);
197
- }
198
- }
199
- }
200
- );
201
- }
202
-
203
- export { machine };
@@ -1,23 +0,0 @@
1
- import type { Style } from "@zag-js/types";
2
- import type { MachineContext as Ctx, SharedContext } from "./slider.types";
3
- declare function getThumbOffset(ctx: SharedContext): string;
4
- declare function getThumbStyle(ctx: SharedContext): Style;
5
- declare function getRangeStyle(ctx: Pick<SharedContext, "isVertical" | "isRtl">): Style;
6
- declare function getControlStyle(): Style;
7
- declare function getRootStyle(ctx: Ctx): Style;
8
- declare function getMarkerStyle(ctx: Pick<SharedContext, "isHorizontal" | "isRtl">, percent: number): Style;
9
- declare function getLabelStyle(): Style;
10
- declare function getTrackStyle(): Style;
11
- declare function getMarkerGroupStyle(): Style;
12
- export declare const styles: {
13
- getThumbOffset: typeof getThumbOffset;
14
- getControlStyle: typeof getControlStyle;
15
- getThumbStyle: typeof getThumbStyle;
16
- getRangeStyle: typeof getRangeStyle;
17
- getRootStyle: typeof getRootStyle;
18
- getMarkerStyle: typeof getMarkerStyle;
19
- getLabelStyle: typeof getLabelStyle;
20
- getTrackStyle: typeof getTrackStyle;
21
- getMarkerGroupStyle: typeof getMarkerGroupStyle;
22
- };
23
- export {};
@@ -1,117 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const numericRange = require('@zag-js/numeric-range');
6
-
7
- function getVerticalThumbOffset(ctx) {
8
- const { height = 0 } = ctx.thumbSize ?? {};
9
- const getValue = numericRange.getValueTransformer([ctx.min, ctx.max], [-height / 2, height / 2]);
10
- return parseFloat(getValue(ctx.value).toFixed(2));
11
- }
12
- function getHorizontalThumbOffset(ctx) {
13
- const { width = 0 } = ctx.thumbSize ?? {};
14
- if (ctx.isRtl) {
15
- const getValue2 = numericRange.getValueTransformer([ctx.max, ctx.min], [-width * 1.5, -width / 2]);
16
- return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
17
- }
18
- const getValue = numericRange.getValueTransformer([ctx.min, ctx.max], [-width / 2, width / 2]);
19
- return parseFloat(getValue(ctx.value).toFixed(2));
20
- }
21
- function getThumbOffset(ctx) {
22
- const percent = numericRange.getValuePercent(ctx.value, ctx.min, ctx.max) * 100;
23
- if (ctx.thumbAlignment === "center") {
24
- return `${percent}%`;
25
- }
26
- const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
27
- return `calc(${percent}% - ${offset}px)`;
28
- }
29
- function getVisibility(ctx) {
30
- let visibility = "visible";
31
- if (ctx.thumbAlignment === "contain" && !ctx.hasMeasuredThumbSize) {
32
- visibility = "hidden";
33
- }
34
- return visibility;
35
- }
36
- function getThumbStyle(ctx) {
37
- const placementProp = ctx.isVertical ? "bottom" : ctx.isRtl ? "right" : "left";
38
- return {
39
- visibility: getVisibility(ctx),
40
- position: "absolute",
41
- transform: "var(--slider-thumb-transform)",
42
- [placementProp]: "var(--slider-thumb-offset)"
43
- };
44
- }
45
- function getRangeOffsets(ctx) {
46
- let start = "0%";
47
- let end = `${100 - ctx.valuePercent}%`;
48
- if (ctx.origin === "center") {
49
- const isNegative = ctx.valuePercent < 50;
50
- start = isNegative ? `${ctx.valuePercent}%` : "50%";
51
- end = isNegative ? "50%" : end;
52
- }
53
- return { start, end };
54
- }
55
- function getRangeStyle(ctx) {
56
- if (ctx.isVertical) {
57
- return {
58
- position: "absolute",
59
- bottom: "var(--slider-range-start)",
60
- top: "var(--slider-range-end)"
61
- };
62
- }
63
- return {
64
- position: "absolute",
65
- [ctx.isRtl ? "right" : "left"]: "var(--slider-range-start)",
66
- [ctx.isRtl ? "left" : "right"]: "var(--slider-range-end)"
67
- };
68
- }
69
- function getControlStyle() {
70
- return {
71
- touchAction: "none",
72
- userSelect: "none",
73
- position: "relative"
74
- };
75
- }
76
- function getRootStyle(ctx) {
77
- const range = getRangeOffsets(ctx);
78
- return {
79
- "--slider-thumb-transform": ctx.isVertical ? "translateY(50%)" : "translateX(-50%)",
80
- "--slider-thumb-offset": getThumbOffset(ctx),
81
- "--slider-range-start": range.start,
82
- "--slider-range-end": range.end
83
- };
84
- }
85
- function getMarkerStyle(ctx, percent) {
86
- return {
87
- position: "absolute",
88
- pointerEvents: "none",
89
- [ctx.isHorizontal ? "left" : "bottom"]: `${(ctx.isRtl ? 1 - percent : percent) * 100}%`
90
- };
91
- }
92
- function getLabelStyle() {
93
- return { userSelect: "none" };
94
- }
95
- function getTrackStyle() {
96
- return { position: "relative" };
97
- }
98
- function getMarkerGroupStyle() {
99
- return {
100
- userSelect: "none",
101
- pointerEvents: "none",
102
- position: "relative"
103
- };
104
- }
105
- const styles = {
106
- getThumbOffset,
107
- getControlStyle,
108
- getThumbStyle,
109
- getRangeStyle,
110
- getRootStyle,
111
- getMarkerStyle,
112
- getLabelStyle,
113
- getTrackStyle,
114
- getMarkerGroupStyle
115
- };
116
-
117
- exports.styles = styles;
@@ -1,113 +0,0 @@
1
- import { getValuePercent, getValueTransformer } from '@zag-js/numeric-range';
2
-
3
- function getVerticalThumbOffset(ctx) {
4
- const { height = 0 } = ctx.thumbSize ?? {};
5
- const getValue = getValueTransformer([ctx.min, ctx.max], [-height / 2, height / 2]);
6
- return parseFloat(getValue(ctx.value).toFixed(2));
7
- }
8
- function getHorizontalThumbOffset(ctx) {
9
- const { width = 0 } = ctx.thumbSize ?? {};
10
- if (ctx.isRtl) {
11
- const getValue2 = getValueTransformer([ctx.max, ctx.min], [-width * 1.5, -width / 2]);
12
- return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
13
- }
14
- const getValue = getValueTransformer([ctx.min, ctx.max], [-width / 2, width / 2]);
15
- return parseFloat(getValue(ctx.value).toFixed(2));
16
- }
17
- function getThumbOffset(ctx) {
18
- const percent = getValuePercent(ctx.value, ctx.min, ctx.max) * 100;
19
- if (ctx.thumbAlignment === "center") {
20
- return `${percent}%`;
21
- }
22
- const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
23
- return `calc(${percent}% - ${offset}px)`;
24
- }
25
- function getVisibility(ctx) {
26
- let visibility = "visible";
27
- if (ctx.thumbAlignment === "contain" && !ctx.hasMeasuredThumbSize) {
28
- visibility = "hidden";
29
- }
30
- return visibility;
31
- }
32
- function getThumbStyle(ctx) {
33
- const placementProp = ctx.isVertical ? "bottom" : ctx.isRtl ? "right" : "left";
34
- return {
35
- visibility: getVisibility(ctx),
36
- position: "absolute",
37
- transform: "var(--slider-thumb-transform)",
38
- [placementProp]: "var(--slider-thumb-offset)"
39
- };
40
- }
41
- function getRangeOffsets(ctx) {
42
- let start = "0%";
43
- let end = `${100 - ctx.valuePercent}%`;
44
- if (ctx.origin === "center") {
45
- const isNegative = ctx.valuePercent < 50;
46
- start = isNegative ? `${ctx.valuePercent}%` : "50%";
47
- end = isNegative ? "50%" : end;
48
- }
49
- return { start, end };
50
- }
51
- function getRangeStyle(ctx) {
52
- if (ctx.isVertical) {
53
- return {
54
- position: "absolute",
55
- bottom: "var(--slider-range-start)",
56
- top: "var(--slider-range-end)"
57
- };
58
- }
59
- return {
60
- position: "absolute",
61
- [ctx.isRtl ? "right" : "left"]: "var(--slider-range-start)",
62
- [ctx.isRtl ? "left" : "right"]: "var(--slider-range-end)"
63
- };
64
- }
65
- function getControlStyle() {
66
- return {
67
- touchAction: "none",
68
- userSelect: "none",
69
- position: "relative"
70
- };
71
- }
72
- function getRootStyle(ctx) {
73
- const range = getRangeOffsets(ctx);
74
- return {
75
- "--slider-thumb-transform": ctx.isVertical ? "translateY(50%)" : "translateX(-50%)",
76
- "--slider-thumb-offset": getThumbOffset(ctx),
77
- "--slider-range-start": range.start,
78
- "--slider-range-end": range.end
79
- };
80
- }
81
- function getMarkerStyle(ctx, percent) {
82
- return {
83
- position: "absolute",
84
- pointerEvents: "none",
85
- [ctx.isHorizontal ? "left" : "bottom"]: `${(ctx.isRtl ? 1 - percent : percent) * 100}%`
86
- };
87
- }
88
- function getLabelStyle() {
89
- return { userSelect: "none" };
90
- }
91
- function getTrackStyle() {
92
- return { position: "relative" };
93
- }
94
- function getMarkerGroupStyle() {
95
- return {
96
- userSelect: "none",
97
- pointerEvents: "none",
98
- position: "relative"
99
- };
100
- }
101
- const styles = {
102
- getThumbOffset,
103
- getControlStyle,
104
- getThumbStyle,
105
- getRangeStyle,
106
- getRootStyle,
107
- getMarkerStyle,
108
- getLabelStyle,
109
- getTrackStyle,
110
- getMarkerGroupStyle
111
- };
112
-
113
- export { styles };
@@ -1,165 +0,0 @@
1
- import type { StateMachine as S } from "@zag-js/core";
2
- import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
3
- type ElementIds = Partial<{
4
- root: string;
5
- thumb: string;
6
- control: string;
7
- track: string;
8
- range: string;
9
- label: string;
10
- output: string;
11
- hiddenInput: string;
12
- }>;
13
- type PublicContext = DirectionProperty & CommonProperties & {
14
- /**
15
- * The ids of the elements in the slider. Useful for composition.
16
- */
17
- ids?: ElementIds;
18
- /**
19
- * The value of the slider
20
- */
21
- value: number;
22
- /**
23
- * The name associated with the slider (when used in a form)
24
- */
25
- name?: string;
26
- /**
27
- * The associate form of the underlying input element.
28
- */
29
- form?: string;
30
- /**
31
- * Whether the slider is disabled
32
- */
33
- disabled?: boolean;
34
- /**
35
- * Whether the slider is read-only
36
- */
37
- readOnly?: boolean;
38
- /**
39
- * Whether the slider value is invalid
40
- */
41
- invalid?: boolean;
42
- /**
43
- * The minimum value of the slider
44
- */
45
- min: number;
46
- /**
47
- * The maximum value of the slider
48
- */
49
- max: number;
50
- /**
51
- * The step value of the slider
52
- */
53
- step: number;
54
- /**
55
- * The orientation of the slider
56
- */
57
- orientation?: "vertical" | "horizontal";
58
- /**
59
- * - "start": Useful when the value represents an absolute value
60
- * - "center": Useful when the value represents an offset (relative)
61
- */
62
- origin?: "start" | "center";
63
- /**
64
- * The aria-label of the slider. Useful for providing an accessible name to the slider
65
- */
66
- "aria-label"?: string;
67
- /**
68
- * The `id` of the element that labels the slider. Useful for providing an accessible name to the slider
69
- */
70
- "aria-labelledby"?: string;
71
- /**
72
- * Whether to focus the slider thumb after interaction (scrub and keyboard)
73
- */
74
- focusThumbOnChange?: boolean;
75
- /**
76
- * Function that returns a human readable value for the slider
77
- */
78
- getAriaValueText?(value: number): string;
79
- /**
80
- * Function invoked when the value of the slider changes
81
- */
82
- onChange?(details: {
83
- value: number;
84
- }): void;
85
- /**
86
- * Function invoked when the slider value change is done
87
- */
88
- onChangeEnd?(details: {
89
- value: number;
90
- }): void;
91
- /**
92
- * Function invoked when the slider value change is started
93
- */
94
- onChangeStart?(details: {
95
- value: number;
96
- }): void;
97
- /**
98
- * The alignment of the slider thumb relative to the track
99
- * - `center`: the thumb will extend beyond the bounds of the slider track.
100
- * - `contain`: the thumb will be contained within the bounds of the track.
101
- */
102
- thumbAlignment?: "contain" | "center";
103
- };
104
- export type UserDefinedContext = RequiredBy<PublicContext, "id">;
105
- type ComputedContext = Readonly<{
106
- /**
107
- * @computed
108
- * Whether the slider is interactive
109
- */
110
- readonly isInteractive: boolean;
111
- /**
112
- * @computed
113
- * Whether the thumb size has been measured
114
- */
115
- readonly hasMeasuredThumbSize: boolean;
116
- /**
117
- * @computed
118
- * Whether the slider is horizontal
119
- */
120
- readonly isHorizontal: boolean;
121
- /**
122
- * @computed
123
- * Whether the slider is vertical
124
- */
125
- readonly isVertical: boolean;
126
- /**
127
- * @computed
128
- * Whether the slider is in RTL mode
129
- */
130
- readonly isRtl: boolean;
131
- /**
132
- * @computed
133
- * The value of the slider as a percentage
134
- */
135
- readonly valuePercent: number;
136
- }>;
137
- type PrivateContext = Context<{}>;
138
- export type MachineContext = PublicContext & ComputedContext & PrivateContext;
139
- export type MachineState = {
140
- value: "idle" | "dragging" | "focus";
141
- };
142
- export type State = S.State<MachineContext, MachineState>;
143
- export type Send = S.Send<S.AnyEventObject>;
144
- export type SharedContext = {
145
- min: number;
146
- max: number;
147
- step: number;
148
- dir?: "ltr" | "rtl";
149
- isRtl: boolean;
150
- isVertical: boolean;
151
- isHorizontal: boolean;
152
- value: number;
153
- thumbSize: {
154
- width: number;
155
- height: number;
156
- } | null;
157
- thumbAlignment?: "contain" | "center";
158
- orientation?: "horizontal" | "vertical";
159
- readonly hasMeasuredThumbSize: boolean;
160
- };
161
- export type Point = {
162
- x: number;
163
- y: number;
164
- };
165
- export {};
@@ -1,5 +0,0 @@
1
- import type { MachineContext as Ctx } from "./slider.types";
2
- export declare function clampPercent(percent: number): number;
3
- export declare function constrainValue(ctx: Ctx, value: number): number;
4
- export declare function decrement(ctx: Ctx, step?: number): number;
5
- export declare function increment(ctx: Ctx, step?: number): number;
@@ -1,32 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const numericRange = require('@zag-js/numeric-range');
6
-
7
- function constrainValue(ctx, value) {
8
- const snapValue = numericRange.snapValueToStep(value, ctx.min, ctx.max, ctx.step);
9
- return numericRange.clampValue(snapValue, ctx.min, ctx.max);
10
- }
11
- function decrement(ctx, step) {
12
- const index = 0;
13
- const values = numericRange.getPreviousStepValue(index, {
14
- ...ctx,
15
- step: step ?? ctx.step,
16
- values: [ctx.value]
17
- });
18
- return values[index];
19
- }
20
- function increment(ctx, step) {
21
- const index = 0;
22
- const values = numericRange.getNextStepValue(index, {
23
- ...ctx,
24
- step: step ?? ctx.step,
25
- values: [ctx.value]
26
- });
27
- return values[index];
28
- }
29
-
30
- exports.constrainValue = constrainValue;
31
- exports.decrement = decrement;
32
- exports.increment = increment;
@@ -1,26 +0,0 @@
1
- import { snapValueToStep, clampValue, getPreviousStepValue, getNextStepValue } from '@zag-js/numeric-range';
2
-
3
- function constrainValue(ctx, value) {
4
- const snapValue = snapValueToStep(value, ctx.min, ctx.max, ctx.step);
5
- return clampValue(snapValue, ctx.min, ctx.max);
6
- }
7
- function decrement(ctx, step) {
8
- const index = 0;
9
- const values = getPreviousStepValue(index, {
10
- ...ctx,
11
- step: step ?? ctx.step,
12
- values: [ctx.value]
13
- });
14
- return values[index];
15
- }
16
- function increment(ctx, step) {
17
- const index = 0;
18
- const values = getNextStepValue(index, {
19
- ...ctx,
20
- step: step ?? ctx.step,
21
- values: [ctx.value]
22
- });
23
- return values[index];
24
- }
25
-
26
- export { constrainValue, decrement, increment };