File size: 813 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
import { useBreakpoint } from '@automattic/viewport-react';
import React, { ReactNode, useEffect } from 'react';

const ComponentSwapper = ( {
	className,
	breakpoint = '<660px', // breakpoints from mediaQueryLists from '@automattic/viewport'
	breakpointActiveComponent,
	breakpointInactiveComponent,
	onSwap,
	children,
}: {
	children?: ReactNode;
	breakpoint: string;
	breakpointActiveComponent: React.ReactElement;
	breakpointInactiveComponent: React.ReactElement;
	onSwap?: () => void;
	className?: string;
} ) => {
	const isActive = useBreakpoint( breakpoint );

	useEffect( () => {
		onSwap?.();
	}, [ isActive, onSwap ] );

	return (
		<div className={ className }>
			{ isActive ? breakpointActiveComponent : breakpointInactiveComponent }
			{ children }
		</div>
	);
};

export default ComponentSwapper;