-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use refs in useAnimation and write documentation
- Loading branch information
1 parent
28b380d
commit 5dfbca7
Showing
23 changed files
with
837 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,6 @@ node_modules/ | |
|
||
dist/ | ||
.docs/ | ||
|
||
# npmrc contains auth token for private gsap npm registry | ||
.npmrc |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
21 changes: 20 additions & 1 deletion
21
packages/react-animation/src/SplitTextWrapper/SplitTextWrapper.mdx
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 |
---|---|---|
@@ -1,5 +1,24 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
import { Meta, Canvas, Story, Source } from '@storybook/blocks'; | ||
import { Example } from './SplitTextWrapper.stories'; | ||
|
||
<Meta title="components/SplitTextWrapper" /> | ||
|
||
# SplitTextWrapper | ||
|
||
The SplitTextWrapper creates a SplitText instance that can be retrieved using a ref. The SplitText | ||
is available as soon as the just before the components is finished mounting. A new SplitText | ||
instance is created when the children or variables change. | ||
|
||
## Example | ||
|
||
<Canvas> | ||
<Story name="Example" /> | ||
</Canvas> | ||
|
||
## Usage | ||
|
||
The SplitTextWrapper accepts 1 child, this child is rendered to HTML inside the component to make | ||
sure that compnents from the vDOM are not changed on render making them untargetable in the created | ||
SplitText instance. | ||
|
||
<Source name="Example" dark language="tsx" /> |
50 changes: 50 additions & 0 deletions
50
packages/react-animation/src/SplitTextWrapper/SplitTextWrapper.stories.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,50 @@ | ||
/* eslint-disable react/jsx-no-literals */ | ||
import gsap from 'gsap'; | ||
import { useCallback, useRef, type ReactElement } from 'react'; | ||
import { useAnimation } from '../useAnimation/useAnimation.js'; | ||
import { SplitTextWrapper } from './SplitTextWrapper.js'; | ||
|
||
export default { | ||
title: 'components/SplitTextWrapper', | ||
}; | ||
|
||
export function Example(): ReactElement { | ||
const splitTextRef = useRef<SplitText>(null); | ||
|
||
const animation = useAnimation(() => { | ||
if (!splitTextRef.current) { | ||
return; | ||
} | ||
|
||
return gsap.from(splitTextRef.current.words, { | ||
y: 20, | ||
x: 4, | ||
opacity: 0, | ||
duration: 0.2, | ||
stagger: 0.05, | ||
}); | ||
}, []); | ||
|
||
const onReplay = useCallback(() => { | ||
animation.current?.play(0); | ||
}, [animation]); | ||
|
||
return ( | ||
<> | ||
<h1> | ||
<SplitTextWrapper ref={splitTextRef}> | ||
<> | ||
Lorem ipsum dolor sit <i>amet consectetur</i> | ||
<br /> adipisicing elit. <b>Tenetur perspiciatis</b> eius ea, ratione, | ||
<br /> illo molestias, <code>quia sapiente</code> modi quo | ||
<br /> molestiae temporibus. | ||
</> | ||
</SplitTextWrapper> | ||
</h1> | ||
|
||
<button onClick={onReplay} type="button"> | ||
Replay | ||
</button> | ||
</> | ||
); | ||
} |
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,38 @@ | ||
class AnimationsMap extends Map { | ||
private readonly callbacks = new Set<() => void>(); | ||
|
||
private updateQueued = false; | ||
|
||
public set(key: unknown, value: gsap.core.Animation): this { | ||
const result = super.set(key, value); | ||
|
||
if (this.updateQueued) { | ||
return result; | ||
} | ||
|
||
this.updateQueued = true; | ||
|
||
queueMicrotask(() => { | ||
for (const callback of this.callbacks) { | ||
callback(); | ||
} | ||
|
||
this.updateQueued = false; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
public listen(callback: () => void): () => void { | ||
this.callbacks.add(callback); | ||
|
||
return () => { | ||
this.callbacks.delete(callback); | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Global map of animations that can be accessed by reference | ||
*/ | ||
export const animations = new AnimationsMap(); |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './SplitTextWrapper/SplitTextWrapper.js'; | ||
export * from './useAnimation/useAnimation.js'; | ||
export * from './useExposeAnimation/useExposeAnimation.js'; | ||
export * from './useScrollAnimation/useScrollAnimation.js'; |
Oops, something went wrong.