Skip to content

Commit 2809965

Browse files
committed
fix drawer close
fix drawer close
1 parent fbc8f8c commit 2809965

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

src/Drawer.js

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React, { Component, PropTypes } from "react";
22
import {
33
Animated,
44
Dimensions,
@@ -9,12 +9,12 @@ import {
99
StatusBar,
1010
Text,
1111
TouchableWithoutFeedback,
12-
View,
13-
} from 'react-native';
14-
import Icon from 'react-native-vector-icons/Ionicons';
12+
View
13+
} from "react-native";
14+
import Icon from "react-native-vector-icons/Ionicons";
1515

1616
// Get screen dimensions
17-
const { width, height } = Dimensions.get('window');
17+
const { width, height } = Dimensions.get("window");
1818

1919
export default class Drawer extends Component {
2020
// Define prop types
@@ -28,15 +28,15 @@ export default class Drawer extends Component {
2828
// Header height
2929
headerHeight: PropTypes.number,
3030
// Height of the visible teaser area at the bottom of the screen
31-
teaserHeight: PropTypes.number,
31+
teaserHeight: PropTypes.number
3232
};
3333

3434
// Set default prop values
3535
static defaultProps = {
3636
isOpen: false,
37-
header: 'Messages',
37+
header: "Messages",
3838
headerHeight: 70,
39-
teaserHeight: 75,
39+
teaserHeight: 75
4040
};
4141

4242
// Define state
@@ -46,7 +46,7 @@ export default class Drawer extends Component {
4646
// Whether the window is being pulled up/down or not
4747
pulling: false,
4848
// Zero means user haven't scrolled the content yet
49-
scrollOffset: 0,
49+
scrollOffset: 0
5050
};
5151

5252
// Configure animations
@@ -62,18 +62,18 @@ export default class Drawer extends Component {
6262
// minimal possible value - a bit lower the top of the screen
6363
min: this.props.headerHeight,
6464
// When animated triggers these value updates
65-
animates: [() => this._animatedOpacity, () => this._animatedWidth],
65+
animates: [() => this._animatedOpacity, () => this._animatedWidth]
6666
},
6767
// Window width
6868
width: {
6969
end: width, // takes full with once opened
70-
start: width - 20, // slightly narrower than screen when closed
70+
start: width - 20 // slightly narrower than screen when closed
7171
},
7272
// Window backdrop opacity
7373
opacity: {
7474
start: 0, // fully transparent when closed
75-
end: 1, // not transparent once opened
76-
},
75+
end: 1 // not transparent once opened
76+
}
7777
};
7878

7979
// Pan responder to handle gestures
@@ -87,18 +87,18 @@ export default class Drawer extends Component {
8787

8888
// Animates window position
8989
_animatedPosition = new Animated.Value(
90-
this.props.isOpen ? this.config.position.end : this.config.position.start,
90+
this.props.isOpen ? this.config.position.end : this.config.position.start
9191
);
9292

9393
componentWillMount() {
9494
// Set current position
9595
this._currentPosition = this._animatedPosition._value;
9696
// Listen for this._animatedPosition changes
97-
this._animatedPosition.addListener((value) => {
97+
this._animatedPosition.addListener(value => {
9898
// Update _currentPosition
9999
this._currentPosition = value.value;
100100
// Animate depending values
101-
this.config.position.animates.map((item) => {
101+
this.config.position.animates.map(item => {
102102
item().setValue(value.value);
103103
});
104104
});
@@ -115,7 +115,7 @@ export default class Drawer extends Component {
115115
onPanResponderTerminationRequest: (evt, gestureState) => true,
116116
onPanResponderRelease: this._handlePanResponderEnd,
117117
onPanResponderTerminate: this._handlePanResponderEnd,
118-
onShouldBlockNativeResponder: (evt, gestureState) => true,
118+
onShouldBlockNativeResponder: (evt, gestureState) => true
119119
});
120120
}
121121

@@ -135,27 +135,27 @@ export default class Drawer extends Component {
135135
// Interpolate position value into opacity value
136136
animatedOpacity = this._animatedOpacity.interpolate({
137137
inputRange: [this.config.position.end, this.config.position.start],
138-
outputRange: [this.config.opacity.end, this.config.opacity.start],
138+
outputRange: [this.config.opacity.end, this.config.opacity.start]
139139
}),
140140
// Interpolate position value into width value
141141
animatedWidth = this._animatedWidth.interpolate({
142142
inputRange: [
143143
this.config.position.min, // top of the screen
144144
this.config.position.start - 50, // 50 pixels higher than next point
145145
this.config.position.start, // a bit higher than the bottom of the screen
146-
this.config.position.max, // the bottom of the screen
146+
this.config.position.max // the bottom of the screen
147147
],
148148
outputRange: [
149149
this.config.width.end, // keep max width after next point
150150
this.config.width.end, // end: max width at 50 pixel higher
151151
this.config.width.start, // start: min width at the bottom
152-
this.config.width.start, // keep min width before previous point
153-
],
152+
this.config.width.start // keep min width before previous point
153+
]
154154
});
155155
return (
156156
<Animated.View style={[styles.container, this.getContainerStyle()]}>
157157
{/* Use light status bar because we have dark background */}
158-
<StatusBar barStyle={'light-content'} />
158+
<StatusBar barStyle={"light-content"} />
159159
{/* Backdrop with animated opacity */}
160160
<Animated.View style={[styles.backdrop, { opacity: animatedOpacity }]}>
161161
{/* Close window when tapped on header */}
@@ -184,16 +184,16 @@ export default class Drawer extends Component {
184184
// Animate position on the screen
185185
transform: [
186186
{ translateY: this._animatedPosition },
187-
{ translateX: 0 },
188-
],
189-
},
187+
{ translateX: 0 }
188+
]
189+
}
190190
]}
191191
// Handle gestures
192192
{...this._panResponder.panHandlers}
193193
>
194194
{/* Put all content in a scrollable container */}
195195
<ScrollView
196-
ref={(scrollView) => {
196+
ref={scrollView => {
197197
this._scrollView = scrollView;
198198
}}
199199
// Enable scrolling only when the window is open
@@ -249,6 +249,7 @@ export default class Drawer extends Component {
249249

250250
// Called when gesture ended
251251
_handlePanResponderEnd = (evt, gestureState) => {
252+
console.log(gestureState);
252253
// Reset offset
253254
this._animatedPosition.flattenOffset();
254255
// Reset pulling state
@@ -262,20 +263,25 @@ export default class Drawer extends Component {
262263
} else if (this.tapped(gestureState)) {
263264
// Toggle if tapped
264265
return this.toggle();
266+
} else if (this.down(gestureState)) {
267+
// Toggle if tapped
268+
return this.toggle();
265269
}
266270
// Restore back to appropriate position otherwise
267271
this.restore();
268272
};
269273

270274
// Handle content scrolling
271-
_handleScroll = (event) => {
275+
_handleScroll = event => {
272276
const { y } = event.nativeEvent.contentOffset;
273277
this.setState({ scrollOffset: y });
274278
};
275279

276280
// Check if gesture was a tap
277281
tapped = gestureState => gestureState.dx === 0 && gestureState.dy === 0;
278282

283+
down = gestureState => gestureState.dy > 0;
284+
279285
// Check if pulled up
280286
pulledUp = gestureState => gestureState.dy < 0;
281287

@@ -298,7 +304,7 @@ export default class Drawer extends Component {
298304
this.setState({ open: true }, () => {
299305
Animated.timing(this._animatedPosition, {
300306
toValue: this.config.position.end,
301-
duration: 400,
307+
duration: 400
302308
}).start();
303309
});
304310
};
@@ -308,10 +314,10 @@ export default class Drawer extends Component {
308314
this._scrollView.scrollTo({ y: 0 });
309315
Animated.timing(this._animatedPosition, {
310316
toValue: this.config.position.start,
311-
duration: 400,
317+
duration: 400
312318
}).start(() => {
313319
this.setState({
314-
open: false,
320+
open: false
315321
});
316322

317323
this._scrollView.scrollTo({ y: 0 });
@@ -339,57 +345,57 @@ export default class Drawer extends Component {
339345
// Get header style
340346
getHeaderStyle = () => ({
341347
height:
342-
Platform.OS === 'ios'
348+
Platform.OS === "ios"
343349
? this.props.headerHeight
344-
: this.props.headerHeight - 40, // compensate for the status bar
350+
: this.props.headerHeight - 40 // compensate for the status bar
345351
});
346352

347353
// Get container style
348354
getContainerStyle = () => ({
349355
// Move the view below others if not open or moving
350356
// to not block gesture handlers on other views
351-
zIndex: this.state.pulling || this.state.open ? 1 : -1,
357+
zIndex: this.state.pulling || this.state.open ? 1 : -1
352358
});
353359
}
354360

355361
const styles = StyleSheet.create({
356362
// Main container
357363
container: {
358364
...StyleSheet.absoluteFillObject, // fill up all screen
359-
alignItems: 'center', // center children
360-
justifyContent: 'flex-end', // align popup at the bottom
361-
backgroundColor: 'transparent', // transparent background
362-
elevation: 999, // fix android dynamic zindex issue
365+
alignItems: "center", // center children
366+
justifyContent: "flex-end", // align popup at the bottom
367+
backgroundColor: "transparent", // transparent background
368+
elevation: 999 // fix android dynamic zindex issue
363369
},
364370
// Semi-transparent background below popup
365371
backdrop: {
366372
...StyleSheet.absoluteFillObject, // fill up all screen
367-
alignItems: 'center', // center children
368-
justifyContent: 'flex-start', // align popup at the bottom
369-
backgroundColor: 'black',
373+
alignItems: "center", // center children
374+
justifyContent: "flex-start", // align popup at the bottom
375+
backgroundColor: "black"
370376
},
371377
// Body
372378
content: {
373-
backgroundColor: 'black',
374-
height,
379+
backgroundColor: "black",
380+
height
375381
},
376382
// Header
377383
header: {
378-
flexDirection: 'row', // arrange children in a row
379-
alignItems: 'center', // center vertically
384+
flexDirection: "row", // arrange children in a row
385+
alignItems: "center", // center vertically
380386
paddingTop: 20,
381-
paddingHorizontal: 20,
387+
paddingHorizontal: 20
382388
},
383389
headerIcon: {
384-
marginRight: 10,
390+
marginRight: 10
385391
},
386392
headerTitle: {
387-
flex: 1, // take up all available space
393+
flex: 1 // take up all available space
388394
},
389395
headerText: {
390-
color: 'white',
391-
fontFamily: 'Avenir',
392-
fontWeight: '600',
393-
fontSize: 16,
394-
},
396+
color: "white",
397+
fontFamily: "Avenir",
398+
fontWeight: "600",
399+
fontSize: 16
400+
}
395401
});

0 commit comments

Comments
 (0)