Skip to content

Instantly share code, notes, and snippets.

@chirag04
Created July 8, 2016 23:32
Show Gist options
  • Save chirag04/6d20700b21c674c9b1aee3019e57468e to your computer and use it in GitHub Desktop.
Save chirag04/6d20700b21c674c9b1aee3019e57468e to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactNative from 'react-native';
const { View, LayoutAnimation, Keyboard } = ReactNative;
class KeyboardSpacer extends React.Component {
constructor(props) {
super(props);
this.state = {
keyboardHeight: 0,
};
}
componentWillMount() {
this.subscriptions = [
Keyboard.addListener('keyboardDidShow', this.keyboardDidShow),
Keyboard.addListener('keyboardWillHide', this.keyboardWillHide),
];
}
componentWillUnmount() {
this.subscriptions.forEach((sub) => sub.remove());
}
setLayoutAnimation({ duration, easing }) {
if (duration) {
LayoutAnimation.configureNext({
duration: duration,
update: {
duration: duration,
type: LayoutAnimation.Types[easing] || 'keyboard',
},
});
}
}
keyboardDidShow = (event) => {
this.setLayoutAnimation(event);
this.setState({
keyboardHeight: event.endCoordinates.height - this.props.offset,
});
};
keyboardWillHide = (event) => {
this.setLayoutAnimation(event);
this.setState({ keyboardHeight: 0 });
};
render() {
const style={ height: this.state.keyboardHeight };
return (
<View style={style} />
);
}
}
KeyboardSpacer.defaultProps = {
offset: 0,
};
KeyboardSpacer.propTypes = {
offset: React.PropTypes.number.isRequired,
};
export default KeyboardSpacer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment