Skip to content

Instantly share code, notes, and snippets.

@reggie3
Last active June 6, 2018 00:28
Show Gist options
  • Save reggie3/281764f555bb65138e096f636bacba0a to your computer and use it in GitHub Desktop.
Save reggie3/281764f555bb65138e096f636bacba0a to your computer and use it in GitHub Desktop.
Problem With Animation
import React, { Component } from 'react';
import {
View,
StyleSheet,
Modal,
Keyboard,
KeyboardAvoidingView,
Animated,
TouchableWithoutFeedback,
Easing
} from 'react-native';
import { globalStyles } from '../globals/styles';
import { RkButton } from 'react-native-ui-kitten';
import { WebViewQuillEditor } from 'react-native-webview-quilljs';
import StoryHeaderDisplay from './StoryHeaderDisplay';
import ModalTitle from './ModalTitle';
import ModalHR from './ModalHR';
const ANIMATION_DURATION = 1000;
// TODO: replace all animation durations with a number that is the same as the keyboard
// show and hide animation durations
class Modal_WriteReview extends Component {
constructor(props) {
super(props);
this.keyboardHeight = new Animated.Value(0);
this.imageHeight = new Animated.Value(100);
this.state = {
footerHeight: null,
headerHeight: null,
initialHeaderLayoutInfo: null,
initialFooterLayoutInfo: null,
};
}
componentWillMount() {
this.keyboardWillShowSub = Keyboard.addListener('keyboardWillShow', this.keyboardAppears);
this.keyboardWillHideSub = Keyboard.addListener('keyboardWillHide', this.keyboardDisappears);
this.keyboardDidShow = Keyboard.addListener('keyboardDidShow', this.keyboardAppears);
this.keyboardDidHide = Keyboard.addListener('keyboardDidHide', this.keyboardDisappears);
}
componentWillUnmount() {
this.keyboardWillShowSub.remove();
this.keyboardWillHideSub.remove();
this.keyboardDidShow.remove();
this.keyboardDidHide.remove();
}
keyboardAppears = event => {
Animated.parallel([
Animated.timing(this.state.headerHeight, {
duration: ANIMATION_DURATION,
toValue: 0.01,
easing:Easing.linear
}),
Animated.timing(this.state.footerHeight, {
duration: ANIMATION_DURATION,
toValue: 0.01,
easing: Easing.linear
}),
]).start();
};
keyboardDisappears = event => {
Animated.parallel([
Animated.timing(this.state.headerHeight, {
duration: ANIMATION_DURATION,
toValue: this.state.initialHeaderLayoutInfo.height,
easing: Easing.linear
}),
Animated.timing(this.state.footerHeight, {
duration: ANIMATION_DURATION,
toValue: this.state.initialFooterLayoutInfo.height,
easing: Easing.linear
}),
]).start();
};
onDeltaChangeCallback = () => {
debugger;
};
setInitialHeaderLayoutInfo = ({ nativeEvent: { layout: { x, y, width, height } = {} } = {} }) => {
this.setState({
initialHeaderLayoutInfo: {
x,
y,
width,
height,
},
headerHeight: new Animated.Value(height),
});
};
setInitialFooterLayoutInfo = ({ nativeEvent: { layout: { x, y, width, height } = {} } = {} }) => {
this.setState({
initialFooterLayoutInfo: {
x,
y,
width,
height,
},
footerHeight: new Animated.Value(height),
});
};
onTouchablePressed = () => {
console.log('touchable pressed');
};
render() {
console.log('header = ', this.state.headerHeight, '\nfooter = ',this.state.footerHeight);
if (this.props.visible) {
return (
<KeyboardAvoidingView style={styles.container} behavior="padding">
<Modal
animationType={'slide'}
transparent={true}
visible={this.props.visible}
onRequestClose={this.props.onClose}>
<View style={styles.modalBackground}>
<View style={styles.modalOverlay}>
<View
style={{
display: 'flex',
justifyContent: 'space-between',
...StyleSheet.absoluteFillObject,
padding: 10,
}}>
<Animated.View
id="header"
ref={component => {
this.header = component;
}}
style={{height: this.state.headerHeight ? this.state.headerHeight : 'auto'}}
onLayout={this.setInitialHeaderLayoutInfo}>
<ModalTitle text="Write Review" />
<ModalHR />
<StoryHeaderDisplay {...this.props.header} showDescription={false} />
</Animated.View>
<View style={{ flex: 1 }}>
<WebViewQuillEditor
ref={component => (this.webViewQuillEditor = component)}
getDeltaCallback={this.getDeltaCallback}
onDeltaChangeCallback={this.onDeltaChangeCallback}
/>
</View>
<Animated.View
id="footer"
ref={component => {
this.footer = component;
}}
style={{
height: this.state.footerHeight ? this.state.footerHeight : 'auto',
}}
onLayout={this.setInitialFooterLayoutInfo}>
<View style={styles.buttonPadder}>
<RkButton onPress={this.props.onClose} rkType="success stretch">
Save Review
</RkButton>
</View>
<ModalHR />
<View style={styles.footerButtonContainer}>
<View style={styles.buttonPadder}>
<RkButton onPress={this.props.onClose} rkType="stretch danger">
Close
</RkButton>
</View>
</View>
</Animated.View>
</View>
</View>
</View>
</Modal>
</KeyboardAvoidingView>
);
} else {
return null;
}
}
}
export default Modal_WriteReview;
const styles = StyleSheet.create({
...globalStyles,
modalBackground: {
flex: 1,
flexGrow: 1,
backgroundColor: 'rgba(255, 255, 255, 0.7)',
},
modalOverlay: {
flex: 1,
flexGrow: 1,
alignItems: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
marginHorizontal: 20,
borderRadius: 5,
flexDirection: 'column',
paddingHorizontal: 10,
padding: 15,
marginVertical: 25,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment