File size: 1,617 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import { useResizeObserver } from '@wordpress/compose';
import { useLayoutEffect, useRef } from 'react';
import type { CSSProperties } from 'react';
interface Props {
className?: string;
children: JSX.Element;
isFullscreen?: boolean;
enabled?: boolean;
}
const AnimatedFullscreen = ( { className, children, isFullscreen, enabled }: Props ) => {
const positionerRef = useRef< HTMLDivElement | null >( null );
const targetRef = useRef< HTMLDivElement | null >( null );
const [ containerResizeListener, { width, height } ] = useResizeObserver();
useLayoutEffect( () => {
if ( ! enabled || ! positionerRef.current || ! targetRef.current ) {
return;
}
const { offsetLeft, offsetTop, offsetHeight, offsetWidth } = positionerRef.current;
targetRef.current.style.height = `${ offsetHeight }px`;
targetRef.current.style.width = `${ offsetWidth }px`;
targetRef.current.style.left = `${ offsetLeft }px`;
targetRef.current.style.top = `${ offsetTop }px`;
}, [ isFullscreen, width, height, enabled ] );
if ( ! enabled ) {
return <div className={ className }>{ children }</div>;
}
return (
<>
<div
className={ className }
ref={ positionerRef }
style={ {
opacity: 0,
pointerEvents: 'none',
zIndex: -1,
} }
aria-hidden="true"
>
{ containerResizeListener }
</div>
<div
className={ className }
ref={ targetRef }
style={
{
position: 'absolute',
transition: 'all 0.3s ease-out',
isolation: 'isolate',
} as CSSProperties
}
>
{ children }
</div>
</>
);
};
export default AnimatedFullscreen;
|