-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
78 lines (67 loc) · 1.9 KB
/
index.js
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
/**
* WordPress dependencies
*/
import { useEffect, useState } from '@wordpress/element';
/**
* Function to resize the editor window.
*
* @param {string} deviceType Used for determining the size of the container (e.g. Desktop, Tablet, Mobile)
*
* @return {Object} Inline styles to be added to resizable container.
*/
export default function useResizeCanvas( deviceType ) {
const [ actualWidth, updateActualWidth ] = useState( window.innerWidth );
useEffect( () => {
if ( deviceType === 'Desktop' ) {
return;
}
const resizeListener = () => updateActualWidth( window.innerWidth );
window.addEventListener( 'resize', resizeListener );
return () => {
window.removeEventListener( 'resize', resizeListener );
};
}, [ deviceType ] );
const getCanvasWidth = ( device ) => {
let deviceWidth;
switch ( device ) {
case 'Tablet':
deviceWidth = 780;
break;
case 'Mobile':
deviceWidth = 360;
break;
default:
return null;
}
return deviceWidth < actualWidth ? deviceWidth : actualWidth;
};
const marginValue = () => ( window.innerHeight < 800 ? 36 : 72 );
const contentInlineStyles = ( device ) => {
const height = device === 'Mobile' ? '768px' : '1024px';
const marginVertical = marginValue() + 'px';
const marginHorizontal = 'auto';
switch ( device ) {
case 'Tablet':
case 'Mobile':
return {
width: getCanvasWidth( device ),
// Keeping margin styles separate to avoid warnings
// when those props get overridden in the iframe component
marginTop: marginVertical,
marginBottom: marginVertical,
marginLeft: marginHorizontal,
marginRight: marginHorizontal,
height,
borderRadius: '2px 2px 2px 2px',
border: '1px solid #ddd',
overflowY: 'auto',
};
default:
return {
marginLeft: marginHorizontal,
marginRight: marginHorizontal,
};
}
};
return contentInlineStyles( deviceType );
}