-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathThumb.tsx
More file actions
144 lines (133 loc) · 4 KB
/
Copy pathThumb.tsx
File metadata and controls
144 lines (133 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, {useCallback} from 'react';
import {StyleSheet, ViewProps, ViewStyle, LayoutChangeEvent} from 'react-native';
import {SharedValue, useAnimatedStyle, useSharedValue, withSpring} from 'react-native-reanimated';
import {GestureDetector, Gesture} from 'react-native-gesture-handler';
import {Colors} from '../../style';
import View from '../../components/view';
import {Constants} from '../../commons/new';
interface ThumbProps extends ViewProps {
start: SharedValue<number>;
end: SharedValue<number>;
offset: SharedValue<number>;
stepInterpolatedValue: SharedValue<number>;
stepInterpolation?: () => number;
shouldBounceToStep?: boolean;
gap?: number;
disabled?: boolean;
secondary?: boolean;
defaultStyle?: SharedValue<ViewStyle>;
activeStyle?: SharedValue<ViewStyle>;
disableActiveStyling?: boolean;
hitSlop?: ViewProps['hitSlop'];
shouldDisableRTL?: boolean;
onSeekStart?: () => void;
onSeekEnd?: () => void;
enableShadow?: boolean;
isActive: SharedValue<boolean>;
}
const SHADOW_RADIUS = 4;
const THUMB_SIZE = 24;
const THUMB_ACCESSIBLE_HITSLOP = Math.max(0, 48 - THUMB_SIZE) / 2;
const DEFAULT_THUMB_HIT_SLOP = {
top: THUMB_ACCESSIBLE_HITSLOP,
bottom: THUMB_ACCESSIBLE_HITSLOP,
left: THUMB_ACCESSIBLE_HITSLOP,
right: THUMB_ACCESSIBLE_HITSLOP
} as const;
const Thumb = (props: ThumbProps) => {
const {
disabled,
disableActiveStyling,
activeStyle,
defaultStyle,
hitSlop = DEFAULT_THUMB_HIT_SLOP,
onSeekStart,
onSeekEnd,
start,
end,
offset,
shouldDisableRTL,
shouldBounceToStep,
stepInterpolatedValue,
gap = 0,
secondary,
enableShadow,
isActive
} = props;
const rtlFix = Constants.isRTL ? -1 : 1;
const isPressed = useSharedValue(false);
const thumbSize = useSharedValue({width: THUMB_SIZE, height: THUMB_SIZE});
const lastOffset = useSharedValue(0);
const gesture = Gesture.Pan()
.hitSlop(hitSlop)
.onBegin(() => {
onSeekStart?.();
isPressed.value = true;
lastOffset.value = offset.value;
})
.onUpdate(e => {
let newX = lastOffset.value + e.translationX * (shouldDisableRTL ? 1 : rtlFix);
if (newX < start.value) {
// adjust start edge
newX = start.value;
} else if (newX > end.value) {
// adjust end edge
newX = end.value;
}
if (!secondary && newX <= gap ||
secondary && newX >= end.value - gap ||
newX < end.value - gap && newX > start.value + gap) {
offset.value = newX;
}
})
.onEnd(() => {
onSeekEnd?.();
})
.onFinalize(() => {
isPressed.value = false;
if (shouldBounceToStep) {
offset.value = Math.round(offset.value / stepInterpolatedValue.value) * stepInterpolatedValue.value;
}
});
gesture.enabled(!disabled);
const animatedStyle = useAnimatedStyle(() => {
const active = isPressed.value || isActive.value;
const customStyle = active ? activeStyle?.value : defaultStyle?.value;
return {
...customStyle,
transform: [
{translateX: (offset.value - thumbSize.value.width / 2) * rtlFix},
{scale: withSpring(!disableActiveStyling && active ? 1.3 : 1)}
]
};
});
const onThumbLayout = useCallback((event: LayoutChangeEvent) => {
const width = event.nativeEvent.layout.width;
const height = event.nativeEvent.layout.height;
thumbSize.value = {width, height};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<GestureDetector gesture={gesture}>
<View
reanimated
style={[styles.thumbPosition, enableShadow && styles.thumbShadow, animatedStyle]}
hitSlop={hitSlop}
onLayout={onThumbLayout}
/>
</GestureDetector>
);
};
const styles = StyleSheet.create({
thumbPosition: {
position: 'absolute'
},
thumbShadow: {
shadowColor: Colors.rgba(Colors.black, 0.3),
shadowOffset: {width: 0, height: 0},
shadowOpacity: 0.9,
shadowRadius: SHADOW_RADIUS,
elevation: 2
}
});
export default Thumb;