@zag-js/slider 1.47.0 → 1.48.0
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/dist/index.d.mts +6 -8
- package/dist/index.d.ts +6 -8
- package/dist/index.js +16 -38
- package/dist/index.mjs +2 -7
- package/dist/slider.anatomy.d.mts +2 -6
- package/dist/slider.anatomy.d.ts +2 -6
- package/dist/slider.anatomy.js +8 -44
- package/dist/slider.anatomy.mjs +4 -18
- package/dist/slider.connect.d.mts +3 -7
- package/dist/slider.connect.d.ts +3 -7
- package/dist/slider.connect.js +371 -351
- package/dist/slider.connect.mjs +366 -330
- package/dist/slider.dom.d.mts +27 -30
- package/dist/slider.dom.d.ts +27 -30
- package/dist/slider.dom.js +93 -137
- package/dist/slider.dom.mjs +70 -93
- package/dist/slider.machine.d.mts +2 -7
- package/dist/slider.machine.d.ts +2 -7
- package/dist/slider.machine.js +322 -367
- package/dist/slider.machine.mjs +318 -351
- package/dist/slider.props.d.mts +7 -12
- package/dist/slider.props.d.ts +7 -12
- package/dist/slider.props.js +45 -73
- package/dist/slider.props.mjs +36 -43
- package/dist/slider.style.d.mts +15 -17
- package/dist/slider.style.d.ts +15 -17
- package/dist/slider.style.js +126 -156
- package/dist/slider.style.mjs +114 -123
- package/dist/slider.types.d.mts +307 -309
- package/dist/slider.types.d.ts +307 -309
- package/dist/slider.types.js +4 -18
- package/dist/slider.types.mjs +1 -0
- package/dist/slider.utils.d.mts +18 -21
- package/dist/slider.utils.d.ts +18 -21
- package/dist/slider.utils.js +144 -168
- package/dist/slider.utils.mjs +132 -135
- package/package.json +8 -8
package/dist/slider.utils.d.mts
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import '@zag-js/types';
|
|
4
|
-
|
|
1
|
+
import { SliderSchema, ThumbCollisionBehavior } from "./slider.types.mjs";
|
|
2
|
+
import { Params } from "@zag-js/core";
|
|
5
3
|
type Ctx = Params<SliderSchema>;
|
|
6
4
|
interface ThumbCollisionResult {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
values: number[];
|
|
6
|
+
index: number;
|
|
7
|
+
swapped: boolean;
|
|
10
8
|
}
|
|
11
|
-
declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
|
|
12
|
-
declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
|
|
13
|
-
declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
export declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
|
|
10
|
+
export declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
|
|
11
|
+
export declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
|
|
12
|
+
min: number;
|
|
13
|
+
max: number;
|
|
14
|
+
value: number;
|
|
17
15
|
};
|
|
18
|
-
declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
|
|
19
|
-
declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
20
|
-
declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
21
|
-
declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
|
|
22
|
-
declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
|
|
23
|
-
declare function assignArray(current: number[], next: number[]): void;
|
|
24
|
-
|
|
25
|
-
export { assignArray, constrainValue, decrement, getClosestIndex, getRangeAtIndex, increment, normalizeValues, resolveThumbCollision, selectMovableThumb };
|
|
16
|
+
export declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
|
|
17
|
+
export declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
18
|
+
export declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
19
|
+
export declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
|
|
20
|
+
export declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
|
|
21
|
+
export declare function assignArray(current: number[], next: number[]): void;
|
|
22
|
+
export {};
|
package/dist/slider.utils.d.ts
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import '@zag-js/types';
|
|
4
|
-
|
|
1
|
+
import { SliderSchema, ThumbCollisionBehavior } from "./slider.types.js";
|
|
2
|
+
import { Params } from "@zag-js/core";
|
|
5
3
|
type Ctx = Params<SliderSchema>;
|
|
6
4
|
interface ThumbCollisionResult {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
values: number[];
|
|
6
|
+
index: number;
|
|
7
|
+
swapped: boolean;
|
|
10
8
|
}
|
|
11
|
-
declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
|
|
12
|
-
declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
|
|
13
|
-
declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
export declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
|
|
10
|
+
export declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
|
|
11
|
+
export declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
|
|
12
|
+
min: number;
|
|
13
|
+
max: number;
|
|
14
|
+
value: number;
|
|
17
15
|
};
|
|
18
|
-
declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
|
|
19
|
-
declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
20
|
-
declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
21
|
-
declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
|
|
22
|
-
declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
|
|
23
|
-
declare function assignArray(current: number[], next: number[]): void;
|
|
24
|
-
|
|
25
|
-
export { assignArray, constrainValue, decrement, getClosestIndex, getRangeAtIndex, increment, normalizeValues, resolveThumbCollision, selectMovableThumb };
|
|
16
|
+
export declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
|
|
17
|
+
export declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
18
|
+
export declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
|
|
19
|
+
export declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
|
|
20
|
+
export declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
|
|
21
|
+
export declare function assignArray(current: number[], next: number[]): void;
|
|
22
|
+
export {};
|
package/dist/slider.utils.js
CHANGED
|
@@ -1,197 +1,173 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/slider.utils.ts
|
|
21
|
-
var slider_utils_exports = {};
|
|
22
|
-
__export(slider_utils_exports, {
|
|
23
|
-
assignArray: () => assignArray,
|
|
24
|
-
constrainValue: () => constrainValue,
|
|
25
|
-
decrement: () => decrement,
|
|
26
|
-
getClosestIndex: () => getClosestIndex,
|
|
27
|
-
getRangeAtIndex: () => getRangeAtIndex,
|
|
28
|
-
increment: () => increment,
|
|
29
|
-
normalizeValues: () => normalizeValues,
|
|
30
|
-
resolveThumbCollision: () => resolveThumbCollision,
|
|
31
|
-
selectMovableThumb: () => selectMovableThumb
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
32
4
|
});
|
|
33
|
-
|
|
34
|
-
|
|
5
|
+
let _zag_js_utils = require("@zag-js/utils");
|
|
6
|
+
// src/slider.utils.ts
|
|
35
7
|
function getThumbBounds(ctx) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
8
|
+
const { index, values, min, max, gap } = ctx;
|
|
9
|
+
const prevThumb = values[index - 1];
|
|
10
|
+
const nextThumb = values[index + 1];
|
|
11
|
+
return {
|
|
12
|
+
min: prevThumb != null ? prevThumb + gap : min,
|
|
13
|
+
max: nextThumb != null ? nextThumb - gap : max
|
|
14
|
+
};
|
|
43
15
|
}
|
|
44
16
|
function round(value) {
|
|
45
|
-
|
|
17
|
+
return Math.round(value * 1e10) / 1e10;
|
|
46
18
|
}
|
|
47
19
|
function handleNone(ctx) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
20
|
+
const { index, value, values } = ctx;
|
|
21
|
+
const bounds = getThumbBounds(ctx);
|
|
22
|
+
const nextValues = values.slice();
|
|
23
|
+
nextValues[index] = round((0, _zag_js_utils.clampValue)(value, bounds.min, bounds.max));
|
|
24
|
+
return {
|
|
25
|
+
values: nextValues,
|
|
26
|
+
index,
|
|
27
|
+
swapped: false
|
|
28
|
+
};
|
|
53
29
|
}
|
|
54
30
|
function handlePush(ctx) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
31
|
+
const { index, value, values, min, max, gap } = ctx;
|
|
32
|
+
const nextValues = values.slice();
|
|
33
|
+
const absoluteMin = min + index * gap;
|
|
34
|
+
const absoluteMax = max - (values.length - 1 - index) * gap;
|
|
35
|
+
nextValues[index] = round((0, _zag_js_utils.clampValue)(value, absoluteMin, absoluteMax));
|
|
36
|
+
for (let i = index + 1; i < values.length; i++) {
|
|
37
|
+
const minAllowed = nextValues[i - 1] + gap;
|
|
38
|
+
if (nextValues[i] < minAllowed) nextValues[i] = round(minAllowed);
|
|
39
|
+
}
|
|
40
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
41
|
+
const maxAllowed = nextValues[i + 1] - gap;
|
|
42
|
+
if (nextValues[i] > maxAllowed) nextValues[i] = round(maxAllowed);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
values: nextValues,
|
|
46
|
+
index,
|
|
47
|
+
swapped: false
|
|
48
|
+
};
|
|
73
49
|
}
|
|
74
50
|
function handleSwap(ctx, startValue) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
51
|
+
const { index, value, values, gap } = ctx;
|
|
52
|
+
const prevThumb = values[index - 1];
|
|
53
|
+
const nextThumb = values[index + 1];
|
|
54
|
+
const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
|
|
55
|
+
const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
|
|
56
|
+
if (!crossingNext && !crossingPrev) return handleNone(ctx);
|
|
57
|
+
const swapIndex = crossingNext ? index + 1 : index - 1;
|
|
58
|
+
const nextValues = values.slice();
|
|
59
|
+
const bounds = getThumbBounds({
|
|
60
|
+
...ctx,
|
|
61
|
+
index: swapIndex
|
|
62
|
+
});
|
|
63
|
+
nextValues[swapIndex] = round((0, _zag_js_utils.clampValue)(value, bounds.min, bounds.max));
|
|
64
|
+
nextValues[index] = values[swapIndex];
|
|
65
|
+
if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) nextValues[index] = round(nextValues[swapIndex] - gap);
|
|
66
|
+
else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) nextValues[index] = round(nextValues[swapIndex] + gap);
|
|
67
|
+
return {
|
|
68
|
+
values: nextValues,
|
|
69
|
+
index: swapIndex,
|
|
70
|
+
swapped: true
|
|
71
|
+
};
|
|
95
72
|
}
|
|
96
73
|
function resolveThumbCollision(behavior, index, value, values, min, max, step, minStepsBetweenThumbs, startValue) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
74
|
+
if (values.length === 1) return {
|
|
75
|
+
values: [round((0, _zag_js_utils.clampValue)(value, min, max))],
|
|
76
|
+
index: 0,
|
|
77
|
+
swapped: false
|
|
78
|
+
};
|
|
79
|
+
const ctx = {
|
|
80
|
+
behavior,
|
|
81
|
+
index,
|
|
82
|
+
value,
|
|
83
|
+
values,
|
|
84
|
+
min,
|
|
85
|
+
max,
|
|
86
|
+
gap: step * minStepsBetweenThumbs
|
|
87
|
+
};
|
|
88
|
+
switch (behavior) {
|
|
89
|
+
case "push": return handlePush(ctx);
|
|
90
|
+
case "swap": return handleSwap(ctx, startValue ?? values[index]);
|
|
91
|
+
default: return handleNone(ctx);
|
|
92
|
+
}
|
|
111
93
|
}
|
|
112
94
|
function normalizeValues(params, nextValues) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
95
|
+
return nextValues.map((value, index) => {
|
|
96
|
+
return constrainValue(params, value, index);
|
|
97
|
+
});
|
|
116
98
|
}
|
|
117
99
|
function getRangeAtIndex(params, index) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
100
|
+
const { context, prop } = params;
|
|
101
|
+
const step = prop("step") * prop("minStepsBetweenThumbs");
|
|
102
|
+
return (0, _zag_js_utils.getValueRanges)(context.get("value"), prop("min"), prop("max"), step)[index];
|
|
121
103
|
}
|
|
122
104
|
function constrainValue(params, value, index) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
105
|
+
const { prop } = params;
|
|
106
|
+
const range = getRangeAtIndex(params, index);
|
|
107
|
+
const snapValue = (0, _zag_js_utils.snapValueToStep)(value, prop("min"), prop("max"), prop("step"));
|
|
108
|
+
return (0, _zag_js_utils.clampValue)(snapValue, range.min, range.max);
|
|
127
109
|
}
|
|
128
110
|
function decrement(params, index, step) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
111
|
+
const { context, prop } = params;
|
|
112
|
+
const idx = index ?? context.get("focusedIndex");
|
|
113
|
+
const range = getRangeAtIndex(params, idx);
|
|
114
|
+
const nextValues = (0, _zag_js_utils.getPreviousStepValue)(idx, {
|
|
115
|
+
...range,
|
|
116
|
+
step: step ?? prop("step"),
|
|
117
|
+
values: context.get("value")
|
|
118
|
+
});
|
|
119
|
+
nextValues[idx] = (0, _zag_js_utils.clampValue)(nextValues[idx], range.min, range.max);
|
|
120
|
+
return nextValues;
|
|
139
121
|
}
|
|
140
122
|
function increment(params, index, step) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
123
|
+
const { context, prop } = params;
|
|
124
|
+
const idx = index ?? context.get("focusedIndex");
|
|
125
|
+
const range = getRangeAtIndex(params, idx);
|
|
126
|
+
const nextValues = (0, _zag_js_utils.getNextStepValue)(idx, {
|
|
127
|
+
...range,
|
|
128
|
+
step: step ?? prop("step"),
|
|
129
|
+
values: context.get("value")
|
|
130
|
+
});
|
|
131
|
+
nextValues[idx] = (0, _zag_js_utils.clampValue)(nextValues[idx], range.min, range.max);
|
|
132
|
+
return nextValues;
|
|
151
133
|
}
|
|
152
134
|
function getClosestIndex(params, pointValue) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
135
|
+
const { context } = params;
|
|
136
|
+
const values = context.get("value");
|
|
137
|
+
let closestIndex = 0;
|
|
138
|
+
let minDistance = Math.abs(values[0] - pointValue);
|
|
139
|
+
for (let i = 1; i < values.length; i++) {
|
|
140
|
+
const distance = Math.abs(values[i] - pointValue);
|
|
141
|
+
if (distance <= minDistance) {
|
|
142
|
+
closestIndex = i;
|
|
143
|
+
minDistance = distance;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return selectMovableThumb(params, closestIndex);
|
|
165
147
|
}
|
|
166
148
|
function selectMovableThumb(params, index) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return movableIndex;
|
|
177
|
-
}
|
|
178
|
-
return index;
|
|
149
|
+
const { context, prop } = params;
|
|
150
|
+
const values = context.get("value");
|
|
151
|
+
const max = prop("max");
|
|
152
|
+
if (values[index] === max) {
|
|
153
|
+
let movableIndex = index;
|
|
154
|
+
while (movableIndex > 0 && values[movableIndex - 1] === max) movableIndex -= 1;
|
|
155
|
+
return movableIndex;
|
|
156
|
+
}
|
|
157
|
+
return index;
|
|
179
158
|
}
|
|
180
159
|
function assignArray(current, next) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
160
|
+
for (let i = 0; i < next.length; i++) {
|
|
161
|
+
const value = next[i];
|
|
162
|
+
current[i] = value;
|
|
163
|
+
}
|
|
185
164
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
resolveThumbCollision,
|
|
196
|
-
selectMovableThumb
|
|
197
|
-
});
|
|
165
|
+
exports.assignArray = assignArray;
|
|
166
|
+
exports.constrainValue = constrainValue;
|
|
167
|
+
exports.decrement = decrement;
|
|
168
|
+
exports.getClosestIndex = getClosestIndex;
|
|
169
|
+
exports.getRangeAtIndex = getRangeAtIndex;
|
|
170
|
+
exports.increment = increment;
|
|
171
|
+
exports.normalizeValues = normalizeValues;
|
|
172
|
+
exports.resolveThumbCollision = resolveThumbCollision;
|
|
173
|
+
exports.selectMovableThumb = selectMovableThumb;
|