File size: 5,505 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
	__experimentalHStack as HStack,
	__experimentalVStack as VStack,
} from '@wordpress/components';
import { useResizeObserver, useViewportMatch } from '@wordpress/compose';
import { useMemo, useState } from 'react';
import {
	FONT_PREVIEW_LARGE_WIDTH,
	FONT_PREVIEW_LARGE_HEIGHT,
	FONT_PREVIEW_WIDTH,
	FONT_PREVIEW_HEIGHT,
	SYSTEM_FONT_SLUG,
} from '../../constants';
import { useGlobalSetting, useGlobalStyle } from '../../gutenberg-bridge';
import GlobalStylesVariationContainer from '../global-styles-variation-container';
import FontFamiliesLoader from './font-families-loader';
import type { FontFamily } from '../../types';

const DEFAULT_LARGE_FONT_STYLES: React.CSSProperties = {
	fontSize: '13vw', // 18px for min-width wide breakpoint and 15px for max-width wide
	lineHeight: '20px',
	color: '#000000',
};

const FontPairingVariationPreview = () => {
	const [ defaultFontFamilies ] = useGlobalSetting( 'typography.fontFamilies.default' ) as [
		FontFamily[],
	];
	const [ themeFontFamilies ] = useGlobalSetting( 'typography.fontFamilies.theme' ) as [
		FontFamily[],
	];
	const [ textFontFamily = 'serif' ] = useGlobalStyle( 'typography.fontFamily' );
	const [ textFontStyle = 'normal' ] = useGlobalStyle( 'typography.fontStyle' );
	const [ textLetterSpacing = '-0.15px' ] = useGlobalStyle( 'typography.letterSpacing' );
	const [ textFontWeight = 400 ] = useGlobalStyle( 'typography.fontWeight' );

	const [ baseHeadingFontFamily = textFontFamily ] = useGlobalStyle(
		'elements.heading.typography.fontFamily'
	);
	const [ baseHeadingFontStyle = textFontStyle ] = useGlobalStyle(
		'elements.heading.typography.fontStyle'
	);
	const [ baseHeadingFontWeight = textFontWeight ] = useGlobalStyle(
		'elements.heading.typography.fontWeight'
	);
	const [ baseHeadingLetterSpacing = textLetterSpacing ] = useGlobalStyle(
		'elements.heading.typography.letterSpacing'
	);

	const [ headingFontFamily = baseHeadingFontFamily ] = useGlobalStyle(
		'elements.h1.typography.fontFamily'
	);
	const [ headingFontStyle = baseHeadingFontStyle ] = useGlobalStyle(
		'elements.h1.typography.fontStyle'
	);
	const [ headingFontWeight = baseHeadingFontWeight ] = useGlobalStyle(
		'elements.h1.typography.fontWeight'
	);
	const [ headingLetterSpacing = baseHeadingLetterSpacing ] = useGlobalStyle(
		'elements.h1.typography.letterSpacing'
	);

	const [ containerResizeListener, { width } ] = useResizeObserver();
	const isDesktop = useViewportMatch( 'large' );
	const defaultWidth = isDesktop ? FONT_PREVIEW_LARGE_WIDTH : FONT_PREVIEW_WIDTH;
	const defaultHeight = isDesktop ? FONT_PREVIEW_LARGE_HEIGHT : FONT_PREVIEW_HEIGHT;
	const ratio = width ? width / defaultWidth : 1;
	const normalizedHeight = Math.ceil( defaultHeight * ratio );
	const fontFamilies = useMemo(
		() => [ ...defaultFontFamilies, ...themeFontFamilies ],
		[ defaultFontFamilies, themeFontFamilies ]
	);

	const getFontFamilyName = ( targetFontFamily: string ) => {
		const fontFamily = fontFamilies.find( ( { fontFamily } ) => fontFamily === targetFontFamily );
		return fontFamily?.name || fontFamily?.fontFamily || targetFontFamily;
	};

	const textFontFamilyName = useMemo(
		() => getFontFamilyName( textFontFamily ),
		[ textFontFamily, fontFamilies ]
	);

	const headingFontFamilyName = useMemo(
		() => getFontFamilyName( headingFontFamily ),
		[ headingFontFamily, fontFamilies ]
	);

	const externalFontFamilies = fontFamilies
		.filter( ( { slug } ) => slug !== SYSTEM_FONT_SLUG )
		.filter( ( { fontFamily } ) => [ textFontFamily, headingFontFamily ].includes( fontFamily ) );

	const [ isLoaded, setIsLoaded ] = useState( ! externalFontFamilies.length );

	const handleOnLoad = () => setIsLoaded( true );

	return (
		<GlobalStylesVariationContainer
			width={ width }
			height={ normalizedHeight }
			containerResizeListener={ containerResizeListener }
		>
			<>
				<div
					style={ {
						// Apply the normalized height only when the width is available
						height: width ? normalizedHeight : 0,
						width: '100%',
						background: 'white',
						cursor: 'pointer',
					} }
				>
					<div
						style={ {
							height: '100%',
							overflow: 'hidden',
							opacity: isLoaded ? 1 : 0,
						} }
					>
						<HStack
							spacing={ 10 * ratio }
							justify="flex-start"
							style={ {
								height: '100%',
								overflow: 'hidden',
							} }
						>
							<VStack
								spacing={ 1 }
								style={ {
									margin: '10px',
									width: '100%',
									textAlign: isDesktop ? 'center' : 'left',
								} }
							>
								<div
									aria-label={ headingFontFamilyName }
									style={ {
										...DEFAULT_LARGE_FONT_STYLES,
										letterSpacing: headingLetterSpacing,
										fontWeight: headingFontWeight,
										fontFamily: headingFontFamily,
										fontStyle: headingFontStyle,
									} }
								>
									{ headingFontFamilyName }
								</div>
								<div
									aria-label={ textFontFamilyName }
									style={ {
										...DEFAULT_LARGE_FONT_STYLES,
										fontSize: '13px',
										letterSpacing: textLetterSpacing,
										fontWeight: textFontWeight,
										fontFamily: textFontFamily,
										fontStyle: textFontStyle,
									} }
								>
									{ textFontFamilyName }
								</div>
							</VStack>
						</HStack>
					</div>
				</div>
				<FontFamiliesLoader fontFamilies={ externalFontFamilies } onLoad={ handleOnLoad } />
			</>
		</GlobalStylesVariationContainer>
	);
};

export default FontPairingVariationPreview;