-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-motions): add
imperativeRef()
prop (#29897)
- Loading branch information
1 parent
4c1cd4b
commit 5f05726
Showing
17 changed files
with
159 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/react-components/react-motions-preview/src/hooks/useMotionImperativeRef.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import * as React from 'react'; | ||
|
||
import type { MotionImperativeRef } from '../types'; | ||
import { useMotionImperativeRef } from './useMotionImperativeRef'; | ||
|
||
describe('useMotionImperativeRef', () => { | ||
it('exposes methods to control motions on a passed ref', () => { | ||
const imperativeRef = React.createRef<MotionImperativeRef>(); | ||
const { result } = renderHook(() => useMotionImperativeRef(imperativeRef)); | ||
|
||
expect(result.current).toMatchObject({ current: undefined }); | ||
expect(imperativeRef.current).toMatchObject({ | ||
setPlayState: expect.any(Function), | ||
setPlaybackRate: expect.any(Function), | ||
}); | ||
}); | ||
|
||
it('exposed methods control a web animation', () => { | ||
const animationMock = { | ||
play: jest.fn(), | ||
pause: jest.fn(), | ||
} as Partial<Animation> as Animation; | ||
|
||
const setPlaybackRate = jest.fn(); | ||
Object.defineProperty(animationMock, 'playbackRate', { | ||
set: setPlaybackRate, | ||
}); | ||
|
||
const imperativeRef = React.createRef<MotionImperativeRef>(); | ||
const { result } = renderHook(() => useMotionImperativeRef(imperativeRef)); | ||
|
||
result.current.current = animationMock; | ||
|
||
imperativeRef.current?.setPlayState('running'); | ||
expect(animationMock.play).toHaveBeenCalled(); | ||
|
||
imperativeRef.current?.setPlayState('paused'); | ||
expect(animationMock.pause).toHaveBeenCalled(); | ||
|
||
imperativeRef.current?.setPlaybackRate(0.5); | ||
expect(setPlaybackRate).toHaveBeenCalledWith(0.5); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/react-components/react-motions-preview/src/hooks/useMotionImperativeRef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as React from 'react'; | ||
import type { MotionImperativeRef } from '../types'; | ||
|
||
export function useMotionImperativeRef(imperativeRef: React.Ref<MotionImperativeRef | undefined> | undefined) { | ||
const animationRef = React.useRef<Animation | undefined>(); | ||
|
||
React.useImperativeHandle(imperativeRef, () => ({ | ||
setPlayState: state => { | ||
if (state === 'running') { | ||
animationRef.current?.play(); | ||
} | ||
|
||
if (state === 'paused') { | ||
animationRef.current?.pause(); | ||
} | ||
}, | ||
setPlaybackRate: rate => { | ||
if (animationRef.current) { | ||
animationRef.current.playbackRate = rate; | ||
} | ||
}, | ||
})); | ||
|
||
return animationRef; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
.../react-components/react-motions-preview/stories/Motion/AtomPlayState.stories.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...mponents/react-motions-preview/stories/Motion/ImperativeRefPlayState.stories.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
By default, the child component will be animated when it first mounts. The state of a motion can be controlled using `setPlayState()` via `imperativeRef` prop. | ||
|
||
`imperativeRef` works with both `createAtom` and `createTransition` factories. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.