@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.
@@ -1,194 +1,198 @@
1
+ import { clampValue, getValuePercent, getValueRanges, setValueAtIndex, snapValueToStep } from "@zag-js/utils";
1
2
  // src/slider.utils.ts
2
- import {
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
- const { index, values, min, max, gap } = ctx;
12
- const prevThumb = values[index - 1];
13
- const nextThumb = values[index + 1];
14
- return {
15
- min: prevThumb != null ? prevThumb + gap : min,
16
- max: nextThumb != null ? nextThumb - gap : max
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
- return Math.round(value * 1e10) / 1e10;
14
+ return Math.round(value * 1e10) / 1e10;
21
15
  }
22
16
  function handleNone(ctx) {
23
- const { index, value, values } = ctx;
24
- const bounds = getThumbBounds(ctx);
25
- const nextValues = values.slice();
26
- nextValues[index] = round(clampValue(value, bounds.min, bounds.max));
27
- return { values: nextValues, index, swapped: false };
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
- const { index, value, values, min, max, gap } = ctx;
31
- const nextValues = values.slice();
32
- const absoluteMin = min + index * gap;
33
- const absoluteMax = max - (values.length - 1 - index) * gap;
34
- nextValues[index] = round(clampValue(value, absoluteMin, absoluteMax));
35
- for (let i = index + 1; i < values.length; i++) {
36
- const minAllowed = nextValues[i - 1] + gap;
37
- if (nextValues[i] < minAllowed) {
38
- nextValues[i] = round(minAllowed);
39
- }
40
- }
41
- for (let i = index - 1; i >= 0; i--) {
42
- const maxAllowed = nextValues[i + 1] - gap;
43
- if (nextValues[i] > maxAllowed) {
44
- nextValues[i] = round(maxAllowed);
45
- }
46
- }
47
- return { values: nextValues, index, swapped: false };
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
- const { index, value, values, gap } = ctx;
51
- const prevThumb = values[index - 1];
52
- const nextThumb = values[index + 1];
53
- const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
54
- const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
55
- if (!crossingNext && !crossingPrev) {
56
- return handleNone(ctx);
57
- }
58
- const swapIndex = crossingNext ? index + 1 : index - 1;
59
- const nextValues = values.slice();
60
- const newCtx = { ...ctx, index: swapIndex };
61
- const bounds = getThumbBounds(newCtx);
62
- nextValues[swapIndex] = round(clampValue(value, bounds.min, bounds.max));
63
- nextValues[index] = values[swapIndex];
64
- if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) {
65
- nextValues[index] = round(nextValues[swapIndex] - gap);
66
- } else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) {
67
- nextValues[index] = round(nextValues[swapIndex] + gap);
68
- }
69
- return { values: nextValues, index: swapIndex, swapped: true };
70
- }
71
- function resolveThumbCollision(behavior, index, value, values, min, max, step, minStepsBetweenThumbs, startValue) {
72
- if (values.length === 1) {
73
- return { values: [round(clampValue(value, min, max))], index: 0, swapped: false };
74
- }
75
- const gap = step * minStepsBetweenThumbs;
76
- const ctx = { behavior, index, value, values, min, max, gap };
77
- switch (behavior) {
78
- case "push":
79
- return handlePush(ctx);
80
- case "swap":
81
- return handleSwap(ctx, startValue ?? values[index]);
82
- case "none":
83
- default:
84
- return handleNone(ctx);
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
- return nextValues.map((value, index) => {
89
- return constrainValue(params, value, index);
90
- });
112
+ return nextValues.map((value, index) => {
113
+ return constrainValue(params, value, index);
114
+ });
91
115
  }
92
116
  function getRangeAtIndex(params, index) {
93
- const { context, prop } = params;
94
- const step = prop("step") * prop("minStepsBetweenThumbs");
95
- return getValueRanges(context.get("value"), prop("min"), prop("max"), step)[index];
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
- const { prop } = params;
99
- const range = getRangeAtIndex(params, index);
100
- const snapValue = snapValueToStep(value, prop("min"), prop("max"), prop("step"));
101
- return clampValue(snapValue, range.min, range.max);
102
- }
103
- function decrement(params, index, step) {
104
- const { context, prop } = params;
105
- const idx = index ?? context.get("focusedIndex");
106
- const range = getRangeAtIndex(params, idx);
107
- const nextValues = getPreviousStepValue(idx, {
108
- ...range,
109
- step: step ?? prop("step"),
110
- values: context.get("value")
111
- });
112
- nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
113
- return nextValues;
114
- }
115
- function increment(params, index, step) {
116
- const { context, prop } = params;
117
- const idx = index ?? context.get("focusedIndex");
118
- const range = getRangeAtIndex(params, idx);
119
- const nextValues = getNextStepValue(idx, {
120
- ...range,
121
- step: step ?? prop("step"),
122
- values: context.get("value")
123
- });
124
- nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
125
- return nextValues;
126
- }
127
- function getClosestIndex(params, pointValue) {
128
- const { context } = params;
129
- const values = context.get("value");
130
- let closestIndex = 0;
131
- let minDistance = Math.abs(values[0] - pointValue);
132
- for (let i = 1; i < values.length; i++) {
133
- const distance = Math.abs(values[i] - pointValue);
134
- if (distance <= minDistance) {
135
- closestIndex = i;
136
- minDistance = distance;
137
- }
138
- }
139
- return selectMovableThumb(params, closestIndex);
140
- }
141
- function selectMovableThumb(params, index) {
142
- const { context, prop } = params;
143
- const values = context.get("value");
144
- const max = prop("max");
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
- if (typeof origin === "number") {
163
- return clampValue(100 * getValuePercent(origin, min, max), 0, 100);
164
- }
165
- switch (origin) {
166
- case "end":
167
- return 100;
168
- case "center":
169
- return 50;
170
- case "start":
171
- default:
172
- return 0;
173
- }
174
- }
175
- function getFillRange(valuePercent, originPercent) {
176
- const isBeforeOrigin = valuePercent < originPercent;
177
- return {
178
- start: isBeforeOrigin ? valuePercent : originPercent,
179
- end: isBeforeOrigin ? 100 - originPercent : 100 - valuePercent
180
- };
181
- }
182
- export {
183
- assignArray,
184
- constrainValue,
185
- decrement,
186
- getClosestIndex,
187
- getFillRange,
188
- getOriginPercent,
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.2",
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.2",
34
- "@zag-js/core": "2.0.0-next.2",
35
- "@zag-js/dom-query": "2.0.0-next.2",
36
- "@zag-js/utils": "2.0.0-next.2",
37
- "@zag-js/types": "2.0.0-next.2"
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": "tsup",
70
+ "build": "tsdown",
71
71
  "lint": "oxlint src",
72
72
  "typecheck": "tsc --noEmit"
73
73
  }