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