import { useResizeObserver } from '@wordpress/compose'; import clsx from 'clsx'; import { useState, useEffect, useRef } from 'react'; import { DEVICE_TYPES } from './constants'; import DeviceSwitcherToolbar from './toolbar'; import type { Device } from './types'; import './device-switcher.scss'; interface Props { children: React.ReactNode; className?: string; defaultDevice?: Device; isShowDeviceSwitcherToolbar?: boolean; isShowFrameBorder?: boolean; isShowFrameShadow?: boolean; isFullscreen?: boolean; frameRef?: React.MutableRefObject< HTMLDivElement | null >; onDeviceChange?: ( device: Device ) => void; onViewportChange?: ( height?: number ) => void; } // Transition animation delay const ANIMATION_DURATION = 250; const { COMPUTER, TABLET, PHONE } = DEVICE_TYPES; const DeviceSwitcher = ( { children, className = '', defaultDevice = COMPUTER, isShowDeviceSwitcherToolbar, isShowFrameBorder, isShowFrameShadow = true, isFullscreen, frameRef, onDeviceChange, onViewportChange, }: Props ) => { const [ device, setDevice ] = useState< Device >( defaultDevice ); const [ containerResizeListener, { width, height } ] = useResizeObserver(); const timerRef = useRef< null | ReturnType< typeof setTimeout > >( null ); const handleDeviceClick = ( nextDevice: Device ) => { setDevice( nextDevice ); onDeviceChange?.( nextDevice ); }; // Animate on viewport size update useEffect( () => { const clearAnimationEndTimer = () => { if ( timerRef.current ) { clearTimeout( timerRef.current ); } }; // Trigger animation end after the duration timerRef.current = setTimeout( () => { timerRef.current = null; const frameHeight = frameRef?.current?.getBoundingClientRect()?.height; if ( frameHeight ) { onViewportChange?.( frameHeight ); } }, ANIMATION_DURATION ); return clearAnimationEndTimer; }, [ width, height ] ); return (