|
10 | 10 |
|
11 | 11 | 'use strict';
|
12 | 12 |
|
13 |
| -const AnExBobble = require('./AnExBobble'); |
14 |
| -const AnExChained = require('./AnExChained'); |
15 |
| -const AnExScroll = require('./AnExScroll'); |
16 |
| -const AnExTilt = require('./AnExTilt'); |
17 |
| -const React = require('react'); |
18 |
| -const { |
19 |
| - Animated, |
20 |
| - PanResponder, |
21 |
| - StyleSheet, |
22 |
| - Text, |
23 |
| - View, |
24 |
| -} = require('react-native'); |
| 13 | +import AnExBobble from './AnExBobble'; |
| 14 | +import AnExChained from './AnExChained'; |
| 15 | +import AnExScroll from './AnExScroll'; |
| 16 | +import AnExTilt from './AnExTilt'; |
| 17 | +import React, {useRef, useState} from 'react'; |
| 18 | +import {Animated, PanResponder, StyleSheet, Text, View} from 'react-native'; |
25 | 19 |
|
26 |
| -class AnExSet extends React.Component<Object, any> { |
27 |
| - constructor(props: Object) { |
28 |
| - super(props); |
29 |
| - function randColor() { |
30 |
| - const colors = [0, 1, 2].map(() => Math.floor(Math.random() * 150 + 100)); |
31 |
| - return 'rgb(' + colors.join(',') + ')'; |
32 |
| - } |
33 |
| - this.state = { |
34 |
| - closeColor: randColor(), |
35 |
| - openColor: randColor(), |
36 |
| - }; |
37 |
| - } |
38 |
| - render(): React.Node { |
39 |
| - const backgroundColor = this.props.openVal |
40 |
| - ? this.props.openVal.interpolate({ |
41 |
| - inputRange: [0, 1], |
42 |
| - outputRange: [ |
43 |
| - this.state.closeColor, // interpolates color strings |
44 |
| - this.state.openColor, |
45 |
| - ], |
46 |
| - }) |
47 |
| - : this.state.closeColor; |
48 |
| - const panelWidth = |
49 |
| - (this.props.containerLayout && this.props.containerLayout.width) || 320; |
50 |
| - return ( |
51 |
| - <View style={styles.container}> |
52 |
| - <Animated.View |
53 |
| - style={[styles.header, {backgroundColor}]} |
54 |
| - {...this.state.dismissResponder.panHandlers}> |
55 |
| - <Text style={[styles.text, styles.headerText]}>{this.props.id}</Text> |
56 |
| - </Animated.View> |
57 |
| - {this.props.isActive && ( |
58 |
| - <View style={styles.stream}> |
59 |
| - <View style={styles.card}> |
60 |
| - <Text style={styles.text}>July 2nd</Text> |
61 |
| - <AnExTilt isActive={this.props.isActive} /> |
62 |
| - <AnExBobble /> |
63 |
| - </View> |
64 |
| - <AnExScroll panelWidth={panelWidth} /> |
65 |
| - <AnExChained /> |
66 |
| - </View> |
67 |
| - )} |
68 |
| - </View> |
69 |
| - ); |
70 |
| - } |
| 20 | +const randColor = () => { |
| 21 | + const colors = [0, 1, 2].map(() => Math.floor(Math.random() * 150 + 100)); |
| 22 | + return `rgb(${colors.join(',')})`; |
| 23 | +}; |
| 24 | + |
| 25 | +type AnExSetProps = $ReadOnly<{| |
| 26 | + openVal: Animated.Value, |
| 27 | + containerLayout: {width: number, height: number}, |
| 28 | + id: string, |
| 29 | + isActive: boolean, |
| 30 | + onDismiss: (velocity: number) => void, |
| 31 | +|}>; |
71 | 32 |
|
72 |
| - UNSAFE_componentWillMount() { |
73 |
| - this.state.dismissY = new Animated.Value(0); |
74 |
| - this.state.dismissResponder = PanResponder.create({ |
75 |
| - onStartShouldSetPanResponder: () => this.props.isActive, |
76 |
| - onPanResponderGrant: () => { |
77 |
| - Animated.spring(this.props.openVal, { |
78 |
| - // Animated value passed in. |
79 |
| - toValue: this.state.dismissY.interpolate({ |
80 |
| - // Track dismiss gesture |
81 |
| - inputRange: [0, 300], // and interpolate pixel distance |
82 |
| - outputRange: [1, 0], // to a fraction. |
83 |
| - }), |
| 33 | +const AnExSet = ({ |
| 34 | + openVal, |
| 35 | + containerLayout, |
| 36 | + id, |
| 37 | + isActive, |
| 38 | + onDismiss, |
| 39 | +}: AnExSetProps): React.Node => { |
| 40 | + const [closeColor] = useState(randColor()); |
| 41 | + const [openColor] = useState(randColor()); |
| 42 | + const dismissY = useRef(new Animated.Value(0)).current; |
84 | 43 |
|
| 44 | + const dismissResponder = PanResponder.create({ |
| 45 | + onStartShouldSetPanResponder: () => isActive, |
| 46 | + onPanResponderGrant: () => { |
| 47 | + Animated.spring(openVal, { |
| 48 | + // Animated value passed in. |
| 49 | + toValue: dismissY.interpolate({ |
| 50 | + // Track dismiss gesture |
| 51 | + inputRange: [0, 300], // and interpolate pixel distance |
| 52 | + outputRange: [1, 0], // to a fraction. |
| 53 | + }), |
| 54 | + useNativeDriver: false, |
| 55 | + }).start(); |
| 56 | + }, |
| 57 | + onPanResponderMove: Animated.event( |
| 58 | + [null, {dy: dismissY}], // track pan gesture |
| 59 | + {useNativeDriver: false}, |
| 60 | + ), |
| 61 | + onPanResponderRelease: (e, gestureState) => { |
| 62 | + if (gestureState.dy > 100) { |
| 63 | + onDismiss(gestureState.vy); // delegates dismiss action to parent |
| 64 | + } else { |
| 65 | + Animated.spring(openVal, { |
| 66 | + // animate back open if released early |
| 67 | + toValue: 1, |
85 | 68 | useNativeDriver: false,
|
86 | 69 | }).start();
|
87 |
| - }, |
88 |
| - onPanResponderMove: Animated.event( |
89 |
| - [null, {dy: this.state.dismissY}], // track pan gesture |
90 |
| - {useNativeDriver: false}, |
91 |
| - ), |
92 |
| - onPanResponderRelease: (e, gestureState) => { |
93 |
| - if (gestureState.dy > 100) { |
94 |
| - this.props.onDismiss(gestureState.vy); // delegates dismiss action to parent |
95 |
| - } else { |
96 |
| - Animated.spring(this.props.openVal, { |
97 |
| - // animate back open if released early |
98 |
| - toValue: 1, |
| 70 | + } |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + const backgroundColor = openVal |
| 75 | + ? openVal.interpolate({ |
| 76 | + inputRange: [0, 1], |
| 77 | + outputRange: [ |
| 78 | + closeColor, // interpolates color strings |
| 79 | + openColor, |
| 80 | + ], |
| 81 | + }) |
| 82 | + : closeColor; |
99 | 83 |
|
100 |
| - useNativeDriver: false, |
101 |
| - }).start(); |
102 |
| - } |
103 |
| - }, |
104 |
| - }); |
105 |
| - } |
106 |
| -} |
| 84 | + const panelWidth = (containerLayout && containerLayout.width) || 320; |
| 85 | + |
| 86 | + return ( |
| 87 | + <View style={styles.container}> |
| 88 | + <Animated.View |
| 89 | + style={[styles.header, {backgroundColor}]} |
| 90 | + {...dismissResponder.panHandlers}> |
| 91 | + <Text style={[styles.text, styles.headerText]}>{id}</Text> |
| 92 | + </Animated.View> |
| 93 | + {isActive && ( |
| 94 | + <View style={styles.stream}> |
| 95 | + <View style={styles.card}> |
| 96 | + <Text style={styles.text}>July 2nd</Text> |
| 97 | + <AnExTilt isActive={isActive} /> |
| 98 | + <AnExBobble /> |
| 99 | + </View> |
| 100 | + <AnExScroll panelWidth={panelWidth} /> |
| 101 | + <AnExChained /> |
| 102 | + </View> |
| 103 | + )} |
| 104 | + </View> |
| 105 | + ); |
| 106 | +}; |
107 | 107 |
|
108 | 108 | const styles = StyleSheet.create({
|
109 | 109 | container: {
|
@@ -144,4 +144,4 @@ const styles = StyleSheet.create({
|
144 | 144 | },
|
145 | 145 | });
|
146 | 146 |
|
147 |
| -module.exports = AnExSet; |
| 147 | +export default AnExSet; |
0 commit comments