From 28384546a4ab3efd87797c5dac0875d9c48cd653 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 15 Mar 2024 13:33:36 +0000 Subject: [PATCH] Global Styles: Try color and typography presets in Site View (#59594) * Global Styles: Try presets in Site View * get typography working in mobile * add border radius * tidy up styles * only output the preset headers if there are presets * Filtering for settings before showing titles. * Remove the filters that might remove variations that are valid * move the unique typography variations to a hook --------- Co-authored-by: ramon Co-authored-by: scruffian Co-authored-by: ramonjd Co-authored-by: jasmussen Co-authored-by: jameskoster --- .../src/components/global-styles/hooks.js | 43 ++++++++++++- .../global-styles/preview-iframe.js | 1 - .../components/global-styles/screen-colors.js | 2 +- .../src/components/global-styles/style.scss | 5 +- .../global-styles/typography-example.js | 2 - .../variations/variations-typography.js | 64 ++++++------------- .../index.js | 38 ++++++++++- .../sidebar-navigation-screen/style.scss | 11 +++- 8 files changed, 111 insertions(+), 55 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/hooks.js b/packages/edit-site/src/components/global-styles/hooks.js index 123bda7497320..97c71afa456f8 100644 --- a/packages/edit-site/src/components/global-styles/hooks.js +++ b/packages/edit-site/src/components/global-styles/hooks.js @@ -9,14 +9,20 @@ import a11yPlugin from 'colord/plugins/a11y'; */ import { store as blocksStore } from '@wordpress/blocks'; import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; +import { useContext } from '@wordpress/element'; /** * Internal dependencies */ +import { mergeBaseAndUserConfigs } from './global-styles-provider'; +import { useCurrentMergeThemeStyleVariationsWithUserConfig } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property'; +import { getFontFamilies } from './utils'; import { unlock } from '../../lock-unlock'; import { useSelect } from '@wordpress/data'; -const { useGlobalSetting, useGlobalStyle } = unlock( blockEditorPrivateApis ); +const { useGlobalSetting, useGlobalStyle, GlobalStylesContext } = unlock( + blockEditorPrivateApis +); // Enable colord's a11y plugin. extend( [ a11yPlugin ] ); @@ -92,3 +98,38 @@ export function useSupportedStyles( name, element ) { return supportedPanels; } + +export function useUniqueTypographyVariations() { + const typographyVariations = + useCurrentMergeThemeStyleVariationsWithUserConfig( { + property: 'typography', + } ); + + const { base } = useContext( GlobalStylesContext ); + /* + * Filter duplicate variations based on the font families used in the variation. + */ + return typographyVariations?.length + ? Object.values( + typographyVariations.reduce( ( acc, variation ) => { + const [ bodyFontFamily, headingFontFamily ] = + getFontFamilies( + mergeBaseAndUserConfigs( base, variation ) + ); + if ( + headingFontFamily?.name && + bodyFontFamily?.name && + ! acc[ + `${ headingFontFamily?.name }:${ bodyFontFamily?.name }` + ] + ) { + acc[ + `${ headingFontFamily?.name }:${ bodyFontFamily?.name }` + ] = variation; + } + + return acc; + }, {} ) + ) + : []; +} diff --git a/packages/edit-site/src/components/global-styles/preview-iframe.js b/packages/edit-site/src/components/global-styles/preview-iframe.js index ea4207e35a113..e830accf6d939 100644 --- a/packages/edit-site/src/components/global-styles/preview-iframe.js +++ b/packages/edit-site/src/components/global-styles/preview-iframe.js @@ -118,7 +118,6 @@ export default function PreviewIframe( {