@zag-js/slider 1.34.0 → 1.35.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.js CHANGED
@@ -1,1060 +1,39 @@
1
- 'use strict';
2
-
3
- var anatomy$1 = require('@zag-js/anatomy');
4
- var domQuery = require('@zag-js/dom-query');
5
- var utils = require('@zag-js/utils');
6
- var core = require('@zag-js/core');
7
- var types = require('@zag-js/types');
8
-
9
- // src/slider.anatomy.ts
10
- var anatomy = anatomy$1.createAnatomy("slider").parts(
11
- "root",
12
- "label",
13
- "thumb",
14
- "valueText",
15
- "track",
16
- "range",
17
- "control",
18
- "markerGroup",
19
- "marker",
20
- "draggingIndicator"
21
- );
22
- var parts = anatomy.build();
23
- var getRootId = (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`;
24
- var getThumbId = (ctx, index) => ctx.ids?.thumb?.(index) ?? `slider:${ctx.id}:thumb:${index}`;
25
- var getHiddenInputId = (ctx, index) => ctx.ids?.hiddenInput?.(index) ?? `slider:${ctx.id}:input:${index}`;
26
- var getControlId = (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`;
27
- var getTrackId = (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:track`;
28
- var getRangeId = (ctx) => ctx.ids?.range ?? `slider:${ctx.id}:range`;
29
- var getLabelId = (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`;
30
- var getValueTextId = (ctx) => ctx.ids?.valueText ?? `slider:${ctx.id}:value-text`;
31
- var getMarkerId = (ctx, value) => ctx.ids?.marker?.(value) ?? `slider:${ctx.id}:marker:${value}`;
32
- var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
33
- var getThumbEl = (ctx, index) => ctx.getById(getThumbId(ctx, index));
34
- var getThumbEls = (ctx) => domQuery.queryAll(getControlEl(ctx), "[role=slider]");
35
- var getFirstThumbEl = (ctx) => getThumbEls(ctx)[0];
36
- var getHiddenInputEl = (ctx, index) => ctx.getById(getHiddenInputId(ctx, index));
37
- var getControlEl = (ctx) => ctx.getById(getControlId(ctx));
38
- var getThumbInset = (thumbSize, thumbAlignment, orientation) => {
39
- const isContain = thumbAlignment === "contain";
40
- const isVertical = orientation === "vertical";
41
- return isContain ? (isVertical ? thumbSize?.height ?? 0 : thumbSize?.width ?? 0) / 2 : 0;
42
- };
43
- var getPointValue = (params, point) => {
44
- const { context, prop, scope, refs } = params;
45
- const controlEl = getControlEl(scope);
46
- if (!controlEl) return;
47
- const offset = refs.get("thumbDragOffset");
48
- const adjustedPoint = {
49
- x: point.x - (offset?.x ?? 0),
50
- y: point.y - (offset?.y ?? 0)
51
- };
52
- const thumbInset = getThumbInset(context.get("thumbSize"), prop("thumbAlignment"), prop("orientation"));
53
- const relativePoint = getRelativePointWithInset(adjustedPoint, controlEl, thumbInset);
54
- const percent = relativePoint.getPercentValue({
55
- orientation: prop("orientation"),
56
- dir: prop("dir"),
57
- inverted: { y: true }
58
- });
59
- return utils.getPercentValue(percent, prop("min"), prop("max"), prop("step"));
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 });
60
9
  };
61
- function getRelativePointWithInset(point, element, inset) {
62
- const { left, top, width, height } = element.getBoundingClientRect();
63
- const effectiveWidth = width - inset * 2;
64
- const effectiveHeight = height - inset * 2;
65
- const effectiveLeft = left + inset;
66
- const effectiveTop = top + inset;
67
- const offset = {
68
- x: point.x - effectiveLeft,
69
- y: point.y - effectiveTop
70
- };
71
- const percent = {
72
- x: effectiveWidth > 0 ? utils.clampPercent(offset.x / effectiveWidth) : 0,
73
- y: effectiveHeight > 0 ? utils.clampPercent(offset.y / effectiveHeight) : 0
74
- };
75
- function getPercentValue3(options = {}) {
76
- const { dir = "ltr", orientation = "horizontal", inverted } = options;
77
- const invertX = typeof inverted === "object" ? inverted.x : inverted;
78
- const invertY = typeof inverted === "object" ? inverted.y : inverted;
79
- if (orientation === "horizontal") {
80
- return dir === "rtl" || invertX ? 1 - percent.x : percent.x;
81
- }
82
- return invertY ? 1 - percent.y : percent.y;
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 });
83
15
  }
84
- return { offset, percent, getPercentValue: getPercentValue3 };
85
- }
86
- var dispatchChangeEvent = (ctx, value) => {
87
- value.forEach((value2, index) => {
88
- const inputEl = getHiddenInputEl(ctx, index);
89
- if (!inputEl) return;
90
- domQuery.dispatchInputValueEvent(inputEl, { value: value2 });
91
- });
16
+ return to;
92
17
  };
93
- var getOffsetRect = (el) => ({
94
- left: el?.offsetLeft ?? 0,
95
- top: el?.offsetTop ?? 0,
96
- width: el?.offsetWidth ?? 0,
97
- height: el?.offsetHeight ?? 0
98
- });
99
- function getBounds(value) {
100
- const firstValue = value[0];
101
- const lastThumb = value[value.length - 1];
102
- return [firstValue, lastThumb];
103
- }
104
- function getRangeOffsets(params) {
105
- const { prop, computed } = params;
106
- const valuePercent = computed("valuePercent");
107
- const [firstPercent, lastPercent] = getBounds(valuePercent);
108
- if (valuePercent.length === 1) {
109
- if (prop("origin") === "center") {
110
- const isNegative = valuePercent[0] < 50;
111
- const start = isNegative ? `${valuePercent[0]}%` : "50%";
112
- const end = isNegative ? "50%" : `${100 - valuePercent[0]}%`;
113
- return { start, end };
114
- }
115
- if (prop("origin") === "end") {
116
- return { start: `${lastPercent}%`, end: "0%" };
117
- }
118
- return { start: "0%", end: `${100 - lastPercent}%` };
119
- }
120
- return { start: `${firstPercent}%`, end: `${100 - lastPercent}%` };
121
- }
122
- function getRangeStyle(params) {
123
- const { computed } = params;
124
- const isVertical = computed("isVertical");
125
- const isRtl = computed("isRtl");
126
- if (isVertical) {
127
- return {
128
- position: "absolute",
129
- bottom: "var(--slider-range-start)",
130
- top: "var(--slider-range-end)"
131
- };
132
- }
133
- return {
134
- position: "absolute",
135
- [isRtl ? "right" : "left"]: "var(--slider-range-start)",
136
- [isRtl ? "left" : "right"]: "var(--slider-range-end)"
137
- };
138
- }
139
- function getVerticalThumbOffset(params, value) {
140
- const { context, prop } = params;
141
- const { height = 0 } = context.get("thumbSize") ?? {};
142
- const getValue = utils.getValueTransformer([prop("min"), prop("max")], [-height / 2, height / 2]);
143
- return parseFloat(getValue(value).toFixed(2));
144
- }
145
- function getHorizontalThumbOffset(params, value) {
146
- const { computed, context, prop } = params;
147
- const { width = 0 } = context.get("thumbSize") ?? {};
148
- const isRtl = computed("isRtl");
149
- if (isRtl) {
150
- const getValue2 = utils.getValueTransformer([prop("max"), prop("min")], [-width / 2, width / 2]);
151
- return -1 * parseFloat(getValue2(value).toFixed(2));
152
- }
153
- const getValue = utils.getValueTransformer([prop("min"), prop("max")], [-width / 2, width / 2]);
154
- return parseFloat(getValue(value).toFixed(2));
155
- }
156
- function getOffset(params, percent, value) {
157
- const { computed, prop } = params;
158
- if (prop("thumbAlignment") === "center") return `${percent}%`;
159
- const offset = computed("isVertical") ? getVerticalThumbOffset(params, value) : getHorizontalThumbOffset(params, value);
160
- return `calc(${percent}% - ${offset}px)`;
161
- }
162
- function getThumbOffset(params, value) {
163
- const { prop } = params;
164
- const percent = utils.getValuePercent(value, prop("min"), prop("max")) * 100;
165
- return getOffset(params, percent, value);
166
- }
167
- function getVisibility(params) {
168
- const { computed, prop } = params;
169
- let visibility = "visible";
170
- if (prop("thumbAlignment") === "contain" && !computed("hasMeasuredThumbSize")) {
171
- visibility = "hidden";
172
- }
173
- return visibility;
174
- }
175
- function getThumbStyle(params, index) {
176
- const { computed, context } = params;
177
- const placementProp = computed("isVertical") ? "bottom" : "insetInlineStart";
178
- const focusedIndex = context.get("focusedIndex");
179
- return {
180
- visibility: getVisibility(params),
181
- position: "absolute",
182
- transform: "var(--slider-thumb-transform)",
183
- [placementProp]: `var(--slider-thumb-offset-${index})`,
184
- zIndex: focusedIndex === index ? 1 : void 0
185
- };
186
- }
187
- function getControlStyle() {
188
- return {
189
- touchAction: "none",
190
- userSelect: "none",
191
- WebkitUserSelect: "none",
192
- position: "relative"
193
- };
194
- }
195
- function getRootStyle(params) {
196
- const { context, computed } = params;
197
- const isVertical = computed("isVertical");
198
- const isRtl = computed("isRtl");
199
- const range = getRangeOffsets(params);
200
- const thumbSize = context.get("thumbSize");
201
- const offsetStyles = context.get("value").reduce((styles, value, index) => {
202
- const offset = getThumbOffset(params, value);
203
- return { ...styles, [`--slider-thumb-offset-${index}`]: offset };
204
- }, {});
205
- return {
206
- ...offsetStyles,
207
- "--slider-thumb-width": utils.toPx(thumbSize?.width),
208
- "--slider-thumb-height": utils.toPx(thumbSize?.height),
209
- "--slider-thumb-transform": isVertical ? "translateY(50%)" : isRtl ? "translateX(50%)" : "translateX(-50%)",
210
- "--slider-range-start": range.start,
211
- "--slider-range-end": range.end
212
- };
213
- }
214
- function getMarkerStyle(params, value) {
215
- const { computed } = params;
216
- const isHorizontal = computed("isHorizontal");
217
- const isRtl = computed("isRtl");
218
- return {
219
- visibility: getVisibility(params),
220
- position: "absolute",
221
- pointerEvents: "none",
222
- [isHorizontal ? "insetInlineStart" : "bottom"]: getThumbOffset(params, value),
223
- translate: "var(--translate-x) var(--translate-y)",
224
- "--translate-x": isHorizontal ? isRtl ? "50%" : "-50%" : "0%",
225
- "--translate-y": !isHorizontal ? "50%" : "0%"
226
- };
227
- }
228
- function getMarkerGroupStyle() {
229
- return {
230
- userSelect: "none",
231
- WebkitUserSelect: "none",
232
- pointerEvents: "none",
233
- position: "relative"
234
- };
235
- }
236
- function getThumbBounds(ctx) {
237
- const { index, values, min, max, gap } = ctx;
238
- const prevThumb = values[index - 1];
239
- const nextThumb = values[index + 1];
240
- return {
241
- min: prevThumb != null ? prevThumb + gap : min,
242
- max: nextThumb != null ? nextThumb - gap : max
243
- };
244
- }
245
- function round(value) {
246
- return Math.round(value * 1e10) / 1e10;
247
- }
248
- function handleNone(ctx) {
249
- const { index, value, values } = ctx;
250
- const bounds = getThumbBounds(ctx);
251
- const nextValues = values.slice();
252
- nextValues[index] = round(utils.clampValue(value, bounds.min, bounds.max));
253
- return { values: nextValues, index, swapped: false };
254
- }
255
- function handlePush(ctx) {
256
- const { index, value, values, min, max, gap } = ctx;
257
- const nextValues = values.slice();
258
- const absoluteMin = min + index * gap;
259
- const absoluteMax = max - (values.length - 1 - index) * gap;
260
- nextValues[index] = round(utils.clampValue(value, absoluteMin, absoluteMax));
261
- for (let i = index + 1; i < values.length; i++) {
262
- const minAllowed = nextValues[i - 1] + gap;
263
- if (nextValues[i] < minAllowed) {
264
- nextValues[i] = round(minAllowed);
265
- }
266
- }
267
- for (let i = index - 1; i >= 0; i--) {
268
- const maxAllowed = nextValues[i + 1] - gap;
269
- if (nextValues[i] > maxAllowed) {
270
- nextValues[i] = round(maxAllowed);
271
- }
272
- }
273
- return { values: nextValues, index, swapped: false };
274
- }
275
- function handleSwap(ctx, startValue) {
276
- const { index, value, values, gap } = ctx;
277
- const prevThumb = values[index - 1];
278
- const nextThumb = values[index + 1];
279
- const crossingNext = nextThumb != null && value >= nextThumb && value > startValue;
280
- const crossingPrev = prevThumb != null && value <= prevThumb && value < startValue;
281
- if (!crossingNext && !crossingPrev) {
282
- return handleNone(ctx);
283
- }
284
- const swapIndex = crossingNext ? index + 1 : index - 1;
285
- const nextValues = values.slice();
286
- const newCtx = { ...ctx, index: swapIndex };
287
- const bounds = getThumbBounds(newCtx);
288
- nextValues[swapIndex] = round(utils.clampValue(value, bounds.min, bounds.max));
289
- nextValues[index] = values[swapIndex];
290
- if (crossingNext && nextValues[index] > nextValues[swapIndex] - gap) {
291
- nextValues[index] = round(nextValues[swapIndex] - gap);
292
- } else if (crossingPrev && nextValues[index] < nextValues[swapIndex] + gap) {
293
- nextValues[index] = round(nextValues[swapIndex] + gap);
294
- }
295
- return { values: nextValues, index: swapIndex, swapped: true };
296
- }
297
- function resolveThumbCollision(behavior, index, value, values, min, max, step, minStepsBetweenThumbs, startValue) {
298
- if (values.length === 1) {
299
- return { values: [round(utils.clampValue(value, min, max))], index: 0, swapped: false };
300
- }
301
- const gap = step * minStepsBetweenThumbs;
302
- const ctx = { behavior, index, value, values, min, max, gap };
303
- switch (behavior) {
304
- case "push":
305
- return handlePush(ctx);
306
- case "swap":
307
- return handleSwap(ctx, startValue ?? values[index]);
308
- case "none":
309
- default:
310
- return handleNone(ctx);
311
- }
312
- }
313
- function normalizeValues(params, nextValues) {
314
- return nextValues.map((value, index) => {
315
- return constrainValue(params, value, index);
316
- });
317
- }
318
- function getRangeAtIndex(params, index) {
319
- const { context, prop } = params;
320
- const step = prop("step") * prop("minStepsBetweenThumbs");
321
- return utils.getValueRanges(context.get("value"), prop("min"), prop("max"), step)[index];
322
- }
323
- function constrainValue(params, value, index) {
324
- const { prop } = params;
325
- const range = getRangeAtIndex(params, index);
326
- const snapValue = utils.snapValueToStep(value, prop("min"), prop("max"), prop("step"));
327
- return utils.clampValue(snapValue, range.min, range.max);
328
- }
329
- function decrement(params, index, step) {
330
- const { context, prop } = params;
331
- const idx = index ?? context.get("focusedIndex");
332
- const range = getRangeAtIndex(params, idx);
333
- const nextValues = utils.getPreviousStepValue(idx, {
334
- ...range,
335
- step: step ?? prop("step"),
336
- values: context.get("value")
337
- });
338
- nextValues[idx] = utils.clampValue(nextValues[idx], range.min, range.max);
339
- return nextValues;
340
- }
341
- function increment(params, index, step) {
342
- const { context, prop } = params;
343
- const idx = index ?? context.get("focusedIndex");
344
- const range = getRangeAtIndex(params, idx);
345
- const nextValues = utils.getNextStepValue(idx, {
346
- ...range,
347
- step: step ?? prop("step"),
348
- values: context.get("value")
349
- });
350
- nextValues[idx] = utils.clampValue(nextValues[idx], range.min, range.max);
351
- return nextValues;
352
- }
353
- function getClosestIndex(params, pointValue) {
354
- const { context } = params;
355
- const values = context.get("value");
356
- let closestIndex = 0;
357
- let minDistance = Math.abs(values[0] - pointValue);
358
- for (let i = 1; i < values.length; i++) {
359
- const distance = Math.abs(values[i] - pointValue);
360
- if (distance <= minDistance) {
361
- closestIndex = i;
362
- minDistance = distance;
363
- }
364
- }
365
- return selectMovableThumb(params, closestIndex);
366
- }
367
- function selectMovableThumb(params, index) {
368
- const { context, prop } = params;
369
- const values = context.get("value");
370
- const max = prop("max");
371
- const thumbValue = values[index];
372
- if (thumbValue === max) {
373
- let movableIndex = index;
374
- while (movableIndex > 0 && values[movableIndex - 1] === max) {
375
- movableIndex -= 1;
376
- }
377
- return movableIndex;
378
- }
379
- return index;
380
- }
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
381
20
 
382
- // src/slider.connect.ts
383
- function connect(service, normalize2) {
384
- const { state, send, context, prop, computed, scope } = service;
385
- const ariaLabel = prop("aria-label");
386
- const ariaLabelledBy = prop("aria-labelledby");
387
- const sliderValue = context.get("value");
388
- const focusedIndex = context.get("focusedIndex");
389
- const focused = state.matches("focus");
390
- const dragging = state.matches("dragging");
391
- const disabled = computed("isDisabled");
392
- const invalid = prop("invalid");
393
- const interactive = computed("isInteractive");
394
- const isHorizontal = prop("orientation") === "horizontal";
395
- const isVertical = prop("orientation") === "vertical";
396
- function getValuePercentFn(value) {
397
- return utils.getValuePercent(value, prop("min"), prop("max"));
398
- }
399
- function getPercentValueFn(percent) {
400
- return utils.getPercentValue(percent, prop("min"), prop("max"), prop("step"));
401
- }
402
- return {
403
- value: sliderValue,
404
- dragging,
405
- focused,
406
- setValue(value) {
407
- send({ type: "SET_VALUE", value });
408
- },
409
- getThumbValue(index) {
410
- return sliderValue[index];
411
- },
412
- setThumbValue(index, value) {
413
- send({ type: "SET_VALUE", index, value });
414
- },
415
- getValuePercent: getValuePercentFn,
416
- getPercentValue: getPercentValueFn,
417
- getThumbPercent(index) {
418
- return getValuePercentFn(sliderValue[index]);
419
- },
420
- setThumbPercent(index, percent) {
421
- const value = getPercentValueFn(percent);
422
- send({ type: "SET_VALUE", index, value });
423
- },
424
- getThumbMin(index) {
425
- return getRangeAtIndex(service, index).min;
426
- },
427
- getThumbMax(index) {
428
- return getRangeAtIndex(service, index).max;
429
- },
430
- increment(index) {
431
- send({ type: "INCREMENT", index });
432
- },
433
- decrement(index) {
434
- send({ type: "DECREMENT", index });
435
- },
436
- focus() {
437
- if (!interactive) return;
438
- send({ type: "FOCUS", index: 0 });
439
- },
440
- getLabelProps() {
441
- return normalize2.label({
442
- ...parts.label.attrs,
443
- dir: prop("dir"),
444
- "data-disabled": domQuery.dataAttr(disabled),
445
- "data-orientation": prop("orientation"),
446
- "data-invalid": domQuery.dataAttr(invalid),
447
- "data-dragging": domQuery.dataAttr(dragging),
448
- "data-focus": domQuery.dataAttr(focused),
449
- id: getLabelId(scope),
450
- htmlFor: getHiddenInputId(scope, 0),
451
- onClick(event) {
452
- if (!interactive) return;
453
- event.preventDefault();
454
- getFirstThumbEl(scope)?.focus();
455
- },
456
- style: {
457
- userSelect: "none",
458
- WebkitUserSelect: "none"
459
- }
460
- });
461
- },
462
- getRootProps() {
463
- return normalize2.element({
464
- ...parts.root.attrs,
465
- "data-disabled": domQuery.dataAttr(disabled),
466
- "data-orientation": prop("orientation"),
467
- "data-dragging": domQuery.dataAttr(dragging),
468
- "data-invalid": domQuery.dataAttr(invalid),
469
- "data-focus": domQuery.dataAttr(focused),
470
- id: getRootId(scope),
471
- dir: prop("dir"),
472
- style: getRootStyle(service)
473
- });
474
- },
475
- getValueTextProps() {
476
- return normalize2.element({
477
- ...parts.valueText.attrs,
478
- dir: prop("dir"),
479
- "data-disabled": domQuery.dataAttr(disabled),
480
- "data-orientation": prop("orientation"),
481
- "data-invalid": domQuery.dataAttr(invalid),
482
- "data-focus": domQuery.dataAttr(focused),
483
- id: getValueTextId(scope)
484
- });
485
- },
486
- getTrackProps() {
487
- return normalize2.element({
488
- ...parts.track.attrs,
489
- dir: prop("dir"),
490
- id: getTrackId(scope),
491
- "data-disabled": domQuery.dataAttr(disabled),
492
- "data-invalid": domQuery.dataAttr(invalid),
493
- "data-dragging": domQuery.dataAttr(dragging),
494
- "data-orientation": prop("orientation"),
495
- "data-focus": domQuery.dataAttr(focused),
496
- style: { position: "relative" }
497
- });
498
- },
499
- getThumbProps(props2) {
500
- const { index = 0, name } = props2;
501
- const value = sliderValue[index];
502
- const range = getRangeAtIndex(service, index);
503
- const valueText = prop("getAriaValueText")?.({ value, index });
504
- const _ariaLabel = Array.isArray(ariaLabel) ? ariaLabel[index] : ariaLabel;
505
- const _ariaLabelledBy = Array.isArray(ariaLabelledBy) ? ariaLabelledBy[index] : ariaLabelledBy;
506
- return normalize2.element({
507
- ...parts.thumb.attrs,
508
- dir: prop("dir"),
509
- "data-index": index,
510
- "data-name": name,
511
- id: getThumbId(scope, index),
512
- "data-disabled": domQuery.dataAttr(disabled),
513
- "data-orientation": prop("orientation"),
514
- "data-focus": domQuery.dataAttr(focused && focusedIndex === index),
515
- "data-dragging": domQuery.dataAttr(dragging && focusedIndex === index),
516
- draggable: false,
517
- "aria-disabled": domQuery.ariaAttr(disabled),
518
- "aria-label": _ariaLabel,
519
- "aria-labelledby": _ariaLabelledBy ?? getLabelId(scope),
520
- "aria-orientation": prop("orientation"),
521
- "aria-valuemax": range.max,
522
- "aria-valuemin": range.min,
523
- "aria-valuenow": sliderValue[index],
524
- "aria-valuetext": valueText,
525
- role: "slider",
526
- tabIndex: disabled ? void 0 : 0,
527
- style: getThumbStyle(service, index),
528
- onPointerDown(event) {
529
- if (!interactive) return;
530
- if (!domQuery.isLeftClick(event)) return;
531
- const thumbEl = event.currentTarget;
532
- const rect = thumbEl.getBoundingClientRect();
533
- const midpoint = {
534
- x: rect.left + rect.width / 2,
535
- y: rect.top + rect.height / 2
536
- };
537
- const offset = {
538
- x: event.clientX - midpoint.x,
539
- y: event.clientY - midpoint.y
540
- };
541
- send({ type: "THUMB_POINTER_DOWN", index, offset });
542
- event.stopPropagation();
543
- },
544
- onBlur() {
545
- if (!interactive) return;
546
- send({ type: "BLUR" });
547
- },
548
- onFocus() {
549
- if (!interactive) return;
550
- send({ type: "FOCUS", index });
551
- },
552
- onKeyDown(event) {
553
- if (event.defaultPrevented) return;
554
- if (!interactive) return;
555
- const step = domQuery.getEventStep(event) * prop("step");
556
- const keyMap = {
557
- ArrowUp() {
558
- if (isHorizontal) return;
559
- send({ type: "ARROW_INC", step, src: "ArrowUp" });
560
- },
561
- ArrowDown() {
562
- if (isHorizontal) return;
563
- send({ type: "ARROW_DEC", step, src: "ArrowDown" });
564
- },
565
- ArrowLeft() {
566
- if (isVertical) return;
567
- send({ type: "ARROW_DEC", step, src: "ArrowLeft" });
568
- },
569
- ArrowRight() {
570
- if (isVertical) return;
571
- send({ type: "ARROW_INC", step, src: "ArrowRight" });
572
- },
573
- PageUp() {
574
- send({ type: "ARROW_INC", step, src: "PageUp" });
575
- },
576
- PageDown() {
577
- send({ type: "ARROW_DEC", step, src: "PageDown" });
578
- },
579
- Home() {
580
- send({ type: "HOME" });
581
- },
582
- End() {
583
- send({ type: "END" });
584
- }
585
- };
586
- const key = domQuery.getEventKey(event, {
587
- dir: prop("dir"),
588
- orientation: prop("orientation")
589
- });
590
- const exec = keyMap[key];
591
- if (exec) {
592
- exec(event);
593
- event.preventDefault();
594
- event.stopPropagation();
595
- }
596
- }
597
- });
598
- },
599
- getHiddenInputProps(props2) {
600
- const { index = 0, name } = props2;
601
- return normalize2.input({
602
- name: name ?? (prop("name") ? prop("name") + (sliderValue.length > 1 ? "[]" : "") : void 0),
603
- form: prop("form"),
604
- type: "text",
605
- hidden: true,
606
- defaultValue: sliderValue[index],
607
- id: getHiddenInputId(scope, index)
608
- });
609
- },
610
- getRangeProps() {
611
- return normalize2.element({
612
- id: getRangeId(scope),
613
- ...parts.range.attrs,
614
- dir: prop("dir"),
615
- "data-dragging": domQuery.dataAttr(dragging),
616
- "data-focus": domQuery.dataAttr(focused),
617
- "data-invalid": domQuery.dataAttr(invalid),
618
- "data-disabled": domQuery.dataAttr(disabled),
619
- "data-orientation": prop("orientation"),
620
- style: getRangeStyle(service)
621
- });
622
- },
623
- getControlProps() {
624
- return normalize2.element({
625
- ...parts.control.attrs,
626
- dir: prop("dir"),
627
- id: getControlId(scope),
628
- "data-dragging": domQuery.dataAttr(dragging),
629
- "data-disabled": domQuery.dataAttr(disabled),
630
- "data-orientation": prop("orientation"),
631
- "data-invalid": domQuery.dataAttr(invalid),
632
- "data-focus": domQuery.dataAttr(focused),
633
- style: getControlStyle(),
634
- onPointerDown(event) {
635
- if (!interactive) return;
636
- if (!domQuery.isLeftClick(event)) return;
637
- if (domQuery.isModifierKey(event)) return;
638
- const point = domQuery.getEventPoint(event);
639
- send({ type: "POINTER_DOWN", point });
640
- event.preventDefault();
641
- event.stopPropagation();
642
- }
643
- });
644
- },
645
- getMarkerGroupProps() {
646
- return normalize2.element({
647
- ...parts.markerGroup.attrs,
648
- role: "presentation",
649
- dir: prop("dir"),
650
- "aria-hidden": true,
651
- "data-orientation": prop("orientation"),
652
- style: getMarkerGroupStyle()
653
- });
654
- },
655
- getMarkerProps(props2) {
656
- const style = getMarkerStyle(service, props2.value);
657
- let markerState;
658
- if (props2.value < utils.first(sliderValue)) {
659
- markerState = "under-value";
660
- } else if (props2.value > utils.last(sliderValue)) {
661
- markerState = "over-value";
662
- } else {
663
- markerState = "at-value";
664
- }
665
- return normalize2.element({
666
- ...parts.marker.attrs,
667
- id: getMarkerId(scope, props2.value),
668
- role: "presentation",
669
- dir: prop("dir"),
670
- "data-orientation": prop("orientation"),
671
- "data-value": props2.value,
672
- "data-disabled": domQuery.dataAttr(disabled),
673
- "data-state": markerState,
674
- style
675
- });
676
- },
677
- getDraggingIndicatorProps(props2) {
678
- const { index = 0 } = props2;
679
- const isDragging = index === focusedIndex && dragging;
680
- return normalize2.element({
681
- ...parts.draggingIndicator.attrs,
682
- role: "presentation",
683
- dir: prop("dir"),
684
- hidden: !isDragging,
685
- "data-orientation": prop("orientation"),
686
- "data-state": isDragging ? "open" : "closed",
687
- style: getThumbStyle(service, index)
688
- });
689
- }
690
- };
691
- }
692
- var isEqualSize = (a, b) => {
693
- return a?.width === b?.width && a?.height === b?.height;
694
- };
695
- var normalize = (value, min, max, step, minStepsBetweenThumbs) => {
696
- const ranges = utils.getValueRanges(value, min, max, minStepsBetweenThumbs * step);
697
- return ranges.map((range) => {
698
- const snapValue = utils.snapValueToStep(range.value, range.min, range.max, step);
699
- const rangeValue = utils.clampValue(snapValue, range.min, range.max);
700
- if (!utils.isValueWithinRange(rangeValue, min, max)) {
701
- throw new Error(
702
- "[zag-js/slider] The configured `min`, `max`, `step` or `minStepsBetweenThumbs` values are invalid"
703
- );
704
- }
705
- return rangeValue;
706
- });
707
- };
708
- var machine = core.createMachine({
709
- props({ props: props2 }) {
710
- const min = props2.min ?? 0;
711
- const max = props2.max ?? 100;
712
- const step = props2.step ?? 1;
713
- const defaultValue = props2.defaultValue ?? [min];
714
- const minStepsBetweenThumbs = props2.minStepsBetweenThumbs ?? 0;
715
- return {
716
- dir: "ltr",
717
- thumbAlignment: "contain",
718
- origin: "start",
719
- orientation: "horizontal",
720
- thumbCollisionBehavior: "none",
721
- minStepsBetweenThumbs,
722
- ...props2,
723
- defaultValue: normalize(defaultValue, min, max, step, minStepsBetweenThumbs),
724
- value: props2.value ? normalize(props2.value, min, max, step, minStepsBetweenThumbs) : void 0,
725
- max,
726
- step,
727
- min
728
- };
729
- },
730
- initialState() {
731
- return "idle";
732
- },
733
- context({ prop, bindable, getContext }) {
734
- return {
735
- thumbSize: bindable(() => ({
736
- defaultValue: prop("thumbSize") || null
737
- })),
738
- value: bindable(() => ({
739
- defaultValue: prop("defaultValue"),
740
- value: prop("value"),
741
- isEqual: utils.isEqual,
742
- hash(a) {
743
- return a.join(",");
744
- },
745
- onChange(value) {
746
- prop("onValueChange")?.({ value });
747
- }
748
- })),
749
- focusedIndex: bindable(() => ({
750
- defaultValue: -1,
751
- onChange(value) {
752
- const ctx = getContext();
753
- prop("onFocusChange")?.({ focusedIndex: value, value: ctx.get("value") });
754
- }
755
- })),
756
- fieldsetDisabled: bindable(() => ({
757
- defaultValue: false
758
- }))
759
- };
760
- },
761
- refs() {
762
- return {
763
- thumbDragOffset: null,
764
- thumbDragStartValue: null
765
- };
766
- },
767
- computed: {
768
- isHorizontal: ({ prop }) => prop("orientation") === "horizontal",
769
- isVertical: ({ prop }) => prop("orientation") === "vertical",
770
- isRtl: ({ prop }) => prop("orientation") === "horizontal" && prop("dir") === "rtl",
771
- isDisabled: ({ context, prop }) => !!prop("disabled") || context.get("fieldsetDisabled"),
772
- isInteractive: ({ prop, computed }) => !(prop("readOnly") || computed("isDisabled")),
773
- hasMeasuredThumbSize: ({ context }) => context.get("thumbSize") != null,
774
- valuePercent: core.memo(
775
- ({ context, prop }) => [context.get("value"), prop("min"), prop("max")],
776
- ([value, min, max]) => value.map((value2) => 100 * utils.getValuePercent(value2, min, max))
777
- )
778
- },
779
- watch({ track, action, context, computed, send }) {
780
- track([() => context.hash("value")], () => {
781
- action(["syncInputElements", "dispatchChangeEvent"]);
782
- });
783
- track([() => computed("isDisabled")], () => {
784
- if (computed("isDisabled")) {
785
- send({ type: "POINTER_CANCEL" });
786
- }
787
- });
788
- },
789
- effects: ["trackFormControlState", "trackThumbSize"],
790
- on: {
791
- SET_VALUE: [
792
- {
793
- guard: "hasIndex",
794
- actions: ["setValueAtIndex", "invokeOnChangeEnd"]
795
- },
796
- {
797
- actions: ["setValue", "invokeOnChangeEnd"]
798
- }
799
- ],
800
- INCREMENT: {
801
- actions: ["incrementThumbAtIndex", "invokeOnChangeEnd"]
802
- },
803
- DECREMENT: {
804
- actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
805
- }
806
- },
807
- states: {
808
- idle: {
809
- on: {
810
- POINTER_DOWN: {
811
- target: "dragging",
812
- actions: ["setClosestThumbIndex", "setThumbDragStartValue", "setPointerValue", "focusActiveThumb"]
813
- },
814
- FOCUS: {
815
- target: "focus",
816
- actions: ["setFocusedIndex"]
817
- },
818
- THUMB_POINTER_DOWN: {
819
- target: "dragging",
820
- actions: ["setFocusedIndex", "setThumbDragOffset", "setThumbDragStartValue", "focusActiveThumb"]
821
- }
822
- }
823
- },
824
- focus: {
825
- entry: ["focusActiveThumb"],
826
- on: {
827
- POINTER_DOWN: {
828
- target: "dragging",
829
- actions: ["setClosestThumbIndex", "setThumbDragStartValue", "setPointerValue", "focusActiveThumb"]
830
- },
831
- THUMB_POINTER_DOWN: {
832
- target: "dragging",
833
- actions: ["setFocusedIndex", "setThumbDragOffset", "setThumbDragStartValue", "focusActiveThumb"]
834
- },
835
- ARROW_DEC: {
836
- actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
837
- },
838
- ARROW_INC: {
839
- actions: ["incrementThumbAtIndex", "invokeOnChangeEnd"]
840
- },
841
- HOME: {
842
- actions: ["setFocusedThumbToMin", "invokeOnChangeEnd"]
843
- },
844
- END: {
845
- actions: ["setFocusedThumbToMax", "invokeOnChangeEnd"]
846
- },
847
- BLUR: {
848
- target: "idle",
849
- actions: ["clearFocusedIndex"]
850
- }
851
- }
852
- },
853
- dragging: {
854
- entry: ["focusActiveThumb"],
855
- effects: ["trackPointerMove"],
856
- on: {
857
- POINTER_UP: {
858
- target: "focus",
859
- actions: ["invokeOnChangeEnd", "clearThumbDragOffset", "clearThumbDragStartValue"]
860
- },
861
- POINTER_MOVE: {
862
- actions: ["setPointerValue"]
863
- },
864
- POINTER_CANCEL: {
865
- target: "idle",
866
- actions: ["clearFocusedIndex", "clearThumbDragOffset", "clearThumbDragStartValue"]
867
- }
868
- }
869
- }
870
- },
871
- implementations: {
872
- guards: {
873
- hasIndex: ({ event }) => event.index != null
874
- },
875
- effects: {
876
- trackFormControlState({ context, scope }) {
877
- return domQuery.trackFormControl(getRootEl(scope), {
878
- onFieldsetDisabledChange(disabled) {
879
- context.set("fieldsetDisabled", disabled);
880
- },
881
- onFormReset() {
882
- context.set("value", context.initial("value"));
883
- }
884
- });
885
- },
886
- trackPointerMove({ scope, send }) {
887
- return domQuery.trackPointerMove(scope.getDoc(), {
888
- onPointerMove(info) {
889
- send({ type: "POINTER_MOVE", point: info.point });
890
- },
891
- onPointerUp() {
892
- send({ type: "POINTER_UP" });
893
- }
894
- });
895
- },
896
- trackThumbSize({ context, scope, prop }) {
897
- if (prop("thumbAlignment") !== "contain" || prop("thumbSize")) return;
898
- const exec = (el) => {
899
- const rect = getOffsetRect(el);
900
- const size = utils.pick(rect, ["width", "height"]);
901
- if (isEqualSize(context.get("thumbSize"), size)) return;
902
- context.set("thumbSize", size);
903
- };
904
- const thumbEls = getThumbEls(scope);
905
- thumbEls.forEach(exec);
906
- const cleanups = thumbEls.map((el) => domQuery.resizeObserverBorderBox.observe(el, () => exec(el)));
907
- return utils.callAll(...cleanups);
908
- }
909
- },
910
- actions: {
911
- dispatchChangeEvent({ context, scope }) {
912
- dispatchChangeEvent(scope, context.get("value"));
913
- },
914
- syncInputElements({ context, scope }) {
915
- context.get("value").forEach((value, index) => {
916
- const inputEl = getHiddenInputEl(scope, index);
917
- domQuery.setElementValue(inputEl, value.toString());
918
- });
919
- },
920
- invokeOnChangeEnd({ prop, context }) {
921
- queueMicrotask(() => {
922
- prop("onValueChangeEnd")?.({ value: context.get("value") });
923
- });
924
- },
925
- setClosestThumbIndex(params) {
926
- const { context, event } = params;
927
- const pointValue = getPointValue(params, event.point);
928
- if (pointValue == null) return;
929
- const focusedIndex = getClosestIndex(params, pointValue);
930
- context.set("focusedIndex", focusedIndex);
931
- },
932
- setFocusedIndex(params) {
933
- const { context, event } = params;
934
- const movableIndex = selectMovableThumb(params, event.index);
935
- context.set("focusedIndex", movableIndex);
936
- },
937
- clearFocusedIndex({ context }) {
938
- context.set("focusedIndex", -1);
939
- },
940
- setThumbDragOffset(params) {
941
- const { refs, event } = params;
942
- refs.set("thumbDragOffset", event.offset ?? null);
943
- },
944
- clearThumbDragOffset({ refs }) {
945
- refs.set("thumbDragOffset", null);
946
- },
947
- setThumbDragStartValue({ refs, context }) {
948
- refs.set("thumbDragStartValue", context.get("value").slice());
949
- },
950
- clearThumbDragStartValue({ refs }) {
951
- refs.set("thumbDragStartValue", null);
952
- },
953
- setPointerValue(params) {
954
- queueMicrotask(() => {
955
- const { context, event, prop, refs } = params;
956
- const pointValue = getPointValue(params, event.point);
957
- if (pointValue == null) return;
958
- const focusedIndex = context.get("focusedIndex");
959
- const startValues = refs.get("thumbDragStartValue");
960
- const result = resolveThumbCollision(
961
- prop("thumbCollisionBehavior"),
962
- focusedIndex,
963
- pointValue,
964
- context.get("value"),
965
- prop("min"),
966
- prop("max"),
967
- prop("step"),
968
- prop("minStepsBetweenThumbs"),
969
- startValues?.[focusedIndex]
970
- );
971
- if (result.swapped) {
972
- context.set("focusedIndex", result.index);
973
- }
974
- context.set("value", result.values);
975
- });
976
- },
977
- focusActiveThumb({ scope, context }) {
978
- domQuery.raf(() => {
979
- const thumbEl = getThumbEl(scope, context.get("focusedIndex"));
980
- thumbEl?.focus({ preventScroll: true });
981
- });
982
- },
983
- decrementThumbAtIndex(params) {
984
- const { context, event } = params;
985
- const value = decrement(params, event.index, event.step);
986
- context.set("value", value);
987
- },
988
- incrementThumbAtIndex(params) {
989
- const { context, event } = params;
990
- const value = increment(params, event.index, event.step);
991
- context.set("value", value);
992
- },
993
- setFocusedThumbToMin(params) {
994
- const { context } = params;
995
- const index = context.get("focusedIndex");
996
- const { min } = getRangeAtIndex(params, index);
997
- context.set("value", (prev) => utils.setValueAtIndex(prev, index, min));
998
- },
999
- setFocusedThumbToMax(params) {
1000
- const { context } = params;
1001
- const index = context.get("focusedIndex");
1002
- const { max } = getRangeAtIndex(params, index);
1003
- context.set("value", (prev) => utils.setValueAtIndex(prev, index, max));
1004
- },
1005
- setValueAtIndex(params) {
1006
- const { context, event } = params;
1007
- const value = constrainValue(params, event.value, event.index);
1008
- context.set("value", (prev) => utils.setValueAtIndex(prev, event.index, value));
1009
- },
1010
- setValue(params) {
1011
- const { context, event } = params;
1012
- const value = normalizeValues(params, event.value);
1013
- context.set("value", value);
1014
- }
1015
- }
1016
- }
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ anatomy: () => import_slider.anatomy,
25
+ connect: () => import_slider2.connect,
26
+ machine: () => import_slider3.machine
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_slider = require("./slider.anatomy.cjs");
30
+ var import_slider2 = require("./slider.connect.cjs");
31
+ var import_slider3 = require("./slider.machine.cjs");
32
+ __reExport(index_exports, require("./slider.props.cjs"), module.exports);
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ anatomy,
36
+ connect,
37
+ machine,
38
+ ...require("./slider.props.cjs")
1017
39
  });
1018
- var props = types.createProps()([
1019
- "aria-label",
1020
- "aria-labelledby",
1021
- "dir",
1022
- "disabled",
1023
- "form",
1024
- "getAriaValueText",
1025
- "getRootNode",
1026
- "id",
1027
- "ids",
1028
- "invalid",
1029
- "max",
1030
- "min",
1031
- "minStepsBetweenThumbs",
1032
- "name",
1033
- "onFocusChange",
1034
- "onValueChange",
1035
- "onValueChangeEnd",
1036
- "orientation",
1037
- "origin",
1038
- "readOnly",
1039
- "step",
1040
- "thumbAlignment",
1041
- "thumbCollisionBehavior",
1042
- "thumbSize",
1043
- "value",
1044
- "defaultValue"
1045
- ]);
1046
- var splitProps = utils.createSplitProps(props);
1047
- var thumbProps = types.createProps()(["index", "name"]);
1048
- var splitThumbProps = utils.createSplitProps(thumbProps);
1049
- var markerProps = types.createProps()(["value"]);
1050
- var splitMarkerProps = utils.createSplitProps(markerProps);
1051
-
1052
- exports.anatomy = anatomy;
1053
- exports.connect = connect;
1054
- exports.machine = machine;
1055
- exports.markerProps = markerProps;
1056
- exports.props = props;
1057
- exports.splitMarkerProps = splitMarkerProps;
1058
- exports.splitProps = splitProps;
1059
- exports.splitThumbProps = splitThumbProps;
1060
- exports.thumbProps = thumbProps;