-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.native.js
85 lines (72 loc) · 1.85 KB
/
index.native.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
79
80
81
82
83
84
85
/**
* External dependencies
*/
import { View, Dimensions } from 'react-native';
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { ALIGNMENT_BREAKPOINTS, WIDE_ALIGNMENTS } from '@wordpress/components';
/**
* Internal dependencies
*/
import styles from './style.scss';
const PIXEL_RATIO = 2;
const ReadableContentView = ( { align, reversed, children, style } ) => {
const { width, height } = Dimensions.get( 'window' );
const [ windowWidth, setWindowWidth ] = useState( width );
const [ windowRatio, setWindowRatio ] = useState( width / height );
function onDimensionsChange( { window } ) {
setWindowWidth( window.width );
setWindowRatio( window.width / window.height );
}
useEffect( () => {
const dimensionsChangeSubscription = Dimensions.addEventListener(
'change',
onDimensionsChange
);
return () => {
dimensionsChangeSubscription.remove();
};
}, [] );
function getWideStyles() {
if (
windowRatio >= PIXEL_RATIO &&
windowWidth < ALIGNMENT_BREAKPOINTS.large
) {
return styles.wideLandscape;
}
if ( windowWidth <= ALIGNMENT_BREAKPOINTS.small ) {
return { maxWidth: windowWidth };
}
if (
windowWidth >= ALIGNMENT_BREAKPOINTS.medium &&
windowWidth < ALIGNMENT_BREAKPOINTS.wide
) {
return styles.wideMedium;
}
}
return (
<View style={ styles.container }>
<View
style={ [
reversed
? styles.reversedCenteredContent
: styles.centeredContent,
style,
styles[ align ],
align === WIDE_ALIGNMENTS.alignments.wide &&
getWideStyles(),
] }
>
{ children }
</View>
</View>
);
};
const isContentMaxWidth = () => {
const { width } = Dimensions.get( 'window' );
return width > styles.centeredContent.maxWidth;
};
ReadableContentView.isContentMaxWidth = isContentMaxWidth;
export default ReadableContentView;