Skip to content

Commit 82bcd6e

Browse files
committed
Merge branch 'feature/animate-hoc' into develop
Merge branch 'feature/animate-hoc' into develop
2 parents f35b23b + 7162cba commit 82bcd6e

File tree

8 files changed

+174
-4
lines changed

8 files changed

+174
-4
lines changed

.storybook/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { configure } from '@kadira/storybook';
22

33
function loadStories() {
4+
require('../stories/Animate');
45
require('../stories/Attention');
56
require('../stories/Bounce');
67
require('../stories/Especials');

README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,51 @@ Example:
6363
- prop for represent display css property
6464
- default `block`
6565

66-
## Examples
66+
## Animate - HOC
67+
68+
For convenience you can use Animate HOC to use animations stacked, you could pass a unique component to `Animation` prop or an array of animations, example:
69+
70+
```
71+
import Animate, {
72+
Flash,
73+
Bounce
74+
} from 'animate-css-styled-components';
75+
76+
<Animate
77+
Animation={[Flash, Bounce]}
78+
duration="0.8s"
79+
delay="0.2s">
80+
<Card>
81+
card content...
82+
</Card>
83+
</Animate>
84+
```
85+
In this example that you could see [here](https://dielduarte.github.io/animate-css-styled-components/?selectedKind=Animate%20%28HOC%29&selectedStory=Multiple%20Animations&full=0&down=1&left=1&panelRight=0&downPanel=kadirahq%2Fstorybook-addon-actions%2Factions-panel), the Bounce Animation will run after when Flash animation is finished, respecting the duration time + delay time, duration and delay are same for all animations, but you could pass diferents values to each animation prop, look:
86+
87+
```
88+
import Animate, {
89+
Flash,
90+
Bounce,
91+
FadeOut,
92+
FadeIn
93+
} from 'animate-css-styled-components';
94+
95+
<Animate
96+
Animation={[Flash, Bounce, FadeOut, FadeIn]}
97+
duration={["0.8s", "3s", "2s", "0.4s"]}
98+
delay={["0.2s", "0.1s", "0.5s", "1s"]}>
99+
<Card>
100+
card content...
101+
</Card>
102+
</Animate>
103+
104+
```
105+
106+
See this example [here](https://dielduarte.github.io/animate-css-styled-components/?selectedKind=Animate%20%28HOC%29&selectedStory=Multiple%20Animations%20with%20diferent%20times%20and%20delays&full=0&down=1&left=1&panelRight=0&downPanel=kadirahq%2Fstorybook-addon-actions%2Factions-panel)
107+
108+
Don't forget, you coul pass any [animations props](https://github.com/dielduarte/animate-css-styled-components#props) as single string if the value are same for all animations stacked or an array of values.
109+
110+
## Examples - Storybook
67111

68112
See all examples [here](https://dielduarte.github.io/animate-css-styled-components/)
69113

@@ -100,6 +144,3 @@ Example:
100144

101145
now your animation is a styled-component and you can use this like any other styled-component,
102146
passing the all BaseAnimation [props](https://github.com/dielduarte/animate-css-styled-components#props).
103-
104-
# WIP
105-
- `animate` component, wrapper for all animations with a only component.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "./lib/index.js",
66
"scripts": {
77
"build": "babel src -d ./lib",
8+
"build-watch": "babel src -d ./lib -w",
89
"storybook": "start-storybook -p 6006",
910
"build-storybook": "build-storybook",
1011
"prepublish": "babel src -d ./lib",

src/Animate/StackAnimates.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { PureComponent } from 'react';
2+
import * as Utils from './Utils';
3+
4+
class StackAnimates extends PureComponent {
5+
constructor(props) {
6+
super(props);
7+
this.state = Utils.buildState(props);
8+
}
9+
10+
get getNextIndex() {
11+
return this.state.index +1;
12+
}
13+
14+
get getNextStartTime() {
15+
const time = Utils.convertToMs(parseFloat(Utils.removeUnit(this.state.duration)));
16+
const delay = Utils.convertToMs(parseFloat(Utils.removeUnit(this.state.delay)));
17+
return time + delay;
18+
}
19+
20+
get HaveMoreAnimations() {
21+
return this.getNextIndex < this.props.Animation.length;
22+
}
23+
24+
renderNextAnimate() {
25+
if (this.HaveMoreAnimations) {
26+
setTimeout(() => {
27+
this.setState(
28+
Utils.buildState(this.props, this.getNextIndex),
29+
() => this.renderNextAnimate()
30+
);
31+
}, this.getNextStartTime);
32+
}
33+
}
34+
35+
render() {
36+
const { Animation } = this.state;
37+
38+
return (
39+
<Animation {...this.state}>
40+
{this.props.children}
41+
</Animation>
42+
);
43+
}
44+
45+
componentDidMount() {
46+
this.renderNextAnimate();
47+
}
48+
}
49+
50+
export default StackAnimates;

src/Animate/Utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const isArray = (obj) => Array.isArray(obj);
2+
3+
export const buildState = (props, index = 0) => {
4+
let state = {};
5+
Object.entries(props).forEach((prop) => {
6+
const key = prop[0];
7+
const value = prop[1];
8+
9+
if(key !== 'children') {
10+
state[key] = isArray(value) ? value[index] : value;
11+
}
12+
});
13+
14+
state['index'] = index;
15+
return state;
16+
};
17+
18+
export const removeUnit = (time) => time.replace('s', '');
19+
20+
export const convertToMs = (time) => time * 1000;

src/Animate/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { PureComponent } from 'react';
2+
3+
import * as Utils from './Utils';
4+
import StackAnimates from './StackAnimates';
5+
6+
class Animate extends PureComponent {
7+
render() {
8+
const { Animation } = this.props;
9+
10+
if (Utils.isArray(Animation)) {
11+
return <StackAnimates {...this.props} />;
12+
}
13+
14+
return (
15+
<Animation {...this.props}>
16+
{this.props.children}
17+
</Animation>
18+
);
19+
}
20+
}
21+
22+
export default Animate;

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import ZoomOutLeft from './Zoom/OutLeft';
8989
import ZoomOutRight from './Zoom/OutRight';
9090
import ZoomOutUp from './Zoom/OutUp';
9191

92+
import Animate from './Animate';
93+
9294
export {
9395
Bounce,
9496
Flash,
@@ -168,3 +170,5 @@ export {
168170
ZoomOutUp,
169171
BaseAnimation
170172
};
173+
174+
export default Animate;

stories/Animate.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { storiesOf } from '@kadira/storybook';
3+
import FullView from '../Example/FullView';
4+
import Card from '../Example/Card';
5+
import Animate, {
6+
Flash,
7+
Bounce,
8+
FadeOut,
9+
FadeIn
10+
} from 'animate-css-styled-components';
11+
12+
13+
storiesOf('Animate (HOC)', module)
14+
.add('Multiple Animations', () => (
15+
<FullView center>
16+
<Animate Animation={[Flash, Bounce, FadeOut, FadeIn]} duration="0.8s" delay="0.2s">
17+
<Card rounded width="100px" height="100px">
18+
card content...
19+
</Card>
20+
</Animate>
21+
</FullView>
22+
))
23+
.add('Multiple Animations with diferent times and delays', () => (
24+
<FullView center>
25+
<Animate Animation={[Flash, Bounce, FadeOut, FadeIn]} duration={["0.8s", "3s", "2s", "0.4s"]} delay={["0.2s", "0.1s", "0.5s", "1s"]}>
26+
<Card rounded width="100px" height="100px">
27+
card content...
28+
</Card>
29+
</Animate>
30+
</FullView>
31+
));

0 commit comments

Comments
 (0)