@zag-js/slider 1.34.1 → 1.35.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.
- package/dist/index.d.mts +8 -335
- package/dist/index.d.ts +8 -335
- package/dist/index.js +34 -1055
- package/dist/index.mjs +9 -1049
- package/dist/slider.anatomy.d.mts +6 -0
- package/dist/slider.anatomy.d.ts +6 -0
- package/dist/slider.anatomy.js +45 -0
- package/dist/slider.anatomy.mjs +19 -0
- package/dist/slider.connect.d.mts +7 -0
- package/dist/slider.connect.d.ts +7 -0
- package/dist/slider.connect.js +354 -0
- package/dist/slider.connect.mjs +334 -0
- package/dist/slider.dom.d.mts +30 -0
- package/dist/slider.dom.d.ts +30 -0
- package/dist/slider.dom.js +144 -0
- package/dist/slider.dom.mjs +101 -0
- package/dist/slider.machine.d.mts +7 -0
- package/dist/slider.machine.d.ts +7 -0
- package/dist/slider.machine.js +370 -0
- package/dist/slider.machine.mjs +354 -0
- package/dist/slider.props.d.mts +12 -0
- package/dist/slider.props.d.ts +12 -0
- package/dist/slider.props.js +74 -0
- package/dist/slider.props.mjs +44 -0
- package/dist/slider.style.d.mts +19 -0
- package/dist/slider.style.d.ts +19 -0
- package/dist/slider.style.js +183 -0
- package/dist/slider.style.mjs +150 -0
- package/dist/slider.types.d.mts +320 -0
- package/dist/slider.types.d.ts +320 -0
- package/dist/slider.types.js +18 -0
- package/dist/slider.types.mjs +0 -0
- package/dist/slider.utils.d.mts +25 -0
- package/dist/slider.utils.d.ts +25 -0
- package/dist/slider.utils.js +197 -0
- package/dist/slider.utils.mjs +164 -0
- package/package.json +17 -7
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// src/slider.utils.ts
|
|
2
|
+
import { clampValue, getNextStepValue, getPreviousStepValue, getValueRanges, snapValueToStep } from "@zag-js/utils";
|
|
3
|
+
function getThumbBounds(ctx) {
|
|
4
|
+
const { index, values, min, max, gap } = ctx;
|
|
5
|
+
const prevThumb = values[index - 1];
|
|
6
|
+
const nextThumb = values[index + 1];
|
|
7
|
+
return {
|
|
8
|
+
min: prevThumb != null ? prevThumb + gap : min,
|
|
9
|
+
max: nextThumb != null ? nextThumb - gap : max
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function round(value) {
|
|
13
|
+
return Math.round(value * 1e10) / 1e10;
|
|
14
|
+
}
|
|
15
|
+
function handleNone(ctx) {
|
|
16
|
+
const { index, value, values } = ctx;
|
|
17
|
+
const bounds = getThumbBounds(ctx);
|
|
18
|
+
const nextValues = values.slice();
|
|
19
|
+
nextValues[index] = round(clampValue(value, bounds.min, bounds.max));
|
|
20
|
+
return { values: nextValues, index, swapped: false };
|
|
21
|
+
}
|
|
22
|
+
function handlePush(ctx) {
|
|
23
|
+
const { index, value, values, min, max, gap } = ctx;
|
|
24
|
+
const nextValues = values.slice();
|
|
25
|
+
const absoluteMin = min + index * gap;
|
|
26
|
+
const absoluteMax = max - (values.length - 1 - index) * gap;
|
|
27
|
+
nextValues[index] = round(clampValue(value, absoluteMin, absoluteMax));
|
|
28
|
+
for (let i = index + 1; i < values.length; i++) {
|
|
29
|
+
const minAllowed = nextValues[i - 1] + gap;
|
|
30
|
+
if (nextValues[i] < minAllowed) {
|
|
31
|
+
nextValues[i] = round(minAllowed);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
35
|
+
const maxAllowed = nextValues[i + 1] - gap;
|
|
36
|
+
if (nextValues[i] > maxAllowed) {
|
|
37
|
+
nextValues[i] = round(maxAllowed);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { values: nextValues, index, swapped: false };
|
|
41
|
+
}
|
|
42
|
+
function handleSwap(ctx, startValue) {
|
|
43
|
+
const { index, value, values, gap } = ctx;
|
|
44
|
+
const prevThumb = values[index - 1];
|
|
45
|
+
const nextThumb = values[index + 1];
|
|
46
|
+
const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
|
|
47
|
+
const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
|
|
48
|
+
if (!crossingNext && !crossingPrev) {
|
|
49
|
+
return handleNone(ctx);
|
|
50
|
+
}
|
|
51
|
+
const swapIndex = crossingNext ? index + 1 : index - 1;
|
|
52
|
+
const nextValues = values.slice();
|
|
53
|
+
const newCtx = { ...ctx, index: swapIndex };
|
|
54
|
+
const bounds = getThumbBounds(newCtx);
|
|
55
|
+
nextValues[swapIndex] = round(clampValue(value, bounds.min, bounds.max));
|
|
56
|
+
nextValues[index] = values[swapIndex];
|
|
57
|
+
if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) {
|
|
58
|
+
nextValues[index] = round(nextValues[swapIndex] - gap);
|
|
59
|
+
} else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) {
|
|
60
|
+
nextValues[index] = round(nextValues[swapIndex] + gap);
|
|
61
|
+
}
|
|
62
|
+
return { values: nextValues, index: swapIndex, swapped: true };
|
|
63
|
+
}
|
|
64
|
+
function resolveThumbCollision(behavior, index, value, values, min, max, step, minStepsBetweenThumbs, startValue) {
|
|
65
|
+
if (values.length === 1) {
|
|
66
|
+
return { values: [round(clampValue(value, min, max))], index: 0, swapped: false };
|
|
67
|
+
}
|
|
68
|
+
const gap = step * minStepsBetweenThumbs;
|
|
69
|
+
const ctx = { behavior, index, value, values, min, max, gap };
|
|
70
|
+
switch (behavior) {
|
|
71
|
+
case "push":
|
|
72
|
+
return handlePush(ctx);
|
|
73
|
+
case "swap":
|
|
74
|
+
return handleSwap(ctx, startValue ?? values[index]);
|
|
75
|
+
case "none":
|
|
76
|
+
default:
|
|
77
|
+
return handleNone(ctx);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function normalizeValues(params, nextValues) {
|
|
81
|
+
return nextValues.map((value, index) => {
|
|
82
|
+
return constrainValue(params, value, index);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function getRangeAtIndex(params, index) {
|
|
86
|
+
const { context, prop } = params;
|
|
87
|
+
const step = prop("step") * prop("minStepsBetweenThumbs");
|
|
88
|
+
return getValueRanges(context.get("value"), prop("min"), prop("max"), step)[index];
|
|
89
|
+
}
|
|
90
|
+
function constrainValue(params, value, index) {
|
|
91
|
+
const { prop } = params;
|
|
92
|
+
const range = getRangeAtIndex(params, index);
|
|
93
|
+
const snapValue = snapValueToStep(value, prop("min"), prop("max"), prop("step"));
|
|
94
|
+
return clampValue(snapValue, range.min, range.max);
|
|
95
|
+
}
|
|
96
|
+
function decrement(params, index, step) {
|
|
97
|
+
const { context, prop } = params;
|
|
98
|
+
const idx = index ?? context.get("focusedIndex");
|
|
99
|
+
const range = getRangeAtIndex(params, idx);
|
|
100
|
+
const nextValues = getPreviousStepValue(idx, {
|
|
101
|
+
...range,
|
|
102
|
+
step: step ?? prop("step"),
|
|
103
|
+
values: context.get("value")
|
|
104
|
+
});
|
|
105
|
+
nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
|
|
106
|
+
return nextValues;
|
|
107
|
+
}
|
|
108
|
+
function increment(params, index, step) {
|
|
109
|
+
const { context, prop } = params;
|
|
110
|
+
const idx = index ?? context.get("focusedIndex");
|
|
111
|
+
const range = getRangeAtIndex(params, idx);
|
|
112
|
+
const nextValues = getNextStepValue(idx, {
|
|
113
|
+
...range,
|
|
114
|
+
step: step ?? prop("step"),
|
|
115
|
+
values: context.get("value")
|
|
116
|
+
});
|
|
117
|
+
nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
|
|
118
|
+
return nextValues;
|
|
119
|
+
}
|
|
120
|
+
function getClosestIndex(params, pointValue) {
|
|
121
|
+
const { context } = params;
|
|
122
|
+
const values = context.get("value");
|
|
123
|
+
let closestIndex = 0;
|
|
124
|
+
let minDistance = Math.abs(values[0] - pointValue);
|
|
125
|
+
for (let i = 1; i < values.length; i++) {
|
|
126
|
+
const distance = Math.abs(values[i] - pointValue);
|
|
127
|
+
if (distance <= minDistance) {
|
|
128
|
+
closestIndex = i;
|
|
129
|
+
minDistance = distance;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return selectMovableThumb(params, closestIndex);
|
|
133
|
+
}
|
|
134
|
+
function selectMovableThumb(params, index) {
|
|
135
|
+
const { context, prop } = params;
|
|
136
|
+
const values = context.get("value");
|
|
137
|
+
const max = prop("max");
|
|
138
|
+
const thumbValue = values[index];
|
|
139
|
+
if (thumbValue === max) {
|
|
140
|
+
let movableIndex = index;
|
|
141
|
+
while (movableIndex > 0 && values[movableIndex - 1] === max) {
|
|
142
|
+
movableIndex -= 1;
|
|
143
|
+
}
|
|
144
|
+
return movableIndex;
|
|
145
|
+
}
|
|
146
|
+
return index;
|
|
147
|
+
}
|
|
148
|
+
function assignArray(current, next) {
|
|
149
|
+
for (let i = 0; i < next.length; i++) {
|
|
150
|
+
const value = next[i];
|
|
151
|
+
current[i] = value;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
assignArray,
|
|
156
|
+
constrainValue,
|
|
157
|
+
decrement,
|
|
158
|
+
getClosestIndex,
|
|
159
|
+
getRangeAtIndex,
|
|
160
|
+
increment,
|
|
161
|
+
normalizeValues,
|
|
162
|
+
resolveThumbCollision,
|
|
163
|
+
selectMovableThumb
|
|
164
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/slider",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.35.1",
|
|
4
4
|
"description": "Core logic for the slider widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -27,16 +27,16 @@
|
|
|
27
27
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@zag-js/anatomy": "1.
|
|
31
|
-
"@zag-js/core": "1.
|
|
32
|
-
"@zag-js/dom-query": "1.
|
|
33
|
-
"@zag-js/utils": "1.
|
|
34
|
-
"@zag-js/types": "1.
|
|
30
|
+
"@zag-js/anatomy": "1.35.1",
|
|
31
|
+
"@zag-js/core": "1.35.1",
|
|
32
|
+
"@zag-js/dom-query": "1.35.1",
|
|
33
|
+
"@zag-js/utils": "1.35.1",
|
|
34
|
+
"@zag-js/types": "1.35.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"clean-package": "2.2.0"
|
|
38
38
|
},
|
|
39
|
-
"clean-package": "
|
|
39
|
+
"clean-package": "./clean-package.config.json",
|
|
40
40
|
"main": "dist/index.js",
|
|
41
41
|
"module": "dist/index.mjs",
|
|
42
42
|
"types": "dist/index.d.ts",
|
|
@@ -51,6 +51,16 @@
|
|
|
51
51
|
"default": "./dist/index.js"
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
|
+
"./anatomy": {
|
|
55
|
+
"import": {
|
|
56
|
+
"types": "./dist/slider.anatomy.d.mts",
|
|
57
|
+
"default": "./dist/slider.anatomy.mjs"
|
|
58
|
+
},
|
|
59
|
+
"require": {
|
|
60
|
+
"types": "./dist/slider.anatomy.d.ts",
|
|
61
|
+
"default": "./dist/slider.anatomy.js"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
54
64
|
"./package.json": "./package.json"
|
|
55
65
|
},
|
|
56
66
|
"scripts": {
|