@zag-js/slider 0.46.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.
- package/dist/index.d.mts +9 -5
- package/dist/index.d.ts +9 -5
- package/dist/index.js +42 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
- package/src/slider.connect.ts +44 -41
- package/src/slider.types.ts +8 -3
package/src/slider.connect.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
getEventStep,
|
|
5
5
|
getNativeEvent,
|
|
6
6
|
isLeftClick,
|
|
7
|
-
|
|
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
|
|
24
|
-
const
|
|
23
|
+
const focused = state.matches("focus")
|
|
24
|
+
const dragging = state.matches("dragging")
|
|
25
25
|
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
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
|
-
|
|
41
|
-
|
|
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 (!
|
|
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(
|
|
80
|
+
"data-disabled": dataAttr(disabled),
|
|
81
81
|
"data-orientation": state.context.orientation,
|
|
82
|
-
"data-invalid": dataAttr(
|
|
83
|
-
"data-focus": dataAttr(
|
|
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 (!
|
|
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(
|
|
98
|
+
"data-disabled": dataAttr(disabled),
|
|
99
99
|
"data-orientation": state.context.orientation,
|
|
100
|
-
"data-invalid": dataAttr(
|
|
101
|
-
"data-focus": dataAttr(
|
|
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(
|
|
110
|
+
"data-disabled": dataAttr(disabled),
|
|
111
111
|
"data-orientation": state.context.orientation,
|
|
112
|
-
"data-invalid": dataAttr(
|
|
113
|
-
"data-focus": dataAttr(
|
|
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(
|
|
122
|
-
"data-invalid": dataAttr(
|
|
121
|
+
"data-disabled": dataAttr(disabled),
|
|
122
|
+
"data-invalid": dataAttr(invalid),
|
|
123
123
|
"data-orientation": state.context.orientation,
|
|
124
|
-
"data-focus": dataAttr(
|
|
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
|
|
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(
|
|
143
|
+
"data-disabled": dataAttr(disabled),
|
|
144
144
|
"data-orientation": state.context.orientation,
|
|
145
|
-
"data-focus": dataAttr(
|
|
145
|
+
"data-focus": dataAttr(focused && state.context.focusedIndex === index),
|
|
146
146
|
draggable: false,
|
|
147
|
-
"aria-disabled": ariaAttr(
|
|
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":
|
|
154
|
+
"aria-valuetext": valueText,
|
|
155
155
|
role: "slider",
|
|
156
|
-
tabIndex:
|
|
156
|
+
tabIndex: disabled ? undefined : 0,
|
|
157
157
|
style: dom.getThumbStyle(state.context, index),
|
|
158
158
|
onPointerDown(event) {
|
|
159
|
-
if (!
|
|
159
|
+
if (!interactive) return
|
|
160
160
|
send({ type: "THUMB_POINTER_DOWN", index })
|
|
161
161
|
event.stopPropagation()
|
|
162
162
|
},
|
|
163
163
|
onBlur() {
|
|
164
|
-
if (!
|
|
164
|
+
if (!interactive) return
|
|
165
165
|
send("BLUR")
|
|
166
166
|
},
|
|
167
167
|
onFocus() {
|
|
168
|
-
if (!
|
|
168
|
+
if (!interactive) return
|
|
169
169
|
send({ type: "FOCUS", index })
|
|
170
170
|
},
|
|
171
171
|
onKeyDown(event) {
|
|
172
|
-
if (
|
|
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(
|
|
238
|
-
"data-invalid": dataAttr(
|
|
239
|
-
"data-disabled": dataAttr(
|
|
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(
|
|
251
|
+
"data-disabled": dataAttr(disabled),
|
|
249
252
|
"data-orientation": state.context.orientation,
|
|
250
|
-
"data-invalid": dataAttr(
|
|
251
|
-
"data-focus": dataAttr(
|
|
253
|
+
"data-invalid": dataAttr(invalid),
|
|
254
|
+
"data-focus": dataAttr(focused),
|
|
252
255
|
style: dom.getControlStyle(),
|
|
253
256
|
onPointerDown(event) {
|
|
254
|
-
if (!
|
|
257
|
+
if (!interactive) return
|
|
255
258
|
|
|
256
259
|
const evt = getNativeEvent(event)
|
|
257
|
-
if (!isLeftClick(evt) ||
|
|
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(
|
|
301
|
+
"data-disabled": dataAttr(disabled),
|
|
299
302
|
"data-state": markerState,
|
|
300
303
|
style,
|
|
301
304
|
})
|
package/src/slider.types.ts
CHANGED
|
@@ -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?(
|
|
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
|
-
|
|
222
|
+
dragging: boolean
|
|
218
223
|
/**
|
|
219
224
|
* Whether the slider is focused.
|
|
220
225
|
*/
|
|
221
|
-
|
|
226
|
+
focused: boolean
|
|
222
227
|
/**
|
|
223
228
|
* Function to set the value of the slider.
|
|
224
229
|
*/
|