File size: 2,729 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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 (
		<div
			className={ clsx( className, 'device-switcher__container', {
				'device-switcher__container--frame-shadow': isShowFrameShadow,
				'device-switcher__container--frame-bordered': isShowFrameBorder,
				'device-switcher__container--is-computer': device === COMPUTER,
				'device-switcher__container--is-tablet': device === TABLET,
				'device-switcher__container--is-phone': device === PHONE,
				'device-switcher__container--is-fullscreen': isFullscreen,
			} ) }
		>
			<div className="device-switcher__header">
				{ isShowDeviceSwitcherToolbar && (
					<DeviceSwitcherToolbar device={ device } onDeviceClick={ handleDeviceClick } />
				) }
			</div>
			<div className="device-switcher__frame" ref={ frameRef }>
				{ children }
			</div>
			{ containerResizeListener }
		</div>
	);
};

export default DeviceSwitcher;