fix: cys assembler font loading optimisations (#40458)
* fix: cys assembler font loading optimisations * lint
This commit is contained in:
parent
4ac861d94c
commit
ceb5f942ea
|
@ -45,6 +45,7 @@ import { addFilter } from '@wordpress/hooks';
|
||||||
import { CustomizeStoreComponent } from '../types';
|
import { CustomizeStoreComponent } from '../types';
|
||||||
import { Layout } from './layout';
|
import { Layout } from './layout';
|
||||||
import './style.scss';
|
import './style.scss';
|
||||||
|
import { PreloadFonts } from './preload-fonts';
|
||||||
|
|
||||||
const { RouterProvider } = unlock( routerPrivateApis );
|
const { RouterProvider } = unlock( routerPrivateApis );
|
||||||
|
|
||||||
|
@ -149,6 +150,7 @@ export const AssemblerHub: CustomizeStoreComponent = ( props ) => {
|
||||||
<RouterProvider>
|
<RouterProvider>
|
||||||
<Layout />
|
<Layout />
|
||||||
</RouterProvider>
|
</RouterProvider>
|
||||||
|
<PreloadFonts />
|
||||||
</GlobalStylesProvider>
|
</GlobalStylesProvider>
|
||||||
</ShortcutProvider>
|
</ShortcutProvider>
|
||||||
</CustomizeStoreContext.Provider>
|
</CustomizeStoreContext.Provider>
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
import { FONT_PAIRINGS } from './sidebar/global-styles/font-pairing-variations/constants';
|
||||||
|
import { FontFamiliesLoader } from './sidebar/global-styles/font-pairing-variations/font-families-loader';
|
||||||
|
|
||||||
|
export const PreloadFonts = () => {
|
||||||
|
const allFontChoices = FONT_PAIRINGS.map(
|
||||||
|
( fontPair ) => fontPair?.settings?.typography?.fontFamilies?.theme
|
||||||
|
);
|
||||||
|
const fontFamilies = allFontChoices.map( ( fontPair ) => {
|
||||||
|
return [
|
||||||
|
...fontPair.map( ( font ) => {
|
||||||
|
return {
|
||||||
|
...font,
|
||||||
|
name: font.fontFamily,
|
||||||
|
};
|
||||||
|
} ),
|
||||||
|
];
|
||||||
|
} );
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{ fontFamilies.map( ( fontPair ) => (
|
||||||
|
<FontFamiliesLoader
|
||||||
|
fontFamilies={ fontPair }
|
||||||
|
key={ fontPair.map( ( font ) => font.slug ).join( '-' ) }
|
||||||
|
preload
|
||||||
|
/>
|
||||||
|
) ) }
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
|
@ -15,14 +15,21 @@ export type FontFamily = {
|
||||||
type Props = {
|
type Props = {
|
||||||
fontFamilies: FontFamily[];
|
fontFamilies: FontFamily[];
|
||||||
onLoad?: () => void;
|
onLoad?: () => void;
|
||||||
|
preload?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
// See https://developers.google.com/fonts/docs/css2
|
// See https://developers.google.com/fonts/docs/css2
|
||||||
const FONT_API_BASE = 'https://fonts-api.wp.com/css2';
|
const FONT_API_BASE = 'https://fonts-api.wp.com/css2';
|
||||||
|
// this is the actual host that the .woff files are at, the above is the one for the .css files with the @font-face declarations
|
||||||
|
const FONT_HOST = 'https://fonts.wp.com'; // used for preconnecting so that fonts can get loaded faster
|
||||||
|
|
||||||
const FONT_AXIS = 'ital,wght@0,400;0,700;1,400;1,700';
|
const FONT_AXIS = 'ital,wght@0,400;0,700;1,400;1,700';
|
||||||
|
|
||||||
export const FontFamiliesLoader = ( { fontFamilies, onLoad }: Props ) => {
|
export const FontFamiliesLoader = ( {
|
||||||
|
fontFamilies,
|
||||||
|
onLoad,
|
||||||
|
preload = false,
|
||||||
|
}: Props ) => {
|
||||||
const params = useMemo(
|
const params = useMemo(
|
||||||
() =>
|
() =>
|
||||||
new URLSearchParams( [
|
new URLSearchParams( [
|
||||||
|
@ -31,8 +38,14 @@ export const FontFamiliesLoader = ( { fontFamilies, onLoad }: Props ) => {
|
||||||
`${ fontFamily }:${ FONT_AXIS }`,
|
`${ fontFamily }:${ FONT_AXIS }`,
|
||||||
] ),
|
] ),
|
||||||
[ 'display', 'swap' ],
|
[ 'display', 'swap' ],
|
||||||
|
[
|
||||||
|
'text', // optimise the fonts fetched by subsetting to only the characters used in the display
|
||||||
|
`${ fontFamilies
|
||||||
|
.map( ( { fontFamily } ) => fontFamily )
|
||||||
|
.join( ' ' ) }`,
|
||||||
|
],
|
||||||
] ),
|
] ),
|
||||||
fontFamilies
|
[ fontFamilies ]
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( ! params.getAll( 'family' ).length ) {
|
if ( ! params.getAll( 'family' ).length ) {
|
||||||
|
@ -40,12 +53,16 @@ export const FontFamiliesLoader = ( { fontFamilies, onLoad }: Props ) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<link
|
<>
|
||||||
rel="stylesheet"
|
{ preload ? <link rel="preconnect" href={ FONT_HOST } /> : null }
|
||||||
type="text/css"
|
<link
|
||||||
href={ `${ FONT_API_BASE }?${ params }` }
|
rel={ preload ? 'preload' : 'stylesheet' }
|
||||||
onLoad={ onLoad }
|
type="text/css"
|
||||||
onError={ onLoad }
|
as={ preload ? 'style' : undefined }
|
||||||
/>
|
href={ `${ FONT_API_BASE }?${ params }` }
|
||||||
|
onLoad={ onLoad }
|
||||||
|
onError={ onLoad }
|
||||||
|
/>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,6 +19,10 @@ export const FontPairing = () => {
|
||||||
columns={ 2 }
|
columns={ 2 }
|
||||||
gap={ 3 }
|
gap={ 3 }
|
||||||
className="woocommerce-customize-store_font-pairing-container"
|
className="woocommerce-customize-store_font-pairing-container"
|
||||||
|
style={ {
|
||||||
|
opacity: 0,
|
||||||
|
animation: 'containerFadeIn 1000ms ease-in-out forwards',
|
||||||
|
} }
|
||||||
>
|
>
|
||||||
{ FONT_PAIRINGS.map( ( variation, index ) => (
|
{ FONT_PAIRINGS.map( ( variation, index ) => (
|
||||||
<VariationContainer key={ index } variation={ variation }>
|
<VariationContainer key={ index } variation={ variation }>
|
||||||
|
|
|
@ -11,7 +11,7 @@ import {
|
||||||
__experimentalVStack as VStack,
|
__experimentalVStack as VStack,
|
||||||
} from '@wordpress/components';
|
} from '@wordpress/components';
|
||||||
import { useResizeObserver, useViewportMatch } from '@wordpress/compose';
|
import { useResizeObserver, useViewportMatch } from '@wordpress/compose';
|
||||||
import { useMemo, useState } from '@wordpress/element';
|
import { useMemo } from '@wordpress/element';
|
||||||
import {
|
import {
|
||||||
privateApis as blockEditorPrivateApis,
|
privateApis as blockEditorPrivateApis,
|
||||||
// @ts-ignore no types exist yet.
|
// @ts-ignore no types exist yet.
|
||||||
|
@ -19,13 +19,12 @@ import {
|
||||||
// @ts-ignore No types for this exist yet.
|
// @ts-ignore No types for this exist yet.
|
||||||
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
||||||
import { GlobalStylesVariationIframe } from '../global-styles-variation-iframe';
|
import { GlobalStylesVariationIframe } from '../global-styles-variation-iframe';
|
||||||
import { FontFamiliesLoader, FontFamily } from './font-families-loader';
|
import { FontFamily } from './font-families-loader';
|
||||||
import {
|
import {
|
||||||
FONT_PREVIEW_LARGE_WIDTH,
|
FONT_PREVIEW_LARGE_WIDTH,
|
||||||
FONT_PREVIEW_LARGE_HEIGHT,
|
FONT_PREVIEW_LARGE_HEIGHT,
|
||||||
FONT_PREVIEW_WIDTH,
|
FONT_PREVIEW_WIDTH,
|
||||||
FONT_PREVIEW_HEIGHT,
|
FONT_PREVIEW_HEIGHT,
|
||||||
SYSTEM_FONT_SLUG,
|
|
||||||
} from './constants';
|
} from './constants';
|
||||||
|
|
||||||
const { useGlobalStyle, useGlobalSetting } = unlock( blockEditorPrivateApis );
|
const { useGlobalStyle, useGlobalSetting } = unlock( blockEditorPrivateApis );
|
||||||
|
@ -75,10 +74,6 @@ export const FontPairingVariationPreview = () => {
|
||||||
: FONT_PREVIEW_HEIGHT;
|
: FONT_PREVIEW_HEIGHT;
|
||||||
const ratio = width ? width / defaultWidth : 1;
|
const ratio = width ? width / defaultWidth : 1;
|
||||||
const normalizedHeight = Math.ceil( defaultHeight * ratio );
|
const normalizedHeight = Math.ceil( defaultHeight * ratio );
|
||||||
const externalFontFamilies = fontFamilies.filter(
|
|
||||||
( { slug } ) => slug !== SYSTEM_FONT_SLUG
|
|
||||||
);
|
|
||||||
const [ isLoaded, setIsLoaded ] = useState( ! externalFontFamilies.length );
|
|
||||||
const getFontFamilyName = ( targetFontFamily: string ) => {
|
const getFontFamilyName = ( targetFontFamily: string ) => {
|
||||||
const fontFamily = fontFamilies.find(
|
const fontFamily = fontFamilies.find(
|
||||||
( { fontFamily: _fontFamily } ) => _fontFamily === targetFontFamily
|
( { fontFamily: _fontFamily } ) => _fontFamily === targetFontFamily
|
||||||
|
@ -96,8 +91,6 @@ export const FontPairingVariationPreview = () => {
|
||||||
[ headingFontFamily, fontFamilies ]
|
[ headingFontFamily, fontFamilies ]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleOnLoad = () => setIsLoaded( true );
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GlobalStylesVariationIframe
|
<GlobalStylesVariationIframe
|
||||||
width={ width }
|
width={ width }
|
||||||
|
@ -118,7 +111,7 @@ export const FontPairingVariationPreview = () => {
|
||||||
style={ {
|
style={ {
|
||||||
height: '100%',
|
height: '100%',
|
||||||
overflow: 'hidden',
|
overflow: 'hidden',
|
||||||
opacity: isLoaded ? 1 : 0,
|
opacity: 1,
|
||||||
} }
|
} }
|
||||||
>
|
>
|
||||||
<HStack
|
<HStack
|
||||||
|
@ -166,10 +159,6 @@ export const FontPairingVariationPreview = () => {
|
||||||
</HStack>
|
</HStack>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<FontFamiliesLoader
|
|
||||||
fontFamilies={ externalFontFamilies }
|
|
||||||
onLoad={ handleOnLoad }
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
</GlobalStylesVariationIframe>
|
</GlobalStylesVariationIframe>
|
||||||
);
|
);
|
||||||
|
|
|
@ -39,7 +39,13 @@ const SidebarNavigationScreenColorPaletteContent = () => {
|
||||||
// rendering the iframe. Without this, the iframe previews will not render
|
// rendering the iframe. Without this, the iframe previews will not render
|
||||||
// in mobile viewport sizes, where the editor canvas is hidden.
|
// in mobile viewport sizes, where the editor canvas is hidden.
|
||||||
return (
|
return (
|
||||||
<div className="woocommerce-customize-store_sidebar-color-content">
|
<div
|
||||||
|
className="woocommerce-customize-store_sidebar-color-content"
|
||||||
|
style={ {
|
||||||
|
opacity: 0,
|
||||||
|
animation: 'containerFadeIn 1000ms ease-in-out forwards',
|
||||||
|
} }
|
||||||
|
>
|
||||||
<BlockEditorProvider
|
<BlockEditorProvider
|
||||||
settings={ storedSettings }
|
settings={ storedSettings }
|
||||||
onChange={ noop }
|
onChange={ noop }
|
||||||
|
|
|
@ -43,6 +43,16 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@keyframes containerFadeIn {
|
||||||
|
0%,
|
||||||
|
80% {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.woocommerce-profile-wizard__step-assemblerHub {
|
.woocommerce-profile-wizard__step-assemblerHub {
|
||||||
a {
|
a {
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: fix
|
||||||
|
|
||||||
|
CYS: Optimised loading and animation of font variation containers
|
Loading…
Reference in New Issue