Skip to content

Commit

Permalink
Merge pull request chenqingspring#27 from elliotcobb/master
Browse files Browse the repository at this point in the history
Keep options passed with a new animation
  • Loading branch information
chenqingspring authored Apr 4, 2018
2 parents e6be679 + 7a34be5 commit 7264dab
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Lottie extends React.Component {
if (this.options.animationData !== nextProps.options.animationData) {
this.deRegisterEvents(this.props.eventListeners);
this.destroy();
this.options.animationData = nextProps.options.animationData;
this.options = {...this.options, ...nextProps.options};
this.anim = lottie.loadAnimation(this.options);
this.registerEvents(nextProps.eventListeners);
}
Expand Down
55 changes: 55 additions & 0 deletions src/stories/TransitionWithOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import Lottie from '../index';
import * as animationDataA from './TwitterHeart.json';
import * as animationDataB from './beating-heart.json';

/**
* TransitionWithOptions: demonstrates how you can switch to a
* new animation with different options. For example, you can start
* with a looped animation and change to one that plays only once
* without mounting the component again
*/
export default class TransitionWithOptions extends React.Component {
constructor(props) {
super(props);

this.state = {
showLoopedAnimation: true,
};
}

clickHandler = () => {
this.setState({ showLoopedAnimation: !this.state.showLoopedAnimation });
};

render() {
const centerStyle = {
display: 'block',
margin: '20px auto',
textAlign: 'center',
};
const { showLoopedAnimation } = this.state;
const animationOptionsWithLoop = {
animationData: animationDataA,
loop: true,
};
const animationOptionsWithoutLoop = {
animationData: animationDataB,
loop: false,
};

return (
<div>
<Lottie
options={showLoopedAnimation ? animationOptionsWithLoop : animationOptionsWithoutLoop}
height={400}
width={400}
/>
<p style={centerStyle}>This animation is {showLoopedAnimation ? 'looped' : 'not looped'}</p>
<button style={centerStyle} onClick={this.clickHandler}>
switch
</button>
</div>
);
}
}
2 changes: 2 additions & 0 deletions src/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import LottieControl from './lottie-control';
import LottieControlSegments from './lottie-control-segments';
import ToggleLike from './toggle-like';
import TransitionLoop from './TransitionLoop';
import TransitionWithOptions from './TransitionWithOptions';

storiesOf('Lottie Animation View', module)
.add('with control', () => <LottieControl />)
.add('toggle like', () => <ToggleLike />)
.add('transitions & loops', () => <TransitionLoop />)
.add('transitions with options', () => <TransitionWithOptions />)
.add('with segments', () => <LottieControlSegments />);

0 comments on commit 7264dab

Please sign in to comment.