Reveal
The Reveal component is basically an Intersection Observer and a Timeline component combined.
All children Tween and Timeline components get played when they are visible in viewport.
import { Reveal, Tween } from 'react-gsap';
Usage with Tween
Add a little space for testing
|
so scroll down
|
|
|
|
|
|
|
|
|
|
This headline is fading in
Fade in from left example
Of course you can outsource animations to separate components. In this example a FadeInLeft animation:
const FadeInLeft = ({ children }) => (<Tweenfrom={{ opacity: 0, transform: 'translate3d(-100vw, 0, 0)' }}ease="back.out(1.4)">{children}</Tween>);
Also note the use of the trigger
prop.
In this case the <H3>
wouldn't trigger because it's out of the viewport initially: translate3d(-100vw, 0, 0)
If no trigger is passed, the first element from any child Tween or Timeline that gets beyond the threshold is used.
This headline is coming from left
forwardRef trigger
You can also create a forwardRef component as trigger if you need a more complex wrapper or trigger.
export const AnimationTrigger = React.forwardRef<HTMLDivElement>(({ children }, ref) => (<divref={ref}style={{paddingTop: '200px',paddingBottom: '200px',background: '#f0f0f0',textAlign: 'center',}}>{children}</div>));
I get triggered later
Props
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | ||
trigger? | React.ReactElement | null | This element triggers the animation if it gets into view. Needs to be a "refable" element like HTML element or forwardRef component | |
repeat? | boolean | false | Trigger the animation again and again? |
root? | Element | null | null | |
rootMargin? | string | 0px | |
threshold? | number | 0.66 | As opposed to the threshold value from IntersectionObserver options this is just a single number |