@zag-js/slider 0.70.0 → 0.72.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.
@@ -1,312 +0,0 @@
1
- import type { Machine, StateMachine as S } from "@zag-js/core"
2
- import type { CommonProperties, DirectionProperty, PropTypes, RequiredBy } from "@zag-js/types"
3
-
4
- /* -----------------------------------------------------------------------------
5
- * Callback details
6
- * -----------------------------------------------------------------------------*/
7
-
8
- export interface ValueChangeDetails {
9
- value: number[]
10
- }
11
-
12
- export interface FocusChangeDetails {
13
- focusedIndex: number
14
- value: number[]
15
- }
16
-
17
- export interface ValueTextDetails {
18
- value: number
19
- index: number
20
- }
21
-
22
- /* -----------------------------------------------------------------------------
23
- * Machine context
24
- * -----------------------------------------------------------------------------*/
25
-
26
- export type ElementIds = Partial<{
27
- root: string
28
- thumb(index: number): string
29
- hiddenInput(index: number): string
30
- control: string
31
- track: string
32
- range: string
33
- label: string
34
- valueText: string
35
- marker(index: number): string
36
- }>
37
-
38
- interface PublicContext extends DirectionProperty, CommonProperties {
39
- /**
40
- * The ids of the elements in the range slider. Useful for composition.
41
- */
42
- ids?: ElementIds
43
- /**
44
- * The aria-label of each slider thumb. Useful for providing an accessible name to the slider
45
- */
46
- "aria-label"?: string[]
47
- /**
48
- * The `id` of the elements that labels each slider thumb. Useful for providing an accessible name to the slider
49
- */
50
- "aria-labelledby"?: string[]
51
- /**
52
- * The name associated with each slider thumb (when used in a form)
53
- */
54
- name?: string
55
- /**
56
- * The associate form of the underlying input element.
57
- */
58
- form?: string
59
- /**
60
- * The value of the range slider
61
- */
62
- value: number[]
63
- /**
64
- * Whether the slider is disabled
65
- */
66
- disabled?: boolean
67
- /**
68
- * Whether the slider is read-only
69
- */
70
- readOnly?: boolean
71
- /**
72
- * Whether the slider is invalid
73
- */
74
- invalid?: boolean
75
- /**
76
- * Function invoked when the value of the slider changes
77
- */
78
- onValueChange?(details: ValueChangeDetails): void
79
- /**
80
- * Function invoked when the slider value change is done
81
- */
82
- onValueChangeEnd?(details: ValueChangeDetails): void
83
- /**
84
- * Function invoked when the slider's focused index changes
85
- */
86
- onFocusChange?(details: FocusChangeDetails): void
87
- /**
88
- * Function that returns a human readable value for the slider thumb
89
- */
90
- getAriaValueText?(details: ValueTextDetails): string
91
- /**
92
- * The minimum value of the slider
93
- * @default 0
94
- */
95
- min: number
96
- /**
97
- * The maximum value of the slider
98
- * @default 100
99
- */
100
- max: number
101
- /**
102
- * The step value of the slider
103
- * @default 1
104
- */
105
- step: number
106
- /**
107
- * The minimum permitted steps between multiple thumbs.
108
- * @default 0
109
- */
110
- minStepsBetweenThumbs: number
111
- /**
112
- * The orientation of the slider
113
- * @default "horizontal"
114
- */
115
- orientation: "vertical" | "horizontal"
116
- /**
117
- * The origin of the slider range
118
- * - "start": Useful when the value represents an absolute value
119
- * - "center": Useful when the value represents an offset (relative)
120
- *
121
- * @default "start"
122
- */
123
- origin?: "start" | "center"
124
- /**
125
- * The alignment of the slider thumb relative to the track
126
- * - `center`: the thumb will extend beyond the bounds of the slider track.
127
- * - `contain`: the thumb will be contained within the bounds of the track.
128
- *
129
- * @default "contain"
130
- */
131
- thumbAlignment?: "contain" | "center"
132
- /**
133
- * The slider thumbs dimensions
134
- */
135
- thumbSize: { width: number; height: number } | null
136
- }
137
-
138
- export type UserDefinedContext = RequiredBy<PublicContext, "id">
139
-
140
- type ComputedContext = Readonly<{
141
- /**
142
- * @computed
143
- * Whether the slider thumb has been measured
144
- */
145
- hasMeasuredThumbSize: boolean
146
- /**
147
- * @computed
148
- * Whether the slider is interactive
149
- */
150
- isInteractive: boolean
151
- /**
152
- * @computed
153
- * Whether the slider is vertical
154
- */
155
- isVertical: boolean
156
- /**
157
- * @computed
158
- * Whether the slider is horizontal
159
- */
160
- isHorizontal: boolean
161
- /**
162
- * @computed
163
- * Whether the slider is in RTL mode
164
- */
165
- isRtl: boolean
166
- /**
167
- * @computed
168
- * The percentage of the slider value relative to the slider min/max
169
- */
170
- valuePercent: number[]
171
- /**
172
- * @computed
173
- * Whether the slider is disabled
174
- */
175
- isDisabled: boolean
176
- }>
177
-
178
- interface PrivateContext {
179
- /**
180
- * @internal
181
- * The active index of the range slider. This represents
182
- * the currently dragged/focused thumb.
183
- */
184
- focusedIndex: number
185
- /**
186
- * @internal
187
- * Whether the slider fieldset is disabled
188
- */
189
- fieldsetDisabled: boolean
190
- }
191
-
192
- export interface MachineContext extends PublicContext, ComputedContext, PrivateContext {}
193
-
194
- export interface MachineState {
195
- value: "idle" | "dragging" | "focus"
196
- }
197
-
198
- export type State = S.State<MachineContext, MachineState>
199
-
200
- export type Send = S.Send<S.AnyEventObject>
201
-
202
- export type Service = Machine<MachineContext, MachineState, S.AnyEventObject>
203
-
204
- /* -----------------------------------------------------------------------------
205
- * Component API
206
- * -----------------------------------------------------------------------------*/
207
-
208
- interface Size {
209
- width: number
210
- height: number
211
- }
212
-
213
- export interface MarkerProps {
214
- value: number
215
- }
216
-
217
- export interface ThumbProps {
218
- index: number
219
- name?: string
220
- }
221
-
222
- export interface MachineApi<T extends PropTypes = PropTypes> {
223
- /**
224
- * The value of the slider.
225
- */
226
- value: number[]
227
- /**
228
- * Whether the slider is being dragged.
229
- */
230
- dragging: boolean
231
- /**
232
- * Whether the slider is focused.
233
- */
234
- focused: boolean
235
- /**
236
- * Function to set the value of the slider.
237
- */
238
- setValue(value: number[]): void
239
- /**
240
- * Returns the value of the thumb at the given index.
241
- */
242
- getThumbValue(index: number): number
243
- /**
244
- * Sets the value of the thumb at the given index.
245
- */
246
- setThumbValue(index: number, value: number): void
247
- /**
248
- * Returns the percent of the thumb at the given index.
249
- */
250
- getValuePercent: (value: number) => number
251
- /**
252
- * Returns the value of the thumb at the given percent.
253
- */
254
- getPercentValue: (percent: number) => number
255
- /**
256
- * Returns the percent of the thumb at the given index.
257
- */
258
- getThumbPercent(index: number): number
259
- /**
260
- * Sets the percent of the thumb at the given index.
261
- */
262
- setThumbPercent(index: number, percent: number): void
263
- /**
264
- * Returns the min value of the thumb at the given index.
265
- */
266
- getThumbMin(index: number): number
267
- /**
268
- * Returns the max value of the thumb at the given index.
269
- */
270
- getThumbMax(index: number): number
271
- /**
272
- * Function to increment the value of the slider at the given index.
273
- */
274
- increment(index: number): void
275
- /**
276
- * Function to decrement the value of the slider at the given index.
277
- */
278
- decrement(index: number): void
279
- /**
280
- * Function to focus the slider. This focuses the first thumb.
281
- */
282
- focus(): void
283
- getLabelProps(): T["label"]
284
- getRootProps(): T["element"]
285
- getValueTextProps(): T["element"]
286
- getTrackProps(): T["element"]
287
- getThumbProps(props: ThumbProps): T["element"]
288
- getHiddenInputProps(props: ThumbProps): T["input"]
289
- getRangeProps(): T["element"]
290
- getControlProps(): T["element"]
291
- getMarkerGroupProps(): T["element"]
292
- getMarkerProps(props: MarkerProps): T["element"]
293
- }
294
-
295
- /* -----------------------------------------------------------------------------
296
- * Re-exported types
297
- * -----------------------------------------------------------------------------*/
298
-
299
- export interface SharedContext {
300
- min: number
301
- max: number
302
- step: number
303
- dir?: "ltr" | "rtl"
304
- isRtl: boolean
305
- isVertical: boolean
306
- isHorizontal: boolean
307
- value: number
308
- thumbSize: Size | null
309
- thumbAlignment?: "contain" | "center"
310
- orientation?: "horizontal" | "vertical"
311
- readonly hasMeasuredThumbSize: boolean
312
- }
@@ -1,64 +0,0 @@
1
- import {
2
- clampValue,
3
- getClosestValueIndex,
4
- getNextStepValue,
5
- getPreviousStepValue,
6
- getValueRanges,
7
- snapValueToStep,
8
- } from "@zag-js/numeric-range"
9
- import type { MachineContext as Ctx } from "./slider.types"
10
-
11
- export function normalizeValues(ctx: Ctx, nextValues: number[]) {
12
- return nextValues.map((value, index, values) => {
13
- return constrainValue({ ...ctx, value: values }, value, index)
14
- })
15
- }
16
-
17
- export function clampPercent(percent: number) {
18
- return clampValue(percent, 0, 1)
19
- }
20
-
21
- export function getRangeAtIndex(ctx: Ctx, index: number) {
22
- return getValueRanges(ctx.value, ctx.min, ctx.max, ctx.minStepsBetweenThumbs)[index]
23
- }
24
-
25
- export function constrainValue(ctx: Ctx, value: number, index: number) {
26
- const range = getRangeAtIndex(ctx, index)
27
- const snapValue = snapValueToStep(value, ctx.min, ctx.max, ctx.step)
28
- return clampValue(snapValue, range.min, range.max)
29
- }
30
-
31
- export function decrement(ctx: Ctx, index?: number, step?: number) {
32
- const idx = index ?? ctx.focusedIndex
33
- const range = getRangeAtIndex(ctx, idx)
34
- const nextValues = getPreviousStepValue(idx, {
35
- ...range,
36
- step: step ?? ctx.step,
37
- values: ctx.value,
38
- })
39
- nextValues[idx] = clampValue(nextValues[idx], range.min, range.max)
40
- return nextValues
41
- }
42
-
43
- export function increment(ctx: Ctx, index?: number, step?: number) {
44
- const idx = index ?? ctx.focusedIndex
45
- const range = getRangeAtIndex(ctx, idx)
46
- const nextValues = getNextStepValue(idx, {
47
- ...range,
48
- step: step ?? ctx.step,
49
- values: ctx.value,
50
- })
51
- nextValues[idx] = clampValue(nextValues[idx], range.min, range.max)
52
- return nextValues
53
- }
54
-
55
- export function getClosestIndex(ctx: Ctx, pointValue: number) {
56
- return getClosestValueIndex(ctx.value, pointValue)
57
- }
58
-
59
- export function assignArray(current: number[], next: number[]) {
60
- for (let i = 0; i < next.length; i++) {
61
- const value = next[i]
62
- current[i] = value
63
- }
64
- }