|
| 1 | +--- |
| 2 | +name: ScrollTrigger |
| 3 | +menu: Components |
| 4 | +--- |
| 5 | + |
| 6 | +import { Fragment } from 'react'; |
| 7 | +import { Playground, Props } from 'docz' |
| 8 | +import { Controls, PlayState, ScrollTrigger } from './../../../react-gsap/src/' |
| 9 | +import { Tween } from './Tween' |
| 10 | +import { Timeline, TargetWithNames } from './Timeline' |
| 11 | + |
| 12 | +# ScrollTrigger |
| 13 | + |
| 14 | +The ScrollTrigger component is a small helper for the ScrollTrigger plugin. |
| 15 | + |
| 16 | +Read the official documentation: https://greensock.com/docs/v3/Plugins/ScrollTrigger |
| 17 | + |
| 18 | +It's available since version 3.2.0. |
| 19 | +Before you also could use the ScrollTrigger plugin by importing/registering and using it in a Tween or Timeline by yourself: |
| 20 | + |
| 21 | + import { Tween } from 'react-gsap'; |
| 22 | + |
| 23 | + import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'; |
| 24 | + gsap.registerPlugin(ScrollTrigger); |
| 25 | + |
| 26 | + <Tween |
| 27 | + to={{ |
| 28 | + x: '300px', |
| 29 | + scrollTrigger: { |
| 30 | + trigger: '.square', |
| 31 | + start: '-200px center', |
| 32 | + end: '200px center', |
| 33 | + scrub: 0.5, |
| 34 | + markers: true, |
| 35 | + }, |
| 36 | + }} |
| 37 | + > |
| 38 | + <div className="square" style={{ width: '100px', height: '100px', background: '#ccc' }} /> |
| 39 | + </Tween> |
| 40 | + |
| 41 | +## Basic usage |
| 42 | + |
| 43 | +With the ScrollTrigger component, it looks like the following. If you don't add a trigger prop, it will use the ref from the Tween target. |
| 44 | + |
| 45 | +<Playground> |
| 46 | + <ScrollTrigger |
| 47 | + start="-200px center" |
| 48 | + end="200px center" |
| 49 | + scrub={0.5} |
| 50 | + markers |
| 51 | + > |
| 52 | + <Tween |
| 53 | + to={{ |
| 54 | + x: '300px', |
| 55 | + }} |
| 56 | + > |
| 57 | + <div style={{ width: '100px', height: '100px', background: '#ccc' }} /> |
| 58 | + </Tween> |
| 59 | + <Tween |
| 60 | + to={{ |
| 61 | + x: '300px', |
| 62 | + }} |
| 63 | + > |
| 64 | + <div style={{ width: '100px', height: '100px', background: '#999' }} /> |
| 65 | + </Tween> |
| 66 | + </ScrollTrigger> |
| 67 | +</Playground> |
| 68 | + |
| 69 | +## Use "trigger" prop |
| 70 | + |
| 71 | +Currently it's not possible to change the props on the fly. **So this will not work for the `trigger` prop**: |
| 72 | + |
| 73 | + const triggerRef = useRef(null); |
| 74 | + const [trigger, setTrigger] = useState(triggerRef.current); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + setTrigger(triggerRef.current); |
| 78 | + }, []); |
| 79 | + |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <ScrollTrigger trigger={trigger}> |
| 83 | + <Tween |
| 84 | + to={{ |
| 85 | + x: '500px', |
| 86 | + }} |
| 87 | + > |
| 88 | + <div>This element gets not tweened by ref</div> |
| 89 | + </Tween> |
| 90 | + </ScrollTrigger> |
| 91 | + |
| 92 | + <Square ref={triggerRef}> |
| 93 | + This element is the trigger |
| 94 | + </Square> |
| 95 | + </> |
| 96 | + ); |
| 97 | + |
| 98 | +If you want to target a ref directly instead of using a CSS selector you can use a Timeline with a forwardRef target: |
| 99 | + |
| 100 | + // This is the target component which "exports" 4 refs |
| 101 | + const TargetWithNames = forwardRef((props, ref: any) => { |
| 102 | + const div1 = useRef(null); |
| 103 | + const div2 = useRef<MutableRefObject<any>[]>([]); |
| 104 | + const div3 = useRef(null); |
| 105 | + const trigger = useRef(null); |
| 106 | + |
| 107 | + useImperativeHandle(ref, () => ({ |
| 108 | + div1, |
| 109 | + div2, |
| 110 | + div3, |
| 111 | + trigger, |
| 112 | + })); |
| 113 | + |
| 114 | + return ( |
| 115 | + <div ref={trigger} style={{ textAlign: 'center' }}> |
| 116 | + <h3 ref={div1}>THIS</h3> |
| 117 | + <SplitChars |
| 118 | + ref={(charRef: MutableRefObject<any>) => div2.current.push(charRef)} |
| 119 | + wrapper={<h3 style={{ display: 'inline-block' }} />} |
| 120 | + > |
| 121 | + TEST |
| 122 | + </SplitChars> |
| 123 | + <h3 ref={div3}>IS A</h3> |
| 124 | + </div> |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | +You can then use the key of the exported refs in the `trigger` or `target` props. |
| 129 | +If it doesn't find a ref with this key it will use the `trigger` string as CSS selector. |
| 130 | + |
| 131 | +<Playground> |
| 132 | + <ScrollTrigger |
| 133 | + trigger="trigger" |
| 134 | + start="top center" |
| 135 | + end="400px center" |
| 136 | + scrub={0.5} |
| 137 | + markers={true} |
| 138 | + pin={true} |
| 139 | + > |
| 140 | + <Timeline target={<TargetWithNames />}> |
| 141 | + <Tween |
| 142 | + from={{ |
| 143 | + x: '-100vw', |
| 144 | + }} |
| 145 | + target="div1" |
| 146 | + /> |
| 147 | + <Tween |
| 148 | + from={{ |
| 149 | + x: '-100vw', |
| 150 | + }} |
| 151 | + target="div3" |
| 152 | + /> |
| 153 | + <Tween |
| 154 | + from={{ |
| 155 | + x: '-100vw', |
| 156 | + }} |
| 157 | + stagger={0.5} |
| 158 | + target="div2" |
| 159 | + /> |
| 160 | + </Timeline> |
| 161 | + </ScrollTrigger> |
| 162 | +</Playground> |
| 163 | + |
| 164 | +## Standalone |
| 165 | + |
| 166 | +If you don't pass children to the component a GSAP ScrollTrigger instance will be created and can be used standalone. |
| 167 | + |
| 168 | +You can get the instance by calling `getGSAP()` on the ref. |
| 169 | + |
| 170 | +<Playground> |
| 171 | + {() => { |
| 172 | + const scrollTrigger = React.useRef(null); |
| 173 | + return ( |
| 174 | + <Fragment> |
| 175 | + <div id="id" style={{width: '100px', height: '100px', background: '#ccc'}}/> |
| 176 | + <div id="otherID" style={{width: '100px', height: '100px', background: '#999'}}/> |
| 177 | + <ScrollTrigger |
| 178 | + ref={scrollTrigger} |
| 179 | + trigger="#id" |
| 180 | + start="top top" |
| 181 | + endTrigger="#otherID" |
| 182 | + end="bottom 50%+=100px" |
| 183 | + markers |
| 184 | + onToggle={self => console.log("toggled, isActive:", self.isActive)} |
| 185 | + onUpdate={self => { |
| 186 | + console.log("progress:", self.progress.toFixed(3), "direction:", self.direction, "velocity", self.getVelocity()); |
| 187 | + }} |
| 188 | + /> |
| 189 | + <button onClick={() => scrollTrigger.current.getGSAP().disable()}>Disable ScrollTrigger</button> |
| 190 | + </Fragment> |
| 191 | + ) |
| 192 | + }} |
| 193 | +</Playground> |
0 commit comments