@zag-js/slider 0.10.2 → 0.10.4

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.
@@ -1,212 +0,0 @@
1
- import {
2
- dom
3
- } from "./chunk-NYN3VIY4.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,265 +0,0 @@
1
- import {
2
- parts
3
- } from "./chunk-3Y7IIPR5.mjs";
4
- import {
5
- dom
6
- } from "./chunk-NYN3VIY4.mjs";
7
-
8
- // src/slider.connect.ts
9
- import {
10
- getEventKey,
11
- getEventPoint,
12
- getEventStep,
13
- getNativeEvent,
14
- isLeftClick,
15
- isModifiedEvent
16
- } from "@zag-js/dom-event";
17
- import { ariaAttr, dataAttr } from "@zag-js/dom-query";
18
- import { getPercentValue, getValuePercent } from "@zag-js/numeric-range";
19
- function connect(state, send, normalize) {
20
- const ariaLabel = state.context["aria-label"];
21
- const ariaLabelledBy = state.context["aria-labelledby"];
22
- const ariaValueText = state.context.getAriaValueText?.(state.context.value);
23
- const isFocused = state.matches("focus");
24
- const isDragging = state.matches("dragging");
25
- const isDisabled = state.context.disabled;
26
- const isInteractive = state.context.isInteractive;
27
- const isInvalid = state.context.invalid;
28
- function getPercentValueFn(percent) {
29
- return getPercentValue(percent, state.context.min, state.context.max, state.context.step);
30
- }
31
- function getValuePercentFn(value) {
32
- return getValuePercent(value, state.context.min, state.context.max);
33
- }
34
- return {
35
- /**
36
- * Whether the slider is focused.
37
- */
38
- isFocused,
39
- /**
40
- * Whether the slider is being dragged.
41
- */
42
- isDragging,
43
- /**
44
- * The value of the slider.
45
- */
46
- value: state.context.value,
47
- /**
48
- * The value of the slider as a percent.
49
- */
50
- percent: getValuePercent(state.context.value, state.context.min, state.context.max),
51
- /**
52
- * Function to set the value of the slider.
53
- */
54
- setValue(value) {
55
- send({ type: "SET_VALUE", value });
56
- },
57
- /**
58
- * Returns the value of the slider at the given percent.
59
- */
60
- getPercentValue: getPercentValueFn,
61
- /**
62
- * Returns the percent of the slider at the given value.
63
- */
64
- getValuePercent: getValuePercentFn,
65
- /**
66
- * Function to focus the slider.
67
- */
68
- focus() {
69
- dom.getThumbEl(state.context)?.focus();
70
- },
71
- /**
72
- * Function to increment the value of the slider by the step.
73
- */
74
- increment() {
75
- send("INCREMENT");
76
- },
77
- /**
78
- * Function to decrement the value of the slider by the step.
79
- */
80
- decrement() {
81
- send("DECREMENT");
82
- },
83
- rootProps: normalize.element({
84
- ...parts.root.attrs,
85
- "data-disabled": dataAttr(isDisabled),
86
- "data-focus": dataAttr(isFocused),
87
- "data-orientation": state.context.orientation,
88
- "data-invalid": dataAttr(isInvalid),
89
- id: dom.getRootId(state.context),
90
- dir: state.context.dir,
91
- style: dom.getRootStyle(state.context)
92
- }),
93
- labelProps: normalize.label({
94
- ...parts.label.attrs,
95
- "data-disabled": dataAttr(isDisabled),
96
- "data-invalid": dataAttr(isInvalid),
97
- "data-focus": dataAttr(isFocused),
98
- id: dom.getLabelId(state.context),
99
- htmlFor: dom.getHiddenInputId(state.context),
100
- onClick(event) {
101
- if (!isInteractive)
102
- return;
103
- event.preventDefault();
104
- dom.getThumbEl(state.context)?.focus();
105
- },
106
- style: dom.getLabelStyle()
107
- }),
108
- thumbProps: normalize.element({
109
- ...parts.thumb.attrs,
110
- id: dom.getThumbId(state.context),
111
- "data-disabled": dataAttr(isDisabled),
112
- "data-orientation": state.context.orientation,
113
- "data-focus": dataAttr(isFocused),
114
- draggable: false,
115
- "aria-invalid": ariaAttr(isInvalid),
116
- "data-invalid": dataAttr(isInvalid),
117
- "aria-disabled": ariaAttr(isDisabled),
118
- "aria-label": ariaLabel,
119
- "aria-labelledby": ariaLabel ? void 0 : ariaLabelledBy ?? dom.getLabelId(state.context),
120
- "aria-orientation": state.context.orientation,
121
- "aria-valuemax": state.context.max,
122
- "aria-valuemin": state.context.min,
123
- "aria-valuenow": state.context.value,
124
- "aria-valuetext": ariaValueText,
125
- role: "slider",
126
- tabIndex: isDisabled ? void 0 : 0,
127
- onBlur() {
128
- if (!isInteractive)
129
- return;
130
- send("BLUR");
131
- },
132
- onFocus() {
133
- if (!isInteractive)
134
- return;
135
- send("FOCUS");
136
- },
137
- onKeyDown(event) {
138
- if (!isInteractive)
139
- return;
140
- const step = getEventStep(event) * state.context.step;
141
- let prevent = true;
142
- const keyMap = {
143
- ArrowUp() {
144
- send({ type: "ARROW_UP", step });
145
- prevent = state.context.isVertical;
146
- },
147
- ArrowDown() {
148
- send({ type: "ARROW_DOWN", step });
149
- prevent = state.context.isVertical;
150
- },
151
- ArrowLeft() {
152
- send({ type: "ARROW_LEFT", step });
153
- prevent = state.context.isHorizontal;
154
- },
155
- ArrowRight() {
156
- send({ type: "ARROW_RIGHT", step });
157
- prevent = state.context.isHorizontal;
158
- },
159
- PageUp() {
160
- send({ type: "PAGE_UP", step });
161
- },
162
- PageDown() {
163
- send({ type: "PAGE_DOWN", step });
164
- },
165
- Home() {
166
- send("HOME");
167
- },
168
- End() {
169
- send("END");
170
- }
171
- };
172
- const key = getEventKey(event, state.context);
173
- const exec = keyMap[key];
174
- if (!exec)
175
- return;
176
- exec(event);
177
- if (prevent) {
178
- event.preventDefault();
179
- }
180
- },
181
- style: dom.getThumbStyle(state.context)
182
- }),
183
- hiddenInputProps: normalize.input({
184
- ...parts.hiddenInput.attrs,
185
- type: "text",
186
- defaultValue: state.context.value,
187
- name: state.context.name,
188
- form: state.context.form,
189
- id: dom.getHiddenInputId(state.context),
190
- hidden: true
191
- }),
192
- outputProps: normalize.output({
193
- ...parts.output.attrs,
194
- "data-disabled": dataAttr(isDisabled),
195
- "data-invalid": dataAttr(isInvalid),
196
- id: dom.getOutputId(state.context),
197
- htmlFor: dom.getHiddenInputId(state.context),
198
- "aria-live": "off"
199
- }),
200
- trackProps: normalize.element({
201
- ...parts.track.attrs,
202
- id: dom.getTrackId(state.context),
203
- "data-disabled": dataAttr(isDisabled),
204
- "data-focus": dataAttr(isFocused),
205
- "data-invalid": dataAttr(isInvalid),
206
- "data-orientation": state.context.orientation,
207
- style: dom.getTrackStyle()
208
- }),
209
- rangeProps: normalize.element({
210
- ...parts.range.attrs,
211
- id: dom.getRangeId(state.context),
212
- "data-focus": dataAttr(isFocused),
213
- "data-invalid": dataAttr(isInvalid),
214
- "data-disabled": dataAttr(isDisabled),
215
- "data-orientation": state.context.orientation,
216
- style: dom.getRangeStyle(state.context)
217
- }),
218
- controlProps: normalize.element({
219
- ...parts.control.attrs,
220
- id: dom.getControlId(state.context),
221
- "data-disabled": dataAttr(isDisabled),
222
- "data-invalid": dataAttr(isInvalid),
223
- "data-orientation": state.context.orientation,
224
- "data-focus": dataAttr(isFocused),
225
- onPointerDown(event) {
226
- if (!isInteractive)
227
- return;
228
- const evt = getNativeEvent(event);
229
- if (!isLeftClick(evt) || isModifiedEvent(evt))
230
- return;
231
- const point = getEventPoint(evt);
232
- send({ type: "POINTER_DOWN", point });
233
- event.preventDefault();
234
- event.stopPropagation();
235
- },
236
- style: dom.getControlStyle()
237
- }),
238
- markerGroupProps: normalize.element({
239
- ...parts.markerGroup.attrs,
240
- role: "presentation",
241
- "aria-hidden": true,
242
- "data-orientation": state.context.orientation,
243
- style: dom.getMarkerGroupStyle()
244
- }),
245
- getMarkerProps({ value }) {
246
- const percent = getValuePercentFn(value);
247
- const style = dom.getMarkerStyle(state.context, percent);
248
- const markerState = value > state.context.value ? "over-value" : value < state.context.value ? "under-value" : "at-value";
249
- return normalize.element({
250
- ...parts.marker.attrs,
251
- id: dom.getMarkerId(state.context, value),
252
- role: "presentation",
253
- "data-orientation": state.context.orientation,
254
- "data-value": value,
255
- "data-disabled": dataAttr(isDisabled),
256
- "data-state": markerState,
257
- style
258
- });
259
- }
260
- };
261
- }
262
-
263
- export {
264
- connect
265
- };
@@ -1,34 +0,0 @@
1
- // src/slider.utils.ts
2
- import { clampValue, getNextStepValue, getPreviousStepValue, snapValueToStep } from "@zag-js/numeric-range";
3
- function clampPercent(percent) {
4
- return clampValue(percent, 0, 1);
5
- }
6
- function constrainValue(ctx, value) {
7
- const snapValue = snapValueToStep(value, ctx.min, ctx.max, ctx.step);
8
- return clampValue(snapValue, ctx.min, ctx.max);
9
- }
10
- function decrement(ctx, step) {
11
- const index = 0;
12
- const values = getPreviousStepValue(index, {
13
- ...ctx,
14
- step: step ?? ctx.step,
15
- values: [ctx.value]
16
- });
17
- return values[index];
18
- }
19
- function increment(ctx, step) {
20
- const index = 0;
21
- const values = getNextStepValue(index, {
22
- ...ctx,
23
- step: step ?? ctx.step,
24
- values: [ctx.value]
25
- });
26
- return values[index];
27
- }
28
-
29
- export {
30
- clampPercent,
31
- constrainValue,
32
- decrement,
33
- increment
34
- };
@@ -1,18 +0,0 @@
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 __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // src/slider.types.ts
17
- var slider_types_exports = {};
18
- module.exports = __toCommonJS(slider_types_exports);
File without changes