Skip to content

Add hamburger animation #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/page/HomePage/HeaderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HeaderView extends React.Component {
</TouchableOpacity>
)}
{this.state && this.state.reportName && (
<Text style={[styles.navText]}>
<Text numberOfLines={2} style={[styles.navText]}>
{this.state.reportName}
</Text>
)}
Expand Down
38 changes: 32 additions & 6 deletions src/page/HomePage/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
StatusBar,
View,
Dimensions,
Animated
} from 'react-native';
import {SafeAreaInsetsContext, SafeAreaProvider} from 'react-native-safe-area-context';
import {Route} from '../../lib/Router';
Expand All @@ -26,7 +27,8 @@ export default class App extends React.Component {
// TODO: Set back to windowSize.width > widthBreakPoint once
// https://github.com/AndrewGable/ReactNativeChat/pull/132 is merged
hamburgerShown: true,
isHamburgerEnabled: windowSize.width <= widthBreakPoint
isHamburgerEnabled: windowSize.width <= widthBreakPoint,
animationTranslateX: new Animated.Value(0),
};

this.toggleHamburger = this.toggleHamburger.bind(this);
Expand Down Expand Up @@ -74,8 +76,25 @@ export default class App extends React.Component {
return;
}

const currentValue = this.state.hamburgerShown;
this.setState({hamburgerShown: !currentValue});
const hamburgerIsShown = this.state.hamburgerShown;
const animationFinalValue = hamburgerIsShown ? -300 : 0;

// If the hamburger currently is not shown, we want to immediately make it visible for the animation
if (!hamburgerIsShown) {
this.setState({hamburgerShown: !hamburgerIsShown});
}

Animated.timing(this.state.animationTranslateX, {
toValue: animationFinalValue,
duration: 250,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add timing functions here? Like easing, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the docs (https://reactnative.dev/docs/animated#configuring-animations), I am happy to tweak to use any animation type!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yes, it does look like there is easing: https://reactnative.dev/docs/easing

useNativeDriver: false
}).start(({finished}) => {
// If the hamburger is currently shown, we want to hide it only after the animation is complete
// Otherwise, we can't see the animation
if (finished && hamburgerIsShown) {
this.setState({hamburgerShown: !hamburgerIsShown});
}
});
}

render() {
Expand All @@ -98,12 +117,19 @@ export default class App extends React.Component {
]}
>
<Route path="/:reportID?">
<View style={[hamburgerStyle, visibility]}>
<Animated.View style={[
hamburgerStyle,
visibility,
{
transform: [{translateX: this.state.animationTranslateX}
]
}]}
>
<Sidebar insets={insets} onLinkClick={this.toggleHamburger} />
</View>
</Animated.View>
<View style={[styles.appContent, appContentStyle, styles.flex1, styles.flexColumn]}>
<Header
shouldShowHamburgerButton={!this.state.hamburgerShown}
shouldShowHamburgerButton={this.state.isHamburgerEnabled}
onHamburgerButtonClicked={this.toggleHamburger}
/>
<Main />
Expand Down