@zag-js/slider 0.10.5 → 0.11.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.
@@ -1,251 +0,0 @@
1
- import { getEventStep, getEventKey, getNativeEvent, isLeftClick, isModifiedEvent, getEventPoint } from '@zag-js/dom-event';
2
- import { dataAttr, ariaAttr } from '@zag-js/dom-query';
3
- import { getValuePercent, getPercentValue } from '@zag-js/numeric-range';
4
- import { parts } from './slider.anatomy.mjs';
5
- import { dom } from './slider.dom.mjs';
6
-
7
- function connect(state, send, normalize) {
8
- const ariaLabel = state.context["aria-label"];
9
- const ariaLabelledBy = state.context["aria-labelledby"];
10
- const ariaValueText = state.context.getAriaValueText?.(state.context.value);
11
- const isFocused = state.matches("focus");
12
- const isDragging = state.matches("dragging");
13
- const isDisabled = state.context.disabled;
14
- const isInteractive = state.context.isInteractive;
15
- const isInvalid = state.context.invalid;
16
- function getPercentValueFn(percent) {
17
- return getPercentValue(percent, state.context.min, state.context.max, state.context.step);
18
- }
19
- function getValuePercentFn(value) {
20
- return getValuePercent(value, state.context.min, state.context.max);
21
- }
22
- return {
23
- /**
24
- * Whether the slider is focused.
25
- */
26
- isFocused,
27
- /**
28
- * Whether the slider is being dragged.
29
- */
30
- isDragging,
31
- /**
32
- * The value of the slider.
33
- */
34
- value: state.context.value,
35
- /**
36
- * The value of the slider as a percent.
37
- */
38
- percent: getValuePercent(state.context.value, state.context.min, state.context.max),
39
- /**
40
- * Function to set the value of the slider.
41
- */
42
- setValue(value) {
43
- send({ type: "SET_VALUE", value });
44
- },
45
- /**
46
- * Returns the value of the slider at the given percent.
47
- */
48
- getPercentValue: getPercentValueFn,
49
- /**
50
- * Returns the percent of the slider at the given value.
51
- */
52
- getValuePercent: getValuePercentFn,
53
- /**
54
- * Function to focus the slider.
55
- */
56
- focus() {
57
- dom.getThumbEl(state.context)?.focus();
58
- },
59
- /**
60
- * Function to increment the value of the slider by the step.
61
- */
62
- increment() {
63
- send("INCREMENT");
64
- },
65
- /**
66
- * Function to decrement the value of the slider by the step.
67
- */
68
- decrement() {
69
- send("DECREMENT");
70
- },
71
- rootProps: normalize.element({
72
- ...parts.root.attrs,
73
- "data-disabled": dataAttr(isDisabled),
74
- "data-focus": dataAttr(isFocused),
75
- "data-orientation": state.context.orientation,
76
- "data-invalid": dataAttr(isInvalid),
77
- id: dom.getRootId(state.context),
78
- dir: state.context.dir,
79
- style: dom.getRootStyle(state.context)
80
- }),
81
- labelProps: normalize.label({
82
- ...parts.label.attrs,
83
- "data-disabled": dataAttr(isDisabled),
84
- "data-invalid": dataAttr(isInvalid),
85
- "data-focus": dataAttr(isFocused),
86
- id: dom.getLabelId(state.context),
87
- htmlFor: dom.getHiddenInputId(state.context),
88
- onClick(event) {
89
- if (!isInteractive)
90
- return;
91
- event.preventDefault();
92
- dom.getThumbEl(state.context)?.focus();
93
- },
94
- style: dom.getLabelStyle()
95
- }),
96
- thumbProps: normalize.element({
97
- ...parts.thumb.attrs,
98
- id: dom.getThumbId(state.context),
99
- "data-disabled": dataAttr(isDisabled),
100
- "data-orientation": state.context.orientation,
101
- "data-focus": dataAttr(isFocused),
102
- draggable: false,
103
- "aria-invalid": ariaAttr(isInvalid),
104
- "data-invalid": dataAttr(isInvalid),
105
- "aria-disabled": ariaAttr(isDisabled),
106
- "aria-label": ariaLabel,
107
- "aria-labelledby": ariaLabel ? void 0 : ariaLabelledBy ?? dom.getLabelId(state.context),
108
- "aria-orientation": state.context.orientation,
109
- "aria-valuemax": state.context.max,
110
- "aria-valuemin": state.context.min,
111
- "aria-valuenow": state.context.value,
112
- "aria-valuetext": ariaValueText,
113
- role: "slider",
114
- tabIndex: isDisabled ? void 0 : 0,
115
- onBlur() {
116
- if (!isInteractive)
117
- return;
118
- send("BLUR");
119
- },
120
- onFocus() {
121
- if (!isInteractive)
122
- return;
123
- send("FOCUS");
124
- },
125
- onKeyDown(event) {
126
- if (!isInteractive)
127
- return;
128
- const step = getEventStep(event) * state.context.step;
129
- let prevent = true;
130
- const keyMap = {
131
- ArrowUp() {
132
- send({ type: "ARROW_UP", step });
133
- prevent = state.context.isVertical;
134
- },
135
- ArrowDown() {
136
- send({ type: "ARROW_DOWN", step });
137
- prevent = state.context.isVertical;
138
- },
139
- ArrowLeft() {
140
- send({ type: "ARROW_LEFT", step });
141
- prevent = state.context.isHorizontal;
142
- },
143
- ArrowRight() {
144
- send({ type: "ARROW_RIGHT", step });
145
- prevent = state.context.isHorizontal;
146
- },
147
- PageUp() {
148
- send({ type: "PAGE_UP", step });
149
- },
150
- PageDown() {
151
- send({ type: "PAGE_DOWN", step });
152
- },
153
- Home() {
154
- send("HOME");
155
- },
156
- End() {
157
- send("END");
158
- }
159
- };
160
- const key = getEventKey(event, state.context);
161
- const exec = keyMap[key];
162
- if (!exec)
163
- return;
164
- exec(event);
165
- if (prevent) {
166
- event.preventDefault();
167
- }
168
- },
169
- style: dom.getThumbStyle(state.context)
170
- }),
171
- hiddenInputProps: normalize.input({
172
- ...parts.hiddenInput.attrs,
173
- type: "text",
174
- defaultValue: state.context.value,
175
- name: state.context.name,
176
- form: state.context.form,
177
- id: dom.getHiddenInputId(state.context),
178
- hidden: true
179
- }),
180
- outputProps: normalize.output({
181
- ...parts.output.attrs,
182
- "data-disabled": dataAttr(isDisabled),
183
- "data-invalid": dataAttr(isInvalid),
184
- id: dom.getOutputId(state.context),
185
- htmlFor: dom.getHiddenInputId(state.context),
186
- "aria-live": "off"
187
- }),
188
- trackProps: normalize.element({
189
- ...parts.track.attrs,
190
- id: dom.getTrackId(state.context),
191
- "data-disabled": dataAttr(isDisabled),
192
- "data-focus": dataAttr(isFocused),
193
- "data-invalid": dataAttr(isInvalid),
194
- "data-orientation": state.context.orientation,
195
- style: dom.getTrackStyle()
196
- }),
197
- rangeProps: normalize.element({
198
- ...parts.range.attrs,
199
- id: dom.getRangeId(state.context),
200
- "data-focus": dataAttr(isFocused),
201
- "data-invalid": dataAttr(isInvalid),
202
- "data-disabled": dataAttr(isDisabled),
203
- "data-orientation": state.context.orientation,
204
- style: dom.getRangeStyle(state.context)
205
- }),
206
- controlProps: normalize.element({
207
- ...parts.control.attrs,
208
- id: dom.getControlId(state.context),
209
- "data-disabled": dataAttr(isDisabled),
210
- "data-invalid": dataAttr(isInvalid),
211
- "data-orientation": state.context.orientation,
212
- "data-focus": dataAttr(isFocused),
213
- onPointerDown(event) {
214
- if (!isInteractive)
215
- return;
216
- const evt = getNativeEvent(event);
217
- if (!isLeftClick(evt) || isModifiedEvent(evt))
218
- return;
219
- const point = getEventPoint(evt);
220
- send({ type: "POINTER_DOWN", point });
221
- event.preventDefault();
222
- event.stopPropagation();
223
- },
224
- style: dom.getControlStyle()
225
- }),
226
- markerGroupProps: normalize.element({
227
- ...parts.markerGroup.attrs,
228
- role: "presentation",
229
- "aria-hidden": true,
230
- "data-orientation": state.context.orientation,
231
- style: dom.getMarkerGroupStyle()
232
- }),
233
- getMarkerProps({ value }) {
234
- const percent = getValuePercentFn(value);
235
- const style = dom.getMarkerStyle(state.context, percent);
236
- const markerState = value > state.context.value ? "over-value" : value < state.context.value ? "under-value" : "at-value";
237
- return normalize.element({
238
- ...parts.marker.attrs,
239
- id: dom.getMarkerId(state.context, value),
240
- role: "presentation",
241
- "data-orientation": state.context.orientation,
242
- "data-value": value,
243
- "data-disabled": dataAttr(isDisabled),
244
- "data-state": markerState,
245
- style
246
- });
247
- }
248
- };
249
- }
250
-
251
- export { connect };
@@ -1,47 +0,0 @@
1
- import type { JSX } from '@zag-js/types';
2
- import type { SharedContext, MachineContext as Ctx, Point } from './slider.types';
3
- export declare const dom: {
4
- getRootNode: (ctx: {
5
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
6
- }) => ShadowRoot | Document;
7
- getDoc: (ctx: {
8
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
9
- }) => Document;
10
- getWin: (ctx: {
11
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
12
- }) => Window & typeof globalThis;
13
- getActiveElement: (ctx: {
14
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
15
- }) => HTMLElement | null;
16
- getById: <T extends HTMLElement = HTMLElement>(ctx: {
17
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
18
- }, id: string) => T | null;
19
- queryById: <T_1 extends HTMLElement = HTMLElement>(ctx: {
20
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
21
- }, id: string) => T_1;
22
- } & {
23
- getRootId: (ctx: Ctx) => string;
24
- getThumbId: (ctx: Ctx) => string;
25
- getControlId: (ctx: Ctx) => string;
26
- getHiddenInputId: (ctx: Ctx) => string;
27
- getOutputId: (ctx: Ctx) => string;
28
- getTrackId: (ctx: Ctx) => string;
29
- getRangeId: (ctx: Ctx) => string;
30
- getLabelId: (ctx: Ctx) => string;
31
- getMarkerId: (ctx: Ctx, value: number) => string;
32
- getRootEl: (ctx: Ctx) => HTMLElement | null;
33
- getThumbEl: (ctx: Ctx) => HTMLElement | null;
34
- getControlEl: (ctx: Ctx) => HTMLElement;
35
- getHiddenInputEl: (ctx: Ctx) => HTMLInputElement | null;
36
- getValueFromPoint(ctx: Ctx, point: Point): number | undefined;
37
- dispatchChangeEvent(ctx: Ctx): void;
38
- getThumbOffset: (ctx: SharedContext) => string;
39
- getControlStyle: () => JSX.CSSProperties;
40
- getThumbStyle: (ctx: SharedContext) => JSX.CSSProperties;
41
- getRangeStyle: (ctx: Pick<SharedContext, "isVertical" | "isRtl">) => JSX.CSSProperties;
42
- getRootStyle: (ctx: Ctx) => JSX.CSSProperties;
43
- getMarkerStyle: (ctx: Pick<SharedContext, "isHorizontal" | "isRtl">, percent: number) => JSX.CSSProperties;
44
- getLabelStyle: () => JSX.CSSProperties;
45
- getTrackStyle: () => JSX.CSSProperties;
46
- getMarkerGroupStyle: () => JSX.CSSProperties;
47
- };
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const domEvent = require('@zag-js/dom-event');
6
- const domQuery = require('@zag-js/dom-query');
7
- const formUtils = require('@zag-js/form-utils');
8
- const numericRange = require('@zag-js/numeric-range');
9
- const slider_style = require('./slider.style.js');
10
-
11
- const dom = domQuery.createScope({
12
- ...slider_style.styles,
13
- getRootId: (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`,
14
- getThumbId: (ctx) => ctx.ids?.thumb ?? `slider:${ctx.id}:thumb`,
15
- getControlId: (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`,
16
- getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `slider:${ctx.id}:input`,
17
- getOutputId: (ctx) => ctx.ids?.output ?? `slider:${ctx.id}:output`,
18
- getTrackId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}track`,
19
- getRangeId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:range`,
20
- getLabelId: (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`,
21
- getMarkerId: (ctx, value) => `slider:${ctx.id}:marker:${value}`,
22
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
23
- getThumbEl: (ctx) => dom.getById(ctx, dom.getThumbId(ctx)),
24
- getControlEl: (ctx) => dom.queryById(ctx, dom.getControlId(ctx)),
25
- getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx)),
26
- getValueFromPoint(ctx, point) {
27
- const relativePoint = domEvent.getRelativePoint(point, dom.getControlEl(ctx));
28
- const percent = relativePoint.getPercentValue({
29
- orientation: ctx.orientation,
30
- dir: ctx.dir,
31
- inverted: { y: true }
32
- });
33
- return numericRange.getPercentValue(percent, ctx.min, ctx.max, ctx.step);
34
- },
35
- dispatchChangeEvent(ctx) {
36
- const input = dom.getHiddenInputEl(ctx);
37
- if (!input)
38
- return;
39
- formUtils.dispatchInputValueEvent(input, { value: ctx.value });
40
- }
41
- });
42
-
43
- exports.dom = dom;
@@ -1,39 +0,0 @@
1
- import { getRelativePoint } from '@zag-js/dom-event';
2
- import { createScope } from '@zag-js/dom-query';
3
- import { dispatchInputValueEvent } from '@zag-js/form-utils';
4
- import { getPercentValue } from '@zag-js/numeric-range';
5
- import { styles } from './slider.style.mjs';
6
-
7
- const dom = createScope({
8
- ...styles,
9
- getRootId: (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`,
10
- getThumbId: (ctx) => ctx.ids?.thumb ?? `slider:${ctx.id}:thumb`,
11
- getControlId: (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`,
12
- getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `slider:${ctx.id}:input`,
13
- getOutputId: (ctx) => ctx.ids?.output ?? `slider:${ctx.id}:output`,
14
- getTrackId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}track`,
15
- getRangeId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:range`,
16
- getLabelId: (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`,
17
- getMarkerId: (ctx, value) => `slider:${ctx.id}:marker:${value}`,
18
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
19
- getThumbEl: (ctx) => dom.getById(ctx, dom.getThumbId(ctx)),
20
- getControlEl: (ctx) => dom.queryById(ctx, dom.getControlId(ctx)),
21
- getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx)),
22
- getValueFromPoint(ctx, point) {
23
- const relativePoint = getRelativePoint(point, dom.getControlEl(ctx));
24
- const percent = relativePoint.getPercentValue({
25
- orientation: ctx.orientation,
26
- dir: ctx.dir,
27
- inverted: { y: true }
28
- });
29
- return getPercentValue(percent, ctx.min, ctx.max, ctx.step);
30
- },
31
- dispatchChangeEvent(ctx) {
32
- const input = dom.getHiddenInputEl(ctx);
33
- if (!input)
34
- return;
35
- dispatchInputValueEvent(input, { value: ctx.value });
36
- }
37
- });
38
-
39
- export { dom };
@@ -1,3 +0,0 @@
1
- import type { Machine, StateMachine } from '@zag-js/core';
2
- import type { MachineContext, MachineState, UserDefinedContext } from "./slider.types";
3
- export declare function machine(userContext: UserDefinedContext): Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
@@ -1,207 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const core = require('@zag-js/core');
6
- const domEvent = require('@zag-js/dom-event');
7
- const domQuery = require('@zag-js/dom-query');
8
- const elementSize = require('@zag-js/element-size');
9
- const formUtils = require('@zag-js/form-utils');
10
- const numericRange = require('@zag-js/numeric-range');
11
- const utils = require('@zag-js/utils');
12
- const slider_dom = require('./slider.dom.js');
13
- const slider_utils = require('./slider.utils.js');
14
-
15
- function machine(userContext) {
16
- const ctx = utils.compact(userContext);
17
- return core.createMachine(
18
- {
19
- id: "slider",
20
- initial: "idle",
21
- context: {
22
- thumbSize: null,
23
- thumbAlignment: "contain",
24
- disabled: false,
25
- threshold: 5,
26
- dir: "ltr",
27
- origin: "start",
28
- orientation: "horizontal",
29
- initialValue: null,
30
- value: 0,
31
- step: 1,
32
- min: 0,
33
- max: 100,
34
- ...ctx
35
- },
36
- computed: {
37
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
38
- isVertical: (ctx2) => ctx2.orientation === "vertical",
39
- isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
40
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly),
41
- hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null,
42
- valuePercent: (ctx2) => 100 * numericRange.getValuePercent(ctx2.value, ctx2.min, ctx2.max)
43
- },
44
- watch: {
45
- value: ["invokeOnChange", "dispatchChangeEvent"]
46
- },
47
- activities: ["trackFormControlState", "trackThumbSize"],
48
- on: {
49
- SET_VALUE: {
50
- actions: "setValue"
51
- },
52
- INCREMENT: {
53
- actions: "increment"
54
- },
55
- DECREMENT: {
56
- actions: "decrement"
57
- }
58
- },
59
- entry: ["checkValue"],
60
- states: {
61
- idle: {
62
- on: {
63
- POINTER_DOWN: {
64
- target: "dragging",
65
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
66
- },
67
- FOCUS: "focus"
68
- }
69
- },
70
- focus: {
71
- entry: "focusThumb",
72
- on: {
73
- POINTER_DOWN: {
74
- target: "dragging",
75
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
76
- },
77
- ARROW_LEFT: {
78
- guard: "isHorizontal",
79
- actions: "decrement"
80
- },
81
- ARROW_RIGHT: {
82
- guard: "isHorizontal",
83
- actions: "increment"
84
- },
85
- ARROW_UP: {
86
- guard: "isVertical",
87
- actions: "increment"
88
- },
89
- ARROW_DOWN: {
90
- guard: "isVertical",
91
- actions: "decrement"
92
- },
93
- PAGE_UP: {
94
- actions: "increment"
95
- },
96
- PAGE_DOWN: {
97
- actions: "decrement"
98
- },
99
- HOME: {
100
- actions: "setToMin"
101
- },
102
- END: {
103
- actions: "setToMax"
104
- },
105
- BLUR: "idle"
106
- }
107
- },
108
- dragging: {
109
- entry: "focusThumb",
110
- activities: "trackPointerMove",
111
- on: {
112
- POINTER_UP: {
113
- target: "focus",
114
- actions: "invokeOnChangeEnd"
115
- },
116
- POINTER_MOVE: {
117
- actions: "setPointerValue"
118
- }
119
- }
120
- }
121
- }
122
- },
123
- {
124
- guards: {
125
- isHorizontal: (ctx2) => ctx2.isHorizontal,
126
- isVertical: (ctx2) => ctx2.isVertical
127
- },
128
- activities: {
129
- trackFormControlState(ctx2) {
130
- return formUtils.trackFormControl(slider_dom.dom.getHiddenInputEl(ctx2), {
131
- onFieldsetDisabled() {
132
- ctx2.disabled = true;
133
- },
134
- onFormReset() {
135
- if (ctx2.initialValue != null) {
136
- ctx2.value = ctx2.initialValue;
137
- }
138
- }
139
- });
140
- },
141
- trackPointerMove(ctx2, _evt, { send }) {
142
- return domEvent.trackPointerMove(slider_dom.dom.getDoc(ctx2), {
143
- onPointerMove(info) {
144
- send({ type: "POINTER_MOVE", point: info.point });
145
- },
146
- onPointerUp() {
147
- send("POINTER_UP");
148
- }
149
- });
150
- },
151
- trackThumbSize(ctx2, _evt) {
152
- if (ctx2.thumbAlignment !== "contain")
153
- return;
154
- return elementSize.trackElementSize(slider_dom.dom.getThumbEl(ctx2), (size) => {
155
- if (size)
156
- ctx2.thumbSize = size;
157
- });
158
- }
159
- },
160
- actions: {
161
- checkValue(ctx2) {
162
- const value = slider_utils.constrainValue(ctx2, ctx2.value);
163
- ctx2.value = value;
164
- ctx2.initialValue = value;
165
- },
166
- invokeOnChangeStart(ctx2) {
167
- ctx2.onChangeStart?.({ value: ctx2.value });
168
- },
169
- invokeOnChangeEnd(ctx2) {
170
- ctx2.onChangeEnd?.({ value: ctx2.value });
171
- },
172
- invokeOnChange(ctx2) {
173
- ctx2.onChange?.({ value: ctx2.value });
174
- },
175
- dispatchChangeEvent(ctx2) {
176
- slider_dom.dom.dispatchChangeEvent(ctx2);
177
- },
178
- setPointerValue(ctx2, evt) {
179
- const value = slider_dom.dom.getValueFromPoint(ctx2, evt.point);
180
- if (value == null)
181
- return;
182
- ctx2.value = numericRange.clampValue(value, ctx2.min, ctx2.max);
183
- },
184
- focusThumb(ctx2) {
185
- domQuery.raf(() => slider_dom.dom.getThumbEl(ctx2)?.focus());
186
- },
187
- decrement(ctx2, evt) {
188
- ctx2.value = slider_utils.decrement(ctx2, evt.step);
189
- },
190
- increment(ctx2, evt) {
191
- ctx2.value = slider_utils.increment(ctx2, evt.step);
192
- },
193
- setToMin(ctx2) {
194
- ctx2.value = ctx2.min;
195
- },
196
- setToMax(ctx2) {
197
- ctx2.value = ctx2.max;
198
- },
199
- setValue(ctx2, evt) {
200
- ctx2.value = slider_utils.constrainValue(ctx2, evt.value);
201
- }
202
- }
203
- }
204
- );
205
- }
206
-
207
- exports.machine = machine;