Skip to content

Commit

Permalink
useNativeDriver for smoother animation in backdrop opacity and modal …
Browse files Browse the repository at this point in the history
…position

corrected various state changes, to ensure they are set before and after animations (in Animated.timing().start() callback, not the other way around)

fixed state.isAnimateBackdrop to always be true on animation start, false on end

ported hardwareAccelerated={true} back from #196 that was lost in the shuffle of merging for 1.5.0
  • Loading branch information
fungilation committed Jun 8, 2018
1 parent 40e8adc commit 1cfe784
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions index.js
Expand Up @@ -169,47 +169,49 @@ var ModalBox = createReactClass({
* Open animation for the backdrop, will fade in
*/
animateBackdropOpen: function() {
if (this.state.isAnimateBackdrop) {
if (this.state.isAnimateBackdrop && this.state.animBackdrop) {
this.state.animBackdrop.stop();
}
this.setState({ isAnimateBackdrop: true });

let animBackdrop = Animated.timing(
this.state.backdropOpacity,
{
toValue: 1,
duration: this.props.animationDuration
duration: this.props.animationDuration,
easing: this.props.easing,
useNativeDriver: true,
}
);

this.setState({
isAnimateBackdrop: true,
animBackdrop
}, () => {
this.state.animBackdrop.start();
).start(() => {
this.setState({
isAnimateBackdrop: false,
animBackdrop
});
});
},

/*
* Close animation for the backdrop, will fade out
*/
animateBackdropClose: function() {
if (this.state.isAnimateBackdrop) {
if (this.state.isAnimateBackdrop && this.state.animBackdrop) {
this.state.animBackdrop.stop();
}
this.setState({ isAnimateBackdrop: true });

let animBackdrop = Animated.timing(
this.state.backdropOpacity,
{
toValue: 0,
duration: this.props.animationDuration
duration: this.props.animationDuration,
easing: this.props.easing,
useNativeDriver: true,
}
);

this.setState({
isAnimateBackdrop: false,
animBackdrop
}, () => {
this.state.animBackdrop.start();
).start(() => {
this.setState({
isAnimateBackdrop: false,
animBackdrop
});
});
},

Expand Down Expand Up @@ -249,17 +251,15 @@ var ModalBox = createReactClass({
toValue: positionDest,
duration: this.props.animationDuration,
easing: this.props.easing,
useNativeDriver: true,
}
);

this.setState({
isAnimateOpen: false,
animOpen,
positionDest
}, () => {
animOpen.start(() => {
if (!this.state.isOpen && this.props.onOpened) this.props.onOpened();
).start(() => {
this.setState({
isAnimateOpen: false,
animOpen,
positionDest
});
if (this.props.onOpened) this.props.onOpened();
});
})
});
Expand Down Expand Up @@ -293,17 +293,17 @@ var ModalBox = createReactClass({
this.state.position,
{
toValue: this.props.entry === 'top' ? -this.state.containerHeight : this.state.containerHeight,
duration: this.props.animationDuration
duration: this.props.animationDuration,
easing: this.props.easing,
useNativeDriver: true,
}
);

this.setState({
isAnimateClose: false,
animClose
}, () => {
animClose.start(() => {
if (this.props.onClosed) this.props.onClosed();
).start(() => {
// Keyboard.dismiss(); // make this optional. Easily user defined in .onClosed() callback
this.setState({
isAnimateClose: false,
animClose
});
if (this.props.onClosed) this.props.onClosed();
});
});
},
Expand Down Expand Up @@ -479,6 +479,7 @@ if (!this.props.coverScreen) return content;
}
}}
supportedOrientations={['landscape', 'portrait', 'portrait-upside-down']} transparent visible={visible}
hardwareAccelerated={true}
>
{content}
</Modal>
Expand Down

0 comments on commit 1cfe784

Please sign in to comment.