├── .gitignore ├── .npmignore ├── note.md ├── package.json ├── LICENSE ├── LoadingSpinnerOverlay.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.[aod] 2 | *.DS_Store 3 | .DS_Store 4 | *Thumbs.db 5 | *.iml 6 | .gradle 7 | .idea 8 | node_modules 9 | npm-debug.log 10 | /android/build 11 | /ios/**/*xcuserdata* 12 | /ios/**/*xcshareddata* -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.[aod] 2 | *.DS_Store 3 | .DS_Store 4 | *Thumbs.db 5 | *.iml 6 | .gradle 7 | .idea 8 | node_modules 9 | npm-debug.log 10 | /android/build 11 | /ios/**/*xcuserdata* 12 | /ios/**/*xcshareddata* -------------------------------------------------------------------------------- /note.md: -------------------------------------------------------------------------------- 1 | 2 | * 需要保证组件为单例, 在显示期间再次调用显示时, 不会产生冲突 3 | * 需要支持完全模态(屏幕可视区域无法点击), 4 | 部分模态(部分区域比如导航栏可以点击), 5 | 非模态(除了加载指示器区域外都可以点击) 6 | * 需要保证上述三种类型, 加载指示器区域显示时相对屏幕可视区域水平及垂直居中 7 | * 需要支持加载指示器区域及内容可自定义样式和内容 8 | 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@react-native-component/react-native-smart-loading-spinner-overlay", 3 | "version": "1.0.2", 4 | "description": "A smart loading spinner overlay for React Native apps, written in JS for cross-platform support. It works on iOS and Android.", 5 | "main": "LoadingSpinnerOverlay.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/react-native-component/react-native-smart-loading-spinner-overlay.git" 12 | }, 13 | "keywords": [ 14 | "react-native", 15 | "smart", 16 | "loading", 17 | "spinner", 18 | "overlay" 19 | ], 20 | "author": "HISAME SHIZUMARU", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/react-native-component/react-native-smart-loading-spinner-overlay/issues" 24 | }, 25 | "homepage": "https://github.com/react-native-component/react-native-smart-loading-spinner-overlay#readme", 26 | "dependencies": { 27 | "react-native-smart-timer-enhance": "^1.0.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LoadingSpinnerOverlay.js: -------------------------------------------------------------------------------- 1 | /* 2 | * A smart loading spinner overlay for React Native apps, written in JS 3 | * https://github.com/react-native-component/react-native-smart-loading-spinner-overlay/ 4 | * Released under the MIT license 5 | * Copyright (c) 2016 react-native-component 6 | */ 7 | 8 | import React, { 9 | Component, 10 | PropTypes, 11 | } from 'react' 12 | import { 13 | View, 14 | Modal, 15 | StyleSheet, 16 | Animated, 17 | Easing, 18 | Dimensions, 19 | ActivityIndicator, 20 | ActivityIndicatorIOS, 21 | ProgressBarAndroid, 22 | } from 'react-native' 23 | 24 | import TimerEnhance from 'react-native-smart-timer-enhance' 25 | 26 | const styles = StyleSheet.create({ 27 | overlay: { 28 | position: 'absolute', 29 | left: 0, 30 | top: 0, 31 | zIndex: 998, 32 | }, 33 | container: { 34 | backgroundColor: 'rgba(0, 0, 0, 0.8)', 35 | position: 'absolute', 36 | borderRadius: 8, 37 | padding: 20.5, 38 | left: -9999, 39 | top: -9999, 40 | zIndex: 999, 41 | }, 42 | }) 43 | const noop = () => {} 44 | 45 | class LoadingSpinnerOverlay extends Component { 46 | 47 | static defaultProps = { 48 | duration: 255, 49 | delay: 0, 50 | marginTop: 0, 51 | modal: true, 52 | } 53 | 54 | static propTypes = { 55 | overlayStyle: View.propTypes.style, 56 | style: View.propTypes.style, 57 | duration: PropTypes.number, 58 | delay: PropTypes.number, 59 | marginTop: PropTypes.number, 60 | modal: PropTypes.bool, 61 | } 62 | 63 | constructor(props) { 64 | super(props) 65 | this.state = { 66 | visible: false, 67 | opacity: new Animated.Value(0), 68 | children: props.children, 69 | modal: props.modal, 70 | marginTop: props.marginTop, 71 | } 72 | this._loadingSpinnerWidth = null 73 | this._loadingSpinnerHeight = null 74 | this._loadingSpinnerShowAnimation = null 75 | this._loadingSpinnerHideAnimation = null 76 | this._loadingSpinnerAnimationToggle = null 77 | } 78 | 79 | render() { 80 | let loadingSpinner = this._renderLoadingSpinner() 81 | return this._renderOverLay(loadingSpinner) 82 | } 83 | 84 | _renderOverLay(loadingSpinner) { 85 | let {width: deviceWidth, height: deviceHeight,} = Dimensions.get('window') 86 | return ( 87 | this.state.modal ? 88 | (this.state.marginTop === 0 ? 89 | 90 | {loadingSpinner} 91 | : 92 | (this.state.visible ? 93 | 95 | {loadingSpinner} 96 | : null)) 97 | : loadingSpinner 98 | ) 99 | } 100 | 101 | _renderLoadingSpinner() { 102 | let children 103 | if(this.state.children == null) { 104 | children = this._renderActivityIndicator() 105 | } 106 | else { 107 | children = React.Children.map(this.state.children, (child) => { 108 | if (!React.isValidElement(child)) { 109 | return null 110 | } 111 | return child 112 | }) 113 | } 114 | return ( 115 | this.state.visible ? 116 | this._container = component } 118 | onLayout={this._onLoadingSpinnerLayout} 119 | style={[styles.container, this.props.style, {opacity:this.state.opacity, }]}> 120 | {children} 121 | : null 122 | ) 123 | } 124 | 125 | show({modal = this.state.modal, marginTop = this.state.marginTop, children = this.state.children, duration = this.props.duration, easing = Easing.linear, delay = this.props.delay, animationEnd,} 126 | = {modal: this.state.modal, marginTop: this.state.marginTop, children: this.state.children, duration: this.props.duration, easing: Easing.linear, delay: this.props.delay,}) { 127 | 128 | this._loadingSpinnerShowAnimation && this._loadingSpinnerShowAnimation.stop() 129 | this._loadingSpinnerHideAnimation && this._loadingSpinnerHideAnimation.stop() 130 | this._loadingSpinnerAnimationToggle && this.clearTimeout(this._loadingSpinnerAnimationToggle) 131 | 132 | if(this.state.visible) { 133 | this._setLoadingSpinnerOverlayPosition({modal, marginTop}) 134 | } 135 | 136 | this.setState({ 137 | children, 138 | modal, 139 | marginTop, 140 | visible: true, 141 | }) 142 | 143 | this._loadingSpinnerShowAnimation = Animated.timing( 144 | this.state.opacity, 145 | { 146 | toValue: 1, 147 | duration, 148 | easing, 149 | delay, 150 | } 151 | ) 152 | this._loadingSpinnerShowAnimation.start(() => { 153 | this._loadingSpinnerShowAnimation = null 154 | animationEnd && animationEnd() 155 | }) 156 | } 157 | 158 | hide({duration = this.props.duration, easing = Easing.linear, delay = this.props.delay, animationEnd,} 159 | = {duration: this.props.duration, easing: Easing.linear, delay: this.props.delay,}) { 160 | 161 | this._loadingSpinnerShowAnimation && this._loadingSpinnerShowAnimation.stop() 162 | this._loadingSpinnerHideAnimation && this._loadingSpinnerHideAnimation.stop() 163 | this.clearTimeout(this._loadingSpinnerAnimationToggle) 164 | 165 | this._loadingSpinnerHideAnimation = Animated.timing( 166 | this.state.opacity, 167 | { 168 | toValue: 0, 169 | duration, 170 | easing, 171 | delay, 172 | } 173 | ) 174 | this._loadingSpinnerHideAnimation.start( () => { 175 | this._loadingSpinnerHideAnimation = null 176 | this.setState({ 177 | visible: false, 178 | }) 179 | animationEnd && animationEnd() 180 | }) 181 | } 182 | 183 | _onLoadingSpinnerLayout = (e) => { 184 | this._loadingSpinnerWidth = e.nativeEvent.layout.width 185 | this._loadingSpinnerHeight = e.nativeEvent.layout.height 186 | this._setLoadingSpinnerOverlayPosition() 187 | } 188 | 189 | _setLoadingSpinnerOverlayPosition({modal, marginTop} = {modal: this.state.modal, marginTop: this.state.marginTop}) { 190 | if(!this._loadingSpinnerWidth || !this._loadingSpinnerHeight) { 191 | return 192 | } 193 | let {width: deviceWidth, height: deviceHeight,} = Dimensions.get('window') 194 | let left = (deviceWidth - this._loadingSpinnerWidth) / 2 195 | let top = (deviceHeight - this._loadingSpinnerHeight) / 2 - (modal && marginTop === 0 ? 0 : marginTop) 196 | this._container.setNativeProps({ 197 | style: { 198 | left, 199 | top, 200 | } 201 | }) 202 | } 203 | 204 | _renderActivityIndicator() { 205 | return ActivityIndicator ? ( 206 | 211 | ) : Platform.OS == 'android' ? 212 | ( 213 | 216 | 217 | ) : ( 218 | 222 | ) 223 | } 224 | 225 | } 226 | 227 | export default TimerEnhance(LoadingSpinnerOverlay) 228 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-smart-loading-spinner-overlay 2 | 3 | [![npm](https://img.shields.io/npm/v/react-native-smart-loading-spinner-overlay.svg)](https://www.npmjs.com/package/react-native-smart-loading-spinner-overlay) 4 | [![npm](https://img.shields.io/npm/dm/react-native-smart-loading-spinner-overlay.svg)](https://www.npmjs.com/package/react-native-smart-loading-spinner-overlay) 5 | [![npm](https://img.shields.io/npm/dt/react-native-smart-loading-spinner-overlay.svg)](https://www.npmjs.com/package/react-native-smart-loading-spinner-overlay) 6 | [![npm](https://img.shields.io/npm/l/react-native-smart-loading-spinner-overlay.svg)](https://github.com/react-native-component/react-native-smart-loading-spinner-overlay/blob/master/LICENSE) 7 | 8 | A smart loading spinner overlay for React Native apps, written in JS for cross-platform support. 9 | It works on iOS and Android. 10 | 11 | This component is compatible with React Native 0.25 and newer. 12 | 13 | ## Preview 14 | 15 | ![react-native-smart-loading-spinner-overlay-preview-ios][1] 16 | ![react-native-smart-loading-spinner-overlay-preview-android][2] 17 | 18 | ## Installation 19 | 20 | ``` 21 | npm install react-native-smart-loading-spinner-overlay --save 22 | ``` 23 | 24 | ## Full Demo 25 | 26 | see [ReactNativeComponentDemos][0] 27 | 28 | ## Usage 29 | 30 | Install the loading-spinner-overlay from npm with `npm install react-native-smart-loading-spinner-overlay --save`. 31 | Then, require it from your app's JavaScript files with `import loading-spinner-overlay from 'react-native-smart-loading-spinner-overlay'`. 32 | 33 | ```js 34 | 35 | import React, { 36 | Component, 37 | } from 'react' 38 | import { 39 | View, 40 | } from 'react-native' 41 | 42 | import Button from 'react-native-smart-button' 43 | import TimerEnhance from 'react-native-smart-timer-enhance' 44 | import Toast from 'react-native-smart-loading-spinner-overlay' 45 | 46 | class LoadingSpinnerOverLayDemo extends Component { 47 | 48 | constructor(props) { 49 | super(props) 50 | this.state = {} 51 | } 52 | 53 | render() { 54 | return ( 55 | 56 | 64 | 73 | 81 | 82 | 90 | 91 | 99 | 100 | this._modalLoadingSpinnerOverLay = component }/> 102 | this._partModalLoadingSpinnerOverLay = component } 104 | modal={true} 105 | marginTop={64}/> 106 | this._LoadingSpinnerOverLay = component } 108 | modal={false}/> 109 | this._modalImmediateLoadingSpinnerOverLay = component }/> 111 | this._modal_2_LoadingSpinnerOverLay = component }> 113 | {this._renderActivityIndicator()} 114 | 115 | 116 | ) 117 | } 118 | 119 | 120 | 121 | 122 | _showModalLoadingSpinnerOverLay = () => { 123 | this._modalLoadingSpinnerOverLay.show() 124 | //simulate http request 125 | this.setTimeout( () => { 126 | this._modalLoadingSpinnerOverLay.hide() 127 | }, 3000) 128 | } 129 | 130 | _showPartModalLoadingSpinnerOverLay = () => { 131 | this._partModalLoadingSpinnerOverLay.show() 132 | //simulate http request 133 | this.setTimeout( () => { 134 | this._partModalLoadingSpinnerOverLay.hide() 135 | }, 3000) 136 | } 137 | 138 | _showNoModalLoadingSpinnerOverLay = () => { 139 | this._LoadingSpinnerOverLay.show() 140 | //simulate http request 141 | this.setTimeout( () => { 142 | this._LoadingSpinnerOverLay.hide() 143 | }, 3000) 144 | } 145 | 146 | _showImmediateLoadingSpinnerOverLayAndImmediateHide = () => { 147 | this._modalImmediateLoadingSpinnerOverLay.show({ 148 | duration: 0, 149 | children: this._renderActivityIndicator(), 150 | }) 151 | //simulate http request 152 | this.setTimeout( () => { 153 | this._modalImmediateLoadingSpinnerOverLay.hide({ 154 | duration: 0, 155 | }) 156 | }, 3000) 157 | } 158 | 159 | _showModal_2_LoadingSpinnerOverLay = () => { 160 | this._modal_2_LoadingSpinnerOverLay.show() 161 | //simulate http request 162 | this.setTimeout( () => { 163 | this._modal_2_LoadingSpinnerOverLay.hide() 164 | }, 3000) 165 | } 166 | 167 | _renderActivityIndicator() { 168 | return ActivityIndicator ? ( 169 | 173 | ) : Platform.OS == 'android' ? 174 | ( 175 | 178 | 179 | ) : ( 180 | 184 | ) 185 | } 186 | 187 | } 188 | 189 | 190 | export default TimerEnhance(LoadingSpinnerOverLayDemo) 191 | ``` 192 | 193 | ## Props 194 | 195 | Prop | Type | Optional | Default | Description 196 | ---------------- | ------ | -------- | ---------------- | ----------- 197 | style | style | Yes | | see [react-native documents][3] 198 | overlayStyle | style | Yes | | see [react-native documents][3] 199 | duration | number | Yes | 255 | determine the duration of loading-spinner-overlay animation 200 | delay | number | Yes | 0 | determine the delay of loading-spinner-overlay animation 201 | marginTop | number | Yes | 0 | determine the marginTop of the root container view, it is used when the modal prop is false 202 | modal | bool | Yes | true | determine if the modal will be used 203 | 204 | ## Method 205 | 206 | * show({modal, marginTop, children, duration, easing, delay, animationEnd,}) 207 | 208 | * modal: determine if the modal will be used 209 | * marginTop: determine the marginTop of the root container view, it is used when the modal prop is false 210 | * children: determine the children of loading-spinner-overlay 211 | * duration: determine the duration of animation 212 | * easing: determine the easing of animation 213 | * delay: determine the delay of animation 214 | * animationEnd: determine the callback when animation is end 215 | 216 | * hide({duration, easing, delay, animationEnd,}) 217 | 218 | * duration: determine the duration of animation 219 | * easing: determine the easing of animation 220 | * delay: determine the delay of animation 221 | * animationEnd: determine the callback when animation is end 222 | 223 | [0]: https://github.com/cyqresig/ReactNativeComponentDemos 224 | [1]: http://cyqresig.github.io/img/react-native-smart-loading-spinner-overlay-preview-ios-v1.0.0.gif 225 | [2]: http://cyqresig.github.io/img/react-native-smart-loading-spinner-overlay-preview-android-v1.0.0.gif 226 | [3]: https://facebook.github.io/react-native/docs/style.html 227 | [4]: http://facebook.github.io/react-native/docs/text.html#style --------------------------------------------------------------------------------