UNPKG

@wordpress/components

Version:
1 lines • 6.8 kB
{"version":3,"file":"use-screen-animate-presence.mjs","names":["isRTL","isRTLFn"],"sources":["../../../src/navigator/navigator-screen/use-screen-animate-presence.ts"],"sourcesContent":["import {\n\tuseState,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseCallback,\n} from '@wordpress/element';\nimport { useReducedMotion } from '@wordpress/compose';\nimport { isRTL as isRTLFn } from '@wordpress/i18n';\nimport { ANIMATION_END_NAMES, TOTAL_ANIMATION_DURATION } from '../animation';\n\n// Possible values:\n// - 'INITIAL': the initial state\n// - 'ANIMATING_IN': start enter animation\n// - 'IN': enter animation has ended\n// - 'ANIMATING_OUT': start exit animation\n// - 'OUT': the exit animation has ended\ntype AnimationStatus =\n\t'INITIAL' | 'ANIMATING_IN' | 'IN' | 'ANIMATING_OUT' | 'OUT';\n\n// Allow an extra 20% of the total animation duration to account for potential\n// event loop delays.\nconst ANIMATION_TIMEOUT_MARGIN = 1.2;\n\nconst isEnterAnimation = (\n\tanimationDirection: 'end' | 'start',\n\tanimationStatus: AnimationStatus,\n\tanimationName: string\n) =>\n\tanimationStatus === 'ANIMATING_IN' &&\n\tanimationName === ANIMATION_END_NAMES[ animationDirection ].in;\n\nconst isExitAnimation = (\n\tanimationDirection: 'end' | 'start',\n\tanimationStatus: AnimationStatus,\n\tanimationName: string\n) =>\n\tanimationStatus === 'ANIMATING_OUT' &&\n\tanimationName === ANIMATION_END_NAMES[ animationDirection ].out;\n\nexport function useScreenAnimatePresence( {\n\tisMatch,\n\tskipAnimation,\n\tisBack,\n\tonAnimationEnd,\n}: {\n\tisMatch: boolean;\n\tskipAnimation: boolean;\n\tisBack?: boolean;\n\tonAnimationEnd?: React.AnimationEventHandler< Element >;\n} ) {\n\tconst isRTL = isRTLFn();\n\tconst prefersReducedMotion = useReducedMotion();\n\n\tconst [ animationStatus, setAnimationStatus ] =\n\t\tuseState< AnimationStatus >( 'INITIAL' );\n\n\t// Start enter and exit animations when the screen is selected or deselected.\n\t// The animation status is set to `IN` or `OUT` immediately if the animation\n\t// should be skipped.\n\tconst becameSelected =\n\t\tanimationStatus !== 'ANIMATING_IN' &&\n\t\tanimationStatus !== 'IN' &&\n\t\tisMatch;\n\tconst becameUnselected =\n\t\tanimationStatus !== 'ANIMATING_OUT' &&\n\t\tanimationStatus !== 'OUT' &&\n\t\t! isMatch;\n\tuseLayoutEffect( () => {\n\t\tif ( becameSelected ) {\n\t\t\tsetAnimationStatus(\n\t\t\t\tskipAnimation || prefersReducedMotion ? 'IN' : 'ANIMATING_IN'\n\t\t\t);\n\t\t} else if ( becameUnselected ) {\n\t\t\tsetAnimationStatus(\n\t\t\t\tskipAnimation || prefersReducedMotion ? 'OUT' : 'ANIMATING_OUT'\n\t\t\t);\n\t\t}\n\t}, [\n\t\tbecameSelected,\n\t\tbecameUnselected,\n\t\tskipAnimation,\n\t\tprefersReducedMotion,\n\t] );\n\n\t// Animation attributes (derived state).\n\tconst animationDirection =\n\t\t( isRTL && isBack ) || ( ! isRTL && ! isBack ) ? 'end' : 'start';\n\tconst isAnimatingIn = animationStatus === 'ANIMATING_IN';\n\tconst isAnimatingOut = animationStatus === 'ANIMATING_OUT';\n\tlet animationType: 'in' | 'out' | undefined;\n\tif ( isAnimatingIn ) {\n\t\tanimationType = 'in';\n\t} else if ( isAnimatingOut ) {\n\t\tanimationType = 'out';\n\t}\n\n\tconst onScreenAnimationEnd = useCallback(\n\t\t( e: React.AnimationEvent< HTMLElement > ) => {\n\t\t\tonAnimationEnd?.( e );\n\n\t\t\tif (\n\t\t\t\tisExitAnimation(\n\t\t\t\t\tanimationDirection,\n\t\t\t\t\tanimationStatus,\n\t\t\t\t\te.animationName\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\t// When the exit animation ends on an unselected screen, set the\n\t\t\t\t// status to 'OUT' to remove the screen contents from the DOM.\n\t\t\t\tsetAnimationStatus( 'OUT' );\n\t\t\t} else if (\n\t\t\t\tisEnterAnimation(\n\t\t\t\t\tanimationDirection,\n\t\t\t\t\tanimationStatus,\n\t\t\t\t\te.animationName\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\t// When the enter animation ends on a selected screen, set the\n\t\t\t\t// status to 'IN' to ensure the screen is rendered in the DOM.\n\t\t\t\tsetAnimationStatus( 'IN' );\n\t\t\t}\n\t\t},\n\t\t[ onAnimationEnd, animationStatus, animationDirection ]\n\t);\n\n\t// Fallback timeout to ensure that the logic is applied even if the\n\t// `animationend` event is not triggered.\n\tuseEffect( () => {\n\t\tlet animationTimeout: number | undefined;\n\n\t\tif ( isAnimatingOut ) {\n\t\t\tanimationTimeout = window.setTimeout( () => {\n\t\t\t\tsetAnimationStatus( 'OUT' );\n\t\t\t\tanimationTimeout = undefined;\n\t\t\t}, TOTAL_ANIMATION_DURATION.OUT * ANIMATION_TIMEOUT_MARGIN );\n\t\t} else if ( isAnimatingIn ) {\n\t\t\tanimationTimeout = window.setTimeout( () => {\n\t\t\t\tsetAnimationStatus( 'IN' );\n\t\t\t\tanimationTimeout = undefined;\n\t\t\t}, TOTAL_ANIMATION_DURATION.IN * ANIMATION_TIMEOUT_MARGIN );\n\t\t}\n\n\t\treturn () => {\n\t\t\tif ( animationTimeout ) {\n\t\t\t\twindow.clearTimeout( animationTimeout );\n\t\t\t\tanimationTimeout = undefined;\n\t\t\t}\n\t\t};\n\t}, [ isAnimatingOut, isAnimatingIn ] );\n\n\treturn {\n\t\t// Render the screen's contents in the DOM not only when the screen is\n\t\t// selected, but also while it is animating out.\n\t\tshouldRenderScreen:\n\t\t\tisMatch ||\n\t\t\tanimationStatus === 'IN' ||\n\t\t\tanimationStatus === 'ANIMATING_OUT',\n\t\tscreenProps: {\n\t\t\tonAnimationEnd: onScreenAnimationEnd,\n\t\t\t'data-animation-direction': animationDirection,\n\t\t\t'data-animation-type': animationType,\n\t\t\t'data-skip-animation': skipAnimation || undefined,\n\t\t},\n\t} as const;\n}\n"],"mappings":";;;;;AAqBA,MAAM,2BAA2B;AAEjC,MAAM,oBACL,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