Skip to content

Commit edcff55

Browse files
author
Grzegorz Sajnog
committed
feat: add options goToAndPlay and goToAndStop
1 parent 56f7284 commit edcff55

File tree

5 files changed

+1302
-111
lines changed

5 files changed

+1302
-111
lines changed

src/index.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default class Lottie extends React.Component {
1515
animationData,
1616
rendererSettings,
1717
segments,
18+
goToAndPlay,
19+
goToAndStop,
1820
} = options;
1921

2022
this.options = {
@@ -23,6 +25,8 @@ export default class Lottie extends React.Component {
2325
loop: loop !== false,
2426
autoplay: autoplay !== false,
2527
segments: segments !== false,
28+
goToAndPlay,
29+
goToAndStop,
2630
animationData,
2731
rendererSettings,
2832
};
@@ -38,13 +42,13 @@ export default class Lottie extends React.Component {
3842
if (this.options.animationData !== nextProps.options.animationData) {
3943
this.deRegisterEvents(this.props.eventListeners);
4044
this.destroy();
41-
this.options = {...this.options, ...nextProps.options};
45+
this.options = { ...this.options, ...nextProps.options };
4246
this.anim = lottie.loadAnimation(this.options);
4347
this.registerEvents(nextProps.eventListeners);
4448
}
4549
}
4650

47-
componentDidUpdate() {
51+
componentDidUpdate(prevProps) {
4852
if (this.props.isStopped) {
4953
this.stop();
5054
} else if (this.props.segments) {
@@ -53,6 +57,14 @@ export default class Lottie extends React.Component {
5357
this.play();
5458
}
5559

60+
if (JSON.stringify(this.props.goToAndPlay) !== JSON.stringify(prevProps.goToAndPlay)) {
61+
this.goToAndPlay();
62+
}
63+
64+
if (JSON.stringify(this.props.goToAndStop) !== JSON.stringify(prevProps.goToAndStop)) {
65+
this.goToAndStop();
66+
}
67+
5668
this.pause();
5769
this.setSpeed();
5870
this.setDirection();
@@ -93,6 +105,16 @@ export default class Lottie extends React.Component {
93105
}
94106
}
95107

108+
goToAndPlay() {
109+
const { value, isFrame } = this.props.goToAndPlay;
110+
this.anim.goToAndPlay(value, isFrame);
111+
}
112+
113+
goToAndStop() {
114+
const { value, isFrame } = this.props.goToAndStop;
115+
this.anim.goToAndStop(value, isFrame);
116+
}
117+
96118
destroy() {
97119
this.anim.destroy();
98120
}
@@ -177,6 +199,14 @@ Lottie.propTypes = {
177199
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
178200
isStopped: PropTypes.bool,
179201
isPaused: PropTypes.bool,
202+
goToAndPlay: PropTypes.shape({
203+
value: PropTypes.number,
204+
isFrame: PropTypes.bool,
205+
}),
206+
goToAndStop: PropTypes.shape({
207+
value: PropTypes.number,
208+
isFrame: PropTypes.bool,
209+
}),
180210
speed: PropTypes.number,
181211
segments: PropTypes.arrayOf(PropTypes.number),
182212
direction: PropTypes.number,
@@ -191,6 +221,14 @@ Lottie.defaultProps = {
191221
eventListeners: [],
192222
isStopped: false,
193223
isPaused: false,
224+
goToAndPlay: {
225+
value: null,
226+
isFrame: false,
227+
},
228+
goToAndStop: {
229+
value: null,
230+
isFrame: false,
231+
},
194232
speed: 1,
195233
ariaRole: 'button',
196234
ariaLabel: 'animation',

src/stories/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import LottieControlSegments from './lottie-control-segments';
55
import ToggleLike from './toggle-like';
66
import TransitionLoop from './TransitionLoop';
77
import TransitionWithOptions from './TransitionWithOptions';
8+
import LottieGoToAndStop from './lottie-goToAndStop';
9+
import LottieGoToAndPlay from './lottie-goToAndPlay';
810

911
storiesOf('Lottie Animation View', module)
1012
.add('with control', () => <LottieControl />)
1113
.add('toggle like', () => <ToggleLike />)
1214
.add('transitions & loops', () => <TransitionLoop />)
1315
.add('transitions with options', () => <TransitionWithOptions />)
14-
.add('with segments', () => <LottieControlSegments />);
16+
.add('with segments', () => <LottieControlSegments />)
17+
.add('with goToAndStop', () => <LottieGoToAndStop />)
18+
.add('with goToAndPlay', () => <LottieGoToAndPlay />);

src/stories/lottie-goToAndPlay.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import Lottie from '../index';
3+
import * as animationData from './TwitterHeart.json';
4+
5+
export default class LottieGoToAndPlay extends React.Component {
6+
7+
constructor(props) {
8+
super(props);
9+
10+
this.state = {
11+
isStopped: true,
12+
value: 0,
13+
isFrame: false,
14+
loop: false,
15+
};
16+
}
17+
18+
render() {
19+
const centerStyle = {
20+
display: 'block',
21+
margin: '10px auto',
22+
textAlign: 'center',
23+
};
24+
const { isStopped, value, isFrame, loop } = this.state;
25+
const defaultOptions = { animationData, loop };
26+
27+
return (<div>
28+
<Lottie
29+
options={defaultOptions}
30+
height={400}
31+
width={400}
32+
isStopped={isStopped}
33+
goToAndPlay={{
34+
value,
35+
isFrame,
36+
}}
37+
/>
38+
39+
<p style={centerStyle}>Go to and play: [value: {value}, isFrame: {isFrame ? 1 : 0}]</p>
40+
<div style={centerStyle}>
41+
<input
42+
type="text" value={value}
43+
onChange={e => this.setState({ value: parseInt(e.currentTarget.value, 10) || 0 })}
44+
/>
45+
46+
<label htmlFor="isFrame">
47+
<input
48+
id="isFrame"
49+
name="isFrame"
50+
type="checkbox"
51+
checked={isFrame}
52+
onChange={e => this.setState({ isFrame: e.currentTarget.checked })}
53+
54+
/>
55+
Is frame
56+
</label>
57+
</div>
58+
</div>);
59+
}
60+
}

src/stories/lottie-goToAndStop.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import Lottie from '../index';
3+
import * as animationData from './TwitterHeart.json';
4+
5+
export default class LottieGoToAndStop extends React.Component {
6+
7+
constructor(props) {
8+
super(props);
9+
10+
this.state = {
11+
isStopped: false,
12+
value: 0,
13+
isFrame: false,
14+
loop: true,
15+
};
16+
}
17+
18+
render() {
19+
const centerStyle = {
20+
display: 'block',
21+
margin: '10px auto',
22+
textAlign: 'center',
23+
};
24+
const { isStopped, value, isFrame, loop } = this.state;
25+
const defaultOptions = { animationData, loop };
26+
27+
return (<div>
28+
<Lottie
29+
options={defaultOptions}
30+
height={400}
31+
width={400}
32+
isStopped={isStopped}
33+
goToAndStop={{
34+
value,
35+
isFrame,
36+
}}
37+
/>
38+
39+
<p style={centerStyle}>Go to and stop: [value: {value}, isFrame: {isFrame ? 1 : 0}]</p>
40+
<div style={centerStyle}>
41+
<input
42+
type="text" value={value}
43+
onChange={e => this.setState({ value: parseInt(e.currentTarget.value, 10) || 0 })}
44+
/>
45+
46+
<label htmlFor="isFrame">
47+
<input
48+
id="isFrame"
49+
name="isFrame"
50+
type="checkbox"
51+
checked={isFrame}
52+
onChange={e => this.setState({ isFrame: e.currentTarget.checked })}
53+
54+
/>
55+
Is frame
56+
</label>
57+
</div>
58+
</div>);
59+
}
60+
}

0 commit comments

Comments
 (0)