UNPKG

3.83 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import React from 'react';
13import { Text, View, Animated, StyleSheet, SafeAreaView, TouchableWithoutFeedback, } from 'react-native';
14import FAB from './FAB';
15import { withTheme } from '../config';
16import Color from 'color';
17const SpeedDialAction = withTheme((_a) => {
18 var { title, titleStyle } = _a, actionProps = __rest(_a, ["title", "titleStyle"]);
19 return (<View style={styles.action}>
20 {title && <Text style={[styles.title, titleStyle]}>{title}</Text>}
21 <FAB {...actionProps} size="small" style={[actionProps.style]}/>
22 </View>);
23}, 'SpeedDialAction');
24const SpeedDial = (_a) => {
25 var _b;
26 var { theme, isOpen, onOpen = () => { }, onClose = () => { }, icon, openIcon, children, transitionDuration = 150, style, overlayColor } = _a, props = __rest(_a, ["theme", "isOpen", "onOpen", "onClose", "icon", "openIcon", "children", "transitionDuration", "style", "overlayColor"]);
27 const animations = React.useRef([...new Array(React.Children.count(children))].map(() => new Animated.Value(Number(isOpen))));
28 React.useEffect(() => {
29 Animated.stagger(50, animations.current
30 .map((animation) => Animated.timing(animation, {
31 toValue: Number(isOpen),
32 duration: transitionDuration,
33 useNativeDriver: true,
34 }))[isOpen ? 'reverse' : 'sort']()).start();
35 }, [isOpen, animations, children, transitionDuration]);
36 return (<View style={[styles.container, style]} pointerEvents="box-none">
37 <TouchableWithoutFeedback onPress={onClose}>
38 <Animated.View style={[
39 StyleSheet.absoluteFillObject,
40 {
41 opacity: animations.current[0],
42 backgroundColor: overlayColor ||
43 Color((_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.black).alpha(0.6).rgb().toString(),
44 },
45 ]} pointerEvents={isOpen ? 'auto' : 'none'}/>
46 </TouchableWithoutFeedback>
47
48 <SafeAreaView pointerEvents="box-none" style={styles.safeArea}>
49 {React.Children.toArray(children).map((ChildAction, i) => (<Animated.View pointerEvents={isOpen ? 'auto' : 'none'} key={i} style={{
50 transform: [{ scale: animations.current[i] }],
51 opacity: animations.current[i],
52 }}>
53 {ChildAction}
54 </Animated.View>))}
55 <FAB style={[styles.fab]} icon={isOpen ? openIcon : icon} {...props} onPress={isOpen ? onClose : onOpen}/>
56 </SafeAreaView>
57 </View>);
58};
59const styles = StyleSheet.create({
60 safeArea: {
61 alignItems: 'flex-end',
62 },
63 container: Object.assign(Object.assign({}, StyleSheet.absoluteFillObject), { justifyContent: 'flex-end' }),
64 fab: {
65 margin: 16,
66 marginTop: 0,
67 },
68 title: {
69 backgroundColor: 'white',
70 color: 'black',
71 borderRadius: 5,
72 paddingHorizontal: 12,
73 paddingVertical: 6,
74 marginVertical: 8,
75 marginHorizontal: 16,
76 elevation: 2,
77 },
78 action: {
79 marginBottom: 16,
80 marginRight: 24,
81 flexDirection: 'row',
82 justifyContent: 'flex-end',
83 alignItems: 'center',
84 },
85});
86export { SpeedDial };
87export default Object.assign(withTheme(SpeedDial, 'SpeedDial'), {
88 Action: SpeedDialAction,
89});