@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,25 +1,22 @@
1
- import { Params } from '@zag-js/core';
2
- import { SliderSchema, ThumbCollisionBehavior } from './slider.types.mjs';
3
- import '@zag-js/types';
4
-
1
+ import { Params } from "@zag-js/core";
2
+ import { SliderSchema, ThumbCollisionBehavior } from "./slider.types.mjs";
5
3
  type Ctx = Params<SliderSchema>;
6
4
  interface ThumbCollisionResult {
7
- values: number[];
8
- index: number;
9
- swapped: boolean;
5
+ values: number[];
6
+ index: number;
7
+ swapped: boolean;
10
8
  }
11
- declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
12
- declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
13
- declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
14
- min: number;
15
- max: number;
16
- value: number;
9
+ export declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
10
+ export declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
11
+ export declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
12
+ min: number;
13
+ max: number;
14
+ value: number;
17
15
  };
18
- declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
19
- declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
20
- declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
21
- declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
22
- declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
23
- declare function assignArray(current: number[], next: number[]): void;
24
-
25
- export { assignArray, constrainValue, decrement, getClosestIndex, getRangeAtIndex, increment, normalizeValues, resolveThumbCollision, selectMovableThumb };
16
+ export declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
17
+ export declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
18
+ export declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
19
+ export declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
20
+ export declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
21
+ export declare function assignArray(current: number[], next: number[]): void;
22
+ export {};
@@ -1,25 +1,22 @@
1
- import { Params } from '@zag-js/core';
2
- import { SliderSchema, ThumbCollisionBehavior } from './slider.types.js';
3
- import '@zag-js/types';
4
-
1
+ import { Params } from "@zag-js/core";
2
+ import { SliderSchema, ThumbCollisionBehavior } from "./slider.types.js";
5
3
  type Ctx = Params<SliderSchema>;
6
4
  interface ThumbCollisionResult {
7
- values: number[];
8
- index: number;
9
- swapped: boolean;
5
+ values: number[];
6
+ index: number;
7
+ swapped: boolean;
10
8
  }
11
- declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
12
- declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
13
- declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
14
- min: number;
15
- max: number;
16
- value: number;
9
+ export declare function resolveThumbCollision(behavior: ThumbCollisionBehavior, index: number, value: number, values: number[], min: number, max: number, step: number, minStepsBetweenThumbs: number, startValue?: number): ThumbCollisionResult;
10
+ export declare function normalizeValues(params: Ctx, nextValues: number[]): number[];
11
+ export declare function getRangeAtIndex(params: Pick<Ctx, "context" | "prop">, index: number): {
12
+ min: number;
13
+ max: number;
14
+ value: number;
17
15
  };
18
- declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
19
- declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
20
- declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
21
- declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
22
- declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
23
- declare function assignArray(current: number[], next: number[]): void;
24
-
25
- export { assignArray, constrainValue, decrement, getClosestIndex, getRangeAtIndex, increment, normalizeValues, resolveThumbCollision, selectMovableThumb };
16
+ export declare function constrainValue(params: Pick<Ctx, "context" | "prop">, value: number, index: number): number;
17
+ export declare function decrement(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
18
+ export declare function increment(params: Pick<Ctx, "context" | "prop">, index?: number, step?: number): number[];
19
+ export declare function getClosestIndex(params: Pick<Ctx, "context" | "prop">, pointValue: number): number;
20
+ export declare function selectMovableThumb(params: Pick<Ctx, "context" | "prop">, index: number): number;
21
+ export declare function assignArray(current: number[], next: number[]): void;
22
+ export {};
@@ -1,197 +1,173 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/slider.utils.ts
21
- var slider_utils_exports = {};
22
- __export(slider_utils_exports, {
23
- assignArray: () => assignArray,
24
- constrainValue: () => constrainValue,
25
- decrement: () => decrement,
26
- getClosestIndex: () => getClosestIndex,
27
- getRangeAtIndex: () => getRangeAtIndex,
28
- increment: () => increment,
29
- normalizeValues: () => normalizeValues,
30
- resolveThumbCollision: () => resolveThumbCollision,
31
- selectMovableThumb: () => selectMovableThumb
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
32
4
  });
33
- module.exports = __toCommonJS(slider_utils_exports);
34
- var import_utils = require("@zag-js/utils");
5
+ let _zag_js_utils = require("@zag-js/utils");
6
+ // src/slider.utils.ts
35
7
  function getThumbBounds(ctx) {
36
- const { index, values, min, max, gap } = ctx;
37
- const prevThumb = values[index - 1];
38
- const nextThumb = values[index + 1];
39
- return {
40
- min: prevThumb != null ? prevThumb + gap : min,
41
- max: nextThumb != null ? nextThumb - gap : max
42
- };
8
+ const { index, values, min, max, gap } = ctx;
9
+ const prevThumb = values[index - 1];
10
+ const nextThumb = values[index + 1];
11
+ return {
12
+ min: prevThumb != null ? prevThumb + gap : min,
13
+ max: nextThumb != null ? nextThumb - gap : max
14
+ };
43
15
  }
44
16
  function round(value) {
45
- return Math.round(value * 1e10) / 1e10;
17
+ return Math.round(value * 1e10) / 1e10;
46
18
  }
47
19
  function handleNone(ctx) {
48
- const { index, value, values } = ctx;
49
- const bounds = getThumbBounds(ctx);
50
- const nextValues = values.slice();
51
- nextValues[index] = round((0, import_utils.clampValue)(value, bounds.min, bounds.max));
52
- return { values: nextValues, index, swapped: false };
20
+ const { index, value, values } = ctx;
21
+ const bounds = getThumbBounds(ctx);
22
+ const nextValues = values.slice();
23
+ nextValues[index] = round((0, _zag_js_utils.clampValue)(value, bounds.min, bounds.max));
24
+ return {
25
+ values: nextValues,
26
+ index,
27
+ swapped: false
28
+ };
53
29
  }
54
30
  function handlePush(ctx) {
55
- const { index, value, values, min, max, gap } = ctx;
56
- const nextValues = values.slice();
57
- const absoluteMin = min + index * gap;
58
- const absoluteMax = max - (values.length - 1 - index) * gap;
59
- nextValues[index] = round((0, import_utils.clampValue)(value, absoluteMin, absoluteMax));
60
- for (let i = index + 1; i < values.length; i++) {
61
- const minAllowed = nextValues[i - 1] + gap;
62
- if (nextValues[i] < minAllowed) {
63
- nextValues[i] = round(minAllowed);
64
- }
65
- }
66
- for (let i = index - 1; i >= 0; i--) {
67
- const maxAllowed = nextValues[i + 1] - gap;
68
- if (nextValues[i] > maxAllowed) {
69
- nextValues[i] = round(maxAllowed);
70
- }
71
- }
72
- return { values: nextValues, index, swapped: false };
31
+ const { index, value, values, min, max, gap } = ctx;
32
+ const nextValues = values.slice();
33
+ const absoluteMin = min + index * gap;
34
+ const absoluteMax = max - (values.length - 1 - index) * gap;
35
+ nextValues[index] = round((0, _zag_js_utils.clampValue)(value, absoluteMin, absoluteMax));
36
+ for (let i = index + 1; i < values.length; i++) {
37
+ const minAllowed = nextValues[i - 1] + gap;
38
+ if (nextValues[i] < minAllowed) nextValues[i] = round(minAllowed);
39
+ }
40
+ for (let i = index - 1; i >= 0; i--) {
41
+ const maxAllowed = nextValues[i + 1] - gap;
42
+ if (nextValues[i] > maxAllowed) nextValues[i] = round(maxAllowed);
43
+ }
44
+ return {
45
+ values: nextValues,
46
+ index,
47
+ swapped: false
48
+ };
73
49
  }
74
50
  function handleSwap(ctx, startValue) {
75
- const { index, value, values, gap } = ctx;
76
- const prevThumb = values[index - 1];
77
- const nextThumb = values[index + 1];
78
- const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
79
- const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
80
- if (!crossingNext && !crossingPrev) {
81
- return handleNone(ctx);
82
- }
83
- const swapIndex = crossingNext ? index + 1 : index - 1;
84
- const nextValues = values.slice();
85
- const newCtx = { ...ctx, index: swapIndex };
86
- const bounds = getThumbBounds(newCtx);
87
- nextValues[swapIndex] = round((0, import_utils.clampValue)(value, bounds.min, bounds.max));
88
- nextValues[index] = values[swapIndex];
89
- if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) {
90
- nextValues[index] = round(nextValues[swapIndex] - gap);
91
- } else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) {
92
- nextValues[index] = round(nextValues[swapIndex] + gap);
93
- }
94
- return { values: nextValues, index: swapIndex, swapped: true };
51
+ const { index, value, values, gap } = ctx;
52
+ const prevThumb = values[index - 1];
53
+ const nextThumb = values[index + 1];
54
+ const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
55
+ const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
56
+ if (!crossingNext && !crossingPrev) return handleNone(ctx);
57
+ const swapIndex = crossingNext ? index + 1 : index - 1;
58
+ const nextValues = values.slice();
59
+ const bounds = getThumbBounds({
60
+ ...ctx,
61
+ index: swapIndex
62
+ });
63
+ nextValues[swapIndex] = round((0, _zag_js_utils.clampValue)(value, bounds.min, bounds.max));
64
+ nextValues[index] = values[swapIndex];
65
+ if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) nextValues[index] = round(nextValues[swapIndex] - gap);
66
+ else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) nextValues[index] = round(nextValues[swapIndex] + gap);
67
+ return {
68
+ values: nextValues,
69
+ index: swapIndex,
70
+ swapped: true
71
+ };
95
72
  }
96
73
  function resolveThumbCollision(behavior, index, value, values, min, max, step, minStepsBetweenThumbs, startValue) {
97
- if (values.length === 1) {
98
- return { values: [round((0, import_utils.clampValue)(value, min, max))], index: 0, swapped: false };
99
- }
100
- const gap = step * minStepsBetweenThumbs;
101
- const ctx = { behavior, index, value, values, min, max, gap };
102
- switch (behavior) {
103
- case "push":
104
- return handlePush(ctx);
105
- case "swap":
106
- return handleSwap(ctx, startValue ?? values[index]);
107
- case "none":
108
- default:
109
- return handleNone(ctx);
110
- }
74
+ if (values.length === 1) return {
75
+ values: [round((0, _zag_js_utils.clampValue)(value, min, max))],
76
+ index: 0,
77
+ swapped: false
78
+ };
79
+ const ctx = {
80
+ behavior,
81
+ index,
82
+ value,
83
+ values,
84
+ min,
85
+ max,
86
+ gap: step * minStepsBetweenThumbs
87
+ };
88
+ switch (behavior) {
89
+ case "push": return handlePush(ctx);
90
+ case "swap": return handleSwap(ctx, startValue ?? values[index]);
91
+ default: return handleNone(ctx);
92
+ }
111
93
  }
112
94
  function normalizeValues(params, nextValues) {
113
- return nextValues.map((value, index) => {
114
- return constrainValue(params, value, index);
115
- });
95
+ return nextValues.map((value, index) => {
96
+ return constrainValue(params, value, index);
97
+ });
116
98
  }
117
99
  function getRangeAtIndex(params, index) {
118
- const { context, prop } = params;
119
- const step = prop("step") * prop("minStepsBetweenThumbs");
120
- return (0, import_utils.getValueRanges)(context.get("value"), prop("min"), prop("max"), step)[index];
100
+ const { context, prop } = params;
101
+ const step = prop("step") * prop("minStepsBetweenThumbs");
102
+ return (0, _zag_js_utils.getValueRanges)(context.get("value"), prop("min"), prop("max"), step)[index];
121
103
  }
122
104
  function constrainValue(params, value, index) {
123
- const { prop } = params;
124
- const range = getRangeAtIndex(params, index);
125
- const snapValue = (0, import_utils.snapValueToStep)(value, prop("min"), prop("max"), prop("step"));
126
- return (0, import_utils.clampValue)(snapValue, range.min, range.max);
105
+ const { prop } = params;
106
+ const range = getRangeAtIndex(params, index);
107
+ const snapValue = (0, _zag_js_utils.snapValueToStep)(value, prop("min"), prop("max"), prop("step"));
108
+ return (0, _zag_js_utils.clampValue)(snapValue, range.min, range.max);
127
109
  }
128
110
  function decrement(params, index, step) {
129
- const { context, prop } = params;
130
- const idx = index ?? context.get("focusedIndex");
131
- const range = getRangeAtIndex(params, idx);
132
- const nextValues = (0, import_utils.getPreviousStepValue)(idx, {
133
- ...range,
134
- step: step ?? prop("step"),
135
- values: context.get("value")
136
- });
137
- nextValues[idx] = (0, import_utils.clampValue)(nextValues[idx], range.min, range.max);
138
- return nextValues;
111
+ const { context, prop } = params;
112
+ const idx = index ?? context.get("focusedIndex");
113
+ const range = getRangeAtIndex(params, idx);
114
+ const nextValues = (0, _zag_js_utils.getPreviousStepValue)(idx, {
115
+ ...range,
116
+ step: step ?? prop("step"),
117
+ values: context.get("value")
118
+ });
119
+ nextValues[idx] = (0, _zag_js_utils.clampValue)(nextValues[idx], range.min, range.max);
120
+ return nextValues;
139
121
  }
140
122
  function increment(params, index, step) {
141
- const { context, prop } = params;
142
- const idx = index ?? context.get("focusedIndex");
143
- const range = getRangeAtIndex(params, idx);
144
- const nextValues = (0, import_utils.getNextStepValue)(idx, {
145
- ...range,
146
- step: step ?? prop("step"),
147
- values: context.get("value")
148
- });
149
- nextValues[idx] = (0, import_utils.clampValue)(nextValues[idx], range.min, range.max);
150
- return nextValues;
123
+ const { context, prop } = params;
124
+ const idx = index ?? context.get("focusedIndex");
125
+ const range = getRangeAtIndex(params, idx);
126
+ const nextValues = (0, _zag_js_utils.getNextStepValue)(idx, {
127
+ ...range,
128
+ step: step ?? prop("step"),
129
+ values: context.get("value")
130
+ });
131
+ nextValues[idx] = (0, _zag_js_utils.clampValue)(nextValues[idx], range.min, range.max);
132
+ return nextValues;
151
133
  }
152
134
  function getClosestIndex(params, pointValue) {
153
- const { context } = params;
154
- const values = context.get("value");
155
- let closestIndex = 0;
156
- let minDistance = Math.abs(values[0] - pointValue);
157
- for (let i = 1; i < values.length; i++) {
158
- const distance = Math.abs(values[i] - pointValue);
159
- if (distance <= minDistance) {
160
- closestIndex = i;
161
- minDistance = distance;
162
- }
163
- }
164
- return selectMovableThumb(params, closestIndex);
135
+ const { context } = params;
136
+ const values = context.get("value");
137
+ let closestIndex = 0;
138
+ let minDistance = Math.abs(values[0] - pointValue);
139
+ for (let i = 1; i < values.length; i++) {
140
+ const distance = Math.abs(values[i] - pointValue);
141
+ if (distance <= minDistance) {
142
+ closestIndex = i;
143
+ minDistance = distance;
144
+ }
145
+ }
146
+ return selectMovableThumb(params, closestIndex);
165
147
  }
166
148
  function selectMovableThumb(params, index) {
167
- const { context, prop } = params;
168
- const values = context.get("value");
169
- const max = prop("max");
170
- const thumbValue = values[index];
171
- if (thumbValue === max) {
172
- let movableIndex = index;
173
- while (movableIndex > 0 && values[movableIndex - 1] === max) {
174
- movableIndex -= 1;
175
- }
176
- return movableIndex;
177
- }
178
- return index;
149
+ const { context, prop } = params;
150
+ const values = context.get("value");
151
+ const max = prop("max");
152
+ if (values[index] === max) {
153
+ let movableIndex = index;
154
+ while (movableIndex > 0 && values[movableIndex - 1] === max) movableIndex -= 1;
155
+ return movableIndex;
156
+ }
157
+ return index;
179
158
  }
180
159
  function assignArray(current, next) {
181
- for (let i = 0; i < next.length; i++) {
182
- const value = next[i];
183
- current[i] = value;
184
- }
160
+ for (let i = 0; i < next.length; i++) {
161
+ const value = next[i];
162
+ current[i] = value;
163
+ }
185
164
  }
186
- // Annotate the CommonJS export names for ESM import in node:
187
- 0 && (module.exports = {
188
- assignArray,
189
- constrainValue,
190
- decrement,
191
- getClosestIndex,
192
- getRangeAtIndex,
193
- increment,
194
- normalizeValues,
195
- resolveThumbCollision,
196
- selectMovableThumb
197
- });
165
+ exports.assignArray = assignArray;
166
+ exports.constrainValue = constrainValue;
167
+ exports.decrement = decrement;
168
+ exports.getClosestIndex = getClosestIndex;
169
+ exports.getRangeAtIndex = getRangeAtIndex;
170
+ exports.increment = increment;
171
+ exports.normalizeValues = normalizeValues;
172
+ exports.resolveThumbCollision = resolveThumbCollision;
173
+ exports.selectMovableThumb = selectMovableThumb;