@zag-js/slider 1.47.0 → 1.49.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.
@@ -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
- 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
- };
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
- return Math.round(value * 1e10) / 1e10;
13
+ return Math.round(value * 1e10) / 1e10;
14
14
  }
15
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 };
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
- 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 };
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
- 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 };
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
- 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
- }
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
- return nextValues.map((value, index) => {
82
- return constrainValue(params, value, index);
83
- });
91
+ return nextValues.map((value, index) => {
92
+ return constrainValue(params, value, index);
93
+ });
84
94
  }
85
95
  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];
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
- 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);
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
- 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
+ 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
- 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
+ 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
- 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);
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
- 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;
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
- for (let i = 0; i < next.length; i++) {
150
- const value = next[i];
151
- current[i] = value;
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.47.0",
3
+ "version": "1.49.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.47.0",
34
- "@zag-js/core": "1.47.0",
35
- "@zag-js/dom-query": "1.47.0",
36
- "@zag-js/utils": "1.47.0",
37
- "@zag-js/types": "1.47.0"
33
+ "@zag-js/anatomy": "1.49.0",
34
+ "@zag-js/core": "1.49.0",
35
+ "@zag-js/dom-query": "1.49.0",
36
+ "@zag-js/utils": "1.49.0",
37
+ "@zag-js/types": "1.49.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": "tsup",
71
- "lint": "eslint src",
70
+ "build": "tsdown",
71
+ "lint": "oxlint src",
72
72
  "typecheck": "tsc --noEmit"
73
73
  }
74
74
  }