@@ -7,6 +7,7 @@ import React, {
77 ReactHTMLElement ,
88 useCallback ,
99 useEffect ,
10+ MutableRefObject ,
1011} from 'react' ;
1112import styled from 'styled-components' ;
1213import { Tween , Timeline , SplitWords , SplitChars , Controls , PlayState } from 'react-gsap' ;
@@ -123,18 +124,28 @@ const TimelineComponent = () => (
123124 </ TimelineStyled >
124125) ;
125126
126- const TargetWithNames = forwardRef ( ( props , targets : any ) => (
127- < div >
128- < div ref = { div => targets . set ( 'div1' , div ) } > first</ div >
129- < SplitChars
130- ref = { ( div : ReactElement ) => targets . set ( 'div2' , [ div ] ) }
131- wrapper = { < span style = { { display : 'inline-block' } } /> }
132- >
133- second
134- </ SplitChars >
135- < div ref = { div => targets . set ( 'div3' , div ) } > third</ div >
136- </ div >
137- ) ) ;
127+ const TargetWithNames = forwardRef ( ( props , ref : any ) => {
128+ const div1 = useRef ( null ) ;
129+ const div2 = useRef < MutableRefObject < any > [ ] > ( [ ] ) ;
130+ const div3 = useRef ( null ) ;
131+ useImperativeHandle ( ref , ( ) => ( {
132+ div1,
133+ div2,
134+ div3,
135+ } ) ) ;
136+ return (
137+ < div >
138+ < div ref = { div1 } > first</ div >
139+ < SplitChars
140+ ref = { ( charRef : MutableRefObject < any > ) => div2 . current . push ( charRef ) }
141+ wrapper = { < span style = { { display : 'inline-block' } } /> }
142+ >
143+ second
144+ </ SplitChars >
145+ < div ref = { div3 } > third</ div >
146+ </ div >
147+ ) ;
148+ } ) ;
138149
139150const TimelineTargets = ( ) => {
140151 return (
@@ -148,18 +159,7 @@ const TimelineTargets = () => {
148159
149160//export default TimelineTargets;
150161
151- const Component = forwardRef ( ( { children } , targets ) => {
152- return (
153- < div >
154- < div ref = { div => targets ?. set && targets . set ( 'div1' , div ) } >
155- < span > { children } </ span >
156- </ div >
157- < div ref = { div => targets ?. set && targets . set ( 'div2' , div ) } > Div 2</ div >
158- </ div >
159- ) ;
160- } ) ;
161-
162- const Component2 = forwardRef ( ( { children } , ref ?) => {
162+ const Component = forwardRef ( ( { children } , ref ?) => {
163163 const div1 = useRef ( null ) ;
164164 const div2 = useRef ( null ) ;
165165 useImperativeHandle ( ref , ( ) => ( {
@@ -235,9 +235,9 @@ const Out = () => {
235235 < div ref = { divRef2 } style = { { width : 200 , height : 200 , background : 'fuchsia' } } />
236236 </ Tween >
237237
238- < Tween to = { { x : '200px' } } target = "div2 " position = "0" />
238+ < Tween to = { { x : '200px' } } target = "div3 " position = "0" />
239239 < Tween to = { { x : '200px' } } target = "div1" position = "0.5" />
240- < Tween to = { { x : '200px' } } target = "div3 " position = "1" stagger = { 0.1 } />
240+ < Tween to = { { x : '200px' } } target = "div2 " position = "1" stagger = { 0.1 } />
241241 </ Timeline >
242242 </ div >
243243 ) ;
0 commit comments