@tamagui/slider 1.0.1-rc.8 → 1.0.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.
- package/dist/cjs/Slider.js +105 -115
- package/dist/cjs/Slider.js.map +2 -2
- package/dist/cjs/SliderImpl.js +3 -6
- package/dist/cjs/SliderImpl.js.map +2 -2
- package/dist/esm/Slider.js +105 -115
- package/dist/esm/Slider.js.map +2 -2
- package/dist/esm/SliderImpl.js +3 -6
- package/dist/esm/SliderImpl.js.map +2 -2
- package/dist/jsx/Slider.js +82 -92
- package/dist/jsx/Slider.js.map +2 -2
- package/dist/jsx/SliderImpl.js +3 -6
- package/dist/jsx/SliderImpl.js.map +2 -2
- package/package.json +10 -10
- package/src/Slider.tsx +130 -149
- package/src/SliderImpl.tsx +5 -8
- package/types/Slider.d.ts +1 -5
package/src/Slider.tsx
CHANGED
|
@@ -54,15 +54,8 @@ import {
|
|
|
54
54
|
|
|
55
55
|
const SliderHorizontal = React.forwardRef<View, SliderHorizontalProps>(
|
|
56
56
|
(props: ScopedProps<SliderHorizontalProps>, forwardedRef) => {
|
|
57
|
-
const {
|
|
58
|
-
|
|
59
|
-
max,
|
|
60
|
-
dir,
|
|
61
|
-
onSlideStart,
|
|
62
|
-
onSlideMove,
|
|
63
|
-
onStepKeyDown,
|
|
64
|
-
...sliderProps
|
|
65
|
-
} = props
|
|
57
|
+
const { min, max, dir, onSlideStart, onSlideMove, onStepKeyDown, ...sliderProps } =
|
|
58
|
+
props
|
|
66
59
|
const direction = useDirection(dir)
|
|
67
60
|
const isDirectionLTR = direction === 'ltr'
|
|
68
61
|
const sliderRef = React.useRef<View>(null)
|
|
@@ -117,7 +110,7 @@ const SliderHorizontal = React.forwardRef<View, SliderHorizontalProps>(
|
|
|
117
110
|
/>
|
|
118
111
|
</SliderOrientationProvider>
|
|
119
112
|
)
|
|
120
|
-
}
|
|
113
|
+
}
|
|
121
114
|
)
|
|
122
115
|
|
|
123
116
|
/* -------------------------------------------------------------------------------------------------
|
|
@@ -126,8 +119,7 @@ const SliderHorizontal = React.forwardRef<View, SliderHorizontalProps>(
|
|
|
126
119
|
|
|
127
120
|
const SliderVertical = React.forwardRef<View, SliderVerticalProps>(
|
|
128
121
|
(props: ScopedProps<SliderVerticalProps>, forwardedRef) => {
|
|
129
|
-
const { min, max, onSlideStart, onSlideMove, onStepKeyDown, ...sliderProps } =
|
|
130
|
-
props
|
|
122
|
+
const { min, max, onSlideStart, onSlideMove, onStepKeyDown, ...sliderProps } = props
|
|
131
123
|
const [state, setState] = React.useState(() => ({ size: 0, offset: 0 }))
|
|
132
124
|
const sliderRef = React.useRef<View>(null)
|
|
133
125
|
|
|
@@ -179,7 +171,7 @@ const SliderVertical = React.forwardRef<View, SliderVerticalProps>(
|
|
|
179
171
|
/>
|
|
180
172
|
</SliderOrientationProvider>
|
|
181
173
|
)
|
|
182
|
-
}
|
|
174
|
+
}
|
|
183
175
|
)
|
|
184
176
|
|
|
185
177
|
/* -------------------------------------------------------------------------------------------------
|
|
@@ -214,7 +206,7 @@ const SliderTrack = React.forwardRef<SliderTrackElement, SliderTrackProps>(
|
|
|
214
206
|
ref={forwardedRef}
|
|
215
207
|
/>
|
|
216
208
|
)
|
|
217
|
-
}
|
|
209
|
+
}
|
|
218
210
|
)
|
|
219
211
|
|
|
220
212
|
SliderTrack.displayName = TRACK_NAME
|
|
@@ -242,7 +234,7 @@ const SliderTrackActive = React.forwardRef<View, SliderTrackActiveProps>(
|
|
|
242
234
|
const composedRefs = useComposedRefs(forwardedRef, ref)
|
|
243
235
|
const valuesCount = context.values.length
|
|
244
236
|
const percentages = context.values.map((value) =>
|
|
245
|
-
convertValueToPercentage(value, context.min, context.max)
|
|
237
|
+
convertValueToPercentage(value, context.min, context.max)
|
|
246
238
|
)
|
|
247
239
|
const offsetStart = valuesCount > 1 ? Math.min(...percentages) : 0
|
|
248
240
|
const offsetEnd = 100 - Math.max(...percentages)
|
|
@@ -269,7 +261,7 @@ const SliderTrackActive = React.forwardRef<View, SliderTrackActiveProps>(
|
|
|
269
261
|
})}
|
|
270
262
|
/>
|
|
271
263
|
)
|
|
272
|
-
}
|
|
264
|
+
}
|
|
273
265
|
)
|
|
274
266
|
|
|
275
267
|
SliderTrackActive.displayName = RANGE_NAME
|
|
@@ -326,9 +318,7 @@ const SliderThumb = React.forwardRef<View, SliderThumbProps>(
|
|
|
326
318
|
// We cast because index could be `-1` which would return undefined
|
|
327
319
|
const value = context.values[index] as number | undefined
|
|
328
320
|
const percent =
|
|
329
|
-
value === undefined
|
|
330
|
-
? 0
|
|
331
|
-
: convertValueToPercentage(value, context.min, context.max)
|
|
321
|
+
value === undefined ? 0 : convertValueToPercentage(value, context.min, context.max)
|
|
332
322
|
const label = getLabel(index, context.values.length)
|
|
333
323
|
const sizeIn = sizeProp ?? context.size ?? '$4'
|
|
334
324
|
const [size, setSize] = React.useState(() => {
|
|
@@ -403,7 +393,7 @@ const SliderThumb = React.forwardRef<View, SliderThumbProps>(
|
|
|
403
393
|
})}
|
|
404
394
|
/>
|
|
405
395
|
)
|
|
406
|
-
}
|
|
396
|
+
}
|
|
407
397
|
)
|
|
408
398
|
|
|
409
399
|
SliderThumb.displayName = THUMB_NAME
|
|
@@ -413,133 +403,130 @@ SliderThumb.displayName = THUMB_NAME
|
|
|
413
403
|
* -----------------------------------------------------------------------------------------------*/
|
|
414
404
|
|
|
415
405
|
const Slider = withStaticProperties(
|
|
416
|
-
React.forwardRef<View, SliderProps>(
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
})
|
|
406
|
+
React.forwardRef<View, SliderProps>((props: ScopedProps<SliderProps>, forwardedRef) => {
|
|
407
|
+
const {
|
|
408
|
+
name,
|
|
409
|
+
min = 0,
|
|
410
|
+
max = 100,
|
|
411
|
+
step = 1,
|
|
412
|
+
orientation = 'horizontal',
|
|
413
|
+
disabled = false,
|
|
414
|
+
minStepsBetweenThumbs = 0,
|
|
415
|
+
defaultValue = [min],
|
|
416
|
+
value,
|
|
417
|
+
onValueChange = () => {},
|
|
418
|
+
size: sizeProp,
|
|
419
|
+
...sliderProps
|
|
420
|
+
} = props
|
|
421
|
+
const sliderRef = React.useRef<View>(null)
|
|
422
|
+
const composedRefs = useComposedRefs(sliderRef, forwardedRef)
|
|
423
|
+
const thumbRefs = React.useRef<SliderContextValue['thumbs']>(new Set())
|
|
424
|
+
const valueIndexToChangeRef = React.useRef<number>(0)
|
|
425
|
+
const isHorizontal = orientation === 'horizontal'
|
|
426
|
+
// We set this to true by default so that events bubble to forms without JS (SSR)
|
|
427
|
+
// const isFormControl =
|
|
428
|
+
// sliderRef.current instanceof HTMLElement ? Boolean(sliderRef.current.closest('form')) : true
|
|
429
|
+
|
|
430
|
+
const [values = [], setValues] = useControllableState({
|
|
431
|
+
prop: value,
|
|
432
|
+
defaultProp: defaultValue,
|
|
433
|
+
transition: true,
|
|
434
|
+
onChange: (value) => {
|
|
435
|
+
if (isWeb) {
|
|
436
|
+
const thumbs = [...thumbRefs.current]
|
|
437
|
+
thumbs[valueIndexToChangeRef.current]?.focus()
|
|
438
|
+
}
|
|
439
|
+
onValueChange(value)
|
|
440
|
+
},
|
|
441
|
+
})
|
|
453
442
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
443
|
+
if (isWeb) {
|
|
444
|
+
React.useEffect(() => {
|
|
445
|
+
// @ts-ignore
|
|
446
|
+
const node = sliderRef.current as HTMLElement
|
|
447
|
+
if (!node) return
|
|
448
|
+
const preventDefault = (e) => {
|
|
449
|
+
e.preventDefault()
|
|
450
|
+
}
|
|
451
|
+
node.addEventListener('touchstart', preventDefault)
|
|
452
|
+
return () => {
|
|
453
|
+
node.removeEventListener('touchstart', preventDefault)
|
|
454
|
+
}
|
|
455
|
+
}, [])
|
|
456
|
+
}
|
|
468
457
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
458
|
+
function handleSlideMove(value: number) {
|
|
459
|
+
updateValues(value, valueIndexToChangeRef.current)
|
|
460
|
+
}
|
|
472
461
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
})
|
|
491
|
-
}
|
|
462
|
+
function updateValues(value: number, atIndex: number) {
|
|
463
|
+
const decimalCount = getDecimalCount(step)
|
|
464
|
+
const snapToStep = roundValue(
|
|
465
|
+
Math.round((value - min) / step) * step + min,
|
|
466
|
+
decimalCount
|
|
467
|
+
)
|
|
468
|
+
const nextValue = clamp(snapToStep, [min, max])
|
|
469
|
+
setValues((prevValues = []) => {
|
|
470
|
+
const nextValues = getNextSortedValues(prevValues, nextValue, atIndex)
|
|
471
|
+
if (hasMinStepsBetweenValues(nextValues, minStepsBetweenThumbs * step)) {
|
|
472
|
+
valueIndexToChangeRef.current = nextValues.indexOf(nextValue)
|
|
473
|
+
return String(nextValues) === String(prevValues) ? prevValues : nextValues
|
|
474
|
+
} else {
|
|
475
|
+
return prevValues
|
|
476
|
+
}
|
|
477
|
+
})
|
|
478
|
+
}
|
|
492
479
|
|
|
493
|
-
|
|
480
|
+
const SliderOriented = isHorizontal ? SliderHorizontal : SliderVertical
|
|
494
481
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
482
|
+
return (
|
|
483
|
+
<SliderProvider
|
|
484
|
+
scope={props.__scopeSlider}
|
|
485
|
+
disabled={disabled}
|
|
486
|
+
min={min}
|
|
487
|
+
max={max}
|
|
488
|
+
valueIndexToChangeRef={valueIndexToChangeRef}
|
|
489
|
+
thumbs={thumbRefs.current}
|
|
490
|
+
values={values}
|
|
491
|
+
orientation={orientation}
|
|
492
|
+
size={sizeProp}
|
|
493
|
+
>
|
|
494
|
+
<SliderOriented
|
|
495
|
+
aria-disabled={disabled}
|
|
496
|
+
data-disabled={disabled ? '' : undefined}
|
|
497
|
+
{...sliderProps}
|
|
498
|
+
ref={composedRefs}
|
|
499
499
|
min={min}
|
|
500
500
|
max={max}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
{...sliderProps}
|
|
511
|
-
ref={composedRefs}
|
|
512
|
-
min={min}
|
|
513
|
-
max={max}
|
|
514
|
-
onSlideStart={
|
|
515
|
-
disabled
|
|
516
|
-
? undefined
|
|
517
|
-
: (value: number, target) => {
|
|
518
|
-
// when starting on the track, move it right away
|
|
519
|
-
// when starting on thumb, dont jump until movemenet as it feels weird
|
|
520
|
-
if (target !== 'thumb') {
|
|
521
|
-
const closestIndex = getClosestValueIndex(values, value)
|
|
522
|
-
updateValues(value, closestIndex)
|
|
523
|
-
}
|
|
501
|
+
onSlideStart={
|
|
502
|
+
disabled
|
|
503
|
+
? undefined
|
|
504
|
+
: (value: number, target) => {
|
|
505
|
+
// when starting on the track, move it right away
|
|
506
|
+
// when starting on thumb, dont jump until movemenet as it feels weird
|
|
507
|
+
if (target !== 'thumb') {
|
|
508
|
+
const closestIndex = getClosestValueIndex(values, value)
|
|
509
|
+
updateValues(value, closestIndex)
|
|
524
510
|
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
onSlideMove={disabled ? undefined : handleSlideMove}
|
|
514
|
+
onHomeKeyDown={() => !disabled && updateValues(min, 0)}
|
|
515
|
+
onEndKeyDown={() => !disabled && updateValues(max, values.length - 1)}
|
|
516
|
+
onStepKeyDown={({ event, direction: stepDirection }) => {
|
|
517
|
+
if (!disabled) {
|
|
518
|
+
const isPageKey = PAGE_KEYS.includes(event.key)
|
|
519
|
+
const isSkipKey =
|
|
520
|
+
isPageKey || (event.shiftKey && ARROW_KEYS.includes(event.key))
|
|
521
|
+
const multiplier = isSkipKey ? 10 : 1
|
|
522
|
+
const atIndex = valueIndexToChangeRef.current
|
|
523
|
+
const value = values[atIndex]
|
|
524
|
+
const stepInDirection = step * multiplier * stepDirection
|
|
525
|
+
updateValues(value + stepInDirection, atIndex)
|
|
525
526
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
onStepKeyDown={({ event, direction: stepDirection }) => {
|
|
530
|
-
if (!disabled) {
|
|
531
|
-
const isPageKey = PAGE_KEYS.includes(event.key)
|
|
532
|
-
const isSkipKey =
|
|
533
|
-
isPageKey || (event.shiftKey && ARROW_KEYS.includes(event.key))
|
|
534
|
-
const multiplier = isSkipKey ? 10 : 1
|
|
535
|
-
const atIndex = valueIndexToChangeRef.current
|
|
536
|
-
const value = values[atIndex]
|
|
537
|
-
const stepInDirection = step * multiplier * stepDirection
|
|
538
|
-
updateValues(value + stepInDirection, atIndex)
|
|
539
|
-
}
|
|
540
|
-
}}
|
|
541
|
-
/>
|
|
542
|
-
{/* {isFormControl &&
|
|
527
|
+
}}
|
|
528
|
+
/>
|
|
529
|
+
{/* {isFormControl &&
|
|
543
530
|
values.map((value, index) => (
|
|
544
531
|
<BubbleInput
|
|
545
532
|
key={index}
|
|
@@ -547,15 +534,14 @@ const Slider = withStaticProperties(
|
|
|
547
534
|
value={value}
|
|
548
535
|
/>
|
|
549
536
|
))} */}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
),
|
|
537
|
+
</SliderProvider>
|
|
538
|
+
)
|
|
539
|
+
}),
|
|
554
540
|
{
|
|
555
541
|
Track: SliderTrack,
|
|
556
542
|
TrackActive: SliderTrackActive,
|
|
557
543
|
Thumb: SliderThumb,
|
|
558
|
-
}
|
|
544
|
+
}
|
|
559
545
|
)
|
|
560
546
|
|
|
561
547
|
Slider.displayName = SLIDER_NAME
|
|
@@ -610,9 +596,4 @@ export {
|
|
|
610
596
|
Thumb,
|
|
611
597
|
}
|
|
612
598
|
|
|
613
|
-
export type {
|
|
614
|
-
SliderProps,
|
|
615
|
-
SliderTrackProps,
|
|
616
|
-
SliderTrackActiveProps,
|
|
617
|
-
SliderThumbProps,
|
|
618
|
-
}
|
|
599
|
+
export type { SliderProps, SliderTrackProps, SliderTrackActiveProps, SliderThumbProps }
|
package/src/SliderImpl.tsx
CHANGED
|
@@ -113,14 +113,11 @@ export const SliderImpl = React.forwardRef<View, SliderImplProps>(
|
|
|
113
113
|
// const target = event.target as HTMLElement
|
|
114
114
|
onSlideMove(event)
|
|
115
115
|
})}
|
|
116
|
-
onResponderRelease={composeEventHandlers(
|
|
117
|
-
|
|
118
|
-
(event)
|
|
119
|
-
|
|
120
|
-
onSlideEnd(event)
|
|
121
|
-
},
|
|
122
|
-
)}
|
|
116
|
+
onResponderRelease={composeEventHandlers(props.onResponderRelease, (event) => {
|
|
117
|
+
// const target = event.target as HTMLElement
|
|
118
|
+
onSlideEnd(event)
|
|
119
|
+
})}
|
|
123
120
|
/>
|
|
124
121
|
)
|
|
125
|
-
}
|
|
122
|
+
}
|
|
126
123
|
)
|
package/types/Slider.d.ts
CHANGED
|
@@ -175,7 +175,6 @@ export declare const SliderThumbFrame: import("@tamagui/core").TamaguiComponent<
|
|
|
175
175
|
readonly fullscreen?: boolean | undefined;
|
|
176
176
|
readonly elevation?: SizeTokens | undefined;
|
|
177
177
|
} & {
|
|
178
|
-
readonly fontFamily?: unknown;
|
|
179
178
|
readonly backgrounded?: boolean | undefined;
|
|
180
179
|
readonly radiused?: boolean | undefined;
|
|
181
180
|
readonly hoverTheme?: boolean | undefined;
|
|
@@ -193,7 +192,6 @@ export declare const SliderThumbFrame: import("@tamagui/core").TamaguiComponent<
|
|
|
193
192
|
readonly fullscreen?: boolean | undefined;
|
|
194
193
|
readonly elevation?: SizeTokens | undefined;
|
|
195
194
|
} & {
|
|
196
|
-
readonly fontFamily?: unknown;
|
|
197
195
|
readonly backgrounded?: boolean | undefined;
|
|
198
196
|
readonly radiused?: boolean | undefined;
|
|
199
197
|
readonly hoverTheme?: boolean | undefined;
|
|
@@ -211,7 +209,6 @@ export declare const SliderThumbFrame: import("@tamagui/core").TamaguiComponent<
|
|
|
211
209
|
readonly fullscreen?: boolean | undefined;
|
|
212
210
|
readonly elevation?: SizeTokens | undefined;
|
|
213
211
|
} & {
|
|
214
|
-
readonly fontFamily?: unknown;
|
|
215
212
|
readonly backgrounded?: boolean | undefined;
|
|
216
213
|
readonly radiused?: boolean | undefined;
|
|
217
214
|
readonly hoverTheme?: boolean | undefined;
|
|
@@ -229,7 +226,6 @@ export declare const SliderThumbFrame: import("@tamagui/core").TamaguiComponent<
|
|
|
229
226
|
readonly fullscreen?: boolean | undefined;
|
|
230
227
|
readonly elevation?: SizeTokens | undefined;
|
|
231
228
|
} & {
|
|
232
|
-
readonly fontFamily?: unknown;
|
|
233
229
|
readonly backgrounded?: boolean | undefined;
|
|
234
230
|
readonly radiused?: boolean | undefined;
|
|
235
231
|
readonly hoverTheme?: boolean | undefined;
|
|
@@ -353,5 +349,5 @@ declare const Range: React.ForwardRefExoticComponent<((Omit<import("react-native
|
|
|
353
349
|
}>>, string | number>) & React.RefAttributes<View>>;
|
|
354
350
|
declare const Thumb: React.ForwardRefExoticComponent<SliderThumbProps & React.RefAttributes<View>>;
|
|
355
351
|
export { Slider, SliderTrack, SliderTrackActive, SliderThumb, Track, Range, Thumb, };
|
|
356
|
-
export type { SliderProps, SliderTrackProps, SliderTrackActiveProps, SliderThumbProps
|
|
352
|
+
export type { SliderProps, SliderTrackProps, SliderTrackActiveProps, SliderThumbProps };
|
|
357
353
|
//# sourceMappingURL=Slider.d.ts.map
|