@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.
@@ -0,0 +1,334 @@
1
+ // src/slider.connect.ts
2
+ import {
3
+ ariaAttr,
4
+ dataAttr,
5
+ getEventKey,
6
+ getEventPoint,
7
+ getEventStep,
8
+ isLeftClick,
9
+ isModifierKey
10
+ } from "@zag-js/dom-query";
11
+ import { first, getPercentValue, getValuePercent, last } from "@zag-js/utils";
12
+ import { parts } from "./slider.anatomy.mjs";
13
+ import * as dom from "./slider.dom.mjs";
14
+ import {
15
+ getControlStyle,
16
+ getMarkerGroupStyle,
17
+ getMarkerStyle,
18
+ getRangeStyle,
19
+ getRootStyle,
20
+ getThumbStyle
21
+ } from "./slider.style.mjs";
22
+ import { getRangeAtIndex } from "./slider.utils.mjs";
23
+ function connect(service, normalize) {
24
+ const { state, send, context, prop, computed, scope } = service;
25
+ const ariaLabel = prop("aria-label");
26
+ const ariaLabelledBy = prop("aria-labelledby");
27
+ const sliderValue = context.get("value");
28
+ const focusedIndex = context.get("focusedIndex");
29
+ const focused = state.matches("focus");
30
+ const dragging = state.matches("dragging");
31
+ const disabled = computed("isDisabled");
32
+ const invalid = prop("invalid");
33
+ const interactive = computed("isInteractive");
34
+ const isHorizontal = prop("orientation") === "horizontal";
35
+ const isVertical = prop("orientation") === "vertical";
36
+ function getValuePercentFn(value) {
37
+ return getValuePercent(value, prop("min"), prop("max"));
38
+ }
39
+ function getPercentValueFn(percent) {
40
+ return getPercentValue(percent, prop("min"), prop("max"), prop("step"));
41
+ }
42
+ return {
43
+ value: sliderValue,
44
+ dragging,
45
+ focused,
46
+ setValue(value) {
47
+ send({ type: "SET_VALUE", value });
48
+ },
49
+ getThumbValue(index) {
50
+ return sliderValue[index];
51
+ },
52
+ setThumbValue(index, value) {
53
+ send({ type: "SET_VALUE", index, value });
54
+ },
55
+ getValuePercent: getValuePercentFn,
56
+ getPercentValue: getPercentValueFn,
57
+ getThumbPercent(index) {
58
+ return getValuePercentFn(sliderValue[index]);
59
+ },
60
+ setThumbPercent(index, percent) {
61
+ const value = getPercentValueFn(percent);
62
+ send({ type: "SET_VALUE", index, value });
63
+ },
64
+ getThumbMin(index) {
65
+ return getRangeAtIndex(service, index).min;
66
+ },
67
+ getThumbMax(index) {
68
+ return getRangeAtIndex(service, index).max;
69
+ },
70
+ increment(index) {
71
+ send({ type: "INCREMENT", index });
72
+ },
73
+ decrement(index) {
74
+ send({ type: "DECREMENT", index });
75
+ },
76
+ focus() {
77
+ if (!interactive) return;
78
+ send({ type: "FOCUS", index: 0 });
79
+ },
80
+ getLabelProps() {
81
+ return normalize.label({
82
+ ...parts.label.attrs,
83
+ dir: prop("dir"),
84
+ "data-disabled": dataAttr(disabled),
85
+ "data-orientation": prop("orientation"),
86
+ "data-invalid": dataAttr(invalid),
87
+ "data-dragging": dataAttr(dragging),
88
+ "data-focus": dataAttr(focused),
89
+ id: dom.getLabelId(scope),
90
+ htmlFor: dom.getHiddenInputId(scope, 0),
91
+ onClick(event) {
92
+ if (!interactive) return;
93
+ event.preventDefault();
94
+ dom.getFirstThumbEl(scope)?.focus();
95
+ },
96
+ style: {
97
+ userSelect: "none",
98
+ WebkitUserSelect: "none"
99
+ }
100
+ });
101
+ },
102
+ getRootProps() {
103
+ return normalize.element({
104
+ ...parts.root.attrs,
105
+ "data-disabled": dataAttr(disabled),
106
+ "data-orientation": prop("orientation"),
107
+ "data-dragging": dataAttr(dragging),
108
+ "data-invalid": dataAttr(invalid),
109
+ "data-focus": dataAttr(focused),
110
+ id: dom.getRootId(scope),
111
+ dir: prop("dir"),
112
+ style: getRootStyle(service)
113
+ });
114
+ },
115
+ getValueTextProps() {
116
+ return normalize.element({
117
+ ...parts.valueText.attrs,
118
+ dir: prop("dir"),
119
+ "data-disabled": dataAttr(disabled),
120
+ "data-orientation": prop("orientation"),
121
+ "data-invalid": dataAttr(invalid),
122
+ "data-focus": dataAttr(focused),
123
+ id: dom.getValueTextId(scope)
124
+ });
125
+ },
126
+ getTrackProps() {
127
+ return normalize.element({
128
+ ...parts.track.attrs,
129
+ dir: prop("dir"),
130
+ id: dom.getTrackId(scope),
131
+ "data-disabled": dataAttr(disabled),
132
+ "data-invalid": dataAttr(invalid),
133
+ "data-dragging": dataAttr(dragging),
134
+ "data-orientation": prop("orientation"),
135
+ "data-focus": dataAttr(focused),
136
+ style: { position: "relative" }
137
+ });
138
+ },
139
+ getThumbProps(props) {
140
+ const { index = 0, name } = props;
141
+ const value = sliderValue[index];
142
+ const range = getRangeAtIndex(service, index);
143
+ const valueText = prop("getAriaValueText")?.({ value, index });
144
+ const _ariaLabel = Array.isArray(ariaLabel) ? ariaLabel[index] : ariaLabel;
145
+ const _ariaLabelledBy = Array.isArray(ariaLabelledBy) ? ariaLabelledBy[index] : ariaLabelledBy;
146
+ return normalize.element({
147
+ ...parts.thumb.attrs,
148
+ dir: prop("dir"),
149
+ "data-index": index,
150
+ "data-name": name,
151
+ id: dom.getThumbId(scope, index),
152
+ "data-disabled": dataAttr(disabled),
153
+ "data-orientation": prop("orientation"),
154
+ "data-focus": dataAttr(focused && focusedIndex === index),
155
+ "data-dragging": dataAttr(dragging && focusedIndex === index),
156
+ draggable: false,
157
+ "aria-disabled": ariaAttr(disabled),
158
+ "aria-label": _ariaLabel,
159
+ "aria-labelledby": _ariaLabelledBy ?? dom.getLabelId(scope),
160
+ "aria-orientation": prop("orientation"),
161
+ "aria-valuemax": range.max,
162
+ "aria-valuemin": range.min,
163
+ "aria-valuenow": sliderValue[index],
164
+ "aria-valuetext": valueText,
165
+ role: "slider",
166
+ tabIndex: disabled ? void 0 : 0,
167
+ style: getThumbStyle(service, index),
168
+ onPointerDown(event) {
169
+ if (!interactive) return;
170
+ if (!isLeftClick(event)) return;
171
+ const thumbEl = event.currentTarget;
172
+ const rect = thumbEl.getBoundingClientRect();
173
+ const midpoint = {
174
+ x: rect.left + rect.width / 2,
175
+ y: rect.top + rect.height / 2
176
+ };
177
+ const offset = {
178
+ x: event.clientX - midpoint.x,
179
+ y: event.clientY - midpoint.y
180
+ };
181
+ send({ type: "THUMB_POINTER_DOWN", index, offset });
182
+ event.stopPropagation();
183
+ },
184
+ onBlur() {
185
+ if (!interactive) return;
186
+ send({ type: "BLUR" });
187
+ },
188
+ onFocus() {
189
+ if (!interactive) return;
190
+ send({ type: "FOCUS", index });
191
+ },
192
+ onKeyDown(event) {
193
+ if (event.defaultPrevented) return;
194
+ if (!interactive) return;
195
+ const step = getEventStep(event) * prop("step");
196
+ const keyMap = {
197
+ ArrowUp() {
198
+ if (isHorizontal) return;
199
+ send({ type: "ARROW_INC", step, src: "ArrowUp" });
200
+ },
201
+ ArrowDown() {
202
+ if (isHorizontal) return;
203
+ send({ type: "ARROW_DEC", step, src: "ArrowDown" });
204
+ },
205
+ ArrowLeft() {
206
+ if (isVertical) return;
207
+ send({ type: "ARROW_DEC", step, src: "ArrowLeft" });
208
+ },
209
+ ArrowRight() {
210
+ if (isVertical) return;
211
+ send({ type: "ARROW_INC", step, src: "ArrowRight" });
212
+ },
213
+ PageUp() {
214
+ send({ type: "ARROW_INC", step, src: "PageUp" });
215
+ },
216
+ PageDown() {
217
+ send({ type: "ARROW_DEC", step, src: "PageDown" });
218
+ },
219
+ Home() {
220
+ send({ type: "HOME" });
221
+ },
222
+ End() {
223
+ send({ type: "END" });
224
+ }
225
+ };
226
+ const key = getEventKey(event, {
227
+ dir: prop("dir"),
228
+ orientation: prop("orientation")
229
+ });
230
+ const exec = keyMap[key];
231
+ if (exec) {
232
+ exec(event);
233
+ event.preventDefault();
234
+ event.stopPropagation();
235
+ }
236
+ }
237
+ });
238
+ },
239
+ getHiddenInputProps(props) {
240
+ const { index = 0, name } = props;
241
+ return normalize.input({
242
+ name: name ?? (prop("name") ? prop("name") + (sliderValue.length > 1 ? "[]" : "") : void 0),
243
+ form: prop("form"),
244
+ type: "text",
245
+ hidden: true,
246
+ defaultValue: sliderValue[index],
247
+ id: dom.getHiddenInputId(scope, index)
248
+ });
249
+ },
250
+ getRangeProps() {
251
+ return normalize.element({
252
+ id: dom.getRangeId(scope),
253
+ ...parts.range.attrs,
254
+ dir: prop("dir"),
255
+ "data-dragging": dataAttr(dragging),
256
+ "data-focus": dataAttr(focused),
257
+ "data-invalid": dataAttr(invalid),
258
+ "data-disabled": dataAttr(disabled),
259
+ "data-orientation": prop("orientation"),
260
+ style: getRangeStyle(service)
261
+ });
262
+ },
263
+ getControlProps() {
264
+ return normalize.element({
265
+ ...parts.control.attrs,
266
+ dir: prop("dir"),
267
+ id: dom.getControlId(scope),
268
+ "data-dragging": dataAttr(dragging),
269
+ "data-disabled": dataAttr(disabled),
270
+ "data-orientation": prop("orientation"),
271
+ "data-invalid": dataAttr(invalid),
272
+ "data-focus": dataAttr(focused),
273
+ style: getControlStyle(),
274
+ onPointerDown(event) {
275
+ if (!interactive) return;
276
+ if (!isLeftClick(event)) return;
277
+ if (isModifierKey(event)) return;
278
+ const point = getEventPoint(event);
279
+ send({ type: "POINTER_DOWN", point });
280
+ event.preventDefault();
281
+ event.stopPropagation();
282
+ }
283
+ });
284
+ },
285
+ getMarkerGroupProps() {
286
+ return normalize.element({
287
+ ...parts.markerGroup.attrs,
288
+ role: "presentation",
289
+ dir: prop("dir"),
290
+ "aria-hidden": true,
291
+ "data-orientation": prop("orientation"),
292
+ style: getMarkerGroupStyle()
293
+ });
294
+ },
295
+ getMarkerProps(props) {
296
+ const style = getMarkerStyle(service, props.value);
297
+ let markerState;
298
+ if (props.value < first(sliderValue)) {
299
+ markerState = "under-value";
300
+ } else if (props.value > last(sliderValue)) {
301
+ markerState = "over-value";
302
+ } else {
303
+ markerState = "at-value";
304
+ }
305
+ return normalize.element({
306
+ ...parts.marker.attrs,
307
+ id: dom.getMarkerId(scope, props.value),
308
+ role: "presentation",
309
+ dir: prop("dir"),
310
+ "data-orientation": prop("orientation"),
311
+ "data-value": props.value,
312
+ "data-disabled": dataAttr(disabled),
313
+ "data-state": markerState,
314
+ style
315
+ });
316
+ },
317
+ getDraggingIndicatorProps(props) {
318
+ const { index = 0 } = props;
319
+ const isDragging = index === focusedIndex && dragging;
320
+ return normalize.element({
321
+ ...parts.draggingIndicator.attrs,
322
+ role: "presentation",
323
+ dir: prop("dir"),
324
+ hidden: !isDragging,
325
+ "data-orientation": prop("orientation"),
326
+ "data-state": isDragging ? "open" : "closed",
327
+ style: getThumbStyle(service, index)
328
+ });
329
+ }
330
+ };
331
+ }
332
+ export {
333
+ connect
334
+ };
@@ -0,0 +1,30 @@
1
+ import { Scope, Params } from '@zag-js/core';
2
+ import { Point } from '@zag-js/types';
3
+ import { SliderSchema } from './slider.types.mjs';
4
+
5
+ declare const getRootId: (ctx: Scope) => any;
6
+ declare const getThumbId: (ctx: Scope, index: number) => any;
7
+ declare const getHiddenInputId: (ctx: Scope, index: number) => any;
8
+ declare const getControlId: (ctx: Scope) => any;
9
+ declare const getTrackId: (ctx: Scope) => any;
10
+ declare const getRangeId: (ctx: Scope) => any;
11
+ declare const getLabelId: (ctx: Scope) => any;
12
+ declare const getValueTextId: (ctx: Scope) => any;
13
+ declare const getMarkerId: (ctx: Scope, value: number) => any;
14
+ declare const getRootEl: (ctx: Scope) => HTMLElement | null;
15
+ declare const getThumbEl: (ctx: Scope, index: number) => HTMLElement | null;
16
+ declare const getThumbEls: (ctx: Scope) => HTMLElement[];
17
+ declare const getFirstThumbEl: (ctx: Scope) => HTMLElement;
18
+ declare const getHiddenInputEl: (ctx: Scope, index: number) => HTMLInputElement | null;
19
+ declare const getControlEl: (ctx: Scope) => HTMLElement | null;
20
+ declare const getRangeEl: (ctx: Scope) => HTMLElement | null;
21
+ declare const getPointValue: (params: Params<SliderSchema>, point: Point) => number | undefined;
22
+ declare const dispatchChangeEvent: (ctx: Scope, value: number[]) => void;
23
+ declare const getOffsetRect: (el: HTMLElement | undefined) => {
24
+ left: number;
25
+ top: number;
26
+ width: number;
27
+ height: number;
28
+ };
29
+
30
+ export { dispatchChangeEvent, getControlEl, getControlId, getFirstThumbEl, getHiddenInputEl, getHiddenInputId, getLabelId, getMarkerId, getOffsetRect, getPointValue, getRangeEl, getRangeId, getRootEl, getRootId, getThumbEl, getThumbEls, getThumbId, getTrackId, getValueTextId };
@@ -0,0 +1,30 @@
1
+ import { Scope, Params } from '@zag-js/core';
2
+ import { Point } from '@zag-js/types';
3
+ import { SliderSchema } from './slider.types.js';
4
+
5
+ declare const getRootId: (ctx: Scope) => any;
6
+ declare const getThumbId: (ctx: Scope, index: number) => any;
7
+ declare const getHiddenInputId: (ctx: Scope, index: number) => any;
8
+ declare const getControlId: (ctx: Scope) => any;
9
+ declare const getTrackId: (ctx: Scope) => any;
10
+ declare const getRangeId: (ctx: Scope) => any;
11
+ declare const getLabelId: (ctx: Scope) => any;
12
+ declare const getValueTextId: (ctx: Scope) => any;
13
+ declare const getMarkerId: (ctx: Scope, value: number) => any;
14
+ declare const getRootEl: (ctx: Scope) => HTMLElement | null;
15
+ declare const getThumbEl: (ctx: Scope, index: number) => HTMLElement | null;
16
+ declare const getThumbEls: (ctx: Scope) => HTMLElement[];
17
+ declare const getFirstThumbEl: (ctx: Scope) => HTMLElement;
18
+ declare const getHiddenInputEl: (ctx: Scope, index: number) => HTMLInputElement | null;
19
+ declare const getControlEl: (ctx: Scope) => HTMLElement | null;
20
+ declare const getRangeEl: (ctx: Scope) => HTMLElement | null;
21
+ declare const getPointValue: (params: Params<SliderSchema>, point: Point) => number | undefined;
22
+ declare const dispatchChangeEvent: (ctx: Scope, value: number[]) => void;
23
+ declare const getOffsetRect: (el: HTMLElement | undefined) => {
24
+ left: number;
25
+ top: number;
26
+ width: number;
27
+ height: number;
28
+ };
29
+
30
+ export { dispatchChangeEvent, getControlEl, getControlId, getFirstThumbEl, getHiddenInputEl, getHiddenInputId, getLabelId, getMarkerId, getOffsetRect, getPointValue, getRangeEl, getRangeId, getRootEl, getRootId, getThumbEl, getThumbEls, getThumbId, getTrackId, getValueTextId };
@@ -0,0 +1,144 @@
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.dom.ts
21
+ var slider_dom_exports = {};
22
+ __export(slider_dom_exports, {
23
+ dispatchChangeEvent: () => dispatchChangeEvent,
24
+ getControlEl: () => getControlEl,
25
+ getControlId: () => getControlId,
26
+ getFirstThumbEl: () => getFirstThumbEl,
27
+ getHiddenInputEl: () => getHiddenInputEl,
28
+ getHiddenInputId: () => getHiddenInputId,
29
+ getLabelId: () => getLabelId,
30
+ getMarkerId: () => getMarkerId,
31
+ getOffsetRect: () => getOffsetRect,
32
+ getPointValue: () => getPointValue,
33
+ getRangeEl: () => getRangeEl,
34
+ getRangeId: () => getRangeId,
35
+ getRootEl: () => getRootEl,
36
+ getRootId: () => getRootId,
37
+ getThumbEl: () => getThumbEl,
38
+ getThumbEls: () => getThumbEls,
39
+ getThumbId: () => getThumbId,
40
+ getTrackId: () => getTrackId,
41
+ getValueTextId: () => getValueTextId
42
+ });
43
+ module.exports = __toCommonJS(slider_dom_exports);
44
+ var import_dom_query = require("@zag-js/dom-query");
45
+ var import_utils = require("@zag-js/utils");
46
+ var getRootId = (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`;
47
+ var getThumbId = (ctx, index) => ctx.ids?.thumb?.(index) ?? `slider:${ctx.id}:thumb:${index}`;
48
+ var getHiddenInputId = (ctx, index) => ctx.ids?.hiddenInput?.(index) ?? `slider:${ctx.id}:input:${index}`;
49
+ var getControlId = (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`;
50
+ var getTrackId = (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:track`;
51
+ var getRangeId = (ctx) => ctx.ids?.range ?? `slider:${ctx.id}:range`;
52
+ var getLabelId = (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`;
53
+ var getValueTextId = (ctx) => ctx.ids?.valueText ?? `slider:${ctx.id}:value-text`;
54
+ var getMarkerId = (ctx, value) => ctx.ids?.marker?.(value) ?? `slider:${ctx.id}:marker:${value}`;
55
+ var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
56
+ var getThumbEl = (ctx, index) => ctx.getById(getThumbId(ctx, index));
57
+ var getThumbEls = (ctx) => (0, import_dom_query.queryAll)(getControlEl(ctx), "[role=slider]");
58
+ var getFirstThumbEl = (ctx) => getThumbEls(ctx)[0];
59
+ var getHiddenInputEl = (ctx, index) => ctx.getById(getHiddenInputId(ctx, index));
60
+ var getControlEl = (ctx) => ctx.getById(getControlId(ctx));
61
+ var getRangeEl = (ctx) => ctx.getById(getRangeId(ctx));
62
+ var getThumbInset = (thumbSize, thumbAlignment, orientation) => {
63
+ const isContain = thumbAlignment === "contain";
64
+ const isVertical = orientation === "vertical";
65
+ return isContain ? (isVertical ? thumbSize?.height ?? 0 : thumbSize?.width ?? 0) / 2 : 0;
66
+ };
67
+ var getPointValue = (params, point) => {
68
+ const { context, prop, scope, refs } = params;
69
+ const controlEl = getControlEl(scope);
70
+ if (!controlEl) return;
71
+ const offset = refs.get("thumbDragOffset");
72
+ const adjustedPoint = {
73
+ x: point.x - (offset?.x ?? 0),
74
+ y: point.y - (offset?.y ?? 0)
75
+ };
76
+ const thumbInset = getThumbInset(context.get("thumbSize"), prop("thumbAlignment"), prop("orientation"));
77
+ const relativePoint = getRelativePointWithInset(adjustedPoint, controlEl, thumbInset);
78
+ const percent = relativePoint.getPercentValue({
79
+ orientation: prop("orientation"),
80
+ dir: prop("dir"),
81
+ inverted: { y: true }
82
+ });
83
+ return (0, import_utils.getPercentValue)(percent, prop("min"), prop("max"), prop("step"));
84
+ };
85
+ function getRelativePointWithInset(point, element, inset) {
86
+ const { left, top, width, height } = element.getBoundingClientRect();
87
+ const effectiveWidth = width - inset * 2;
88
+ const effectiveHeight = height - inset * 2;
89
+ const effectiveLeft = left + inset;
90
+ const effectiveTop = top + inset;
91
+ const offset = {
92
+ x: point.x - effectiveLeft,
93
+ y: point.y - effectiveTop
94
+ };
95
+ const percent = {
96
+ x: effectiveWidth > 0 ? (0, import_utils.clampPercent)(offset.x / effectiveWidth) : 0,
97
+ y: effectiveHeight > 0 ? (0, import_utils.clampPercent)(offset.y / effectiveHeight) : 0
98
+ };
99
+ function getPercentValue2(options = {}) {
100
+ const { dir = "ltr", orientation = "horizontal", inverted } = options;
101
+ const invertX = typeof inverted === "object" ? inverted.x : inverted;
102
+ const invertY = typeof inverted === "object" ? inverted.y : inverted;
103
+ if (orientation === "horizontal") {
104
+ return dir === "rtl" || invertX ? 1 - percent.x : percent.x;
105
+ }
106
+ return invertY ? 1 - percent.y : percent.y;
107
+ }
108
+ return { offset, percent, getPercentValue: getPercentValue2 };
109
+ }
110
+ var dispatchChangeEvent = (ctx, value) => {
111
+ value.forEach((value2, index) => {
112
+ const inputEl = getHiddenInputEl(ctx, index);
113
+ if (!inputEl) return;
114
+ (0, import_dom_query.dispatchInputValueEvent)(inputEl, { value: value2 });
115
+ });
116
+ };
117
+ var getOffsetRect = (el) => ({
118
+ left: el?.offsetLeft ?? 0,
119
+ top: el?.offsetTop ?? 0,
120
+ width: el?.offsetWidth ?? 0,
121
+ height: el?.offsetHeight ?? 0
122
+ });
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ dispatchChangeEvent,
126
+ getControlEl,
127
+ getControlId,
128
+ getFirstThumbEl,
129
+ getHiddenInputEl,
130
+ getHiddenInputId,
131
+ getLabelId,
132
+ getMarkerId,
133
+ getOffsetRect,
134
+ getPointValue,
135
+ getRangeEl,
136
+ getRangeId,
137
+ getRootEl,
138
+ getRootId,
139
+ getThumbEl,
140
+ getThumbEls,
141
+ getThumbId,
142
+ getTrackId,
143
+ getValueTextId
144
+ });
@@ -0,0 +1,101 @@
1
+ // src/slider.dom.ts
2
+ import { dispatchInputValueEvent, queryAll } from "@zag-js/dom-query";
3
+ import { clampPercent, getPercentValue } from "@zag-js/utils";
4
+ var getRootId = (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`;
5
+ var getThumbId = (ctx, index) => ctx.ids?.thumb?.(index) ?? `slider:${ctx.id}:thumb:${index}`;
6
+ var getHiddenInputId = (ctx, index) => ctx.ids?.hiddenInput?.(index) ?? `slider:${ctx.id}:input:${index}`;
7
+ var getControlId = (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`;
8
+ var getTrackId = (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:track`;
9
+ var getRangeId = (ctx) => ctx.ids?.range ?? `slider:${ctx.id}:range`;
10
+ var getLabelId = (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`;
11
+ var getValueTextId = (ctx) => ctx.ids?.valueText ?? `slider:${ctx.id}:value-text`;
12
+ var getMarkerId = (ctx, value) => ctx.ids?.marker?.(value) ?? `slider:${ctx.id}:marker:${value}`;
13
+ var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
14
+ var getThumbEl = (ctx, index) => ctx.getById(getThumbId(ctx, index));
15
+ var getThumbEls = (ctx) => queryAll(getControlEl(ctx), "[role=slider]");
16
+ var getFirstThumbEl = (ctx) => getThumbEls(ctx)[0];
17
+ var getHiddenInputEl = (ctx, index) => ctx.getById(getHiddenInputId(ctx, index));
18
+ var getControlEl = (ctx) => ctx.getById(getControlId(ctx));
19
+ var getRangeEl = (ctx) => ctx.getById(getRangeId(ctx));
20
+ var getThumbInset = (thumbSize, thumbAlignment, orientation) => {
21
+ const isContain = thumbAlignment === "contain";
22
+ const isVertical = orientation === "vertical";
23
+ return isContain ? (isVertical ? thumbSize?.height ?? 0 : thumbSize?.width ?? 0) / 2 : 0;
24
+ };
25
+ var getPointValue = (params, point) => {
26
+ const { context, prop, scope, refs } = params;
27
+ const controlEl = getControlEl(scope);
28
+ if (!controlEl) return;
29
+ const offset = refs.get("thumbDragOffset");
30
+ const adjustedPoint = {
31
+ x: point.x - (offset?.x ?? 0),
32
+ y: point.y - (offset?.y ?? 0)
33
+ };
34
+ const thumbInset = getThumbInset(context.get("thumbSize"), prop("thumbAlignment"), prop("orientation"));
35
+ const relativePoint = getRelativePointWithInset(adjustedPoint, controlEl, thumbInset);
36
+ const percent = relativePoint.getPercentValue({
37
+ orientation: prop("orientation"),
38
+ dir: prop("dir"),
39
+ inverted: { y: true }
40
+ });
41
+ return getPercentValue(percent, prop("min"), prop("max"), prop("step"));
42
+ };
43
+ function getRelativePointWithInset(point, element, inset) {
44
+ const { left, top, width, height } = element.getBoundingClientRect();
45
+ const effectiveWidth = width - inset * 2;
46
+ const effectiveHeight = height - inset * 2;
47
+ const effectiveLeft = left + inset;
48
+ const effectiveTop = top + inset;
49
+ const offset = {
50
+ x: point.x - effectiveLeft,
51
+ y: point.y - effectiveTop
52
+ };
53
+ const percent = {
54
+ x: effectiveWidth > 0 ? clampPercent(offset.x / effectiveWidth) : 0,
55
+ y: effectiveHeight > 0 ? clampPercent(offset.y / effectiveHeight) : 0
56
+ };
57
+ function getPercentValue2(options = {}) {
58
+ const { dir = "ltr", orientation = "horizontal", inverted } = options;
59
+ const invertX = typeof inverted === "object" ? inverted.x : inverted;
60
+ const invertY = typeof inverted === "object" ? inverted.y : inverted;
61
+ if (orientation === "horizontal") {
62
+ return dir === "rtl" || invertX ? 1 - percent.x : percent.x;
63
+ }
64
+ return invertY ? 1 - percent.y : percent.y;
65
+ }
66
+ return { offset, percent, getPercentValue: getPercentValue2 };
67
+ }
68
+ var dispatchChangeEvent = (ctx, value) => {
69
+ value.forEach((value2, index) => {
70
+ const inputEl = getHiddenInputEl(ctx, index);
71
+ if (!inputEl) return;
72
+ dispatchInputValueEvent(inputEl, { value: value2 });
73
+ });
74
+ };
75
+ var getOffsetRect = (el) => ({
76
+ left: el?.offsetLeft ?? 0,
77
+ top: el?.offsetTop ?? 0,
78
+ width: el?.offsetWidth ?? 0,
79
+ height: el?.offsetHeight ?? 0
80
+ });
81
+ export {
82
+ dispatchChangeEvent,
83
+ getControlEl,
84
+ getControlId,
85
+ getFirstThumbEl,
86
+ getHiddenInputEl,
87
+ getHiddenInputId,
88
+ getLabelId,
89
+ getMarkerId,
90
+ getOffsetRect,
91
+ getPointValue,
92
+ getRangeEl,
93
+ getRangeId,
94
+ getRootEl,
95
+ getRootId,
96
+ getThumbEl,
97
+ getThumbEls,
98
+ getThumbId,
99
+ getTrackId,
100
+ getValueTextId
101
+ };
@@ -0,0 +1,7 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { SliderSchema } from './slider.types.mjs';
3
+ import '@zag-js/types';
4
+
5
+ declare const machine: _zag_js_core.Machine<SliderSchema>;
6
+
7
+ export { machine };
@@ -0,0 +1,7 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { SliderSchema } from './slider.types.js';
3
+ import '@zag-js/types';
4
+
5
+ declare const machine: _zag_js_core.Machine<SliderSchema>;
6
+
7
+ export { machine };