Skip to content

Commit

Permalink
fix useAnimateKeyFrames run when state update (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 authored Dec 11, 2020
1 parent d63028d commit c8d2d1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"eslint": "^7.0.0",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react-hooks": "^4.0.2",
"husky": "^4.2.5",
"jest": "^26.0.1",
"lint-staged": "^10.2.6",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"prettier": "^2.2.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-test-renderer": "^16.13.1",
"rollup": "^2.10.7",
"rollup-plugin-typescript2": "^0.27.1",
"ts-jest": "^26.0.0",
"typescript": "^3.9.3",
"uglify-es": "^3.3.9",
"eslint-plugin-react": "^7.20.0"
"uglify-es": "^3.3.9"
},
"peerDependencies": {
"react": "^16.8.0",
Expand Down
14 changes: 9 additions & 5 deletions src/useAnimateKeyframes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ let UseAnimate;
describe('useAnimateKeyframes', () => {
let componentStyle;

const TestHook = ({ callback }) => {
const TestHook = ({ callback }: any) => {
const { style } = callback();
componentStyle = style;
return <div style={style}>errors</div>;
};

const TestComponent = callback => {
const TestComponent = (callback) => {
mount(<TestHook callback={callback} />);
};

beforeEach(() => {
TestComponent(() => {
UseAnimate = useAnimateKeyframes({ keyframes: ['opacity: 0', 'opacity: 1'] });
UseAnimate = useAnimateKeyframes({
keyframes: ['opacity: 0', 'opacity: 1'],
});
return UseAnimate;
});

Expand All @@ -34,11 +36,13 @@ describe('useAnimateKeyframes', () => {
it('should toggle style correctly', () => {
act(() => {
expect(UseAnimate.play(true)).toBeUndefined();
expect(componentStyle).toEqual({ animation: '0.3s linear 0s 1 normal none running ' });
expect(componentStyle).toEqual({
animation: '0.3s linear 0s 1 normal none running ',
});
});

expect(componentStyle).toEqual({
animation: '0.3s linear 0s 1 normal none running ',
animation: '0.3s linear 0s 1 normal none running test',
});
});
});
10 changes: 6 additions & 4 deletions src/useAnimateKeyframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function useAnimateKeyframes(
reverse: { sheet: {} },
});
const { register } = React.useContext(AnimateContext);
const [isPlaying, setIsPlaying] = React.useState(true);
const [isPlaying, setIsPlaying] = React.useState<boolean | null>(null);
const [isPaused, setIsPaused] = React.useState(false);
const playRef = React.useRef<(isPlay: boolean) => void>();

Expand Down Expand Up @@ -76,16 +76,18 @@ export default function useAnimateKeyframes(
animation: `${duration}s ${easeType} ${delay}s ${iterationCount} ${direction} ${fillMode} ${getPlayState(
isPaused,
)} ${
(isPlaying
isPlaying === null
? ''
: isPlaying
? animationNameRef.current.forward
: animationNameRef.current.reverse) || ''
: animationNameRef.current.reverse
}`,
};

return {
style,
play: playRef.current,
pause: setIsPaused,
isPlaying,
isPlaying: !!isPlaying,
};
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4325,10 +4325,10 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==
prettier@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==

pretty-format@^25.2.1, pretty-format@^25.5.0:
version "25.5.0"
Expand Down

0 comments on commit c8d2d1b

Please sign in to comment.