Skip to content

Commit 940bd3c

Browse files
committed
finalize component
1 parent e8bb4b6 commit 940bd3c

File tree

7 files changed

+327
-1
lines changed

7 files changed

+327
-1
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
npm-debug.log
3+
.DS_Store

Diff for: README.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
# react-native-progress-hud
2-
A clean and lightweight progress HUD for your React Native app.
2+
__`react-native-progress-hud`__ is a [React Native](https://facebook.github.io/react-native/) port of the popular [`SVProgressHUD`](https://github.com/TransitApp/SVProgressHUD). It is a clean and easy-to-use HUD meant to display the progress of an ongoing task.
3+
4+
## Install
5+
```shell
6+
npm i --save react-native-progress-hud
7+
```
8+
9+
## Usage
10+
Using the HUD in your app will usually look like this:
11+
```js
12+
var ProgressHUD = require('react-native-progress-hud');
13+
14+
var YourComponent = React.createClass({
15+
mixins: [ProgressHUD.Mixin],
16+
17+
...
18+
19+
render() {
20+
return (
21+
<View>
22+
...
23+
<ProgressHUD
24+
is_visible={this.state.is_hud_visible}
25+
/>
26+
</View>
27+
);
28+
}
29+
```
30+
31+
### Showing the HUD
32+
You can display the HUD by calling:
33+
```js
34+
this.showProgressHUD();
35+
```
36+
### Dismissing the HUD
37+
It can be dismissed by calling:
38+
```js
39+
this.dismissProgressHUD();
40+
```
41+
42+
### Child Components
43+
From time to time, you may need to show the HUD from the a child component. Using the HUD from a child component will look like this:
44+
45+
```js
46+
var YourChildComponent = React.createClass({
47+
render() {
48+
contextTypes: {
49+
showProgressHUD: React.PropTypes.func,
50+
dismissProgressHUD: React.PropTypes.func
51+
},
52+
53+
return (
54+
<View>
55+
...
56+
<TouchableHighlight
57+
onPress={this.context.showProgressHUD}
58+
>
59+
Show Progress HUD
60+
</TouchableHighlight>
61+
</View>
62+
);
63+
}
64+
```
65+
## Props
66+
The following props can be used to modify the HUD's style and/or behaviour:
67+
68+
| Prop | Type | Opt/Required | Default | Note |
69+
|---|---|---|---|---|
70+
|__`isVisible`__|_Boolean_|Required||
71+
|__`isDismissible`__|_Boolean_|Optional|false|When set to true, the HUD is dismissed on user interaction.
72+
|__`backgroundType`__|_String_|Optional|`dark`|`light`, `dark`, `none`. <br/> Default is `dark`.
73+
74+
## License
75+
Copyright (c) 2015, Naoufal Kadhom
76+
77+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
78+
79+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Diff for: index.ios.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./src/index');

Diff for: package.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "react-native-progress-hud",
3+
"version": "0.1.0",
4+
"description": "A clean and lightweight progress HUD for your React Native app.",
5+
"main": "index.ios.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/naoufal/react-native-progress-hud.git"
12+
},
13+
"license": "ISC",
14+
"keywords": [
15+
"react",
16+
"native",
17+
"progress",
18+
"hud",
19+
"popup",
20+
"loader",
21+
"loading"
22+
],
23+
"author": "Naoufal Kadhom <[email protected]> (https://github.com/naoufal)",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/naoufal/react-native-progress-hud/issues"
27+
},
28+
"homepage": "https://github.com/naoufal/react-native-progress-hud",
29+
"dependencies": {
30+
"react-tween-state": "0.0.5"
31+
},
32+
"peerDependencies": {
33+
"react-native": "^0.4.0"
34+
},
35+
"jshintConfig": {
36+
"esnext": true,
37+
"node": true
38+
}
39+
}

Diff for: src/images/index.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/index.js

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
'use strict';
2+
3+
var React = require('react-native');
4+
var tweenState = require('react-tween-state');
5+
6+
var {
7+
Image,
8+
StyleSheet,
9+
TouchableHighlight,
10+
View
11+
} = React;
12+
13+
var styles = require('./styles');
14+
var images = require('./images');
15+
16+
var SPIN_DURATION = 1000;
17+
18+
var ProgressHUDMixin = {
19+
getInitialState: function() {
20+
return {
21+
is_hud_visible: false
22+
};
23+
},
24+
25+
showProgressHUD: function() {
26+
this.setState({
27+
is_hud_visible: true
28+
});
29+
},
30+
31+
dismissProgressHUD: function() {
32+
this.setState({
33+
is_hud_visible: false
34+
});
35+
},
36+
37+
childContextTypes: {
38+
showProgressHUD: React.PropTypes.func,
39+
dismissProgressHUD: React.PropTypes.func
40+
},
41+
42+
getChildContext: function() {
43+
return {
44+
showProgressHUD: this.showProgressHUD,
45+
dismissProgressHUD: this.dismissProgressHUD
46+
};
47+
},
48+
};
49+
50+
var ProgressHUD = React.createClass({
51+
mixins: [tweenState.Mixin],
52+
53+
contextTypes: {
54+
showProgressHUD: React.PropTypes.func.isRequired,
55+
dismissProgressHUD: React.PropTypes.func
56+
},
57+
58+
statics: {
59+
Mixin: ProgressHUDMixin
60+
},
61+
62+
getInitialState: function() {
63+
return {
64+
rotate_deg: 0
65+
};
66+
},
67+
68+
componentDidMount: function() {
69+
// Kick off rotation animation
70+
this.rotateSpinner();
71+
72+
// Set rotation interval
73+
this.interval - setInterval(() => {
74+
this.rotateSpinner();
75+
}, SPIN_DURATION);
76+
},
77+
78+
componentWillUnmount: function() {
79+
clearInterval(this.interval);
80+
},
81+
82+
rotateSpinner: function() {
83+
this.tweenState('rotate_deg', {
84+
easing: tweenState.easingTypes.linear,
85+
duration: SPIN_DURATION,
86+
endValue: this.state.rotate_deg === 0 ? 360 : this.state.rotate_deg + 360
87+
});
88+
},
89+
90+
tick: function() {
91+
this.rotateSpinner();
92+
},
93+
94+
clickHandler: function() {
95+
if (this.props.isDismissible) {
96+
this.context.dismissProgressHUD();
97+
}
98+
},
99+
100+
render: function() {
101+
// Return early if not visible
102+
if (!this.props.isVisible) {
103+
return <View />;
104+
}
105+
106+
// Background color overrides
107+
var bg = 'rgba(0, 0, 0, 0)';
108+
if (this.props.backgroundType === 'light') {
109+
bg = 'rgba(255, 255, 255, 0.25)';
110+
} else if (this.props.backgroundType === 'dark') {
111+
bg = 'rgba(0, 0, 0, 0.11)';
112+
}
113+
114+
// Set rotation property value
115+
var deg = Math.floor(
116+
this.getTweeningValue('rotate_deg')
117+
).toString() + 'deg';
118+
119+
return (
120+
/*jshint ignore:start */
121+
<TouchableHighlight
122+
key="ProgressHUD"
123+
style={[styles.overlay, {
124+
backgroundColor: bg
125+
}]}
126+
onPress={this.clickHandler}
127+
underlayColor={bg}
128+
activeOpacity={1}
129+
>
130+
<View
131+
style={[styles.container, {
132+
left: this.getTweeningValue('left')
133+
}]}
134+
>
135+
<Image
136+
style={[styles.spinner, {
137+
transform: [
138+
{rotate: deg}
139+
]
140+
}]}
141+
source={{
142+
uri: 'data:image/jpeg;base64,' + images['1x'],
143+
isStatic: true
144+
}}
145+
>
146+
<View style={styles.inner_spinner}>
147+
</View>
148+
</Image>
149+
</View>
150+
</TouchableHighlight>
151+
/*jshint ignore:end */
152+
);
153+
}
154+
});
155+
156+
157+
module.exports = ProgressHUD;

Diff for: src/styles.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
var React = require('react-native');
4+
5+
var { StyleSheet } = React;
6+
7+
var styles = StyleSheet.create({
8+
overlay: {
9+
alignItems: 'center',
10+
justifyContent: 'center',
11+
flex: 1,
12+
position: 'absolute',
13+
top: 0,
14+
left: 0,
15+
right: 0,
16+
bottom: 0,
17+
},
18+
container: {
19+
justifyContent: 'center',
20+
alignItems: 'center',
21+
width: 100,
22+
height: 100,
23+
borderRadius: 16,
24+
backgroundColor: '#FFFFFF'
25+
},
26+
spinner: {
27+
alignItems: 'center',
28+
justifyContent: 'center',
29+
width: 50,
30+
height: 50,
31+
borderRadius: 50 / 2,
32+
33+
},
34+
inner_spinner: {
35+
width: 42,
36+
height: 42,
37+
borderRadius: 42 / 2,
38+
backgroundColor: '#FFFFFF'
39+
}
40+
});
41+
42+
module.exports = styles;

0 commit comments

Comments
 (0)