From 846f80b666c9099e71c87f7fdb7436e54fd9dae9 Mon Sep 17 00:00:00 2001 From: Liangyu Date: Mon, 24 Apr 2017 19:27:00 +0800 Subject: [PATCH] v0.1.4 add the hidden and animated properties to the NavigationBar --- README.md | 4 +- components/NavigationBar/NavigationBar.js | 71 +++-- components/NavigationPage/NavigationPage.js | 8 +- components/Overlay/OverlayPullView.js | 4 +- docs/cn/NavigationBar.md | 2 + docs/cn/NavigationPage.md | 1 + example/views/NavigationBarExample.js | 279 +++++++++++++++----- package.json | 2 +- 8 files changed, 288 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index 35b4046..2e81ba6 100755 --- a/README.md +++ b/README.md @@ -86,8 +86,8 @@ The document is being written, please refer to the example source code. ## Overlay ![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay1.png?raw=true) ![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay2.png?raw=true) -![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay3.png?raw=true) ![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay4.png?raw=true) -![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay5.png?raw=true) +![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay3.png?raw=true) ![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay6.png?raw=true) +![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay4.png?raw=true) ![](https://github.com/rilyu/teaset/blob/master/screenshots/15-Overlay5.png?raw=true) ## Toast ![](https://github.com/rilyu/teaset/blob/master/screenshots/16-Toast1.png?raw=true) ![](https://github.com/rilyu/teaset/blob/master/screenshots/16-Toast2.png?raw=true) diff --git a/components/NavigationBar/NavigationBar.js b/components/NavigationBar/NavigationBar.js index e049bed..0ff2f22 100644 --- a/components/NavigationBar/NavigationBar.js +++ b/components/NavigationBar/NavigationBar.js @@ -3,7 +3,7 @@ 'use strict'; import React, {Component, PropTypes} from 'react'; -import {StyleSheet, Platform, StatusBar, View, Text} from 'react-native'; +import {StyleSheet, Platform, StatusBar, View, Text, Animated} from 'react-native'; import Theme from 'teaset/themes/Theme'; import NavigationTitle from './NavigationTitle'; @@ -22,6 +22,8 @@ export default class NavigationBar extends Component { leftView: PropTypes.element, rightView: PropTypes.element, tintColor: PropTypes.string, //bar tint color, default tint color leftView and rightView + hidden: PropTypes.bool, //bar hidden + animated: PropTypes.bool, //hide or show bar with animation statusBarStyle: PropTypes.oneOf(['default', 'light-content']), //status bar style (iOS only) statusBarColor: PropTypes.string, //status bar color, default: style.backgroundColor statusBarHidden: PropTypes.bool, //status bar hidden @@ -31,6 +33,8 @@ export default class NavigationBar extends Component { static defaultProps = { ...View.defaultProps, type: 'ios', + hidden: false, + animated: true, statusBarInsets: true, }; @@ -49,15 +53,23 @@ export default class NavigationBar extends Component { this.state = { leftViewWidth: 0, rightViewWidth: 0, + barTop: new Animated.Value(props.hidden ? -64 : 0), + barOpacity: new Animated.Value(props.hidden ? 0 : 1), }; } + componentWillReceiveProps(nextProps) { + if (nextProps.hidden != this.props.hidden) { + this.checkBarHidden(nextProps.hidden, nextProps.animated); + } + } + getChildContext() { return {tintColor: this.props.tintColor}; } buildProps() { - let {style, type, title, titleStyle, tintColor, statusBarColor, statusBarStyle, statusBarInsets, ...others} = this.props; + let {style, type, title, titleStyle, tintColor, hidden, animated, statusBarColor, statusBarStyle, statusBarInsets, ...others} = this.props; //build style let justifyContent, titleTextAlign; @@ -74,7 +86,6 @@ export default class NavigationBar extends Component { style = [{ backgroundColor: Theme.navColor, position: 'absolute', - top: 0, left: 0, right: 0, height: statusBarInsets && Platform.OS === 'ios' ? 64 : 44, @@ -86,7 +97,9 @@ export default class NavigationBar extends Component { flexDirection: 'row', alignItems: 'center', justifyContent: justifyContent, - }].concat(style); + }].concat(style).concat({ + top: this.state.barTop, //hidden or shown + }); let fs = StyleSheet.flatten(style); @@ -121,17 +134,46 @@ export default class NavigationBar extends Component { height: 44, paddingLeft: paddingLeft, paddingRight: paddingRight, + opacity: this.state.barOpacity, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', - } + }; + + //build leftView and rightView style + let leftRightViewStyle = {opacity: this.state.barOpacity}; //convert string title to NavigationBar.Title if (typeof title === 'string') { title = ; } - this.props = {style, type, title, titleStyle, tintColor, titleViewStyle, statusBarColor, statusBarStyle, statusBarInsets, ...others}; + this.props = {style, type, title, titleStyle, tintColor, titleViewStyle, leftRightViewStyle, hidden, animated, statusBarColor, statusBarStyle, statusBarInsets, ...others}; + } + + checkBarHidden(hidden, animated) { + let {barTop, barOpacity} = this.state; + let barTopValue = hidden ? -this.barHeight : 0; + let barOpacityValue = hidden ? 0 : 1; + if (barTop._value != barTopValue || barOpacity._value != barOpacityValue) { + if (animated) { + Animated.parallel([ + Animated.spring(barTop, {toValue: barTopValue, friction: 9}), + Animated.spring(barOpacity, {toValue: barOpacityValue, friction: 9}), + ]).start(); + } else { + barTop.setValue(barTopValue); + barOpacity.setValue(barOpacityValue); + } + } + } + + onLayout(e) { + if (e.nativeEvent.layout.height != this.barHeight) { + this.barHeight = e.nativeEvent.layout.height; + this.checkBarHidden(this.props.hidden, this.props.animated); + } + this.props.onLayout && this.props.onLayout(e); } onLeftViewLayout(e) { @@ -149,17 +191,14 @@ export default class NavigationBar extends Component { render() { this.buildProps(); - let {style, statusBarStyle, statusBarColor, statusBarHidden, title, titleViewStyle, leftView, rightView, ...others} = this.props; - if (statusBarHidden) return ( -