UNPKG

8.63 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
8
9var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
10
11var _createClass2 = require('babel-runtime/helpers/createClass');
12
13var _createClass3 = _interopRequireDefault(_createClass2);
14
15var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
16
17var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
18
19var _inherits2 = require('babel-runtime/helpers/inherits');
20
21var _inherits3 = _interopRequireDefault(_inherits2);
22
23var _react = require('react');
24
25var _react2 = _interopRequireDefault(_react);
26
27var _reactNative = require('react-native');
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
30
31var styles = _reactNative.StyleSheet.create({
32 wrap: {
33 flex: 1,
34 backgroundColor: 'rgba(0,0,0,0)'
35 },
36 mask: {
37 backgroundColor: 'black',
38 opacity: .5
39 },
40 content: {
41 backgroundColor: 'white'
42 },
43 absolute: {
44 position: 'absolute',
45 top: 0,
46 bottom: 0,
47 left: 0,
48 right: 0
49 }
50});
51var screen = _reactNative.Dimensions.get('window');
52
53var RCModal = function (_React$Component) {
54 (0, _inherits3['default'])(RCModal, _React$Component);
55
56 function RCModal(props) {
57 (0, _classCallCheck3['default'])(this, RCModal);
58
59 var _this = (0, _possibleConstructorReturn3['default'])(this, (RCModal.__proto__ || Object.getPrototypeOf(RCModal)).call(this, props));
60
61 _this.animateMask = function (visible) {
62 _this.stopMaskAnim();
63 _this.state.opacity.setValue(_this.getOpacity(!visible));
64 _this.animMask = _reactNative.Animated.timing(_this.state.opacity, {
65 toValue: _this.getOpacity(visible),
66 duration: _this.props.animationDuration
67 });
68 _this.animMask.start(function () {
69 _this.animMask = null;
70 });
71 };
72 _this.stopMaskAnim = function () {
73 if (_this.animMask) {
74 _this.animMask.stop();
75 _this.animMask = null;
76 }
77 };
78 _this.stopDialogAnim = function () {
79 if (_this.animDialog) {
80 _this.animDialog.stop();
81 _this.animDialog = null;
82 }
83 };
84 _this.animateDialog = function (visible) {
85 _this.stopDialogAnim();
86 _this.animateMask(visible);
87 var _this$props = _this.props,
88 animationType = _this$props.animationType,
89 animationDuration = _this$props.animationDuration;
90
91 animationDuration = animationDuration;
92 if (animationType !== 'none') {
93 if (animationType === 'slide-up' || animationType === 'slide-down') {
94 _this.state.position.setValue(_this.getPosition(!visible));
95 _this.animDialog = _reactNative.Animated.timing(_this.state.position, {
96 toValue: _this.getPosition(visible),
97 duration: animationDuration,
98 easing: visible ? _reactNative.Easing.elastic(0.8) : undefined
99 });
100 } else if (animationType === 'fade') {
101 _this.animDialog = _reactNative.Animated.parallel([_reactNative.Animated.timing(_this.state.opacity, {
102 toValue: _this.getOpacity(visible),
103 duration: animationDuration,
104 easing: visible ? _reactNative.Easing.elastic(0.8) : undefined
105 }), _reactNative.Animated.spring(_this.state.scale, {
106 toValue: _this.getScale(visible)
107 })]);
108 }
109 _this.animDialog.start(function () {
110 _this.animDialog = null;
111 if (!visible) {
112 _this.setState({
113 modalVisible: false
114 });
115 }
116 if (_this.props.onAnimationEnd) {
117 _this.props.onAnimationEnd(visible);
118 }
119 });
120 } else {
121 if (!visible) {
122 _this.setState({
123 modalVisible: false
124 });
125 }
126 }
127 };
128 _this.close = function () {
129 _this.animateDialog(false);
130 };
131 _this.onMaskClose = function () {
132 if (_this.props.maskClosable && _this.props.onClose) {
133 _this.props.onClose();
134 }
135 };
136 _this.getPosition = function (visible) {
137 if (visible) {
138 return 0;
139 }
140 return _this.props.animationType === 'slide-down' ? -screen.height : screen.height;
141 };
142 _this.getScale = function (visible) {
143 return visible ? 1 : 1.05;
144 };
145 _this.getOpacity = function (visible) {
146 return visible ? 1 : 0;
147 };
148 var visible = props.visible;
149
150 _this.state = {
151 position: new _reactNative.Animated.Value(_this.getPosition(visible)),
152 scale: new _reactNative.Animated.Value(_this.getScale(visible)),
153 opacity: new _reactNative.Animated.Value(_this.getOpacity(visible)),
154 modalVisible: visible
155 };
156 return _this;
157 }
158
159 (0, _createClass3['default'])(RCModal, [{
160 key: 'componentWillReceiveProps',
161 value: function componentWillReceiveProps(nextProps) {
162 if (this.shouldComponentUpdate(nextProps, null)) {
163 this.setState({
164 modalVisible: true
165 });
166 }
167 }
168 }, {
169 key: 'shouldComponentUpdate',
170 value: function shouldComponentUpdate(nextProps, nextState) {
171 if (this.props.visible || this.props.visible !== nextProps.visible) {
172 return true;
173 }
174 if (nextState) {
175 if (nextState.modalVisible !== this.state.modalVisible) {
176 return true;
177 }
178 }
179 return false;
180 }
181 }, {
182 key: 'componentDidMount',
183 value: function componentDidMount() {
184 if (this.props.animateAppear && this.props.animationType !== 'none') {
185 this.componentDidUpdate({});
186 }
187 }
188 }, {
189 key: 'componentDidUpdate',
190 value: function componentDidUpdate(prevProps) {
191 var props = this.props;
192
193 if (prevProps.visible !== props.visible) {
194 this.animateDialog(props.visible);
195 }
196 }
197 }, {
198 key: 'render',
199 value: function render() {
200 var props = this.props;
201
202 if (!this.state.modalVisible) {
203 return null;
204 }
205 var animationStyleMap = {
206 none: {},
207 'slide-up': { transform: [{ translateY: this.state.position }] },
208 'slide-down': { transform: [{ translateY: this.state.position }] },
209 fade: { transform: [{ scale: this.state.scale }], opacity: this.state.opacity }
210 };
211 return _react2['default'].createElement(_reactNative.Modal, { visible: true, transparent: true, onRequestClose: this.props.onClose, supportedOrientations: ['portrait', 'landscape'] }, _react2['default'].createElement(_reactNative.View, { style: [styles.wrap, props.wrapStyle] }, _react2['default'].createElement(_reactNative.TouchableWithoutFeedback, { onPress: this.onMaskClose }, _react2['default'].createElement(_reactNative.Animated.View, { style: [styles.absolute, { opacity: this.state.opacity }] }, _react2['default'].createElement(_reactNative.View, { style: [styles.absolute, props.maskStyle] }))), _react2['default'].createElement(_reactNative.Animated.View, { style: [styles.content, props.style, animationStyleMap[props.animationType]] }, this.props.children)));
212 }
213 }]);
214 return RCModal;
215}(_react2['default'].Component);
216
217exports['default'] = RCModal;
218
219RCModal.defaultProps = {
220 wrapStyle: styles.wrap,
221 maskStyle: styles.mask,
222 animationType: 'slide-up',
223 animateAppear: false,
224 animationDuration: 300,
225 visible: false,
226 maskClosable: true,
227 onClose: function onClose() {},
228 onAnimationEnd: function onAnimationEnd(_visible) {}
229};
230module.exports = exports['default'];
\No newline at end of file