This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
extended-action-fab.tsx
194 lines (181 loc) · 5.37 KB
/
extended-action-fab.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
Animated,
StyleSheet,
View,
TouchableWithoutFeedback,
} from 'react-native';
import * as React from 'react';
import {theme} from '../../constants/theme';
import {Surface} from 'react-native-paper';
import {NavigationAwarePortal} from '../navigation-aware-portal/navigation-aware-portal';
import {MutableRefObject} from 'react';
type ActionFabReactNode = (toggleAnimation: () => void) => React.ReactNode;
interface ExtendedActionFabProps {
fab: ActionFabReactNode;
fabActions: ActionFabReactNode;
fabBottom?: MutableRefObject<Animated.Value>;
scale?: MutableRefObject<Animated.Value>;
}
export const ExtendedActionFab = ({
fab,
fabActions,
fabBottom: fabBottomProps,
scale: scaleProps,
}: ExtendedActionFabProps) => {
const [extended, setExtended] = React.useState(false);
const [fabSize, setFabSize] = React.useState({height: 0, width: 0});
const [fabActionSize, setFabActionSize] = React.useState({
height: 0,
width: 0,
});
const fabPanelHeight = React.useRef(new Animated.Value(190));
const actionSizeOpacity = React.useRef(new Animated.Value(0));
const fabOpacity = React.useRef(new Animated.Value(1));
const fabBottomLocal = React.useRef(new Animated.Value(16));
const fabBottom = fabBottomProps || fabBottomLocal;
const scaleLocal = React.useRef(new Animated.Value(1));
const scale = scaleProps || scaleLocal;
React.useEffect(() => {
const height =
fabSize.height < fabActionSize.height
? fabSize.height
: fabActionSize.height;
fabPanelHeight.current = new Animated.Value(height);
}, [fabSize.height, fabActionSize.height]);
const width =
fabSize.width > fabActionSize.width ? fabSize.width : fabActionSize.width;
const toggleAnimation = React.useCallback(() => {
if (extended) {
Animated.parallel([
Animated.timing(fabPanelHeight.current, {
toValue: fabSize.height,
duration: 150,
useNativeDriver: false,
}),
Animated.timing(actionSizeOpacity.current, {
toValue: 0,
duration: 150,
useNativeDriver: false,
}),
Animated.timing(fabOpacity.current, {
toValue: 1,
duration: 150,
useNativeDriver: false,
}),
]).start(() => {
setExtended(false);
});
} else {
Animated.parallel([
Animated.timing(fabPanelHeight.current, {
toValue: fabActionSize.height,
duration: 150,
useNativeDriver: false,
}),
Animated.timing(actionSizeOpacity.current, {
toValue: 1,
duration: 150,
useNativeDriver: false,
}),
Animated.timing(fabOpacity.current, {
toValue: 0,
duration: 150,
useNativeDriver: false,
}),
]).start(() => {
setExtended(true);
});
}
}, [fabActionSize.height, fabSize.height, extended]);
const animatedHeight = {
width: width,
height: fabPanelHeight.current,
};
const animatedFab = {
opacity: fabOpacity.current,
};
const animatedFabActions = {
opacity: actionSizeOpacity.current,
width,
// If this is not present, the actions will overlay the FAB and cause presses to not be passed
marginBottom: extended ? 0 : 1000,
};
const fabDisplay = React.useMemo(() => fab(toggleAnimation), [
fab,
toggleAnimation,
]);
const fabActionDisplay = React.useMemo(() => fabActions(toggleAnimation), [
fabActions,
toggleAnimation,
]);
const fabBottomStyle = {
bottom: fabBottom.current,
// TODO: FIXME // https://github.com/facebook/react-native/issues/19637
// scaleX: scale.current,
// scaleY: scale.current,
};
return (
<NavigationAwarePortal>
<Animated.View style={[styles.MainContainer, fabBottomStyle]}>
<Surface style={styles.fabSurface}>
<Animated.View style={animatedHeight}>
<Animated.View style={[styles.fabContents, animatedFab]}>
<View
onLayout={event => {
const {height, width} = event.nativeEvent.layout;
setFabSize({height, width});
}}>
{fabDisplay}
</View>
</Animated.View>
<Animated.View style={[styles.fabContents, animatedFabActions]}>
<View
onLayout={event => {
const {height, width} = event.nativeEvent.layout;
setFabActionSize({height, width});
}}>
{fabActionDisplay}
</View>
</Animated.View>
</Animated.View>
</Surface>
</Animated.View>
{extended && (
<TouchableWithoutFeedback
style={styles.scrim}
onPress={() => toggleAnimation()}>
<View style={styles.scrim} />
</TouchableWithoutFeedback>
)}
</NavigationAwarePortal>
);
};
const styles = StyleSheet.create({
MainContainer: {
justifyContent: 'center',
alignItems: 'center',
padding: 12,
width: '100%',
position: 'absolute',
},
fabSurface: {
position: 'absolute',
bottom: 0,
zIndex: 10,
borderRadius: theme.roundness,
backgroundColor: theme.colors.accent,
elevation: 6,
},
fabContents: {
position: 'absolute',
bottom: 0,
left: 0,
},
scrim: {
position: 'absolute',
top: 0,
left: 0,
height: '100%',
width: '100%',
},
});