@react-aria/slider 3.7.7-nightly.4555 → 3.7.7-nightly.4560

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/import.mjs CHANGED
@@ -1,21 +1,7 @@
1
- import {useGlobalListeners as $fA3fN$useGlobalListeners, clamp as $fA3fN$clamp, mergeProps as $fA3fN$mergeProps, focusWithoutScrolling as $fA3fN$focusWithoutScrolling, useFormReset as $fA3fN$useFormReset} from "@react-aria/utils";
2
- import {useRef as $fA3fN$useRef, useCallback as $fA3fN$useCallback, useEffect as $fA3fN$useEffect} from "react";
3
- import {useMove as $fA3fN$useMove, setInteractionModality as $fA3fN$setInteractionModality, useKeyboard as $fA3fN$useKeyboard} from "@react-aria/interactions";
4
- import {useLabel as $fA3fN$useLabel} from "@react-aria/label";
5
- import {useLocale as $fA3fN$useLocale} from "@react-aria/i18n";
6
- import {useFocusable as $fA3fN$useFocusable} from "@react-aria/focus";
1
+ import {useSlider as $bcca50147b47f54d$export$56b2c08e277f365} from "./useSlider.mjs";
2
+ import {useSliderThumb as $47b897dc8cdb026b$export$8d15029008292ae} from "./useSliderThumb.mjs";
7
3
 
8
4
  /*
9
- * Copyright 2020 Adobe. All rights reserved.
10
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
11
- * you may not use this file except in compliance with the License. You may obtain a copy
12
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software distributed under
15
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
16
- * OF ANY KIND, either express or implied. See the License for the specific language
17
- * governing permissions and limitations under the License.
18
- */ /*
19
5
  * Copyright 2020 Adobe. All rights reserved.
20
6
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
21
7
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -26,342 +12,6 @@ import {useFocusable as $fA3fN$useFocusable} from "@react-aria/focus";
26
12
  * OF ANY KIND, either express or implied. See the License for the specific language
27
13
  * governing permissions and limitations under the License.
28
14
  */
29
- const $aa519ee6cf463259$export$d6c8d9636a3dc49c = new WeakMap();
30
- function $aa519ee6cf463259$export$68e648cbec363a18(state, index) {
31
- let data = $aa519ee6cf463259$export$d6c8d9636a3dc49c.get(state);
32
- if (!data) throw new Error("Unknown slider state");
33
- return `${data.id}-${index}`;
34
- }
35
-
36
-
37
-
38
-
39
-
40
-
41
- function $bcca50147b47f54d$export$56b2c08e277f365(props, state, trackRef) {
42
- let { labelProps: labelProps, fieldProps: fieldProps } = (0, $fA3fN$useLabel)(props);
43
- let isVertical = props.orientation === "vertical";
44
- var _labelProps_id;
45
- // Attach id of the label to the state so it can be accessed by useSliderThumb.
46
- (0, $aa519ee6cf463259$export$d6c8d9636a3dc49c).set(state, {
47
- id: (_labelProps_id = labelProps.id) !== null && _labelProps_id !== void 0 ? _labelProps_id : fieldProps.id,
48
- "aria-describedby": props["aria-describedby"],
49
- "aria-details": props["aria-details"]
50
- });
51
- let { direction: direction } = (0, $fA3fN$useLocale)();
52
- let { addGlobalListener: addGlobalListener, removeGlobalListener: removeGlobalListener } = (0, $fA3fN$useGlobalListeners)();
53
- // When the user clicks or drags the track, we want the motion to set and drag the
54
- // closest thumb. Hence we also need to install useMove() on the track element.
55
- // Here, we keep track of which index is the "closest" to the drag start point.
56
- // It is set onMouseDown/onTouchDown; see trackProps below.
57
- const realTimeTrackDraggingIndex = (0, $fA3fN$useRef)(null);
58
- const reverseX = direction === "rtl";
59
- const currentPosition = (0, $fA3fN$useRef)(null);
60
- const { moveProps: moveProps } = (0, $fA3fN$useMove)({
61
- onMoveStart () {
62
- currentPosition.current = null;
63
- },
64
- onMove ({ deltaX: deltaX, deltaY: deltaY }) {
65
- let { height: height, width: width } = trackRef.current.getBoundingClientRect();
66
- let size = isVertical ? height : width;
67
- if (currentPosition.current == null) currentPosition.current = state.getThumbPercent(realTimeTrackDraggingIndex.current) * size;
68
- let delta = isVertical ? deltaY : deltaX;
69
- if (isVertical || reverseX) delta = -delta;
70
- currentPosition.current += delta;
71
- if (realTimeTrackDraggingIndex.current != null && trackRef.current) {
72
- const percent = (0, $fA3fN$clamp)(currentPosition.current / size, 0, 1);
73
- state.setThumbPercent(realTimeTrackDraggingIndex.current, percent);
74
- }
75
- },
76
- onMoveEnd () {
77
- if (realTimeTrackDraggingIndex.current != null) {
78
- state.setThumbDragging(realTimeTrackDraggingIndex.current, false);
79
- realTimeTrackDraggingIndex.current = null;
80
- }
81
- }
82
- });
83
- let currentPointer = (0, $fA3fN$useRef)(undefined);
84
- let onDownTrack = (e, id, clientX, clientY)=>{
85
- // We only trigger track-dragging if the user clicks on the track itself and nothing is currently being dragged.
86
- if (trackRef.current && !props.isDisabled && state.values.every((_, i)=>!state.isThumbDragging(i))) {
87
- let { height: height, width: width, top: top, left: left } = trackRef.current.getBoundingClientRect();
88
- let size = isVertical ? height : width;
89
- // Find the closest thumb
90
- const trackPosition = isVertical ? top : left;
91
- const clickPosition = isVertical ? clientY : clientX;
92
- const offset = clickPosition - trackPosition;
93
- let percent = offset / size;
94
- if (direction === "rtl" || isVertical) percent = 1 - percent;
95
- let value = state.getPercentValue(percent);
96
- // to find the closet thumb we split the array based on the first thumb position to the "right/end" of the click.
97
- let closestThumb;
98
- let split = state.values.findIndex((v)=>value - v < 0);
99
- if (split === 0) closestThumb = split;
100
- else if (split === -1) closestThumb = state.values.length - 1;
101
- else {
102
- let lastLeft = state.values[split - 1];
103
- let firstRight = state.values[split];
104
- // Pick the last left/start thumb, unless they are stacked on top of each other, then pick the right/end one
105
- if (Math.abs(lastLeft - value) < Math.abs(firstRight - value)) closestThumb = split - 1;
106
- else closestThumb = split;
107
- }
108
- // Confirm that the found closest thumb is editable, not disabled, and move it
109
- if (closestThumb >= 0 && state.isThumbEditable(closestThumb)) {
110
- // Don't unfocus anything
111
- e.preventDefault();
112
- realTimeTrackDraggingIndex.current = closestThumb;
113
- state.setFocusedThumb(closestThumb);
114
- currentPointer.current = id;
115
- state.setThumbDragging(realTimeTrackDraggingIndex.current, true);
116
- state.setThumbValue(closestThumb, value);
117
- addGlobalListener(window, "mouseup", onUpTrack, false);
118
- addGlobalListener(window, "touchend", onUpTrack, false);
119
- addGlobalListener(window, "pointerup", onUpTrack, false);
120
- } else realTimeTrackDraggingIndex.current = null;
121
- }
122
- };
123
- let onUpTrack = (e)=>{
124
- var _e_changedTouches;
125
- var _e_pointerId;
126
- let id = (_e_pointerId = e.pointerId) !== null && _e_pointerId !== void 0 ? _e_pointerId : (_e_changedTouches = e.changedTouches) === null || _e_changedTouches === void 0 ? void 0 : _e_changedTouches[0].identifier;
127
- if (id === currentPointer.current) {
128
- if (realTimeTrackDraggingIndex.current != null) {
129
- state.setThumbDragging(realTimeTrackDraggingIndex.current, false);
130
- realTimeTrackDraggingIndex.current = null;
131
- }
132
- removeGlobalListener(window, "mouseup", onUpTrack, false);
133
- removeGlobalListener(window, "touchend", onUpTrack, false);
134
- removeGlobalListener(window, "pointerup", onUpTrack, false);
135
- }
136
- };
137
- if ("htmlFor" in labelProps && labelProps.htmlFor) {
138
- // Ideally the `for` attribute should point to the first thumb, but VoiceOver on iOS
139
- // causes this to override the `aria-labelledby` on the thumb. This causes the first
140
- // thumb to only be announced as the slider label rather than its individual name as well.
141
- // See https://bugs.webkit.org/show_bug.cgi?id=172464.
142
- delete labelProps.htmlFor;
143
- labelProps.onClick = ()=>{
144
- var // Safari does not focus <input type="range"> elements when clicking on an associated <label>,
145
- // so do it manually. In addition, make sure we show the focus ring.
146
- _document_getElementById;
147
- (_document_getElementById = document.getElementById((0, $aa519ee6cf463259$export$68e648cbec363a18)(state, 0))) === null || _document_getElementById === void 0 ? void 0 : _document_getElementById.focus();
148
- (0, $fA3fN$setInteractionModality)("keyboard");
149
- };
150
- }
151
- return {
152
- labelProps: labelProps,
153
- // The root element of the Slider will have role="group" to group together
154
- // all the thumb inputs in the Slider. The label of the Slider will
155
- // be used to label the group.
156
- groupProps: {
157
- role: "group",
158
- ...fieldProps
159
- },
160
- trackProps: (0, $fA3fN$mergeProps)({
161
- onMouseDown (e) {
162
- if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) return;
163
- onDownTrack(e, undefined, e.clientX, e.clientY);
164
- },
165
- onPointerDown (e) {
166
- if (e.pointerType === "mouse" && (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey)) return;
167
- onDownTrack(e, e.pointerId, e.clientX, e.clientY);
168
- },
169
- onTouchStart (e) {
170
- onDownTrack(e, e.changedTouches[0].identifier, e.changedTouches[0].clientX, e.changedTouches[0].clientY);
171
- },
172
- style: {
173
- position: "relative",
174
- touchAction: "none"
175
- }
176
- }, moveProps),
177
- outputProps: {
178
- htmlFor: state.values.map((_, index)=>(0, $aa519ee6cf463259$export$68e648cbec363a18)(state, index)).join(" "),
179
- "aria-live": "off"
180
- }
181
- };
182
- }
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
- function $47b897dc8cdb026b$export$8d15029008292ae(opts, state) {
193
- let { index: index = 0, isRequired: isRequired, validationState: validationState, isInvalid: isInvalid, trackRef: trackRef, inputRef: inputRef, orientation: orientation = state.orientation, name: name } = opts;
194
- let isDisabled = opts.isDisabled || state.isDisabled;
195
- let isVertical = orientation === "vertical";
196
- let { direction: direction } = (0, $fA3fN$useLocale)();
197
- let { addGlobalListener: addGlobalListener, removeGlobalListener: removeGlobalListener } = (0, $fA3fN$useGlobalListeners)();
198
- let data = (0, $aa519ee6cf463259$export$d6c8d9636a3dc49c).get(state);
199
- var _opts_arialabelledby;
200
- const { labelProps: labelProps, fieldProps: fieldProps } = (0, $fA3fN$useLabel)({
201
- ...opts,
202
- id: (0, $aa519ee6cf463259$export$68e648cbec363a18)(state, index),
203
- "aria-labelledby": `${data.id} ${(_opts_arialabelledby = opts["aria-labelledby"]) !== null && _opts_arialabelledby !== void 0 ? _opts_arialabelledby : ""}`.trim()
204
- });
205
- const value = state.values[index];
206
- const focusInput = (0, $fA3fN$useCallback)(()=>{
207
- if (inputRef.current) (0, $fA3fN$focusWithoutScrolling)(inputRef.current);
208
- }, [
209
- inputRef
210
- ]);
211
- const isFocused = state.focusedThumb === index;
212
- (0, $fA3fN$useEffect)(()=>{
213
- if (isFocused) focusInput();
214
- }, [
215
- isFocused,
216
- focusInput
217
- ]);
218
- let reverseX = direction === "rtl";
219
- let currentPosition = (0, $fA3fN$useRef)(null);
220
- let { keyboardProps: keyboardProps } = (0, $fA3fN$useKeyboard)({
221
- onKeyDown (e) {
222
- let { getThumbMaxValue: getThumbMaxValue, getThumbMinValue: getThumbMinValue, decrementThumb: decrementThumb, incrementThumb: incrementThumb, setThumbValue: setThumbValue, setThumbDragging: setThumbDragging, pageSize: pageSize } = state;
223
- // these are the cases that useMove or useSlider don't handle
224
- if (!/^(PageUp|PageDown|Home|End)$/.test(e.key)) {
225
- e.continuePropagation();
226
- return;
227
- }
228
- // same handling as useMove, stopPropagation to prevent useSlider from handling the event as well.
229
- e.preventDefault();
230
- // remember to set this so that onChangeEnd is fired
231
- setThumbDragging(index, true);
232
- switch(e.key){
233
- case "PageUp":
234
- incrementThumb(index, pageSize);
235
- break;
236
- case "PageDown":
237
- decrementThumb(index, pageSize);
238
- break;
239
- case "Home":
240
- setThumbValue(index, getThumbMinValue(index));
241
- break;
242
- case "End":
243
- setThumbValue(index, getThumbMaxValue(index));
244
- break;
245
- }
246
- setThumbDragging(index, false);
247
- }
248
- });
249
- let { moveProps: moveProps } = (0, $fA3fN$useMove)({
250
- onMoveStart () {
251
- currentPosition.current = null;
252
- state.setThumbDragging(index, true);
253
- },
254
- onMove ({ deltaX: deltaX, deltaY: deltaY, pointerType: pointerType, shiftKey: shiftKey }) {
255
- const { getThumbPercent: getThumbPercent, setThumbPercent: setThumbPercent, decrementThumb: decrementThumb, incrementThumb: incrementThumb, step: step, pageSize: pageSize } = state;
256
- let { width: width, height: height } = trackRef.current.getBoundingClientRect();
257
- let size = isVertical ? height : width;
258
- if (currentPosition.current == null) currentPosition.current = getThumbPercent(index) * size;
259
- if (pointerType === "keyboard") {
260
- if (deltaX > 0 && reverseX || deltaX < 0 && !reverseX || deltaY > 0) decrementThumb(index, shiftKey ? pageSize : step);
261
- else incrementThumb(index, shiftKey ? pageSize : step);
262
- } else {
263
- let delta = isVertical ? deltaY : deltaX;
264
- if (isVertical || reverseX) delta = -delta;
265
- currentPosition.current += delta;
266
- setThumbPercent(index, (0, $fA3fN$clamp)(currentPosition.current / size, 0, 1));
267
- }
268
- },
269
- onMoveEnd () {
270
- state.setThumbDragging(index, false);
271
- }
272
- });
273
- // Immediately register editability with the state
274
- state.setThumbEditable(index, !isDisabled);
275
- const { focusableProps: focusableProps } = (0, $fA3fN$useFocusable)((0, $fA3fN$mergeProps)(opts, {
276
- onFocus: ()=>state.setFocusedThumb(index),
277
- onBlur: ()=>state.setFocusedThumb(undefined)
278
- }), inputRef);
279
- let currentPointer = (0, $fA3fN$useRef)(undefined);
280
- let onDown = (id)=>{
281
- focusInput();
282
- currentPointer.current = id;
283
- state.setThumbDragging(index, true);
284
- addGlobalListener(window, "mouseup", onUp, false);
285
- addGlobalListener(window, "touchend", onUp, false);
286
- addGlobalListener(window, "pointerup", onUp, false);
287
- };
288
- let onUp = (e)=>{
289
- var _e_changedTouches;
290
- var _e_pointerId;
291
- let id = (_e_pointerId = e.pointerId) !== null && _e_pointerId !== void 0 ? _e_pointerId : (_e_changedTouches = e.changedTouches) === null || _e_changedTouches === void 0 ? void 0 : _e_changedTouches[0].identifier;
292
- if (id === currentPointer.current) {
293
- focusInput();
294
- state.setThumbDragging(index, false);
295
- removeGlobalListener(window, "mouseup", onUp, false);
296
- removeGlobalListener(window, "touchend", onUp, false);
297
- removeGlobalListener(window, "pointerup", onUp, false);
298
- }
299
- };
300
- let thumbPosition = state.getThumbPercent(index);
301
- if (isVertical || direction === "rtl") thumbPosition = 1 - thumbPosition;
302
- let interactions = !isDisabled ? (0, $fA3fN$mergeProps)(keyboardProps, moveProps, {
303
- onMouseDown: (e)=>{
304
- if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) return;
305
- onDown();
306
- },
307
- onPointerDown: (e)=>{
308
- if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) return;
309
- onDown(e.pointerId);
310
- },
311
- onTouchStart: (e)=>{
312
- onDown(e.changedTouches[0].identifier);
313
- }
314
- }) : {};
315
- (0, $fA3fN$useFormReset)(inputRef, value, (v)=>{
316
- state.setThumbValue(index, v);
317
- });
318
- // We install mouse handlers for the drag motion on the thumb div, but
319
- // not the key handler for moving the thumb with the slider. Instead,
320
- // we focus the range input, and let the browser handle the keyboard
321
- // interactions; we then listen to input's onChange to update state.
322
- return {
323
- inputProps: (0, $fA3fN$mergeProps)(focusableProps, fieldProps, {
324
- type: "range",
325
- tabIndex: !isDisabled ? 0 : undefined,
326
- min: state.getThumbMinValue(index),
327
- max: state.getThumbMaxValue(index),
328
- step: state.step,
329
- value: value,
330
- name: name,
331
- disabled: isDisabled,
332
- "aria-orientation": orientation,
333
- "aria-valuetext": state.getThumbValueLabel(index),
334
- "aria-required": isRequired || undefined,
335
- "aria-invalid": isInvalid || validationState === "invalid" || undefined,
336
- "aria-errormessage": opts["aria-errormessage"],
337
- "aria-describedby": [
338
- data["aria-describedby"],
339
- opts["aria-describedby"]
340
- ].filter(Boolean).join(" "),
341
- "aria-details": [
342
- data["aria-details"],
343
- opts["aria-details"]
344
- ].filter(Boolean).join(" "),
345
- onChange: (e)=>{
346
- state.setThumbValue(index, parseFloat(e.target.value));
347
- }
348
- }),
349
- thumbProps: {
350
- ...interactions,
351
- style: {
352
- position: "absolute",
353
- [isVertical ? "top" : "left"]: `${thumbPosition * 100}%`,
354
- transform: "translate(-50%, -50%)",
355
- touchAction: "none"
356
- }
357
- },
358
- labelProps: labelProps,
359
- isDragging: state.isThumbDragging(index),
360
- isDisabled: isDisabled,
361
- isFocused: isFocused
362
- };
363
- }
364
-
365
15
 
366
16
 
367
17