-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathScrollableViewExample.tsx
172 lines (149 loc) · 3.65 KB
/
ScrollableViewExample.tsx
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React from 'react';
import Animated, {
useAnimatedGestureHandler,
useAnimatedStyle,
useSharedValue,
withDecay,
withTiming,
Easing,
} from 'react-native-reanimated';
import {
View,
Dimensions,
Platform,
StyleSheet,
LayoutChangeEvent,
} from 'react-native';
import {
PanGestureHandler,
PanGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
import { useHeaderHeight } from '@react-navigation/elements';
const windowDimensions = Dimensions.get('window');
const colors = [
'black',
'blue',
'green',
'yellow',
'red',
'gray',
'pink',
'orange',
];
const boxHeight = 120;
function friction(value: number) {
'worklet';
const MAX_FRICTION = 200;
const MAX_VALUE = 400;
const res = Math.max(
1,
Math.min(
MAX_FRICTION,
1 + (Math.abs(value) * (MAX_FRICTION - 1)) / MAX_VALUE
)
);
if (value < 0) {
return -res;
}
return res;
}
function ScrollableView({
children,
}: React.PropsWithChildren<Record<never, never>>) {
const translateY = useSharedValue(0);
const loverBound = useSharedValue(0);
const headerHeight = useHeaderHeight();
function onLayout(evt: LayoutChangeEvent) {
loverBound.value =
windowDimensions.height - headerHeight - evt.nativeEvent.layout.height;
}
type AnimatedGHContext = {
startY: number;
};
const handler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
AnimatedGHContext
>({
onStart: (_evt, ctx) => {
const currentY = translateY.value;
ctx.startY = currentY;
translateY.value = currentY; // for stop animation
},
onActive: (evt, ctx) => {
const nextTranslate = ctx.startY + evt.translationY;
if (nextTranslate < loverBound.value) {
translateY.value =
loverBound.value + friction(nextTranslate - loverBound.value);
} else if (nextTranslate > 0) {
translateY.value = friction(nextTranslate);
} else {
translateY.value = nextTranslate;
}
},
onEnd: (evt, _ctx) => {
if (translateY.value < loverBound.value || translateY.value > 0) {
const toValue = translateY.value > 0 ? 0 : loverBound.value;
translateY.value = withTiming(toValue, {
duration: 250,
easing: Easing.bezierFn(0.25, 0.1, 0.25, 1),
});
} else {
translateY.value = withDecay({
clamp: [loverBound.value, 0],
velocity: evt.velocityY,
});
}
},
});
const animatedStyles = useAnimatedStyle(() => {
return {
height: boxHeight * colors.length,
transform: [
{
translateY: translateY.value,
},
],
};
});
return (
<View style={{ flex: 1 }}>
<PanGestureHandler onGestureEvent={handler}>
<Animated.View style={animatedStyles}>
<View onLayout={onLayout}>{children}</View>
</Animated.View>
</PanGestureHandler>
</View>
);
}
function Box({ color }: { color: string }) {
return (
<View
style={{
backgroundColor: color,
height: boxHeight,
borderBottomWidth: 1,
borderBottomColor: 'gray',
}}
/>
);
}
function Example(): React.ReactElement {
const headerHeight = useHeaderHeight();
const height =
Platform.OS === 'web' ? windowDimensions.height - headerHeight : undefined;
return (
<View style={[styles.wrapper, { height }]}>
<ScrollableView>
{colors.map((color) => (
<Box color={color} key={color} />
))}
</ScrollableView>
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
overflow: Platform.OS === 'web' ? 'hidden' : undefined,
},
});
export default Example;