2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule DrawerLayoutAndroid
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-03-30 12:29:10 +00:00
|
|
|
var ColorPropType = require('ColorPropType');
|
2016-11-04 12:40:26 +00:00
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2016-03-30 12:29:10 +00:00
|
|
|
var Platform = require('Platform');
|
2015-09-14 14:35:58 +00:00
|
|
|
var React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
var PropTypes = require('prop-types');
|
2016-10-15 01:50:14 +00:00
|
|
|
var ReactNative = require('ReactNative');
|
2016-03-30 12:29:10 +00:00
|
|
|
var StatusBar = require('StatusBar');
|
2015-09-14 14:35:58 +00:00
|
|
|
var StyleSheet = require('StyleSheet');
|
2015-11-27 13:39:00 +00:00
|
|
|
var UIManager = require('UIManager');
|
2015-09-14 14:35:58 +00:00
|
|
|
var View = require('View');
|
2017-04-12 23:09:48 +00:00
|
|
|
var ViewPropTypes = require('ViewPropTypes');
|
2017-03-24 07:22:57 +00:00
|
|
|
|
2015-11-27 13:39:00 +00:00
|
|
|
var DrawerConsts = UIManager.AndroidDrawerLayout.Constants;
|
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
2015-09-14 14:35:58 +00:00
|
|
|
var dismissKeyboard = require('dismissKeyboard');
|
2015-11-18 16:24:26 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-09-14 14:35:58 +00:00
|
|
|
|
|
|
|
var RK_DRAWER_REF = 'drawerlayout';
|
|
|
|
var INNERVIEW_REF = 'innerView';
|
|
|
|
|
|
|
|
var DRAWER_STATES = [
|
|
|
|
'Idle',
|
|
|
|
'Dragging',
|
|
|
|
'Settling',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React component that wraps the platform `DrawerLayout` (Android only). The
|
|
|
|
* Drawer (typically used for navigation) is rendered with `renderNavigationView`
|
|
|
|
* and direct children are the main view (where your content goes). The navigation
|
|
|
|
* view is initially not visible on the screen, but can be pulled in from the
|
|
|
|
* side of the window specified by the `drawerPosition` prop and its width can
|
|
|
|
* be set by the `drawerWidth` prop.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* render: function() {
|
|
|
|
* var navigationView = (
|
2015-10-08 20:46:33 +00:00
|
|
|
* <View style={{flex: 1, backgroundColor: '#fff'}}>
|
|
|
|
* <Text style={{margin: 10, fontSize: 15, textAlign: 'left'}}>I'm in the Drawer!</Text>
|
|
|
|
* </View>
|
2015-09-14 14:35:58 +00:00
|
|
|
* );
|
|
|
|
* return (
|
|
|
|
* <DrawerLayoutAndroid
|
|
|
|
* drawerWidth={300}
|
|
|
|
* drawerPosition={DrawerLayoutAndroid.positions.Left}
|
|
|
|
* renderNavigationView={() => navigationView}>
|
2015-10-08 20:46:33 +00:00
|
|
|
* <View style={{flex: 1, alignItems: 'center'}}>
|
|
|
|
* <Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>Hello</Text>
|
|
|
|
* <Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>World!</Text>
|
|
|
|
* </View>
|
2015-09-14 14:35:58 +00:00
|
|
|
* </DrawerLayoutAndroid>
|
|
|
|
* );
|
|
|
|
* },
|
|
|
|
* ```
|
|
|
|
*/
|
2017-07-07 21:24:25 +00:00
|
|
|
var DrawerLayoutAndroid = createReactClass({
|
|
|
|
displayName: 'DrawerLayoutAndroid',
|
2015-09-14 14:35:58 +00:00
|
|
|
statics: {
|
|
|
|
positions: DrawerConsts.DrawerPosition,
|
|
|
|
},
|
|
|
|
|
|
|
|
propTypes: {
|
2017-03-24 07:22:57 +00:00
|
|
|
...ViewPropTypes,
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Determines whether the keyboard gets dismissed in response to a drag.
|
|
|
|
* - 'none' (the default), drags do not dismiss the keyboard.
|
|
|
|
* - 'on-drag', the keyboard is dismissed when a drag begins.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
keyboardDismissMode: PropTypes.oneOf([
|
2015-09-14 14:35:58 +00:00
|
|
|
'none', // default
|
|
|
|
'on-drag',
|
|
|
|
]),
|
2016-04-17 05:44:25 +00:00
|
|
|
/**
|
|
|
|
* Specifies the background color of the drawer. The default value is white.
|
|
|
|
* If you want to set the opacity of the drawer, use rgba. Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* return (
|
|
|
|
* <DrawerLayoutAndroid drawerBackgroundColor="rgba(0,0,0,0.5)">
|
|
|
|
* </DrawerLayoutAndroid>
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
drawerBackgroundColor: ColorPropType,
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Specifies the side of the screen from which the drawer will slide in.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
drawerPosition: PropTypes.oneOf([
|
2015-09-14 14:35:58 +00:00
|
|
|
DrawerConsts.DrawerPosition.Left,
|
|
|
|
DrawerConsts.DrawerPosition.Right
|
|
|
|
]),
|
|
|
|
/**
|
|
|
|
* Specifies the width of the drawer, more precisely the width of the view that be pulled in
|
|
|
|
* from the edge of the window.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
drawerWidth: PropTypes.number,
|
2016-02-24 11:24:50 +00:00
|
|
|
/**
|
|
|
|
* Specifies the lock mode of the drawer. The drawer can be locked in 3 states:
|
|
|
|
* - unlocked (default), meaning that the drawer will respond (open/close) to touch gestures.
|
2016-03-10 16:23:53 +00:00
|
|
|
* - locked-closed, meaning that the drawer will stay closed and not respond to gestures.
|
|
|
|
* - locked-open, meaning that the drawer will stay opened and not respond to gestures.
|
2016-02-24 11:24:50 +00:00
|
|
|
* The drawer may still be opened and closed programmatically (`openDrawer`/`closeDrawer`).
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
drawerLockMode: PropTypes.oneOf([
|
2016-02-24 11:24:50 +00:00
|
|
|
'unlocked',
|
|
|
|
'locked-closed',
|
|
|
|
'locked-open'
|
|
|
|
]),
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Function called whenever there is an interaction with the navigation view.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
onDrawerSlide: PropTypes.func,
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Function called when the drawer state has changed. The drawer can be in 3 states:
|
|
|
|
* - idle, meaning there is no interaction with the navigation view happening at the time
|
2015-12-15 17:08:39 +00:00
|
|
|
* - dragging, meaning there is currently an interaction with the navigation view
|
2015-09-14 14:35:58 +00:00
|
|
|
* - settling, meaning that there was an interaction with the navigation view, and the
|
2016-05-08 18:47:08 +00:00
|
|
|
* navigation view is now finishing its closing or opening animation
|
2015-09-14 14:35:58 +00:00
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
onDrawerStateChanged: PropTypes.func,
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Function called whenever the navigation view has been opened.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
onDrawerOpen: PropTypes.func,
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Function called whenever the navigation view has been closed.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
onDrawerClose: PropTypes.func,
|
2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* The navigation view that will be rendered to the side of the screen and can be pulled in.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
renderNavigationView: PropTypes.func.isRequired,
|
2016-03-30 12:29:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the drawer take the entire screen and draw the background of the
|
|
|
|
* status bar to allow it to open over the status bar. It will only have an
|
|
|
|
* effect on API 21+.
|
|
|
|
*/
|
|
|
|
statusBarBackgroundColor: ColorPropType,
|
2015-09-14 14:35:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
|
2016-04-17 05:44:25 +00:00
|
|
|
getDefaultProps: function(): Object {
|
|
|
|
return {
|
|
|
|
drawerBackgroundColor: 'white',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-03-30 12:29:10 +00:00
|
|
|
getInitialState: function() {
|
|
|
|
return {statusBarBackgroundColor: undefined};
|
|
|
|
},
|
|
|
|
|
2015-09-14 14:35:58 +00:00
|
|
|
getInnerViewNode: function() {
|
|
|
|
return this.refs[INNERVIEW_REF].getInnerViewNode();
|
|
|
|
},
|
|
|
|
|
2016-03-30 12:29:10 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
this._updateStatusBarBackground();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidReceiveProps: function() {
|
|
|
|
this._updateStatusBarBackground();
|
|
|
|
},
|
|
|
|
|
2015-09-14 14:35:58 +00:00
|
|
|
render: function() {
|
2016-03-30 12:29:10 +00:00
|
|
|
var drawStatusBar = Platform.Version >= 21 && this.props.statusBarBackgroundColor;
|
2015-09-14 14:35:58 +00:00
|
|
|
var drawerViewWrapper =
|
2016-04-17 05:44:25 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.drawerSubview,
|
|
|
|
{width: this.props.drawerWidth, backgroundColor: this.props.drawerBackgroundColor}
|
|
|
|
]}
|
|
|
|
collapsable={false}>
|
2015-09-14 14:35:58 +00:00
|
|
|
{this.props.renderNavigationView()}
|
2016-03-30 12:29:10 +00:00
|
|
|
{drawStatusBar && <View style={styles.drawerStatusBar} />}
|
2015-09-14 14:35:58 +00:00
|
|
|
</View>;
|
|
|
|
var childrenWrapper =
|
|
|
|
<View ref={INNERVIEW_REF} style={styles.mainSubview} collapsable={false}>
|
2016-03-30 12:29:10 +00:00
|
|
|
{drawStatusBar &&
|
|
|
|
<StatusBar
|
|
|
|
translucent
|
|
|
|
backgroundColor={this.state.statusBarBackgroundColor}
|
|
|
|
/>}
|
|
|
|
{drawStatusBar &&
|
|
|
|
<View style={[
|
|
|
|
styles.statusBar,
|
|
|
|
{backgroundColor: this.props.statusBarBackgroundColor}
|
|
|
|
]} />}
|
2015-09-14 14:35:58 +00:00
|
|
|
{this.props.children}
|
|
|
|
</View>;
|
|
|
|
return (
|
|
|
|
<AndroidDrawerLayout
|
|
|
|
{...this.props}
|
|
|
|
ref={RK_DRAWER_REF}
|
|
|
|
drawerWidth={this.props.drawerWidth}
|
|
|
|
drawerPosition={this.props.drawerPosition}
|
2016-02-24 11:24:50 +00:00
|
|
|
drawerLockMode={this.props.drawerLockMode}
|
2016-03-21 13:23:36 +00:00
|
|
|
style={[styles.base, this.props.style]}
|
2015-09-14 14:35:58 +00:00
|
|
|
onDrawerSlide={this._onDrawerSlide}
|
|
|
|
onDrawerOpen={this._onDrawerOpen}
|
|
|
|
onDrawerClose={this._onDrawerClose}
|
|
|
|
onDrawerStateChanged={this._onDrawerStateChanged}>
|
|
|
|
{childrenWrapper}
|
|
|
|
{drawerViewWrapper}
|
|
|
|
</AndroidDrawerLayout>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onDrawerSlide: function(event) {
|
|
|
|
if (this.props.onDrawerSlide) {
|
|
|
|
this.props.onDrawerSlide(event);
|
|
|
|
}
|
|
|
|
if (this.props.keyboardDismissMode === 'on-drag') {
|
|
|
|
dismissKeyboard();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onDrawerOpen: function() {
|
|
|
|
if (this.props.onDrawerOpen) {
|
|
|
|
this.props.onDrawerOpen();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onDrawerClose: function() {
|
|
|
|
if (this.props.onDrawerClose) {
|
|
|
|
this.props.onDrawerClose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onDrawerStateChanged: function(event) {
|
|
|
|
if (this.props.onDrawerStateChanged) {
|
|
|
|
this.props.onDrawerStateChanged(DRAWER_STATES[event.nativeEvent.drawerState]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-09 18:12:46 +00:00
|
|
|
/**
|
|
|
|
* Opens the drawer.
|
|
|
|
*/
|
2016-04-11 11:26:42 +00:00
|
|
|
openDrawer: function() {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2015-09-14 14:35:58 +00:00
|
|
|
this._getDrawerLayoutHandle(),
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.AndroidDrawerLayout.Commands.openDrawer,
|
2015-09-14 14:35:58 +00:00
|
|
|
null
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-04-09 18:12:46 +00:00
|
|
|
/**
|
|
|
|
* Closes the drawer.
|
|
|
|
*/
|
2015-09-14 14:35:58 +00:00
|
|
|
closeDrawer: function() {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2015-09-14 14:35:58 +00:00
|
|
|
this._getDrawerLayoutHandle(),
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.AndroidDrawerLayout.Commands.closeDrawer,
|
2015-09-14 14:35:58 +00:00
|
|
|
null
|
|
|
|
);
|
|
|
|
},
|
2017-07-24 15:09:52 +00:00
|
|
|
/**
|
|
|
|
* Closing and opening example
|
|
|
|
* Note: To access the drawer you have to give it a ref. Refs do not work on stateless components
|
|
|
|
* render () {
|
|
|
|
* this.openDrawer = () => {
|
|
|
|
* this.refs.DRAWER.openDrawer()
|
|
|
|
* }
|
|
|
|
* this.closeDrawer = () => {
|
|
|
|
* this.refs.DRAWER.closeDrawer()
|
|
|
|
* }
|
|
|
|
* return (
|
|
|
|
* <DrawerLayoutAndroid ref={'DRAWER'}>
|
|
|
|
* </DrawerLayoutAndroid>
|
|
|
|
* )
|
|
|
|
* }
|
|
|
|
*/
|
2015-09-14 14:35:58 +00:00
|
|
|
_getDrawerLayoutHandle: function() {
|
2016-04-08 02:43:49 +00:00
|
|
|
return ReactNative.findNodeHandle(this.refs[RK_DRAWER_REF]);
|
2015-09-14 14:35:58 +00:00
|
|
|
},
|
2016-03-30 12:29:10 +00:00
|
|
|
|
|
|
|
// Update the StatusBar component background color one frame after creating the
|
|
|
|
// status bar background View to avoid a white flicker that happens because
|
|
|
|
// the StatusBar background becomes transparent before the status bar View
|
|
|
|
// from this component has rendered.
|
|
|
|
_updateStatusBarBackground: function() {
|
|
|
|
if (Platform.Version >= 21 && this.props.statusBarBackgroundColor) {
|
|
|
|
// Check if the value is not already transparent to avoid an extra render.
|
|
|
|
if (this.state.statusBarBackgroundColor !== 'transparent') {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.setState({statusBarBackgroundColor: 'transparent'});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.setState({statusBarBackgroundColor: undefined});
|
|
|
|
}
|
|
|
|
},
|
2015-09-14 14:35:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
base: {
|
|
|
|
flex: 1,
|
2016-03-21 13:23:36 +00:00
|
|
|
elevation: 16,
|
2015-09-14 14:35:58 +00:00
|
|
|
},
|
|
|
|
mainSubview: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
drawerSubview: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
2016-03-30 12:29:10 +00:00
|
|
|
statusBar: {
|
|
|
|
height: StatusBar.currentHeight,
|
|
|
|
},
|
|
|
|
drawerStatusBar: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
height: StatusBar.currentHeight,
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0.251)',
|
|
|
|
},
|
2015-09-14 14:35:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// The View that contains both the actual drawer and the main view
|
2015-11-18 16:24:26 +00:00
|
|
|
var AndroidDrawerLayout = requireNativeComponent('AndroidDrawerLayout', DrawerLayoutAndroid);
|
2015-09-14 14:35:58 +00:00
|
|
|
|
|
|
|
module.exports = DrawerLayoutAndroid;
|