forked from Andr3wHur5t/react-native-keyboard-spacer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardSpacer.js
92 lines (81 loc) · 2.42 KB
/
KeyboardSpacer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Created by andrewhurst on 10/5/15.
*/
var React = require('react-native');
var {
DeviceEventEmitter,
LayoutAnimation,
View
} = React;
// From: https://medium.com/man-moon/writing-modern-react-native-ui-e317ff956f02
const animations = {
layout: {
spring: {
duration: 500,
create: {
duration: 300,
type: LayoutAnimation.Types.easeInEaseOut,
property: LayoutAnimation.Properties.opacity
},
update: {
type: LayoutAnimation.Types.spring,
springDamping: 200
}
},
easeInEaseOut: {
duration: 300,
create: {
type: LayoutAnimation.Types.easeInEaseOut,
property: LayoutAnimation.Properties.scaleXY
},
update: {
delay: 100,
type: LayoutAnimation.Types.easeInEaseOut
}
}
}
};
class KeyboardSpacer extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
keyboardSpace: 0,
isKeyboardOpened: false
};
this.updateKeyboardSpace = this.updateKeyboardSpace.bind(this);
this.resetKeyboardSpace = this.resetKeyboardSpace.bind(this);
}
componentWillUpdate(props, state) {
if (state.isKeyboardOpened !== this.state.isKeyboardOpened)
LayoutAnimation.configureNext(animations.layout.spring);
}
updateKeyboardSpace(frames) {
if (!frames.endCoordinates)
return;
this.setState({
keyboardSpace: frames.endCoordinates.height,
isKeyboardOpened: true
});
}
resetKeyboardSpace() {
this.setState({
keyboardSpace: 0,
isKeyboardOpened: false
});
}
componentDidMount() {
this._listeners = [
DeviceEventEmitter.addListener('keyboardWillShow', this.updateKeyboardSpace),
DeviceEventEmitter.addListener('keyboardWillHide', this.resetKeyboardSpace)
];
}
componentWillUnmount() {
this._listeners.forEach(function(/** EmitterSubscription */listener) {
listener.remove();
});
}
render() {
return (<View style={[{height: this.state.keyboardSpace, left: 0, right: 0, bottom: 0}, this.props.style]}/>);
}
}
module.exports = KeyboardSpacer;