@zag-js/slider 0.2.6 → 0.2.8

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,20 @@
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
+ "hiddenInput",
8
+ "output",
9
+ "track",
10
+ "range",
11
+ "control",
12
+ "markerGroup",
13
+ "marker"
14
+ );
15
+ var parts = anatomy.build();
16
+
17
+ export {
18
+ anatomy,
19
+ parts
20
+ };
@@ -0,0 +1,288 @@
1
+ import {
2
+ parts
3
+ } from "./chunk-3Y7IIPR5.mjs";
4
+ import {
5
+ getNativeEvent,
6
+ isLeftClick,
7
+ isModifiedEvent,
8
+ isTouchEvent
9
+ } from "./chunk-SGCWELVB.mjs";
10
+ import {
11
+ dom
12
+ } from "./chunk-J5IGGBVE.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
+ var _a, _b;
28
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
29
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
30
+ }
31
+
32
+ // ../../utilities/dom/src/keyboard-event.ts
33
+ var rtlKeyMap = {
34
+ ArrowLeft: "ArrowRight",
35
+ ArrowRight: "ArrowLeft"
36
+ };
37
+ var sameKeyMap = {
38
+ Up: "ArrowUp",
39
+ Down: "ArrowDown",
40
+ Esc: "Escape",
41
+ " ": "Space",
42
+ ",": "Comma",
43
+ Left: "ArrowLeft",
44
+ Right: "ArrowRight"
45
+ };
46
+ function getEventKey(event, options = {}) {
47
+ var _a;
48
+ const { dir = "ltr", orientation = "horizontal" } = options;
49
+ let { key } = event;
50
+ key = (_a = sameKeyMap[key]) != null ? _a : key;
51
+ const isRtl = dir === "rtl" && orientation === "horizontal";
52
+ if (isRtl && key in rtlKeyMap) {
53
+ key = rtlKeyMap[key];
54
+ }
55
+ return key;
56
+ }
57
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
58
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
59
+ function getEventStep(event) {
60
+ if (event.ctrlKey || event.metaKey) {
61
+ return 0.1;
62
+ } else {
63
+ const isPageKey = PAGE_KEYS.has(event.key);
64
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
65
+ return isSkipKey ? 10 : 1;
66
+ }
67
+ }
68
+
69
+ // src/slider.connect.ts
70
+ import { getPercentValue, getValuePercent } from "@zag-js/numeric-range";
71
+ function connect(state, send, normalize) {
72
+ var _a, _b;
73
+ const ariaLabel = state.context["aria-label"];
74
+ const ariaLabelledBy = state.context["aria-labelledby"];
75
+ const ariaValueText = (_b = (_a = state.context).getAriaValueText) == null ? void 0 : _b.call(_a, state.context.value);
76
+ const isFocused = state.matches("focus");
77
+ const isDragging = state.matches("dragging");
78
+ const isDisabled = state.context.disabled;
79
+ const isInteractive = state.context.isInteractive;
80
+ const isInvalid = state.context.invalid;
81
+ return {
82
+ isFocused,
83
+ isDragging,
84
+ value: state.context.value,
85
+ percent: getValuePercent(state.context.value, state.context.min, state.context.max),
86
+ setValue(value) {
87
+ send({ type: "SET_VALUE", value });
88
+ },
89
+ getPercentValue(percent) {
90
+ return getPercentValue(percent, state.context.min, state.context.max, state.context.step);
91
+ },
92
+ getValuePercent(value) {
93
+ return getValuePercent(value, state.context.min, state.context.max);
94
+ },
95
+ focus() {
96
+ var _a2;
97
+ (_a2 = dom.getThumbEl(state.context)) == null ? void 0 : _a2.focus();
98
+ },
99
+ increment() {
100
+ send("INCREMENT");
101
+ },
102
+ decrement() {
103
+ send("DECREMENT");
104
+ },
105
+ rootProps: normalize.element({
106
+ ...parts.root.attrs,
107
+ "data-disabled": dataAttr(isDisabled),
108
+ "data-focus": dataAttr(isFocused),
109
+ "data-orientation": state.context.orientation,
110
+ "data-invalid": dataAttr(isInvalid),
111
+ id: dom.getRootId(state.context),
112
+ dir: state.context.dir,
113
+ style: dom.getRootStyle(state.context)
114
+ }),
115
+ labelProps: normalize.label({
116
+ ...parts.label.attrs,
117
+ "data-disabled": dataAttr(isDisabled),
118
+ "data-invalid": dataAttr(isInvalid),
119
+ "data-focus": dataAttr(isFocused),
120
+ id: dom.getLabelId(state.context),
121
+ htmlFor: dom.getHiddenInputId(state.context),
122
+ onClick(event) {
123
+ var _a2;
124
+ if (!isInteractive)
125
+ return;
126
+ event.preventDefault();
127
+ (_a2 = dom.getThumbEl(state.context)) == null ? void 0 : _a2.focus();
128
+ },
129
+ style: dom.getLabelStyle()
130
+ }),
131
+ thumbProps: normalize.element({
132
+ ...parts.thumb.attrs,
133
+ id: dom.getThumbId(state.context),
134
+ "data-disabled": dataAttr(isDisabled),
135
+ "data-orientation": state.context.orientation,
136
+ "data-focus": dataAttr(isFocused),
137
+ draggable: false,
138
+ "aria-invalid": isInvalid || void 0,
139
+ "data-invalid": dataAttr(isInvalid),
140
+ "aria-disabled": isDisabled || void 0,
141
+ "aria-label": ariaLabel,
142
+ "aria-labelledby": ariaLabel ? void 0 : ariaLabelledBy != null ? ariaLabelledBy : dom.getLabelId(state.context),
143
+ "aria-orientation": state.context.orientation,
144
+ "aria-valuemax": state.context.max,
145
+ "aria-valuemin": state.context.min,
146
+ "aria-valuenow": state.context.value,
147
+ "aria-valuetext": ariaValueText,
148
+ role: "slider",
149
+ tabIndex: isDisabled ? void 0 : 0,
150
+ onBlur() {
151
+ if (!isInteractive)
152
+ return;
153
+ send("BLUR");
154
+ },
155
+ onFocus() {
156
+ if (!isInteractive)
157
+ return;
158
+ send("FOCUS");
159
+ },
160
+ onKeyDown(event) {
161
+ if (!isInteractive)
162
+ return;
163
+ const step = getEventStep(event) * state.context.step;
164
+ let prevent = true;
165
+ const keyMap = {
166
+ ArrowUp() {
167
+ send({ type: "ARROW_UP", step });
168
+ prevent = state.context.isVertical;
169
+ },
170
+ ArrowDown() {
171
+ send({ type: "ARROW_DOWN", step });
172
+ prevent = state.context.isVertical;
173
+ },
174
+ ArrowLeft() {
175
+ send({ type: "ARROW_LEFT", step });
176
+ prevent = state.context.isHorizontal;
177
+ },
178
+ ArrowRight() {
179
+ send({ type: "ARROW_RIGHT", step });
180
+ prevent = state.context.isHorizontal;
181
+ },
182
+ PageUp() {
183
+ send({ type: "PAGE_UP", step });
184
+ },
185
+ PageDown() {
186
+ send({ type: "PAGE_DOWN", step });
187
+ },
188
+ Home() {
189
+ send("HOME");
190
+ },
191
+ End() {
192
+ send("END");
193
+ }
194
+ };
195
+ const key = getEventKey(event, state.context);
196
+ const exec = keyMap[key];
197
+ if (!exec)
198
+ return;
199
+ exec(event);
200
+ if (prevent) {
201
+ event.preventDefault();
202
+ }
203
+ },
204
+ style: dom.getThumbStyle(state.context)
205
+ }),
206
+ hiddenInputProps: normalize.input({
207
+ ...parts.hiddenInput.attrs,
208
+ type: "text",
209
+ defaultValue: state.context.value,
210
+ name: state.context.name,
211
+ form: state.context.form,
212
+ id: dom.getHiddenInputId(state.context),
213
+ hidden: true
214
+ }),
215
+ outputProps: normalize.output({
216
+ ...parts.output.attrs,
217
+ "data-disabled": dataAttr(isDisabled),
218
+ "data-invalid": dataAttr(isInvalid),
219
+ id: dom.getOutputId(state.context),
220
+ htmlFor: dom.getHiddenInputId(state.context),
221
+ "aria-live": "off"
222
+ }),
223
+ trackProps: normalize.element({
224
+ ...parts.track.attrs,
225
+ id: dom.getTrackId(state.context),
226
+ "data-disabled": dataAttr(isDisabled),
227
+ "data-focus": dataAttr(isFocused),
228
+ "data-invalid": dataAttr(isInvalid),
229
+ "data-orientation": state.context.orientation,
230
+ style: dom.getTrackStyle()
231
+ }),
232
+ rangeProps: normalize.element({
233
+ ...parts.range.attrs,
234
+ id: dom.getRangeId(state.context),
235
+ "data-focus": dataAttr(isFocused),
236
+ "data-invalid": dataAttr(isInvalid),
237
+ "data-disabled": dataAttr(isDisabled),
238
+ "data-orientation": state.context.orientation,
239
+ style: dom.getRangeStyle(state.context)
240
+ }),
241
+ controlProps: normalize.element({
242
+ ...parts.control.attrs,
243
+ id: dom.getControlId(state.context),
244
+ "data-disabled": dataAttr(isDisabled),
245
+ "data-invalid": dataAttr(isInvalid),
246
+ "data-orientation": state.context.orientation,
247
+ "data-focus": dataAttr(isFocused),
248
+ onPointerDown(event) {
249
+ if (!isInteractive)
250
+ return;
251
+ const evt = getNativeEvent(event);
252
+ if (!isLeftClick(evt) || isModifiedEvent(evt))
253
+ return;
254
+ const point = getEventPoint(evt);
255
+ send({ type: "POINTER_DOWN", point });
256
+ event.preventDefault();
257
+ event.stopPropagation();
258
+ },
259
+ style: dom.getControlStyle()
260
+ }),
261
+ markerGroupProps: normalize.element({
262
+ ...parts.markerGroup.attrs,
263
+ role: "presentation",
264
+ "aria-hidden": true,
265
+ "data-orientation": state.context.orientation,
266
+ style: dom.getMarkerGroupStyle()
267
+ }),
268
+ getMarkerProps({ value }) {
269
+ const percent = this.getValuePercent(value);
270
+ const style = dom.getMarkerStyle(state.context, percent);
271
+ const markerState = value > state.context.value ? "over-value" : value < state.context.value ? "under-value" : "at-value";
272
+ return normalize.element({
273
+ ...parts.marker.attrs,
274
+ role: "presentation",
275
+ "data-orientation": state.context.orientation,
276
+ id: dom.getMarkerId(state.context, value),
277
+ "data-value": value,
278
+ "data-disabled": dataAttr(isDisabled),
279
+ "data-state": markerState,
280
+ style
281
+ });
282
+ }
283
+ };
284
+ }
285
+
286
+ export {
287
+ connect
288
+ };