Skip to content

Commit 39d0de4

Browse files
authored
refactor: introduce the withFadeAnimation() HOC (#784)
* refactor: introduce the withFadeAnimation() HOC * revert: cancel file rename for now
1 parent 87f1147 commit 39d0de4

File tree

2 files changed

+61
-59
lines changed

2 files changed

+61
-59
lines changed
Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { Animated } from 'react-native';
33
import styled from 'styled-components';
44
import { colors } from 'config';
5-
import { infiniteAnimation } from 'utils';
5+
import { FadeAnimationProps, withFadeAnimation } from 'utils';
66

77
const Container = styled.View`
88
padding: 10px;
@@ -39,40 +39,16 @@ const TextBar = styled(Animated.View)`
3939
background-color: ${colors.greyDark};
4040
`;
4141

42-
export class LoadingEventListItem extends Component {
43-
constructor() {
44-
super();
45-
this.fadeFrom = 0.3;
46-
this.fadeTo = 0.6;
47-
this.state = {
48-
fadeAnimValue: new Animated.Value(this.fadeTo),
49-
};
50-
}
51-
52-
componentDidMount() {
53-
this.runAnimation();
54-
}
55-
56-
runAnimation() {
57-
infiniteAnimation(
58-
this.state.fadeAnimValue,
59-
this.fadeFrom,
60-
this.fadeTo,
61-
() => {
62-
this.runAnimation();
63-
}
64-
);
65-
}
66-
67-
render() {
68-
return (
69-
<Container>
70-
<Wrapper>
71-
<Avatar style={{ opacity: this.state.fadeAnimValue }} />
72-
<TextBar style={{ opacity: this.state.fadeAnimValue }} />
73-
<Icon style={{ opacity: this.state.fadeAnimValue }} />
74-
</Wrapper>
75-
</Container>
76-
);
77-
}
78-
}
42+
const LoadingEventListItemComponent = ({ opacity }: FadeAnimationProps) => (
43+
<Container>
44+
<Wrapper>
45+
<Avatar style={{ opacity }} />
46+
<TextBar style={{ opacity }} />
47+
<Icon style={{ opacity }} />
48+
</Wrapper>
49+
</Container>
50+
);
51+
52+
export const LoadingEventListItem = withFadeAnimation(
53+
LoadingEventListItemComponent
54+
);

src/utils/loading-animation.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Animated } from 'react-native';
2+
import React from 'react';
23

34
export const loadingAnimation = state => {
45
const duration = 500;
@@ -20,24 +21,49 @@ export const loadingAnimation = state => {
2021
return Animated.sequence(animatedTimings);
2122
};
2223

23-
export const infiniteAnimation = (state, fromValue, toValue, onEnd) => {
24-
const animatedTimings = [];
25-
const duration = 1000;
26-
27-
animatedTimings.push(
28-
Animated.timing(state, {
29-
toValue: fromValue,
30-
duration,
31-
})
32-
);
33-
animatedTimings.push(
34-
Animated.timing(state, {
35-
toValue,
36-
duration,
37-
})
38-
);
39-
40-
return Animated.sequence(animatedTimings).start(() => {
41-
onEnd();
42-
});
24+
export const withFadeAnimation = WrappedComponent => {
25+
return class extends React.Component {
26+
constructor() {
27+
super();
28+
this.fadeFrom = 0.3;
29+
this.fadeTo = 0.6;
30+
this.state = {
31+
fadeAnimValue: new Animated.Value(this.fadeTo),
32+
};
33+
}
34+
35+
componentDidMount() {
36+
this.runAnimation();
37+
}
38+
39+
runAnimation() {
40+
const animatedTimings = [];
41+
const duration = 1000;
42+
43+
animatedTimings.push(
44+
Animated.timing(this.state.fadeAnimValue, {
45+
toValue: this.fadeFrom,
46+
duration,
47+
})
48+
);
49+
animatedTimings.push(
50+
Animated.timing(this.state.fadeAnimValue, {
51+
toValue: this.fadeTo,
52+
duration,
53+
})
54+
);
55+
56+
return Animated.sequence(animatedTimings).start(() => {
57+
this.runAnimation();
58+
});
59+
}
60+
61+
render() {
62+
return <WrappedComponent opacity={this.state.fadeAnimValue} />;
63+
}
64+
};
65+
};
66+
67+
export type FadeAnimationProps = {
68+
opacity: number,
4369
};

0 commit comments

Comments
 (0)