-
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.
#148 Make SplitTextWrapper polymorphic and add option to provide any …
…children
- Loading branch information
1 parent
28e11e1
commit 3c295fb
Showing
8 changed files
with
392 additions
and
114 deletions.
There are no files selected for viewing
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
120 changes: 79 additions & 41 deletions
120
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,64 +1,102 @@ | ||
import { Meta } from '@storybook/blocks'; | ||
import { Canvas } from './../../.storybook/Canvas'; | ||
import { Example } from './SplitTextWrapper.stories'; | ||
import { Meta, Canvas, Controls } from '@storybook/blocks'; | ||
import * as stories from './SplitTextWrapper.stories'; | ||
|
||
<Meta title="components/SplitTextWrapper" /> | ||
<Meta title="components/SplitTextWrapper" of={stories} /> | ||
|
||
# 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. | ||
|
||
<Canvas> | ||
<Example /> | ||
</Canvas> | ||
<Canvas of={stories.Children} /> | ||
|
||
## Usage | ||
## Rendering children | ||
|
||
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. | ||
The `SplitTextWrapper` renders 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. | ||
|
||
> Warning: state inside the rendered children is lost when the children change. | ||
```tsx | ||
function Component(): ReactElement { | ||
const splitTextRef = useRef<SplitText>(null); | ||
|
||
useEffect(() => { | ||
// Do something with `splitTextRef.current` | ||
}); | ||
|
||
return ( | ||
<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> | ||
); | ||
} | ||
``` | ||
|
||
### Demo | ||
|
||
<Canvas of={stories.Children} /> | ||
|
||
## Using dangerouslySetInnerHTML | ||
|
||
The children are rendered to a string, this is not necessary when the `dangerouslySetInnerHTML` | ||
property of a component is used. | ||
|
||
```tsx | ||
function Example(): ReactElement { | ||
function Component(): ReactElement { | ||
const splitTextRef = useRef<SplitText>(null); | ||
|
||
const animation = useAnimation(() => { | ||
if (!splitTextRef.current) { | ||
return; | ||
} | ||
useEffect(() => { | ||
// Do something with `splitTextRef.current` | ||
}); | ||
|
||
return ( | ||
<SplitTextWrapper | ||
ref={splitTextRef} | ||
dangerouslySetInnerHTML={{ | ||
__html: | ||
'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.', | ||
}} | ||
/> | ||
); | ||
} | ||
``` | ||
|
||
### Demo | ||
|
||
return gsap.from(splitTextRef.current.words, { | ||
y: 20, | ||
x: 4, | ||
opacity: 0, | ||
duration: 0.2, | ||
stagger: 0.05, | ||
}); | ||
}, []); | ||
<Canvas of={stories.DangerouslySetInnerHtml} /> | ||
|
||
const onReplay = useCallback(() => { | ||
animation.current?.play(0); | ||
}, [animation]); | ||
## The `as` prop (polymorphic component) | ||
|
||
The `SplitTextWrapper` wrapper renders a `div` element by default. The `as` prop can be used to | ||
render the `SplitTextWrapper` as a different element. | ||
|
||
```tsx | ||
function Component(): ReactElement { | ||
const splitTextRef1 = useRef<SplitText>(null); | ||
const splitTextRef2 = useRef<SplitText>(null); | ||
|
||
useEffect(() => { | ||
// Do something with `splitTextRef1.current` or `splitTextRef2.current` | ||
}); | ||
|
||
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" style={{ cursor: 'pointer' }}> | ||
Replay | ||
</button> | ||
<SplitTextWrapper ref={splitText1Ref} as="h1"> | ||
I'm an h1 element | ||
</SplitTextWrapper> | ||
<SplitTextWrapper ref={splitText2Ref} as="label" htmlFor="value"> | ||
I'm a code element | ||
</SplitTextWrapper> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
### Demo | ||
|
||
<Canvas of={stories.AsProp} /> |
Oops, something went wrong.