@zag-js/slider 0.50.0 → 0.51.1
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 +1 -856
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -837
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -1,838 +1,2 @@
|
|
|
1
|
-
// src/slider.anatomy.ts
|
|
2
|
-
import { createAnatomy } from "@zag-js/anatomy";
|
|
3
|
-
var anatomy = createAnatomy("slider").parts(
|
|
4
|
-
"root",
|
|
5
|
-
"label",
|
|
6
|
-
"thumb",
|
|
7
|
-
"valueText",
|
|
8
|
-
"track",
|
|
9
|
-
"range",
|
|
10
|
-
"control",
|
|
11
|
-
"markerGroup",
|
|
12
|
-
"marker"
|
|
13
|
-
);
|
|
14
|
-
var parts = anatomy.build();
|
|
15
|
-
|
|
16
|
-
// src/slider.connect.ts
|
|
17
|
-
import {
|
|
18
|
-
getEventKey,
|
|
19
|
-
getEventPoint,
|
|
20
|
-
getEventStep,
|
|
21
|
-
getNativeEvent,
|
|
22
|
-
isLeftClick,
|
|
23
|
-
isModifierKey
|
|
24
|
-
} from "@zag-js/dom-event";
|
|
25
|
-
import { ariaAttr, dataAttr } from "@zag-js/dom-query";
|
|
26
|
-
import { getPercentValue as getPercentValue2, getValuePercent as getValuePercent2 } from "@zag-js/numeric-range";
|
|
27
|
-
|
|
28
|
-
// src/slider.dom.ts
|
|
29
|
-
import { getRelativePoint } from "@zag-js/dom-event";
|
|
30
|
-
import { createScope, queryAll } from "@zag-js/dom-query";
|
|
31
|
-
import { dispatchInputValueEvent } from "@zag-js/form-utils";
|
|
32
|
-
import { getPercentValue } from "@zag-js/numeric-range";
|
|
33
|
-
|
|
34
|
-
// src/slider.style.ts
|
|
35
|
-
import { getValuePercent, getValueTransformer } from "@zag-js/numeric-range";
|
|
36
|
-
function getBounds(value) {
|
|
37
|
-
const firstValue = value[0];
|
|
38
|
-
const lastThumb = value[value.length - 1];
|
|
39
|
-
return [firstValue, lastThumb];
|
|
40
|
-
}
|
|
41
|
-
function getRangeOffsets(ctx) {
|
|
42
|
-
const [firstPercent, lastPercent] = getBounds(ctx.valuePercent);
|
|
43
|
-
if (ctx.valuePercent.length === 1) {
|
|
44
|
-
if (ctx.origin === "center") {
|
|
45
|
-
const isNegative = ctx.valuePercent[0] < 50;
|
|
46
|
-
const start = isNegative ? `${ctx.valuePercent[0]}%` : "50%";
|
|
47
|
-
const end = isNegative ? "50%" : `${100 - ctx.valuePercent[0]}%`;
|
|
48
|
-
return { start, end };
|
|
49
|
-
}
|
|
50
|
-
return { start: "0%", end: `${100 - lastPercent}%` };
|
|
51
|
-
}
|
|
52
|
-
return { start: `${firstPercent}%`, end: `${100 - lastPercent}%` };
|
|
53
|
-
}
|
|
54
|
-
function getRangeStyle(ctx) {
|
|
55
|
-
if (ctx.isVertical) {
|
|
56
|
-
return {
|
|
57
|
-
position: "absolute",
|
|
58
|
-
bottom: "var(--slider-range-start)",
|
|
59
|
-
top: "var(--slider-range-end)"
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
position: "absolute",
|
|
64
|
-
[ctx.isRtl ? "right" : "left"]: "var(--slider-range-start)",
|
|
65
|
-
[ctx.isRtl ? "left" : "right"]: "var(--slider-range-end)"
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
function getVerticalThumbOffset(ctx) {
|
|
69
|
-
const { height = 0 } = ctx.thumbSize ?? {};
|
|
70
|
-
const getValue = getValueTransformer([ctx.min, ctx.max], [-height / 2, height / 2]);
|
|
71
|
-
return parseFloat(getValue(ctx.value).toFixed(2));
|
|
72
|
-
}
|
|
73
|
-
function getHorizontalThumbOffset(ctx) {
|
|
74
|
-
const { width = 0 } = ctx.thumbSize ?? {};
|
|
75
|
-
if (ctx.isRtl) {
|
|
76
|
-
const getValue2 = getValueTransformer([ctx.max, ctx.min], [-width / 2, width / 2]);
|
|
77
|
-
return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
|
|
78
|
-
}
|
|
79
|
-
const getValue = getValueTransformer([ctx.min, ctx.max], [-width / 2, width / 2]);
|
|
80
|
-
return parseFloat(getValue(ctx.value).toFixed(2));
|
|
81
|
-
}
|
|
82
|
-
function getOffset(ctx, percent) {
|
|
83
|
-
if (ctx.thumbAlignment === "center")
|
|
84
|
-
return `${percent}%`;
|
|
85
|
-
const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
|
|
86
|
-
return `calc(${percent}% - ${offset}px)`;
|
|
87
|
-
}
|
|
88
|
-
function getThumbOffset(ctx) {
|
|
89
|
-
let percent = getValuePercent(ctx.value, ctx.min, ctx.max) * 100;
|
|
90
|
-
return getOffset(ctx, percent);
|
|
91
|
-
}
|
|
92
|
-
function getVisibility(ctx) {
|
|
93
|
-
let visibility = "visible";
|
|
94
|
-
if (ctx.thumbAlignment === "contain" && !ctx.hasMeasuredThumbSize) {
|
|
95
|
-
visibility = "hidden";
|
|
96
|
-
}
|
|
97
|
-
return visibility;
|
|
98
|
-
}
|
|
99
|
-
function getThumbStyle(ctx, index) {
|
|
100
|
-
const placementProp = ctx.isVertical ? "bottom" : "insetInlineStart";
|
|
101
|
-
return {
|
|
102
|
-
visibility: getVisibility(ctx),
|
|
103
|
-
position: "absolute",
|
|
104
|
-
transform: "var(--slider-thumb-transform)",
|
|
105
|
-
[placementProp]: `var(--slider-thumb-offset-${index})`
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function getControlStyle() {
|
|
109
|
-
return {
|
|
110
|
-
touchAction: "none",
|
|
111
|
-
userSelect: "none",
|
|
112
|
-
position: "relative"
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function getRootStyle(ctx) {
|
|
116
|
-
const range = getRangeOffsets(ctx);
|
|
117
|
-
const offsetStyles = ctx.value.reduce((styles, value, index) => {
|
|
118
|
-
const offset = getThumbOffset({ ...ctx, value });
|
|
119
|
-
return { ...styles, [`--slider-thumb-offset-${index}`]: offset };
|
|
120
|
-
}, {});
|
|
121
|
-
return {
|
|
122
|
-
...offsetStyles,
|
|
123
|
-
"--slider-thumb-transform": ctx.isVertical ? "translateY(50%)" : ctx.isRtl ? "translateX(50%)" : "translateX(-50%)",
|
|
124
|
-
"--slider-range-start": range.start,
|
|
125
|
-
"--slider-range-end": range.end
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
function getMarkerStyle(ctx, value) {
|
|
129
|
-
return {
|
|
130
|
-
// @ts-expect-error
|
|
131
|
-
visibility: getVisibility(ctx),
|
|
132
|
-
position: "absolute",
|
|
133
|
-
pointerEvents: "none",
|
|
134
|
-
// @ts-expect-error
|
|
135
|
-
[ctx.isHorizontal ? "insetInlineStart" : "bottom"]: getThumbOffset({ ...ctx, value }),
|
|
136
|
-
translate: "var(--tx) var(--ty)",
|
|
137
|
-
"--tx": ctx.isHorizontal ? ctx.isRtl ? "50%" : "-50%" : "0%",
|
|
138
|
-
"--ty": !ctx.isHorizontal ? "50%" : "0%"
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
function getMarkerGroupStyle() {
|
|
142
|
-
return {
|
|
143
|
-
userSelect: "none",
|
|
144
|
-
pointerEvents: "none",
|
|
145
|
-
position: "relative"
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
var styleGetterFns = {
|
|
149
|
-
getRootStyle,
|
|
150
|
-
getControlStyle,
|
|
151
|
-
getThumbStyle,
|
|
152
|
-
getRangeStyle,
|
|
153
|
-
getMarkerStyle,
|
|
154
|
-
getMarkerGroupStyle
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
// src/slider.dom.ts
|
|
158
|
-
var dom = createScope({
|
|
159
|
-
...styleGetterFns,
|
|
160
|
-
getRootId: (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`,
|
|
161
|
-
getThumbId: (ctx, index) => ctx.ids?.thumb?.(index) ?? `slider:${ctx.id}:thumb:${index}`,
|
|
162
|
-
getHiddenInputId: (ctx, index) => `slider:${ctx.id}:input:${index}`,
|
|
163
|
-
getControlId: (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`,
|
|
164
|
-
getTrackId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:track`,
|
|
165
|
-
getRangeId: (ctx) => ctx.ids?.range ?? `slider:${ctx.id}:range`,
|
|
166
|
-
getLabelId: (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`,
|
|
167
|
-
getValueTextId: (ctx) => ctx.ids?.valueText ?? `slider:${ctx.id}:valueText`,
|
|
168
|
-
getMarkerId: (ctx, value) => ctx.ids?.marker?.(value) ?? `slider:${ctx.id}:marker:${value}`,
|
|
169
|
-
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
170
|
-
getThumbEl: (ctx, index) => dom.getById(ctx, dom.getThumbId(ctx, index)),
|
|
171
|
-
getHiddenInputEl: (ctx, index) => dom.getById(ctx, dom.getHiddenInputId(ctx, index)),
|
|
172
|
-
getControlEl: (ctx) => dom.getById(ctx, dom.getControlId(ctx)),
|
|
173
|
-
getElements: (ctx) => queryAll(dom.getControlEl(ctx), "[role=slider]"),
|
|
174
|
-
getFirstEl: (ctx) => dom.getElements(ctx)[0],
|
|
175
|
-
getRangeEl: (ctx) => dom.getById(ctx, dom.getRangeId(ctx)),
|
|
176
|
-
getValueFromPoint(ctx, point) {
|
|
177
|
-
const controlEl = dom.getControlEl(ctx);
|
|
178
|
-
if (!controlEl)
|
|
179
|
-
return;
|
|
180
|
-
const relativePoint = getRelativePoint(point, controlEl);
|
|
181
|
-
const percent = relativePoint.getPercentValue({
|
|
182
|
-
orientation: ctx.orientation,
|
|
183
|
-
dir: ctx.dir,
|
|
184
|
-
inverted: { y: true }
|
|
185
|
-
});
|
|
186
|
-
return getPercentValue(percent, ctx.min, ctx.max, ctx.step);
|
|
187
|
-
},
|
|
188
|
-
dispatchChangeEvent(ctx) {
|
|
189
|
-
const valueArray = Array.from(ctx.value);
|
|
190
|
-
valueArray.forEach((value, index) => {
|
|
191
|
-
const inputEl = dom.getHiddenInputEl(ctx, index);
|
|
192
|
-
if (!inputEl)
|
|
193
|
-
return;
|
|
194
|
-
dispatchInputValueEvent(inputEl, { value });
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
// src/slider.utils.ts
|
|
200
|
-
import {
|
|
201
|
-
clampValue,
|
|
202
|
-
getClosestValueIndex,
|
|
203
|
-
getNextStepValue,
|
|
204
|
-
getPreviousStepValue,
|
|
205
|
-
getValueRanges,
|
|
206
|
-
snapValueToStep
|
|
207
|
-
} from "@zag-js/numeric-range";
|
|
208
|
-
function normalizeValues(ctx, nextValues) {
|
|
209
|
-
return nextValues.map((value, index, values) => {
|
|
210
|
-
return constrainValue({ ...ctx, value: values }, value, index);
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
function getRangeAtIndex(ctx, index) {
|
|
214
|
-
return getValueRanges(ctx.value, ctx.min, ctx.max, ctx.minStepsBetweenThumbs)[index];
|
|
215
|
-
}
|
|
216
|
-
function constrainValue(ctx, value, index) {
|
|
217
|
-
const range = getRangeAtIndex(ctx, index);
|
|
218
|
-
const snapValue = snapValueToStep(value, ctx.min, ctx.max, ctx.step);
|
|
219
|
-
return clampValue(snapValue, range.min, range.max);
|
|
220
|
-
}
|
|
221
|
-
function decrement(ctx, index, step) {
|
|
222
|
-
const idx = index ?? ctx.focusedIndex;
|
|
223
|
-
const range = getRangeAtIndex(ctx, idx);
|
|
224
|
-
const nextValues = getPreviousStepValue(idx, {
|
|
225
|
-
...range,
|
|
226
|
-
step: step ?? ctx.step,
|
|
227
|
-
values: ctx.value
|
|
228
|
-
});
|
|
229
|
-
nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
|
|
230
|
-
return nextValues;
|
|
231
|
-
}
|
|
232
|
-
function increment(ctx, index, step) {
|
|
233
|
-
const idx = index ?? ctx.focusedIndex;
|
|
234
|
-
const range = getRangeAtIndex(ctx, idx);
|
|
235
|
-
const nextValues = getNextStepValue(idx, {
|
|
236
|
-
...range,
|
|
237
|
-
step: step ?? ctx.step,
|
|
238
|
-
values: ctx.value
|
|
239
|
-
});
|
|
240
|
-
nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
|
|
241
|
-
return nextValues;
|
|
242
|
-
}
|
|
243
|
-
function getClosestIndex(ctx, pointValue) {
|
|
244
|
-
return getClosestValueIndex(ctx.value, pointValue);
|
|
245
|
-
}
|
|
246
|
-
function assignArray(current, next) {
|
|
247
|
-
for (let i = 0; i < next.length; i++) {
|
|
248
|
-
const value = next[i];
|
|
249
|
-
current[i] = value;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// src/slider.connect.ts
|
|
254
|
-
function connect(state, send, normalize) {
|
|
255
|
-
const ariaLabel = state.context["aria-label"];
|
|
256
|
-
const ariaLabelledBy = state.context["aria-labelledby"];
|
|
257
|
-
const sliderValue = state.context.value;
|
|
258
|
-
const focused = state.matches("focus");
|
|
259
|
-
const dragging = state.matches("dragging");
|
|
260
|
-
const disabled = state.context.isDisabled;
|
|
261
|
-
const invalid = state.context.invalid;
|
|
262
|
-
const interactive = state.context.isInteractive;
|
|
263
|
-
const isHorizontal = state.context.orientation === "horizontal";
|
|
264
|
-
const isVertical = state.context.orientation === "vertical";
|
|
265
|
-
function getValuePercentFn(value) {
|
|
266
|
-
return getValuePercent2(value, state.context.min, state.context.max);
|
|
267
|
-
}
|
|
268
|
-
function getPercentValueFn(percent) {
|
|
269
|
-
return getPercentValue2(percent, state.context.min, state.context.max, state.context.step);
|
|
270
|
-
}
|
|
271
|
-
return {
|
|
272
|
-
value: state.context.value,
|
|
273
|
-
dragging,
|
|
274
|
-
focused,
|
|
275
|
-
setValue(value) {
|
|
276
|
-
send({ type: "SET_VALUE", value });
|
|
277
|
-
},
|
|
278
|
-
getThumbValue(index) {
|
|
279
|
-
return sliderValue[index];
|
|
280
|
-
},
|
|
281
|
-
setThumbValue(index, value) {
|
|
282
|
-
send({ type: "SET_VALUE", index, value });
|
|
283
|
-
},
|
|
284
|
-
getValuePercent: getValuePercentFn,
|
|
285
|
-
getPercentValue: getPercentValueFn,
|
|
286
|
-
getThumbPercent(index) {
|
|
287
|
-
return getValuePercentFn(sliderValue[index]);
|
|
288
|
-
},
|
|
289
|
-
setThumbPercent(index, percent) {
|
|
290
|
-
const value = getPercentValueFn(percent);
|
|
291
|
-
send({ type: "SET_VALUE", index, value });
|
|
292
|
-
},
|
|
293
|
-
getThumbMin(index) {
|
|
294
|
-
return getRangeAtIndex(state.context, index).min;
|
|
295
|
-
},
|
|
296
|
-
getThumbMax(index) {
|
|
297
|
-
return getRangeAtIndex(state.context, index).max;
|
|
298
|
-
},
|
|
299
|
-
increment(index) {
|
|
300
|
-
send({ type: "INCREMENT", index });
|
|
301
|
-
},
|
|
302
|
-
decrement(index) {
|
|
303
|
-
send({ type: "DECREMENT", index });
|
|
304
|
-
},
|
|
305
|
-
focus() {
|
|
306
|
-
if (!interactive)
|
|
307
|
-
return;
|
|
308
|
-
send({ type: "FOCUS", index: 0 });
|
|
309
|
-
},
|
|
310
|
-
labelProps: normalize.label({
|
|
311
|
-
...parts.label.attrs,
|
|
312
|
-
dir: state.context.dir,
|
|
313
|
-
"data-disabled": dataAttr(disabled),
|
|
314
|
-
"data-orientation": state.context.orientation,
|
|
315
|
-
"data-invalid": dataAttr(invalid),
|
|
316
|
-
"data-focus": dataAttr(focused),
|
|
317
|
-
id: dom.getLabelId(state.context),
|
|
318
|
-
htmlFor: dom.getHiddenInputId(state.context, 0),
|
|
319
|
-
onClick(event) {
|
|
320
|
-
if (!interactive)
|
|
321
|
-
return;
|
|
322
|
-
event.preventDefault();
|
|
323
|
-
dom.getFirstEl(state.context)?.focus();
|
|
324
|
-
},
|
|
325
|
-
style: {
|
|
326
|
-
userSelect: "none"
|
|
327
|
-
}
|
|
328
|
-
}),
|
|
329
|
-
rootProps: normalize.element({
|
|
330
|
-
...parts.root.attrs,
|
|
331
|
-
"data-disabled": dataAttr(disabled),
|
|
332
|
-
"data-orientation": state.context.orientation,
|
|
333
|
-
"data-invalid": dataAttr(invalid),
|
|
334
|
-
"data-focus": dataAttr(focused),
|
|
335
|
-
id: dom.getRootId(state.context),
|
|
336
|
-
dir: state.context.dir,
|
|
337
|
-
style: dom.getRootStyle(state.context)
|
|
338
|
-
}),
|
|
339
|
-
valueTextProps: normalize.element({
|
|
340
|
-
...parts.valueText.attrs,
|
|
341
|
-
dir: state.context.dir,
|
|
342
|
-
"data-disabled": dataAttr(disabled),
|
|
343
|
-
"data-orientation": state.context.orientation,
|
|
344
|
-
"data-invalid": dataAttr(invalid),
|
|
345
|
-
"data-focus": dataAttr(focused),
|
|
346
|
-
id: dom.getValueTextId(state.context)
|
|
347
|
-
}),
|
|
348
|
-
trackProps: normalize.element({
|
|
349
|
-
...parts.track.attrs,
|
|
350
|
-
dir: state.context.dir,
|
|
351
|
-
id: dom.getTrackId(state.context),
|
|
352
|
-
"data-disabled": dataAttr(disabled),
|
|
353
|
-
"data-invalid": dataAttr(invalid),
|
|
354
|
-
"data-orientation": state.context.orientation,
|
|
355
|
-
"data-focus": dataAttr(focused),
|
|
356
|
-
style: { position: "relative" }
|
|
357
|
-
}),
|
|
358
|
-
getThumbProps(props2) {
|
|
359
|
-
const { index = 0, name } = props2;
|
|
360
|
-
const value = sliderValue[index];
|
|
361
|
-
const range = getRangeAtIndex(state.context, index);
|
|
362
|
-
const valueText = state.context.getAriaValueText?.({ value, index });
|
|
363
|
-
const _ariaLabel = Array.isArray(ariaLabel) ? ariaLabel[index] : ariaLabel;
|
|
364
|
-
const _ariaLabelledBy = Array.isArray(ariaLabelledBy) ? ariaLabelledBy[index] : ariaLabelledBy;
|
|
365
|
-
return normalize.element({
|
|
366
|
-
...parts.thumb.attrs,
|
|
367
|
-
dir: state.context.dir,
|
|
368
|
-
"data-index": index,
|
|
369
|
-
"data-name": name,
|
|
370
|
-
id: dom.getThumbId(state.context, index),
|
|
371
|
-
"data-disabled": dataAttr(disabled),
|
|
372
|
-
"data-orientation": state.context.orientation,
|
|
373
|
-
"data-focus": dataAttr(focused && state.context.focusedIndex === index),
|
|
374
|
-
draggable: false,
|
|
375
|
-
"aria-disabled": ariaAttr(disabled),
|
|
376
|
-
"aria-label": _ariaLabel,
|
|
377
|
-
"aria-labelledby": _ariaLabelledBy ?? dom.getLabelId(state.context),
|
|
378
|
-
"aria-orientation": state.context.orientation,
|
|
379
|
-
"aria-valuemax": range.max,
|
|
380
|
-
"aria-valuemin": range.min,
|
|
381
|
-
"aria-valuenow": sliderValue[index],
|
|
382
|
-
"aria-valuetext": valueText,
|
|
383
|
-
role: "slider",
|
|
384
|
-
tabIndex: disabled ? void 0 : 0,
|
|
385
|
-
style: dom.getThumbStyle(state.context, index),
|
|
386
|
-
onPointerDown(event) {
|
|
387
|
-
if (!interactive)
|
|
388
|
-
return;
|
|
389
|
-
send({ type: "THUMB_POINTER_DOWN", index });
|
|
390
|
-
event.stopPropagation();
|
|
391
|
-
},
|
|
392
|
-
onBlur() {
|
|
393
|
-
if (!interactive)
|
|
394
|
-
return;
|
|
395
|
-
send("BLUR");
|
|
396
|
-
},
|
|
397
|
-
onFocus() {
|
|
398
|
-
if (!interactive)
|
|
399
|
-
return;
|
|
400
|
-
send({ type: "FOCUS", index });
|
|
401
|
-
},
|
|
402
|
-
onKeyDown(event) {
|
|
403
|
-
if (event.defaultPrevented)
|
|
404
|
-
return;
|
|
405
|
-
if (!interactive)
|
|
406
|
-
return;
|
|
407
|
-
const step = getEventStep(event) * state.context.step;
|
|
408
|
-
const keyMap = {
|
|
409
|
-
ArrowUp() {
|
|
410
|
-
if (isHorizontal)
|
|
411
|
-
return;
|
|
412
|
-
send({ type: "ARROW_INC", step, src: "ArrowUp" });
|
|
413
|
-
},
|
|
414
|
-
ArrowDown() {
|
|
415
|
-
if (isHorizontal)
|
|
416
|
-
return;
|
|
417
|
-
send({ type: "ARROW_DEC", step, src: "ArrowDown" });
|
|
418
|
-
},
|
|
419
|
-
ArrowLeft() {
|
|
420
|
-
if (isVertical)
|
|
421
|
-
return;
|
|
422
|
-
send({ type: "ARROW_DEC", step, src: "ArrowLeft" });
|
|
423
|
-
},
|
|
424
|
-
ArrowRight() {
|
|
425
|
-
if (isVertical)
|
|
426
|
-
return;
|
|
427
|
-
send({ type: "ARROW_INC", step, src: "ArrowRight" });
|
|
428
|
-
},
|
|
429
|
-
PageUp() {
|
|
430
|
-
send({ type: "ARROW_INC", step, src: "PageUp" });
|
|
431
|
-
},
|
|
432
|
-
PageDown() {
|
|
433
|
-
send({ type: "ARROW_DEC", step, src: "PageDown" });
|
|
434
|
-
},
|
|
435
|
-
Home() {
|
|
436
|
-
send("HOME");
|
|
437
|
-
},
|
|
438
|
-
End() {
|
|
439
|
-
send("END");
|
|
440
|
-
}
|
|
441
|
-
};
|
|
442
|
-
const key = getEventKey(event, state.context);
|
|
443
|
-
const exec = keyMap[key];
|
|
444
|
-
if (exec) {
|
|
445
|
-
exec(event);
|
|
446
|
-
event.preventDefault();
|
|
447
|
-
event.stopPropagation();
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
},
|
|
452
|
-
getHiddenInputProps(props2) {
|
|
453
|
-
const { index = 0, name } = props2;
|
|
454
|
-
return normalize.input({
|
|
455
|
-
name: name ?? (state.context.name ? state.context.name + (state.context.value.length > 1 ? "[]" : "") : void 0),
|
|
456
|
-
form: state.context.form,
|
|
457
|
-
type: "text",
|
|
458
|
-
hidden: true,
|
|
459
|
-
defaultValue: state.context.value[index],
|
|
460
|
-
id: dom.getHiddenInputId(state.context, index)
|
|
461
|
-
});
|
|
462
|
-
},
|
|
463
|
-
rangeProps: normalize.element({
|
|
464
|
-
id: dom.getRangeId(state.context),
|
|
465
|
-
...parts.range.attrs,
|
|
466
|
-
dir: state.context.dir,
|
|
467
|
-
"data-focus": dataAttr(focused),
|
|
468
|
-
"data-invalid": dataAttr(invalid),
|
|
469
|
-
"data-disabled": dataAttr(disabled),
|
|
470
|
-
"data-orientation": state.context.orientation,
|
|
471
|
-
style: dom.getRangeStyle(state.context)
|
|
472
|
-
}),
|
|
473
|
-
controlProps: normalize.element({
|
|
474
|
-
...parts.control.attrs,
|
|
475
|
-
dir: state.context.dir,
|
|
476
|
-
id: dom.getControlId(state.context),
|
|
477
|
-
"data-disabled": dataAttr(disabled),
|
|
478
|
-
"data-orientation": state.context.orientation,
|
|
479
|
-
"data-invalid": dataAttr(invalid),
|
|
480
|
-
"data-focus": dataAttr(focused),
|
|
481
|
-
style: dom.getControlStyle(),
|
|
482
|
-
onPointerDown(event) {
|
|
483
|
-
if (!interactive)
|
|
484
|
-
return;
|
|
485
|
-
const evt = getNativeEvent(event);
|
|
486
|
-
if (!isLeftClick(evt) || isModifierKey(evt))
|
|
487
|
-
return;
|
|
488
|
-
const point = getEventPoint(evt);
|
|
489
|
-
send({ type: "POINTER_DOWN", point });
|
|
490
|
-
event.preventDefault();
|
|
491
|
-
event.stopPropagation();
|
|
492
|
-
}
|
|
493
|
-
}),
|
|
494
|
-
markerGroupProps: normalize.element({
|
|
495
|
-
...parts.markerGroup.attrs,
|
|
496
|
-
role: "presentation",
|
|
497
|
-
dir: state.context.dir,
|
|
498
|
-
"aria-hidden": true,
|
|
499
|
-
"data-orientation": state.context.orientation,
|
|
500
|
-
style: dom.getMarkerGroupStyle()
|
|
501
|
-
}),
|
|
502
|
-
getMarkerProps(props2) {
|
|
503
|
-
const style = dom.getMarkerStyle(state.context, props2.value);
|
|
504
|
-
let markerState;
|
|
505
|
-
const first = state.context.value[0];
|
|
506
|
-
const last = state.context.value[state.context.value.length - 1];
|
|
507
|
-
if (props2.value < first) {
|
|
508
|
-
markerState = "under-value";
|
|
509
|
-
} else if (props2.value > last) {
|
|
510
|
-
markerState = "over-value";
|
|
511
|
-
} else {
|
|
512
|
-
markerState = "at-value";
|
|
513
|
-
}
|
|
514
|
-
return normalize.element({
|
|
515
|
-
...parts.marker.attrs,
|
|
516
|
-
id: dom.getMarkerId(state.context, props2.value),
|
|
517
|
-
role: "presentation",
|
|
518
|
-
dir: state.context.dir,
|
|
519
|
-
"data-orientation": state.context.orientation,
|
|
520
|
-
"data-value": props2.value,
|
|
521
|
-
"data-disabled": dataAttr(disabled),
|
|
522
|
-
"data-state": markerState,
|
|
523
|
-
style
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
// src/slider.machine.ts
|
|
530
|
-
import { createMachine } from "@zag-js/core";
|
|
531
|
-
import { trackPointerMove } from "@zag-js/dom-event";
|
|
532
|
-
import { raf } from "@zag-js/dom-query";
|
|
533
|
-
import { trackElementsSize } from "@zag-js/element-size";
|
|
534
|
-
import { trackFormControl } from "@zag-js/form-utils";
|
|
535
|
-
import { getValuePercent as getValuePercent3 } from "@zag-js/numeric-range";
|
|
536
|
-
import { compact, isEqual } from "@zag-js/utils";
|
|
537
|
-
var isEqualSize = (a, b) => {
|
|
538
|
-
return a?.width === b?.width && a?.height === b?.height;
|
|
539
|
-
};
|
|
540
|
-
function machine(userContext) {
|
|
541
|
-
const ctx = compact(userContext);
|
|
542
|
-
return createMachine(
|
|
543
|
-
{
|
|
544
|
-
id: "slider",
|
|
545
|
-
initial: "idle",
|
|
546
|
-
context: {
|
|
547
|
-
thumbSize: null,
|
|
548
|
-
thumbAlignment: "contain",
|
|
549
|
-
min: 0,
|
|
550
|
-
max: 100,
|
|
551
|
-
step: 1,
|
|
552
|
-
value: [0],
|
|
553
|
-
origin: "start",
|
|
554
|
-
orientation: "horizontal",
|
|
555
|
-
dir: "ltr",
|
|
556
|
-
minStepsBetweenThumbs: 0,
|
|
557
|
-
disabled: false,
|
|
558
|
-
...ctx,
|
|
559
|
-
focusedIndex: -1,
|
|
560
|
-
fieldsetDisabled: false
|
|
561
|
-
},
|
|
562
|
-
computed: {
|
|
563
|
-
isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
|
|
564
|
-
isVertical: (ctx2) => ctx2.orientation === "vertical",
|
|
565
|
-
isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
|
|
566
|
-
isDisabled: (ctx2) => !!ctx2.disabled || ctx2.fieldsetDisabled,
|
|
567
|
-
isInteractive: (ctx2) => !(ctx2.readOnly || ctx2.isDisabled),
|
|
568
|
-
hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize != null,
|
|
569
|
-
valuePercent(ctx2) {
|
|
570
|
-
return ctx2.value.map((value) => 100 * getValuePercent3(value, ctx2.min, ctx2.max));
|
|
571
|
-
}
|
|
572
|
-
},
|
|
573
|
-
watch: {
|
|
574
|
-
value: ["syncInputElements"]
|
|
575
|
-
},
|
|
576
|
-
entry: ["coarseValue"],
|
|
577
|
-
activities: ["trackFormControlState", "trackThumbsSize"],
|
|
578
|
-
on: {
|
|
579
|
-
SET_VALUE: [
|
|
580
|
-
{
|
|
581
|
-
guard: "hasIndex",
|
|
582
|
-
actions: "setValueAtIndex"
|
|
583
|
-
},
|
|
584
|
-
{ actions: "setValue" }
|
|
585
|
-
],
|
|
586
|
-
INCREMENT: {
|
|
587
|
-
actions: "incrementThumbAtIndex"
|
|
588
|
-
},
|
|
589
|
-
DECREMENT: {
|
|
590
|
-
actions: "decrementThumbAtIndex"
|
|
591
|
-
}
|
|
592
|
-
},
|
|
593
|
-
states: {
|
|
594
|
-
idle: {
|
|
595
|
-
on: {
|
|
596
|
-
POINTER_DOWN: {
|
|
597
|
-
target: "dragging",
|
|
598
|
-
actions: ["setClosestThumbIndex", "setPointerValue", "focusActiveThumb"]
|
|
599
|
-
},
|
|
600
|
-
FOCUS: {
|
|
601
|
-
target: "focus",
|
|
602
|
-
actions: "setFocusedIndex"
|
|
603
|
-
},
|
|
604
|
-
THUMB_POINTER_DOWN: {
|
|
605
|
-
target: "dragging",
|
|
606
|
-
actions: ["setFocusedIndex", "focusActiveThumb"]
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
},
|
|
610
|
-
focus: {
|
|
611
|
-
entry: "focusActiveThumb",
|
|
612
|
-
on: {
|
|
613
|
-
POINTER_DOWN: {
|
|
614
|
-
target: "dragging",
|
|
615
|
-
actions: ["setClosestThumbIndex", "setPointerValue", "focusActiveThumb"]
|
|
616
|
-
},
|
|
617
|
-
THUMB_POINTER_DOWN: {
|
|
618
|
-
target: "dragging",
|
|
619
|
-
actions: ["setFocusedIndex", "focusActiveThumb"]
|
|
620
|
-
},
|
|
621
|
-
ARROW_DEC: {
|
|
622
|
-
actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
|
|
623
|
-
},
|
|
624
|
-
ARROW_INC: {
|
|
625
|
-
actions: ["incrementThumbAtIndex", "invokeOnChangeEnd"]
|
|
626
|
-
},
|
|
627
|
-
HOME: {
|
|
628
|
-
actions: ["setFocusedThumbToMin", "invokeOnChangeEnd"]
|
|
629
|
-
},
|
|
630
|
-
END: {
|
|
631
|
-
actions: ["setFocusedThumbToMax", "invokeOnChangeEnd"]
|
|
632
|
-
},
|
|
633
|
-
BLUR: {
|
|
634
|
-
target: "idle",
|
|
635
|
-
actions: "clearFocusedIndex"
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
},
|
|
639
|
-
dragging: {
|
|
640
|
-
entry: "focusActiveThumb",
|
|
641
|
-
activities: "trackPointerMove",
|
|
642
|
-
on: {
|
|
643
|
-
POINTER_UP: {
|
|
644
|
-
target: "focus",
|
|
645
|
-
actions: "invokeOnChangeEnd"
|
|
646
|
-
},
|
|
647
|
-
POINTER_MOVE: {
|
|
648
|
-
actions: "setPointerValue"
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
},
|
|
654
|
-
{
|
|
655
|
-
guards: {
|
|
656
|
-
hasIndex: (_ctx, evt) => evt.index != null
|
|
657
|
-
},
|
|
658
|
-
activities: {
|
|
659
|
-
trackFormControlState(ctx2, _evt, { initialContext }) {
|
|
660
|
-
return trackFormControl(dom.getRootEl(ctx2), {
|
|
661
|
-
onFieldsetDisabledChange(disabled) {
|
|
662
|
-
ctx2.fieldsetDisabled = disabled;
|
|
663
|
-
},
|
|
664
|
-
onFormReset() {
|
|
665
|
-
set.value(ctx2, initialContext.value);
|
|
666
|
-
}
|
|
667
|
-
});
|
|
668
|
-
},
|
|
669
|
-
trackPointerMove(ctx2, _evt, { send }) {
|
|
670
|
-
return trackPointerMove(dom.getDoc(ctx2), {
|
|
671
|
-
onPointerMove(info) {
|
|
672
|
-
send({ type: "POINTER_MOVE", point: info.point });
|
|
673
|
-
},
|
|
674
|
-
onPointerUp() {
|
|
675
|
-
send("POINTER_UP");
|
|
676
|
-
}
|
|
677
|
-
});
|
|
678
|
-
},
|
|
679
|
-
trackThumbsSize(ctx2) {
|
|
680
|
-
if (ctx2.thumbAlignment !== "contain" || ctx2.thumbSize)
|
|
681
|
-
return;
|
|
682
|
-
return trackElementsSize({
|
|
683
|
-
getNodes: () => dom.getElements(ctx2),
|
|
684
|
-
observeMutation: true,
|
|
685
|
-
callback(size) {
|
|
686
|
-
if (!size || isEqualSize(ctx2.thumbSize, size))
|
|
687
|
-
return;
|
|
688
|
-
ctx2.thumbSize = size;
|
|
689
|
-
}
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
|
-
},
|
|
693
|
-
actions: {
|
|
694
|
-
syncInputElements(ctx2) {
|
|
695
|
-
ctx2.value.forEach((value, index) => {
|
|
696
|
-
const inputEl = dom.getHiddenInputEl(ctx2, index);
|
|
697
|
-
dom.setValue(inputEl, value);
|
|
698
|
-
});
|
|
699
|
-
},
|
|
700
|
-
invokeOnChangeEnd(ctx2) {
|
|
701
|
-
ctx2.onValueChangeEnd?.({ value: ctx2.value });
|
|
702
|
-
},
|
|
703
|
-
setClosestThumbIndex(ctx2, evt) {
|
|
704
|
-
const pointValue = dom.getValueFromPoint(ctx2, evt.point);
|
|
705
|
-
if (pointValue == null)
|
|
706
|
-
return;
|
|
707
|
-
const focusedIndex = getClosestIndex(ctx2, pointValue);
|
|
708
|
-
set.focusedIndex(ctx2, focusedIndex);
|
|
709
|
-
},
|
|
710
|
-
setFocusedIndex(ctx2, evt) {
|
|
711
|
-
set.focusedIndex(ctx2, evt.index);
|
|
712
|
-
},
|
|
713
|
-
clearFocusedIndex(ctx2) {
|
|
714
|
-
set.focusedIndex(ctx2, -1);
|
|
715
|
-
},
|
|
716
|
-
setPointerValue(ctx2, evt) {
|
|
717
|
-
const pointerValue = dom.getValueFromPoint(ctx2, evt.point);
|
|
718
|
-
if (pointerValue == null)
|
|
719
|
-
return;
|
|
720
|
-
const value = constrainValue(ctx2, pointerValue, ctx2.focusedIndex);
|
|
721
|
-
set.valueAtIndex(ctx2, ctx2.focusedIndex, value);
|
|
722
|
-
},
|
|
723
|
-
focusActiveThumb(ctx2) {
|
|
724
|
-
raf(() => {
|
|
725
|
-
const thumbEl = dom.getThumbEl(ctx2, ctx2.focusedIndex);
|
|
726
|
-
thumbEl?.focus({ preventScroll: true });
|
|
727
|
-
});
|
|
728
|
-
},
|
|
729
|
-
decrementThumbAtIndex(ctx2, evt) {
|
|
730
|
-
const value = decrement(ctx2, evt.index, evt.step);
|
|
731
|
-
set.value(ctx2, value);
|
|
732
|
-
},
|
|
733
|
-
incrementThumbAtIndex(ctx2, evt) {
|
|
734
|
-
const value = increment(ctx2, evt.index, evt.step);
|
|
735
|
-
set.value(ctx2, value);
|
|
736
|
-
},
|
|
737
|
-
setFocusedThumbToMin(ctx2) {
|
|
738
|
-
const { min } = getRangeAtIndex(ctx2, ctx2.focusedIndex);
|
|
739
|
-
set.valueAtIndex(ctx2, ctx2.focusedIndex, min);
|
|
740
|
-
},
|
|
741
|
-
setFocusedThumbToMax(ctx2) {
|
|
742
|
-
const { max } = getRangeAtIndex(ctx2, ctx2.focusedIndex);
|
|
743
|
-
set.valueAtIndex(ctx2, ctx2.focusedIndex, max);
|
|
744
|
-
},
|
|
745
|
-
coarseValue(ctx2) {
|
|
746
|
-
const value = normalizeValues(ctx2, ctx2.value);
|
|
747
|
-
set.value(ctx2, value);
|
|
748
|
-
},
|
|
749
|
-
setValueAtIndex(ctx2, evt) {
|
|
750
|
-
const value = constrainValue(ctx2, evt.value, evt.index);
|
|
751
|
-
set.valueAtIndex(ctx2, evt.index, value);
|
|
752
|
-
},
|
|
753
|
-
setValue(ctx2, evt) {
|
|
754
|
-
const value = normalizeValues(ctx2, evt.value);
|
|
755
|
-
set.value(ctx2, value);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
);
|
|
760
|
-
}
|
|
761
|
-
var invoke = {
|
|
762
|
-
change: (ctx) => {
|
|
763
|
-
ctx.onValueChange?.({
|
|
764
|
-
value: Array.from(ctx.value)
|
|
765
|
-
});
|
|
766
|
-
dom.dispatchChangeEvent(ctx);
|
|
767
|
-
},
|
|
768
|
-
focusChange: (ctx) => {
|
|
769
|
-
ctx.onFocusChange?.({
|
|
770
|
-
value: Array.from(ctx.value),
|
|
771
|
-
focusedIndex: ctx.focusedIndex
|
|
772
|
-
});
|
|
773
|
-
}
|
|
774
|
-
};
|
|
775
|
-
var set = {
|
|
776
|
-
valueAtIndex: (ctx, index, value) => {
|
|
777
|
-
if (isEqual(ctx.value[index], value))
|
|
778
|
-
return;
|
|
779
|
-
ctx.value[index] = value;
|
|
780
|
-
invoke.change(ctx);
|
|
781
|
-
},
|
|
782
|
-
value: (ctx, value) => {
|
|
783
|
-
if (isEqual(ctx.value, value))
|
|
784
|
-
return;
|
|
785
|
-
assignArray(ctx.value, value);
|
|
786
|
-
invoke.change(ctx);
|
|
787
|
-
},
|
|
788
|
-
focusedIndex: (ctx, index) => {
|
|
789
|
-
if (isEqual(ctx.focusedIndex, index))
|
|
790
|
-
return;
|
|
791
|
-
ctx.focusedIndex = index;
|
|
792
|
-
invoke.focusChange(ctx);
|
|
793
|
-
}
|
|
794
|
-
};
|
|
795
|
-
|
|
796
|
-
// src/slider.props.ts
|
|
797
|
-
import { createProps } from "@zag-js/types";
|
|
798
|
-
import { createSplitProps } from "@zag-js/utils";
|
|
799
|
-
var props = createProps()([
|
|
800
|
-
"aria-label",
|
|
801
|
-
"aria-labelledby",
|
|
802
|
-
"dir",
|
|
803
|
-
"disabled",
|
|
804
|
-
"form",
|
|
805
|
-
"getAriaValueText",
|
|
806
|
-
"getRootNode",
|
|
807
|
-
"id",
|
|
808
|
-
"ids",
|
|
809
|
-
"invalid",
|
|
810
|
-
"max",
|
|
811
|
-
"min",
|
|
812
|
-
"minStepsBetweenThumbs",
|
|
813
|
-
"name",
|
|
814
|
-
"onFocusChange",
|
|
815
|
-
"onValueChange",
|
|
816
|
-
"onValueChangeEnd",
|
|
817
|
-
"orientation",
|
|
818
|
-
"origin",
|
|
819
|
-
"readOnly",
|
|
820
|
-
"step",
|
|
821
|
-
"thumbAlignment",
|
|
822
|
-
"thumbAlignment",
|
|
823
|
-
"thumbSize",
|
|
824
|
-
"value"
|
|
825
|
-
]);
|
|
826
|
-
var splitProps = createSplitProps(props);
|
|
827
|
-
var thumbProps = createProps()(["index", "name"]);
|
|
828
|
-
var splitThumbProps = createSplitProps(thumbProps);
|
|
829
|
-
export {
|
|
830
|
-
anatomy,
|
|
831
|
-
connect,
|
|
832
|
-
machine,
|
|
833
|
-
props,
|
|
834
|
-
splitProps,
|
|
835
|
-
splitThumbProps,
|
|
836
|
-
thumbProps
|
|
837
|
-
};
|
|
1
|
+
import{createAnatomy}from"@zag-js/anatomy";var anatomy=createAnatomy("slider").parts("root","label","thumb","valueText","track","range","control","markerGroup","marker");var parts=anatomy.build();import{getEventKey,getEventPoint,getEventStep,getNativeEvent,isLeftClick,isModifierKey}from"@zag-js/dom-event";import{ariaAttr,dataAttr}from"@zag-js/dom-query";import{getPercentValue as getPercentValue2,getValuePercent as getValuePercent2}from"@zag-js/numeric-range";import{getRelativePoint}from"@zag-js/dom-event";import{createScope,queryAll}from"@zag-js/dom-query";import{dispatchInputValueEvent}from"@zag-js/form-utils";import{getPercentValue}from"@zag-js/numeric-range";import{getValuePercent,getValueTransformer}from"@zag-js/numeric-range";function getBounds(value){const firstValue=value[0];const lastThumb=value[value.length-1];return[firstValue,lastThumb]}function getRangeOffsets(ctx){const[firstPercent,lastPercent]=getBounds(ctx.valuePercent);if(ctx.valuePercent.length===1){if(ctx.origin==="center"){const isNegative=ctx.valuePercent[0]<50;const start=isNegative?`${ctx.valuePercent[0]}%`:"50%";const end=isNegative?"50%":`${100-ctx.valuePercent[0]}%`;return{start,end}}return{start:"0%",end:`${100-lastPercent}%`}}return{start:`${firstPercent}%`,end:`${100-lastPercent}%`}}function getRangeStyle(ctx){if(ctx.isVertical){return{position:"absolute",bottom:"var(--slider-range-start)",top:"var(--slider-range-end)"}}return{position:"absolute",[ctx.isRtl?"right":"left"]:"var(--slider-range-start)",[ctx.isRtl?"left":"right"]:"var(--slider-range-end)"}}function getVerticalThumbOffset(ctx){const{height=0}=ctx.thumbSize??{};const getValue=getValueTransformer([ctx.min,ctx.max],[-height/2,height/2]);return parseFloat(getValue(ctx.value).toFixed(2))}function getHorizontalThumbOffset(ctx){const{width=0}=ctx.thumbSize??{};if(ctx.isRtl){const getValue2=getValueTransformer([ctx.max,ctx.min],[-width/2,width/2]);return-1*parseFloat(getValue2(ctx.value).toFixed(2))}const getValue=getValueTransformer([ctx.min,ctx.max],[-width/2,width/2]);return parseFloat(getValue(ctx.value).toFixed(2))}function getOffset(ctx,percent){if(ctx.thumbAlignment==="center")return`${percent}%`;const offset=ctx.isVertical?getVerticalThumbOffset(ctx):getHorizontalThumbOffset(ctx);return`calc(${percent}% - ${offset}px)`}function getThumbOffset(ctx){let percent=getValuePercent(ctx.value,ctx.min,ctx.max)*100;return getOffset(ctx,percent)}function getVisibility(ctx){let visibility="visible";if(ctx.thumbAlignment==="contain"&&!ctx.hasMeasuredThumbSize){visibility="hidden"}return visibility}function getThumbStyle(ctx,index){const placementProp=ctx.isVertical?"bottom":"insetInlineStart";return{visibility:getVisibility(ctx),position:"absolute",transform:"var(--slider-thumb-transform)",[placementProp]:`var(--slider-thumb-offset-${index})`}}function getControlStyle(){return{touchAction:"none",userSelect:"none",position:"relative"}}function getRootStyle(ctx){const range=getRangeOffsets(ctx);const offsetStyles=ctx.value.reduce((styles,value,index)=>{const offset=getThumbOffset({...ctx,value});return{...styles,[`--slider-thumb-offset-${index}`]:offset}},{});return{...offsetStyles,"--slider-thumb-transform":ctx.isVertical?"translateY(50%)":ctx.isRtl?"translateX(50%)":"translateX(-50%)","--slider-range-start":range.start,"--slider-range-end":range.end}}function getMarkerStyle(ctx,value){return{visibility:getVisibility(ctx),position:"absolute",pointerEvents:"none",[ctx.isHorizontal?"insetInlineStart":"bottom"]:getThumbOffset({...ctx,value}),translate:"var(--tx) var(--ty)","--tx":ctx.isHorizontal?ctx.isRtl?"50%":"-50%":"0%","--ty":!ctx.isHorizontal?"50%":"0%"}}function getMarkerGroupStyle(){return{userSelect:"none",pointerEvents:"none",position:"relative"}}var styleGetterFns={getRootStyle,getControlStyle,getThumbStyle,getRangeStyle,getMarkerStyle,getMarkerGroupStyle};var dom=createScope({...styleGetterFns,getRootId:ctx=>ctx.ids?.root??`slider:${ctx.id}`,getThumbId:(ctx,index)=>ctx.ids?.thumb?.(index)??`slider:${ctx.id}:thumb:${index}`,getHiddenInputId:(ctx,index)=>`slider:${ctx.id}:input:${index}`,getControlId:ctx=>ctx.ids?.control??`slider:${ctx.id}:control`,getTrackId:ctx=>ctx.ids?.track??`slider:${ctx.id}:track`,getRangeId:ctx=>ctx.ids?.range??`slider:${ctx.id}:range`,getLabelId:ctx=>ctx.ids?.label??`slider:${ctx.id}:label`,getValueTextId:ctx=>ctx.ids?.valueText??`slider:${ctx.id}:valueText`,getMarkerId:(ctx,value)=>ctx.ids?.marker?.(value)??`slider:${ctx.id}:marker:${value}`,getRootEl:ctx=>dom.getById(ctx,dom.getRootId(ctx)),getThumbEl:(ctx,index)=>dom.getById(ctx,dom.getThumbId(ctx,index)),getHiddenInputEl:(ctx,index)=>dom.getById(ctx,dom.getHiddenInputId(ctx,index)),getControlEl:ctx=>dom.getById(ctx,dom.getControlId(ctx)),getElements:ctx=>queryAll(dom.getControlEl(ctx),"[role=slider]"),getFirstEl:ctx=>dom.getElements(ctx)[0],getRangeEl:ctx=>dom.getById(ctx,dom.getRangeId(ctx)),getValueFromPoint(ctx,point){const controlEl=dom.getControlEl(ctx);if(!controlEl)return;const relativePoint=getRelativePoint(point,controlEl);const percent=relativePoint.getPercentValue({orientation:ctx.orientation,dir:ctx.dir,inverted:{y:true}});return getPercentValue(percent,ctx.min,ctx.max,ctx.step)},dispatchChangeEvent(ctx){const valueArray=Array.from(ctx.value);valueArray.forEach((value,index)=>{const inputEl=dom.getHiddenInputEl(ctx,index);if(!inputEl)return;dispatchInputValueEvent(inputEl,{value})})}});import{clampValue,getClosestValueIndex,getNextStepValue,getPreviousStepValue,getValueRanges,snapValueToStep}from"@zag-js/numeric-range";function normalizeValues(ctx,nextValues){return nextValues.map((value,index,values)=>{return constrainValue({...ctx,value:values},value,index)})}function getRangeAtIndex(ctx,index){return getValueRanges(ctx.value,ctx.min,ctx.max,ctx.minStepsBetweenThumbs)[index]}function constrainValue(ctx,value,index){const range=getRangeAtIndex(ctx,index);const snapValue=snapValueToStep(value,ctx.min,ctx.max,ctx.step);return clampValue(snapValue,range.min,range.max)}function decrement(ctx,index,step){const idx=index??ctx.focusedIndex;const range=getRangeAtIndex(ctx,idx);const nextValues=getPreviousStepValue(idx,{...range,step:step??ctx.step,values:ctx.value});nextValues[idx]=clampValue(nextValues[idx],range.min,range.max);return nextValues}function increment(ctx,index,step){const idx=index??ctx.focusedIndex;const range=getRangeAtIndex(ctx,idx);const nextValues=getNextStepValue(idx,{...range,step:step??ctx.step,values:ctx.value});nextValues[idx]=clampValue(nextValues[idx],range.min,range.max);return nextValues}function getClosestIndex(ctx,pointValue){return getClosestValueIndex(ctx.value,pointValue)}function assignArray(current,next){for(let i=0;i<next.length;i++){const value=next[i];current[i]=value}}function connect(state,send,normalize){const ariaLabel=state.context["aria-label"];const ariaLabelledBy=state.context["aria-labelledby"];const sliderValue=state.context.value;const focused=state.matches("focus");const dragging=state.matches("dragging");const disabled=state.context.isDisabled;const invalid=state.context.invalid;const interactive=state.context.isInteractive;const isHorizontal=state.context.orientation==="horizontal";const isVertical=state.context.orientation==="vertical";function getValuePercentFn(value){return getValuePercent2(value,state.context.min,state.context.max)}function getPercentValueFn(percent){return getPercentValue2(percent,state.context.min,state.context.max,state.context.step)}return{value:state.context.value,dragging,focused,setValue(value){send({type:"SET_VALUE",value})},getThumbValue(index){return sliderValue[index]},setThumbValue(index,value){send({type:"SET_VALUE",index,value})},getValuePercent:getValuePercentFn,getPercentValue:getPercentValueFn,getThumbPercent(index){return getValuePercentFn(sliderValue[index])},setThumbPercent(index,percent){const value=getPercentValueFn(percent);send({type:"SET_VALUE",index,value})},getThumbMin(index){return getRangeAtIndex(state.context,index).min},getThumbMax(index){return getRangeAtIndex(state.context,index).max},increment(index){send({type:"INCREMENT",index})},decrement(index){send({type:"DECREMENT",index})},focus(){if(!interactive)return;send({type:"FOCUS",index:0})},labelProps:normalize.label({...parts.label.attrs,dir:state.context.dir,"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),id:dom.getLabelId(state.context),htmlFor:dom.getHiddenInputId(state.context,0),onClick(event){if(!interactive)return;event.preventDefault();dom.getFirstEl(state.context)?.focus()},style:{userSelect:"none"}}),rootProps:normalize.element({...parts.root.attrs,"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),id:dom.getRootId(state.context),dir:state.context.dir,style:dom.getRootStyle(state.context)}),valueTextProps:normalize.element({...parts.valueText.attrs,dir:state.context.dir,"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),id:dom.getValueTextId(state.context)}),trackProps:normalize.element({...parts.track.attrs,dir:state.context.dir,id:dom.getTrackId(state.context),"data-disabled":dataAttr(disabled),"data-invalid":dataAttr(invalid),"data-orientation":state.context.orientation,"data-focus":dataAttr(focused),style:{position:"relative"}}),getThumbProps(props2){const{index=0,name}=props2;const value=sliderValue[index];const range=getRangeAtIndex(state.context,index);const valueText=state.context.getAriaValueText?.({value,index});const _ariaLabel=Array.isArray(ariaLabel)?ariaLabel[index]:ariaLabel;const _ariaLabelledBy=Array.isArray(ariaLabelledBy)?ariaLabelledBy[index]:ariaLabelledBy;return normalize.element({...parts.thumb.attrs,dir:state.context.dir,"data-index":index,"data-name":name,id:dom.getThumbId(state.context,index),"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-focus":dataAttr(focused&&state.context.focusedIndex===index),draggable:false,"aria-disabled":ariaAttr(disabled),"aria-label":_ariaLabel,"aria-labelledby":_ariaLabelledBy??dom.getLabelId(state.context),"aria-orientation":state.context.orientation,"aria-valuemax":range.max,"aria-valuemin":range.min,"aria-valuenow":sliderValue[index],"aria-valuetext":valueText,role:"slider",tabIndex:disabled?void 0:0,style:dom.getThumbStyle(state.context,index),onPointerDown(event){if(!interactive)return;send({type:"THUMB_POINTER_DOWN",index});event.stopPropagation()},onBlur(){if(!interactive)return;send("BLUR")},onFocus(){if(!interactive)return;send({type:"FOCUS",index})},onKeyDown(event){if(event.defaultPrevented)return;if(!interactive)return;const step=getEventStep(event)*state.context.step;const keyMap={ArrowUp(){if(isHorizontal)return;send({type:"ARROW_INC",step,src:"ArrowUp"})},ArrowDown(){if(isHorizontal)return;send({type:"ARROW_DEC",step,src:"ArrowDown"})},ArrowLeft(){if(isVertical)return;send({type:"ARROW_DEC",step,src:"ArrowLeft"})},ArrowRight(){if(isVertical)return;send({type:"ARROW_INC",step,src:"ArrowRight"})},PageUp(){send({type:"ARROW_INC",step,src:"PageUp"})},PageDown(){send({type:"ARROW_DEC",step,src:"PageDown"})},Home(){send("HOME")},End(){send("END")}};const key=getEventKey(event,state.context);const exec=keyMap[key];if(exec){exec(event);event.preventDefault();event.stopPropagation()}}})},getHiddenInputProps(props2){const{index=0,name}=props2;return normalize.input({name:name??(state.context.name?state.context.name+(state.context.value.length>1?"[]":""):void 0),form:state.context.form,type:"text",hidden:true,defaultValue:state.context.value[index],id:dom.getHiddenInputId(state.context,index)})},rangeProps:normalize.element({id:dom.getRangeId(state.context),...parts.range.attrs,dir:state.context.dir,"data-focus":dataAttr(focused),"data-invalid":dataAttr(invalid),"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,style:dom.getRangeStyle(state.context)}),controlProps:normalize.element({...parts.control.attrs,dir:state.context.dir,id:dom.getControlId(state.context),"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),style:dom.getControlStyle(),onPointerDown(event){if(!interactive)return;const evt=getNativeEvent(event);if(!isLeftClick(evt)||isModifierKey(evt))return;const point=getEventPoint(evt);send({type:"POINTER_DOWN",point});event.preventDefault();event.stopPropagation()}}),markerGroupProps:normalize.element({...parts.markerGroup.attrs,role:"presentation",dir:state.context.dir,"aria-hidden":true,"data-orientation":state.context.orientation,style:dom.getMarkerGroupStyle()}),getMarkerProps(props2){const style=dom.getMarkerStyle(state.context,props2.value);let markerState;const first=state.context.value[0];const last=state.context.value[state.context.value.length-1];if(props2.value<first){markerState="under-value"}else if(props2.value>last){markerState="over-value"}else{markerState="at-value"}return normalize.element({...parts.marker.attrs,id:dom.getMarkerId(state.context,props2.value),role:"presentation",dir:state.context.dir,"data-orientation":state.context.orientation,"data-value":props2.value,"data-disabled":dataAttr(disabled),"data-state":markerState,style})}}}import{createMachine}from"@zag-js/core";import{trackPointerMove}from"@zag-js/dom-event";import{raf}from"@zag-js/dom-query";import{trackElementsSize}from"@zag-js/element-size";import{trackFormControl}from"@zag-js/form-utils";import{getValuePercent as getValuePercent3}from"@zag-js/numeric-range";import{compact,isEqual}from"@zag-js/utils";var isEqualSize=(a,b)=>{return a?.width===b?.width&&a?.height===b?.height};function machine(userContext){const ctx=compact(userContext);return createMachine({id:"slider",initial:"idle",context:{thumbSize:null,thumbAlignment:"contain",min:0,max:100,step:1,value:[0],origin:"start",orientation:"horizontal",dir:"ltr",minStepsBetweenThumbs:0,disabled:false,...ctx,focusedIndex:-1,fieldsetDisabled:false},computed:{isHorizontal:ctx2=>ctx2.orientation==="horizontal",isVertical:ctx2=>ctx2.orientation==="vertical",isRtl:ctx2=>ctx2.orientation==="horizontal"&&ctx2.dir==="rtl",isDisabled:ctx2=>!!ctx2.disabled||ctx2.fieldsetDisabled,isInteractive:ctx2=>!(ctx2.readOnly||ctx2.isDisabled),hasMeasuredThumbSize:ctx2=>ctx2.thumbSize!=null,valuePercent(ctx2){return ctx2.value.map(value=>100*getValuePercent3(value,ctx2.min,ctx2.max))}},watch:{value:["syncInputElements"]},entry:["coarseValue"],activities:["trackFormControlState","trackThumbsSize"],on:{SET_VALUE:[{guard:"hasIndex",actions:"setValueAtIndex"},{actions:"setValue"}],INCREMENT:{actions:"incrementThumbAtIndex"},DECREMENT:{actions:"decrementThumbAtIndex"}},states:{idle:{on:{POINTER_DOWN:{target:"dragging",actions:["setClosestThumbIndex","setPointerValue","focusActiveThumb"]},FOCUS:{target:"focus",actions:"setFocusedIndex"},THUMB_POINTER_DOWN:{target:"dragging",actions:["setFocusedIndex","focusActiveThumb"]}}},focus:{entry:"focusActiveThumb",on:{POINTER_DOWN:{target:"dragging",actions:["setClosestThumbIndex","setPointerValue","focusActiveThumb"]},THUMB_POINTER_DOWN:{target:"dragging",actions:["setFocusedIndex","focusActiveThumb"]},ARROW_DEC:{actions:["decrementThumbAtIndex","invokeOnChangeEnd"]},ARROW_INC:{actions:["incrementThumbAtIndex","invokeOnChangeEnd"]},HOME:{actions:["setFocusedThumbToMin","invokeOnChangeEnd"]},END:{actions:["setFocusedThumbToMax","invokeOnChangeEnd"]},BLUR:{target:"idle",actions:"clearFocusedIndex"}}},dragging:{entry:"focusActiveThumb",activities:"trackPointerMove",on:{POINTER_UP:{target:"focus",actions:"invokeOnChangeEnd"},POINTER_MOVE:{actions:"setPointerValue"}}}}},{guards:{hasIndex:(_ctx,evt)=>evt.index!=null},activities:{trackFormControlState(ctx2,_evt,{initialContext}){return trackFormControl(dom.getRootEl(ctx2),{onFieldsetDisabledChange(disabled){ctx2.fieldsetDisabled=disabled},onFormReset(){set.value(ctx2,initialContext.value)}})},trackPointerMove(ctx2,_evt,{send}){return trackPointerMove(dom.getDoc(ctx2),{onPointerMove(info){send({type:"POINTER_MOVE",point:info.point})},onPointerUp(){send("POINTER_UP")}})},trackThumbsSize(ctx2){if(ctx2.thumbAlignment!=="contain"||ctx2.thumbSize)return;return trackElementsSize({getNodes:()=>dom.getElements(ctx2),observeMutation:true,callback(size){if(!size||isEqualSize(ctx2.thumbSize,size))return;ctx2.thumbSize=size}})}},actions:{syncInputElements(ctx2){ctx2.value.forEach((value,index)=>{const inputEl=dom.getHiddenInputEl(ctx2,index);dom.setValue(inputEl,value)})},invokeOnChangeEnd(ctx2){ctx2.onValueChangeEnd?.({value:ctx2.value})},setClosestThumbIndex(ctx2,evt){const pointValue=dom.getValueFromPoint(ctx2,evt.point);if(pointValue==null)return;const focusedIndex=getClosestIndex(ctx2,pointValue);set.focusedIndex(ctx2,focusedIndex)},setFocusedIndex(ctx2,evt){set.focusedIndex(ctx2,evt.index)},clearFocusedIndex(ctx2){set.focusedIndex(ctx2,-1)},setPointerValue(ctx2,evt){const pointerValue=dom.getValueFromPoint(ctx2,evt.point);if(pointerValue==null)return;const value=constrainValue(ctx2,pointerValue,ctx2.focusedIndex);set.valueAtIndex(ctx2,ctx2.focusedIndex,value)},focusActiveThumb(ctx2){raf(()=>{const thumbEl=dom.getThumbEl(ctx2,ctx2.focusedIndex);thumbEl?.focus({preventScroll:true})})},decrementThumbAtIndex(ctx2,evt){const value=decrement(ctx2,evt.index,evt.step);set.value(ctx2,value)},incrementThumbAtIndex(ctx2,evt){const value=increment(ctx2,evt.index,evt.step);set.value(ctx2,value)},setFocusedThumbToMin(ctx2){const{min}=getRangeAtIndex(ctx2,ctx2.focusedIndex);set.valueAtIndex(ctx2,ctx2.focusedIndex,min)},setFocusedThumbToMax(ctx2){const{max}=getRangeAtIndex(ctx2,ctx2.focusedIndex);set.valueAtIndex(ctx2,ctx2.focusedIndex,max)},coarseValue(ctx2){const value=normalizeValues(ctx2,ctx2.value);set.value(ctx2,value)},setValueAtIndex(ctx2,evt){const value=constrainValue(ctx2,evt.value,evt.index);set.valueAtIndex(ctx2,evt.index,value)},setValue(ctx2,evt){const value=normalizeValues(ctx2,evt.value);set.value(ctx2,value)}}})}var invoke={change:ctx=>{ctx.onValueChange?.({value:Array.from(ctx.value)});dom.dispatchChangeEvent(ctx)},focusChange:ctx=>{ctx.onFocusChange?.({value:Array.from(ctx.value),focusedIndex:ctx.focusedIndex})}};var set={valueAtIndex:(ctx,index,value)=>{if(isEqual(ctx.value[index],value))return;ctx.value[index]=value;invoke.change(ctx)},value:(ctx,value)=>{if(isEqual(ctx.value,value))return;assignArray(ctx.value,value);invoke.change(ctx)},focusedIndex:(ctx,index)=>{if(isEqual(ctx.focusedIndex,index))return;ctx.focusedIndex=index;invoke.focusChange(ctx)}};import{createProps}from"@zag-js/types";import{createSplitProps}from"@zag-js/utils";var props=createProps()(["aria-label","aria-labelledby","dir","disabled","form","getAriaValueText","getRootNode","id","ids","invalid","max","min","minStepsBetweenThumbs","name","onFocusChange","onValueChange","onValueChangeEnd","orientation","origin","readOnly","step","thumbAlignment","thumbAlignment","thumbSize","value"]);var splitProps=createSplitProps(props);var thumbProps=createProps()(["index","name"]);var splitThumbProps=createSplitProps(thumbProps);export{anatomy,connect,machine,props,splitProps,splitThumbProps,thumbProps};
|
|
838
2
|
//# sourceMappingURL=index.mjs.map
|