@zag-js/slider 0.47.0 → 0.48.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.
@@ -4,7 +4,7 @@ import {
4
4
  getEventStep,
5
5
  getNativeEvent,
6
6
  isLeftClick,
7
- isModifiedEvent,
7
+ isModifierKey,
8
8
  type EventKeyMap,
9
9
  } from "@zag-js/dom-event"
10
10
  import { ariaAttr, dataAttr } from "@zag-js/dom-query"
@@ -20,12 +20,12 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
20
20
  const ariaLabelledBy = state.context["aria-labelledby"]
21
21
  const sliderValue = state.context.value
22
22
 
23
- const isFocused = state.matches("focus")
24
- const isDragging = state.matches("dragging")
23
+ const focused = state.matches("focus")
24
+ const dragging = state.matches("dragging")
25
25
 
26
- const isDisabled = state.context.isDisabled
27
- const isInvalid = state.context.invalid
28
- const isInteractive = state.context.isInteractive
26
+ const disabled = state.context.isDisabled
27
+ const invalid = state.context.invalid
28
+ const interactive = state.context.isInteractive
29
29
 
30
30
  function getValuePercentFn(value: number) {
31
31
  return getValuePercent(value, state.context.min, state.context.max)
@@ -37,8 +37,8 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
37
37
 
38
38
  return {
39
39
  value: state.context.value,
40
- isDragging,
41
- isFocused,
40
+ dragging,
41
+ focused,
42
42
  setValue(value) {
43
43
  send({ type: "SET_VALUE", value: value })
44
44
  },
@@ -70,21 +70,21 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
70
70
  send({ type: "DECREMENT", index })
71
71
  },
72
72
  focus() {
73
- if (!isInteractive) return
73
+ if (!interactive) return
74
74
  send({ type: "FOCUS", index: 0 })
75
75
  },
76
76
 
77
77
  labelProps: normalize.label({
78
78
  ...parts.label.attrs,
79
79
  dir: state.context.dir,
80
- "data-disabled": dataAttr(isDisabled),
80
+ "data-disabled": dataAttr(disabled),
81
81
  "data-orientation": state.context.orientation,
82
- "data-invalid": dataAttr(isInvalid),
83
- "data-focus": dataAttr(isFocused),
82
+ "data-invalid": dataAttr(invalid),
83
+ "data-focus": dataAttr(focused),
84
84
  id: dom.getLabelId(state.context),
85
85
  htmlFor: dom.getHiddenInputId(state.context, 0),
86
86
  onClick(event) {
87
- if (!isInteractive) return
87
+ if (!interactive) return
88
88
  event.preventDefault()
89
89
  dom.getFirstEl(state.context)?.focus()
90
90
  },
@@ -95,10 +95,10 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
95
95
 
96
96
  rootProps: normalize.element({
97
97
  ...parts.root.attrs,
98
- "data-disabled": dataAttr(isDisabled),
98
+ "data-disabled": dataAttr(disabled),
99
99
  "data-orientation": state.context.orientation,
100
- "data-invalid": dataAttr(isInvalid),
101
- "data-focus": dataAttr(isFocused),
100
+ "data-invalid": dataAttr(invalid),
101
+ "data-focus": dataAttr(focused),
102
102
  id: dom.getRootId(state.context),
103
103
  dir: state.context.dir,
104
104
  style: dom.getRootStyle(state.context),
@@ -107,10 +107,10 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
107
107
  valueTextProps: normalize.element({
108
108
  ...parts.valueText.attrs,
109
109
  dir: state.context.dir,
110
- "data-disabled": dataAttr(isDisabled),
110
+ "data-disabled": dataAttr(disabled),
111
111
  "data-orientation": state.context.orientation,
112
- "data-invalid": dataAttr(isInvalid),
113
- "data-focus": dataAttr(isFocused),
112
+ "data-invalid": dataAttr(invalid),
113
+ "data-focus": dataAttr(focused),
114
114
  id: dom.getValueTextId(state.context),
115
115
  }),
116
116
 
@@ -118,10 +118,10 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
118
118
  ...parts.track.attrs,
119
119
  dir: state.context.dir,
120
120
  id: dom.getTrackId(state.context),
121
- "data-disabled": dataAttr(isDisabled),
122
- "data-invalid": dataAttr(isInvalid),
121
+ "data-disabled": dataAttr(disabled),
122
+ "data-invalid": dataAttr(invalid),
123
123
  "data-orientation": state.context.orientation,
124
- "data-focus": dataAttr(isFocused),
124
+ "data-focus": dataAttr(focused),
125
125
  style: { position: "relative" },
126
126
  }),
127
127
 
@@ -130,7 +130,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
130
130
 
131
131
  const value = sliderValue[index]
132
132
  const range = getRangeAtIndex(state.context, index)
133
- const ariaValueText = state.context.getAriaValueText?.(value, index)
133
+ const valueText = state.context.getAriaValueText?.({ value, index })
134
134
  const _ariaLabel = Array.isArray(ariaLabel) ? ariaLabel[index] : ariaLabel
135
135
  const _ariaLabelledBy = Array.isArray(ariaLabelledBy) ? ariaLabelledBy[index] : ariaLabelledBy
136
136
 
@@ -140,38 +140,41 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
140
140
  "data-index": index,
141
141
  "data-name": name,
142
142
  id: dom.getThumbId(state.context, index),
143
- "data-disabled": dataAttr(isDisabled),
143
+ "data-disabled": dataAttr(disabled),
144
144
  "data-orientation": state.context.orientation,
145
- "data-focus": dataAttr(isFocused && state.context.focusedIndex === index),
145
+ "data-focus": dataAttr(focused && state.context.focusedIndex === index),
146
146
  draggable: false,
147
- "aria-disabled": ariaAttr(isDisabled),
147
+ "aria-disabled": ariaAttr(disabled),
148
148
  "aria-label": _ariaLabel,
149
149
  "aria-labelledby": _ariaLabelledBy ?? dom.getLabelId(state.context),
150
150
  "aria-orientation": state.context.orientation,
151
151
  "aria-valuemax": range.max,
152
152
  "aria-valuemin": range.min,
153
153
  "aria-valuenow": sliderValue[index],
154
- "aria-valuetext": ariaValueText,
154
+ "aria-valuetext": valueText,
155
155
  role: "slider",
156
- tabIndex: isDisabled ? undefined : 0,
156
+ tabIndex: disabled ? undefined : 0,
157
157
  style: dom.getThumbStyle(state.context, index),
158
158
  onPointerDown(event) {
159
- if (!isInteractive) return
159
+ if (!interactive) return
160
160
  send({ type: "THUMB_POINTER_DOWN", index })
161
161
  event.stopPropagation()
162
162
  },
163
163
  onBlur() {
164
- if (!isInteractive) return
164
+ if (!interactive) return
165
165
  send("BLUR")
166
166
  },
167
167
  onFocus() {
168
- if (!isInteractive) return
168
+ if (!interactive) return
169
169
  send({ type: "FOCUS", index })
170
170
  },
171
171
  onKeyDown(event) {
172
- if (!isInteractive) return
172
+ if (event.defaultPrevented) return
173
+ if (!interactive) return
174
+
173
175
  const step = getEventStep(event) * state.context.step
174
176
  let prevent = true
177
+
175
178
  const keyMap: EventKeyMap = {
176
179
  ArrowUp() {
177
180
  send({ type: "ARROW_UP", step })
@@ -234,9 +237,9 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
234
237
  id: dom.getRangeId(state.context),
235
238
  ...parts.range.attrs,
236
239
  dir: state.context.dir,
237
- "data-focus": dataAttr(isFocused),
238
- "data-invalid": dataAttr(isInvalid),
239
- "data-disabled": dataAttr(isDisabled),
240
+ "data-focus": dataAttr(focused),
241
+ "data-invalid": dataAttr(invalid),
242
+ "data-disabled": dataAttr(disabled),
240
243
  "data-orientation": state.context.orientation,
241
244
  style: dom.getRangeStyle(state.context),
242
245
  }),
@@ -245,16 +248,16 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
245
248
  ...parts.control.attrs,
246
249
  dir: state.context.dir,
247
250
  id: dom.getControlId(state.context),
248
- "data-disabled": dataAttr(isDisabled),
251
+ "data-disabled": dataAttr(disabled),
249
252
  "data-orientation": state.context.orientation,
250
- "data-invalid": dataAttr(isInvalid),
251
- "data-focus": dataAttr(isFocused),
253
+ "data-invalid": dataAttr(invalid),
254
+ "data-focus": dataAttr(focused),
252
255
  style: dom.getControlStyle(),
253
256
  onPointerDown(event) {
254
- if (!isInteractive) return
257
+ if (!interactive) return
255
258
 
256
259
  const evt = getNativeEvent(event)
257
- if (!isLeftClick(evt) || isModifiedEvent(evt)) return
260
+ if (!isLeftClick(evt) || isModifierKey(evt)) return
258
261
 
259
262
  const point = getEventPoint(evt)
260
263
  send({ type: "POINTER_DOWN", point })
@@ -295,7 +298,7 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
295
298
  dir: state.context.dir,
296
299
  "data-orientation": state.context.orientation,
297
300
  "data-value": props.value,
298
- "data-disabled": dataAttr(isDisabled),
301
+ "data-disabled": dataAttr(disabled),
299
302
  "data-state": markerState,
300
303
  style,
301
304
  })
@@ -14,6 +14,11 @@ export interface FocusChangeDetails {
14
14
  value: number[]
15
15
  }
16
16
 
17
+ export interface ValueTextDetails {
18
+ value: number
19
+ index: number
20
+ }
21
+
17
22
  /* -----------------------------------------------------------------------------
18
23
  * Machine context
19
24
  * -----------------------------------------------------------------------------*/
@@ -81,7 +86,7 @@ interface PublicContext extends DirectionProperty, CommonProperties {
81
86
  /**
82
87
  * Function that returns a human readable value for the slider thumb
83
88
  */
84
- getAriaValueText?(value: number, index: number): string
89
+ getAriaValueText?(details: ValueTextDetails): string
85
90
  /**
86
91
  * The minimum value of the slider
87
92
  */
@@ -214,11 +219,11 @@ export interface MachineApi<T extends PropTypes = PropTypes> {
214
219
  /**
215
220
  * Whether the slider is being dragged.
216
221
  */
217
- isDragging: boolean
222
+ dragging: boolean
218
223
  /**
219
224
  * Whether the slider is focused.
220
225
  */
221
- isFocused: boolean
226
+ focused: boolean
222
227
  /**
223
228
  * Function to set the value of the slider.
224
229
  */