@zag-js/slider 0.2.12 → 0.2.14

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,50 @@
1
+ import {
2
+ styles
3
+ } from "./chunk-IJAAAKZQ.mjs";
4
+
5
+ // src/slider.dom.ts
6
+ import { createScope } from "@zag-js/dom-query";
7
+ import { getRelativePointValue } from "@zag-js/dom-event";
8
+ import { dispatchInputValueEvent } from "@zag-js/form-utils";
9
+ import { getPercentValue } from "@zag-js/numeric-range";
10
+ var dom = createScope({
11
+ ...styles,
12
+ getRootId: (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`,
13
+ getThumbId: (ctx) => ctx.ids?.thumb ?? `slider:${ctx.id}:thumb`,
14
+ getControlId: (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`,
15
+ getHiddenInputId: (ctx) => `slider:${ctx.id}:input`,
16
+ getOutputId: (ctx) => ctx.ids?.output ?? `slider:${ctx.id}:output`,
17
+ getTrackId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}track`,
18
+ getRangeId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:range`,
19
+ getLabelId: (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`,
20
+ getMarkerId: (ctx, value) => `slider:${ctx.id}:marker:${value}`,
21
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
22
+ getThumbEl: (ctx) => dom.getById(ctx, dom.getThumbId(ctx)),
23
+ getControlEl: (ctx) => dom.getById(ctx, dom.getControlId(ctx)),
24
+ getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx)),
25
+ getValueFromPoint(ctx, point) {
26
+ const el = dom.getControlEl(ctx);
27
+ if (!el)
28
+ return;
29
+ const relativePoint = getRelativePointValue(point, el);
30
+ const percentX = relativePoint.x / el.offsetWidth;
31
+ const percentY = relativePoint.y / el.offsetHeight;
32
+ let percent;
33
+ if (ctx.isHorizontal) {
34
+ percent = ctx.isRtl ? 1 - percentX : percentX;
35
+ } else {
36
+ percent = 1 - percentY;
37
+ }
38
+ return getPercentValue(percent, ctx.min, ctx.max, ctx.step);
39
+ },
40
+ dispatchChangeEvent(ctx) {
41
+ const input = dom.getHiddenInputEl(ctx);
42
+ if (!input)
43
+ return;
44
+ dispatchInputValueEvent(input, ctx.value);
45
+ }
46
+ });
47
+
48
+ export {
49
+ dom
50
+ };
@@ -0,0 +1,212 @@
1
+ import {
2
+ dom
3
+ } from "./chunk-5CWNUPC7.mjs";
4
+ import {
5
+ constrainValue,
6
+ decrement,
7
+ increment
8
+ } from "./chunk-YGFSMEUO.mjs";
9
+
10
+ // src/slider.machine.ts
11
+ import { createMachine } from "@zag-js/core";
12
+ import { trackPointerMove } from "@zag-js/dom-event";
13
+ import { raf } from "@zag-js/dom-query";
14
+ import { trackElementSize } from "@zag-js/element-size";
15
+ import { trackFormControl } from "@zag-js/form-utils";
16
+ import { clampValue, getValuePercent } from "@zag-js/numeric-range";
17
+ import { compact } from "@zag-js/utils";
18
+ function machine(userContext) {
19
+ const ctx = compact(userContext);
20
+ return createMachine(
21
+ {
22
+ id: "slider",
23
+ initial: "idle",
24
+ context: {
25
+ thumbSize: null,
26
+ thumbAlignment: "contain",
27
+ disabled: false,
28
+ threshold: 5,
29
+ dir: "ltr",
30
+ origin: "start",
31
+ orientation: "horizontal",
32
+ initialValue: null,
33
+ value: 0,
34
+ step: 1,
35
+ min: 0,
36
+ max: 100,
37
+ ...ctx
38
+ },
39
+ computed: {
40
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
41
+ isVertical: (ctx2) => ctx2.orientation === "vertical",
42
+ isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
43
+ isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly),
44
+ hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null,
45
+ valuePercent: (ctx2) => 100 * getValuePercent(ctx2.value, ctx2.min, ctx2.max)
46
+ },
47
+ watch: {
48
+ value: ["invokeOnChange", "dispatchChangeEvent"]
49
+ },
50
+ activities: ["trackFormControlState", "trackThumbSize"],
51
+ on: {
52
+ SET_VALUE: {
53
+ actions: "setValue"
54
+ },
55
+ INCREMENT: {
56
+ actions: "increment"
57
+ },
58
+ DECREMENT: {
59
+ actions: "decrement"
60
+ }
61
+ },
62
+ entry: ["checkValue"],
63
+ states: {
64
+ idle: {
65
+ on: {
66
+ POINTER_DOWN: {
67
+ target: "dragging",
68
+ actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
69
+ },
70
+ FOCUS: "focus"
71
+ }
72
+ },
73
+ focus: {
74
+ entry: "focusThumb",
75
+ on: {
76
+ POINTER_DOWN: {
77
+ target: "dragging",
78
+ actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
79
+ },
80
+ ARROW_LEFT: {
81
+ guard: "isHorizontal",
82
+ actions: "decrement"
83
+ },
84
+ ARROW_RIGHT: {
85
+ guard: "isHorizontal",
86
+ actions: "increment"
87
+ },
88
+ ARROW_UP: {
89
+ guard: "isVertical",
90
+ actions: "increment"
91
+ },
92
+ ARROW_DOWN: {
93
+ guard: "isVertical",
94
+ actions: "decrement"
95
+ },
96
+ PAGE_UP: {
97
+ actions: "increment"
98
+ },
99
+ PAGE_DOWN: {
100
+ actions: "decrement"
101
+ },
102
+ HOME: {
103
+ actions: "setToMin"
104
+ },
105
+ END: {
106
+ actions: "setToMax"
107
+ },
108
+ BLUR: "idle"
109
+ }
110
+ },
111
+ dragging: {
112
+ entry: "focusThumb",
113
+ activities: "trackPointerMove",
114
+ on: {
115
+ POINTER_UP: {
116
+ target: "focus",
117
+ actions: "invokeOnChangeEnd"
118
+ },
119
+ POINTER_MOVE: {
120
+ actions: "setPointerValue"
121
+ }
122
+ }
123
+ }
124
+ }
125
+ },
126
+ {
127
+ guards: {
128
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
129
+ isVertical: (ctx2) => ctx2.isVertical
130
+ },
131
+ activities: {
132
+ trackFormControlState(ctx2) {
133
+ return trackFormControl(dom.getHiddenInputEl(ctx2), {
134
+ onFieldsetDisabled() {
135
+ ctx2.disabled = true;
136
+ },
137
+ onFormReset() {
138
+ if (ctx2.initialValue != null) {
139
+ ctx2.value = ctx2.initialValue;
140
+ }
141
+ }
142
+ });
143
+ },
144
+ trackPointerMove(ctx2, _evt, { send }) {
145
+ return trackPointerMove(dom.getDoc(ctx2), {
146
+ onPointerMove(info) {
147
+ send({ type: "POINTER_MOVE", point: info.point });
148
+ },
149
+ onPointerUp() {
150
+ send("POINTER_UP");
151
+ }
152
+ });
153
+ },
154
+ trackThumbSize(ctx2, _evt) {
155
+ if (ctx2.thumbAlignment !== "contain")
156
+ return;
157
+ return trackElementSize(dom.getThumbEl(ctx2), (size) => {
158
+ if (size)
159
+ ctx2.thumbSize = size;
160
+ });
161
+ }
162
+ },
163
+ actions: {
164
+ checkValue(ctx2) {
165
+ const value = constrainValue(ctx2, ctx2.value);
166
+ ctx2.value = value;
167
+ ctx2.initialValue = value;
168
+ },
169
+ invokeOnChangeStart(ctx2) {
170
+ ctx2.onChangeStart?.({ value: ctx2.value });
171
+ },
172
+ invokeOnChangeEnd(ctx2) {
173
+ ctx2.onChangeEnd?.({ value: ctx2.value });
174
+ },
175
+ invokeOnChange(ctx2) {
176
+ ctx2.onChange?.({ value: ctx2.value });
177
+ },
178
+ dispatchChangeEvent(ctx2) {
179
+ dom.dispatchChangeEvent(ctx2);
180
+ },
181
+ setPointerValue(ctx2, evt) {
182
+ const value = dom.getValueFromPoint(ctx2, evt.point);
183
+ if (value == null)
184
+ return;
185
+ ctx2.value = clampValue(value, ctx2.min, ctx2.max);
186
+ },
187
+ focusThumb(ctx2) {
188
+ raf(() => dom.getThumbEl(ctx2)?.focus());
189
+ },
190
+ decrement(ctx2, evt) {
191
+ ctx2.value = decrement(ctx2, evt.step);
192
+ },
193
+ increment(ctx2, evt) {
194
+ ctx2.value = increment(ctx2, evt.step);
195
+ },
196
+ setToMin(ctx2) {
197
+ ctx2.value = ctx2.min;
198
+ },
199
+ setToMax(ctx2) {
200
+ ctx2.value = ctx2.max;
201
+ },
202
+ setValue(ctx2, evt) {
203
+ ctx2.value = constrainValue(ctx2, evt.value);
204
+ }
205
+ }
206
+ }
207
+ );
208
+ }
209
+
210
+ export {
211
+ machine
212
+ };
@@ -1,70 +1,13 @@
1
1
  import {
2
2
  parts
3
3
  } from "./chunk-3Y7IIPR5.mjs";
4
- import {
5
- getNativeEvent,
6
- isLeftClick,
7
- isModifiedEvent,
8
- isTouchEvent
9
- } from "./chunk-3UP6QL6A.mjs";
10
4
  import {
11
5
  dom
12
- } from "./chunk-55KEN77D.mjs";
13
-
14
- // ../../utilities/dom/src/attrs.ts
15
- var dataAttr = (guard) => {
16
- return guard ? "" : void 0;
17
- };
18
-
19
- // ../../utilities/dom/src/get-event-point.ts
20
- var fallback = {
21
- pageX: 0,
22
- pageY: 0,
23
- clientX: 0,
24
- clientY: 0
25
- };
26
- function getEventPoint(event, type = "page") {
27
- const point = isTouchEvent(event) ? event.touches[0] ?? event.changedTouches[0] ?? fallback : event;
28
- return { x: point[`${type}X`], y: point[`${type}Y`] };
29
- }
30
-
31
- // ../../utilities/dom/src/keyboard-event.ts
32
- var rtlKeyMap = {
33
- ArrowLeft: "ArrowRight",
34
- ArrowRight: "ArrowLeft"
35
- };
36
- var sameKeyMap = {
37
- Up: "ArrowUp",
38
- Down: "ArrowDown",
39
- Esc: "Escape",
40
- " ": "Space",
41
- ",": "Comma",
42
- Left: "ArrowLeft",
43
- Right: "ArrowRight"
44
- };
45
- function getEventKey(event, options = {}) {
46
- const { dir = "ltr", orientation = "horizontal" } = options;
47
- let { key } = event;
48
- key = sameKeyMap[key] ?? key;
49
- const isRtl = dir === "rtl" && orientation === "horizontal";
50
- if (isRtl && key in rtlKeyMap) {
51
- key = rtlKeyMap[key];
52
- }
53
- return key;
54
- }
55
- var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
56
- var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
57
- function getEventStep(event) {
58
- if (event.ctrlKey || event.metaKey) {
59
- return 0.1;
60
- } else {
61
- const isPageKey = PAGE_KEYS.has(event.key);
62
- const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
63
- return isSkipKey ? 10 : 1;
64
- }
65
- }
6
+ } from "./chunk-5CWNUPC7.mjs";
66
7
 
67
8
  // src/slider.connect.ts
9
+ import { getEventKey, getEventStep, getNativeEvent, isLeftClick, isModifiedEvent } from "@zag-js/dom-event";
10
+ import { ariaAttr, dataAttr } from "@zag-js/dom-query";
68
11
  import { getPercentValue, getValuePercent } from "@zag-js/numeric-range";
69
12
  function connect(state, send, normalize) {
70
13
  const ariaLabel = state.context["aria-label"];
@@ -82,21 +25,51 @@ function connect(state, send, normalize) {
82
25
  return getValuePercent(value, state.context.min, state.context.max);
83
26
  }
84
27
  return {
28
+ /**
29
+ * Whether the slider is focused.
30
+ */
85
31
  isFocused,
32
+ /**
33
+ * Whether the slider is being dragged.
34
+ */
86
35
  isDragging,
36
+ /**
37
+ * The value of the slider.
38
+ */
87
39
  value: state.context.value,
40
+ /**
41
+ * The value of the slider as a percent.
42
+ */
88
43
  percent: getValuePercent(state.context.value, state.context.min, state.context.max),
44
+ /**
45
+ * Function to set the value of the slider.
46
+ */
89
47
  setValue(value) {
90
48
  send({ type: "SET_VALUE", value });
91
49
  },
50
+ /**
51
+ * Returns the value of the slider at the given percent.
52
+ */
92
53
  getPercentValue: getPercentValueFn,
54
+ /**
55
+ * Returns the percent of the slider at the given value.
56
+ */
93
57
  getValuePercent: getValuePercentFn,
58
+ /**
59
+ * Function to focus the slider.
60
+ */
94
61
  focus() {
95
62
  dom.getThumbEl(state.context)?.focus();
96
63
  },
64
+ /**
65
+ * Function to increment the value of the slider by the step.
66
+ */
97
67
  increment() {
98
68
  send("INCREMENT");
99
69
  },
70
+ /**
71
+ * Function to decrement the value of the slider by the step.
72
+ */
100
73
  decrement() {
101
74
  send("DECREMENT");
102
75
  },
@@ -132,9 +105,9 @@ function connect(state, send, normalize) {
132
105
  "data-orientation": state.context.orientation,
133
106
  "data-focus": dataAttr(isFocused),
134
107
  draggable: false,
135
- "aria-invalid": isInvalid || void 0,
108
+ "aria-invalid": ariaAttr(isInvalid),
136
109
  "data-invalid": dataAttr(isInvalid),
137
- "aria-disabled": isDisabled || void 0,
110
+ "aria-disabled": ariaAttr(isDisabled),
138
111
  "aria-label": ariaLabel,
139
112
  "aria-labelledby": ariaLabel ? void 0 : ariaLabelledBy ?? dom.getLabelId(state.context),
140
113
  "aria-orientation": state.context.orientation,
@@ -248,7 +221,7 @@ function connect(state, send, normalize) {
248
221
  const evt = getNativeEvent(event);
249
222
  if (!isLeftClick(evt) || isModifiedEvent(evt))
250
223
  return;
251
- const point = getEventPoint(evt);
224
+ const point = { x: evt.clientX, y: evt.clientY };
252
225
  send({ type: "POINTER_DOWN", point });
253
226
  event.preventDefault();
254
227
  event.stopPropagation();