@zag-js/slider 2.0.0-next.2 → 2.0.0-next.4
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 +403 -372
- package/dist/slider.connect.mjs +399 -352
- package/dist/slider.dom.d.mts +15 -30
- package/dist/slider.dom.d.ts +15 -30
- package/dist/slider.dom.js +56 -141
- package/dist/slider.dom.mjs +40 -97
- package/dist/slider.machine.d.mts +2 -7
- package/dist/slider.machine.d.ts +2 -7
- package/dist/slider.machine.js +372 -368
- package/dist/slider.machine.mjs +368 -352
- package/dist/slider.props.d.mts +7 -12
- package/dist/slider.props.d.ts +7 -12
- package/dist/slider.props.js +49 -73
- package/dist/slider.props.mjs +40 -43
- package/dist/slider.style.d.mts +15 -17
- package/dist/slider.style.d.ts +15 -17
- package/dist/slider.style.js +103 -157
- package/dist/slider.style.mjs +91 -124
- package/dist/slider.types.d.mts +402 -377
- package/dist/slider.types.d.ts +402 -377
- package/dist/slider.types.js +4 -18
- package/dist/slider.types.mjs +1 -0
- package/dist/slider.utils.d.mts +23 -27
- package/dist/slider.utils.d.ts +23 -27
- package/dist/slider.utils.js +196 -205
- package/dist/slider.utils.mjs +180 -176
- package/package.json +7 -7
package/dist/slider.utils.mjs
CHANGED
|
@@ -1,194 +1,198 @@
|
|
|
1
|
+
import { clampValue, getValuePercent, getValueRanges, setValueAtIndex, snapValueToStep } from "@zag-js/utils";
|
|
1
2
|
// src/slider.utils.ts
|
|
2
|
-
|
|
3
|
-
clampValue,
|
|
4
|
-
getNextStepValue,
|
|
5
|
-
getPreviousStepValue,
|
|
6
|
-
getValuePercent,
|
|
7
|
-
getValueRanges,
|
|
8
|
-
snapValueToStep
|
|
9
|
-
} from "@zag-js/utils";
|
|
3
|
+
const NO_DISABLED = /* @__PURE__ */ new Set();
|
|
10
4
|
function getThumbBounds(ctx) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
const { index, values, min, max, gap } = ctx;
|
|
6
|
+
const prevThumb = values[index - 1];
|
|
7
|
+
const nextThumb = values[index + 1];
|
|
8
|
+
return {
|
|
9
|
+
min: prevThumb != null ? prevThumb + gap : min,
|
|
10
|
+
max: nextThumb != null ? nextThumb - gap : max
|
|
11
|
+
};
|
|
18
12
|
}
|
|
19
13
|
function round(value) {
|
|
20
|
-
|
|
14
|
+
return Math.round(value * 1e10) / 1e10;
|
|
21
15
|
}
|
|
22
16
|
function handleNone(ctx) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
const { index, value, values } = ctx;
|
|
18
|
+
const bounds = getThumbBounds(ctx);
|
|
19
|
+
const nextValues = values.slice();
|
|
20
|
+
nextValues[index] = round(clampValue(value, bounds.min, bounds.max));
|
|
21
|
+
return {
|
|
22
|
+
values: nextValues,
|
|
23
|
+
index,
|
|
24
|
+
swapped: false
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function getDisabledBounds(ctx) {
|
|
28
|
+
const { index, values, gap, disabled } = ctx;
|
|
29
|
+
let lower = -1e400;
|
|
30
|
+
let upper = 1e400;
|
|
31
|
+
for (let i = index - 1; i >= 0; i--) if (disabled.has(i)) {
|
|
32
|
+
lower = values[i] + (index - i) * gap;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
for (let i = index + 1; i < values.length; i++) if (disabled.has(i)) {
|
|
36
|
+
upper = values[i] - (i - index) * gap;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
lower,
|
|
41
|
+
upper
|
|
42
|
+
};
|
|
28
43
|
}
|
|
29
44
|
function handlePush(ctx) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
const { index, value, values, min, max, gap } = ctx;
|
|
46
|
+
const nextValues = values.slice();
|
|
47
|
+
const walls = getDisabledBounds(ctx);
|
|
48
|
+
const absoluteMin = Math.max(min + index * gap, walls.lower);
|
|
49
|
+
const absoluteMax = Math.min(max - (values.length - 1 - index) * gap, walls.upper);
|
|
50
|
+
nextValues[index] = round(clampValue(value, absoluteMin, absoluteMax));
|
|
51
|
+
for (let i = index + 1; i < values.length; i++) {
|
|
52
|
+
const minAllowed = nextValues[i - 1] + gap;
|
|
53
|
+
if (nextValues[i] < minAllowed) nextValues[i] = round(minAllowed);
|
|
54
|
+
}
|
|
55
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
56
|
+
const maxAllowed = nextValues[i + 1] - gap;
|
|
57
|
+
if (nextValues[i] > maxAllowed) nextValues[i] = round(maxAllowed);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
values: nextValues,
|
|
61
|
+
index,
|
|
62
|
+
swapped: false
|
|
63
|
+
};
|
|
48
64
|
}
|
|
49
65
|
function handleSwap(ctx, startValue) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
66
|
+
const { index, value, values, gap } = ctx;
|
|
67
|
+
const prevThumb = values[index - 1];
|
|
68
|
+
const nextThumb = values[index + 1];
|
|
69
|
+
const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
|
|
70
|
+
const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
|
|
71
|
+
if (!crossingNext && !crossingPrev) return handleNone(ctx);
|
|
72
|
+
const swapIndex = crossingNext ? index + 1 : index - 1;
|
|
73
|
+
if (ctx.disabled.has(swapIndex)) return handleNone(ctx);
|
|
74
|
+
const nextValues = values.slice();
|
|
75
|
+
const bounds = getThumbBounds({
|
|
76
|
+
...ctx,
|
|
77
|
+
index: swapIndex
|
|
78
|
+
});
|
|
79
|
+
nextValues[swapIndex] = round(clampValue(value, bounds.min, bounds.max));
|
|
80
|
+
nextValues[index] = values[swapIndex];
|
|
81
|
+
if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) nextValues[index] = round(nextValues[swapIndex] - gap);
|
|
82
|
+
else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) nextValues[index] = round(nextValues[swapIndex] + gap);
|
|
83
|
+
return {
|
|
84
|
+
values: nextValues,
|
|
85
|
+
index: swapIndex,
|
|
86
|
+
swapped: true
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function resolveThumbCollision(behavior, index, value, values, min, max, step, minStepsBetweenThumbs, startValue, disabled = NO_DISABLED) {
|
|
90
|
+
if (values.length === 1) return {
|
|
91
|
+
values: [round(clampValue(value, min, max))],
|
|
92
|
+
index: 0,
|
|
93
|
+
swapped: false
|
|
94
|
+
};
|
|
95
|
+
const ctx = {
|
|
96
|
+
behavior,
|
|
97
|
+
index,
|
|
98
|
+
value,
|
|
99
|
+
values,
|
|
100
|
+
min,
|
|
101
|
+
max,
|
|
102
|
+
gap: step * minStepsBetweenThumbs,
|
|
103
|
+
disabled
|
|
104
|
+
};
|
|
105
|
+
switch (behavior) {
|
|
106
|
+
case "push": return handlePush(ctx);
|
|
107
|
+
case "swap": return handleSwap(ctx, startValue ?? values[index]);
|
|
108
|
+
default: return handleNone(ctx);
|
|
109
|
+
}
|
|
86
110
|
}
|
|
87
111
|
function normalizeValues(params, nextValues) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
112
|
+
return nextValues.map((value, index) => {
|
|
113
|
+
return constrainValue(params, value, index);
|
|
114
|
+
});
|
|
91
115
|
}
|
|
92
116
|
function getRangeAtIndex(params, index) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
117
|
+
const { context, prop } = params;
|
|
118
|
+
const step = prop("step") * prop("minStepsBetweenThumbs");
|
|
119
|
+
return getValueRanges(context.get("value"), prop("min"), prop("max"), step)[index];
|
|
96
120
|
}
|
|
97
121
|
function constrainValue(params, value, index) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
function
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const thumbValue = values[index];
|
|
146
|
-
if (thumbValue === max) {
|
|
147
|
-
let movableIndex = index;
|
|
148
|
-
while (movableIndex > 0 && values[movableIndex - 1] === max) {
|
|
149
|
-
movableIndex -= 1;
|
|
150
|
-
}
|
|
151
|
-
return movableIndex;
|
|
152
|
-
}
|
|
153
|
-
return index;
|
|
154
|
-
}
|
|
155
|
-
function assignArray(current, next) {
|
|
156
|
-
for (let i = 0; i < next.length; i++) {
|
|
157
|
-
const value = next[i];
|
|
158
|
-
current[i] = value;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
122
|
+
const { prop } = params;
|
|
123
|
+
const range = getRangeAtIndex(params, index);
|
|
124
|
+
const snapValue = snapValueToStep(value, prop("min"), prop("max"), prop("step"));
|
|
125
|
+
return clampValue(snapValue, range.min, range.max);
|
|
126
|
+
}
|
|
127
|
+
function stepThumb(params, delta, index) {
|
|
128
|
+
const { context } = params;
|
|
129
|
+
const idx = index ?? context.get("focusedIndex");
|
|
130
|
+
const values = context.get("value");
|
|
131
|
+
return setValueAtIndex(values, idx, constrainValue(params, values[idx] + delta, idx));
|
|
132
|
+
}
|
|
133
|
+
function getClosestIndex(params, pointValue, disabled = NO_DISABLED) {
|
|
134
|
+
const { context } = params;
|
|
135
|
+
const values = context.get("value");
|
|
136
|
+
let closestIndex = -1;
|
|
137
|
+
let minDistance = 1e400;
|
|
138
|
+
for (let i = 0; i < values.length; i++) {
|
|
139
|
+
if (disabled.has(i)) continue;
|
|
140
|
+
const distance = Math.abs(values[i] - pointValue);
|
|
141
|
+
if (distance <= minDistance) {
|
|
142
|
+
closestIndex = i;
|
|
143
|
+
minDistance = distance;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return closestIndex === -1 ? -1 : selectMovableThumb(params, closestIndex, disabled);
|
|
147
|
+
}
|
|
148
|
+
/** Hand a press on stacked thumbs to the one that can move toward the pointer */
|
|
149
|
+
function resolveStackedThumb(values, index, pointValue, disabled = NO_DISABLED) {
|
|
150
|
+
const value = values[index];
|
|
151
|
+
const dir = pointValue < value ? -1 : pointValue > value ? 1 : 0;
|
|
152
|
+
if (dir === 0) return index;
|
|
153
|
+
let next = index;
|
|
154
|
+
while (values[next + dir] === value && !disabled.has(next + dir)) next += dir;
|
|
155
|
+
return next;
|
|
156
|
+
}
|
|
157
|
+
function selectMovableThumb(params, index, disabled = NO_DISABLED) {
|
|
158
|
+
const { context, prop } = params;
|
|
159
|
+
const values = context.get("value");
|
|
160
|
+
const max = prop("max");
|
|
161
|
+
if (values[index] === max) {
|
|
162
|
+
let movableIndex = index;
|
|
163
|
+
while (movableIndex > 0 && values[movableIndex - 1] === max && !disabled.has(movableIndex - 1)) movableIndex -= 1;
|
|
164
|
+
return movableIndex;
|
|
165
|
+
}
|
|
166
|
+
return index;
|
|
167
|
+
}
|
|
168
|
+
/** Resolves the `origin` prop to a percent position (0-100) within `min`-`max`. */
|
|
161
169
|
function getOriginPercent(origin, min, max) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
getRangeAtIndex,
|
|
190
|
-
increment,
|
|
191
|
-
normalizeValues,
|
|
192
|
-
resolveThumbCollision,
|
|
193
|
-
selectMovableThumb
|
|
170
|
+
if (typeof origin === "number") return clampValue(100 * getValuePercent(origin, min, max), 0, 100);
|
|
171
|
+
switch (origin) {
|
|
172
|
+
case "end": return 100;
|
|
173
|
+
case "center": return 50;
|
|
174
|
+
default: return 0;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function getFillSpan(params) {
|
|
178
|
+
const { computed, prop } = params;
|
|
179
|
+
const percents = computed("valuePercent");
|
|
180
|
+
if (percents.length > 1) return [percents[0], percents[percents.length - 1]];
|
|
181
|
+
const origin = getOriginPercent(prop("origin"), prop("min"), prop("max"));
|
|
182
|
+
const value = percents[0];
|
|
183
|
+
return value < origin ? [value, origin] : [origin, value];
|
|
184
|
+
}
|
|
185
|
+
function getThumbExtent(params) {
|
|
186
|
+
const { context, prop } = params;
|
|
187
|
+
if (prop("thumbAlignment") !== "contain") return 0;
|
|
188
|
+
const size = context.get("thumbSize");
|
|
189
|
+
return (prop("orientation") === "vertical" ? size?.height : size?.width) ?? 0;
|
|
190
|
+
}
|
|
191
|
+
const toThumbSize = (size) => {
|
|
192
|
+
if (size == null) return null;
|
|
193
|
+
return typeof size === "number" ? {
|
|
194
|
+
width: size,
|
|
195
|
+
height: size
|
|
196
|
+
} : size;
|
|
194
197
|
};
|
|
198
|
+
export { constrainValue, getClosestIndex, getFillSpan, getOriginPercent, getRangeAtIndex, getThumbExtent, normalizeValues, resolveStackedThumb, resolveThumbCollision, selectMovableThumb, stepThumb, toThumbSize };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/slider",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.4",
|
|
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": "2.0.0-next.
|
|
34
|
-
"@zag-js/core": "2.0.0-next.
|
|
35
|
-
"@zag-js/dom-query": "2.0.0-next.
|
|
36
|
-
"@zag-js/utils": "2.0.0-next.
|
|
37
|
-
"@zag-js/types": "2.0.0-next.
|
|
33
|
+
"@zag-js/anatomy": "2.0.0-next.4",
|
|
34
|
+
"@zag-js/core": "2.0.0-next.4",
|
|
35
|
+
"@zag-js/dom-query": "2.0.0-next.4",
|
|
36
|
+
"@zag-js/utils": "2.0.0-next.4",
|
|
37
|
+
"@zag-js/types": "2.0.0-next.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"clean-package": "2.2.0"
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"./package.json": "./package.json"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
|
-
"build": "
|
|
70
|
+
"build": "tsdown",
|
|
71
71
|
"lint": "oxlint src",
|
|
72
72
|
"typecheck": "tsc --noEmit"
|
|
73
73
|
}
|