import { TimelineLite } from 'gsap' ; import { SplitText } from './vendor/SplitText' ; ( window as any ) . _gsapPlugins = [ SplitText ] ; const EMPTY_OBJ = { } ; const memoryMap = new WeakMap ( ) ; export interface TypeAnimOptions { splitType : 'chars' | 'chars,words' | 'chars,words,lines' ; typeInterval : number ; } param element param options param options . splitType param options . typeInterval returns export function typeAnim ( element , { splitType = 'chars,words' , typeInterval = 0.03 } = { } ) { const tl = new TimelineLite ( ) ; const split = new SplitText ( element , { type : splitType , charsClass : 'character' , linesClass : 'line' } ) ; memoryMap . set ( element , { split } ) ; switch ( splitType ) { case 'chars' : tl . staggerFromTo ( split . chars , 0.001 , { visibility : 'hidden' } , { visibility : 'visible' } , typeInterval ) ; break ; case 'chars,words' : case 'chars,words,lines' : split . words . forEach ( ( word ) => { tl . staggerFromTo ( word . children , 0.001 , { visibility : 'hidden' } , { visibility : 'visible' } , typeInterval ) ; tl . to ( EMPTY_OBJ , typeInterval , EMPTY_OBJ ) ; } ) ; break ; default : throw new Error ( `Unexpected splitType "${ splitType }"` ) ; } return tl ; } param element param typeInterval returns export function untypeAnim ( element , typeInterval = 0.03 ) { const tl = new TimelineLite ( ) ; if ( ! memoryMap . has ( element ) ) { return tl ; } const split = memoryMap . get ( element ) . split ; if ( split . words ) { split . words . forEach ( ( word ) => { tl . staggerTo ( word . children , 0.001 , { visibility : 'hidden' } , typeInterval ) ; tl . to ( EMPTY_OBJ , typeInterval , EMPTY_OBJ ) ; } ) ; } else { tl . staggerFrom ( split . chars , 0.001 , { visibility : 'hidden' } , typeInterval ) ; } return tl ; }