Skip to content

Commit

Permalink
Create new convenience hook `useCurrentMergeThemeStyleVariationsWithU…
Browse files Browse the repository at this point in the history
…serConfig()`

Replace usage with utils created in #58803
  • Loading branch information
ramonjd authored and scruffian committed Feb 27, 2024
1 parent 13bea45 commit 63b85db
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 143 deletions.
Expand Up @@ -14,7 +14,7 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
*/
import { unlock } from '../../lock-unlock';
import ColorVariations from './variations-color';
import useThemeStyleVariationsByProperty from './use-theme-style-variations-by-property';
import { useCurrentMergeThemeStyleVariationsWithUserConfig } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';

const { useGlobalSetting } = unlock( blockEditorPrivateApis );
const mobilePopoverProps = { placement: 'bottom-start', offset: 8 };
Expand Down Expand Up @@ -47,8 +47,8 @@ export default function ColorPalettePanel( { name } ) {
'color.defaultPalette',
name
);
const colorVariations = useThemeStyleVariationsByProperty( {
styleProperty: 'color',
const colorVariations = useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'color',
filter: ( variation ) =>
variation?.settings?.color &&
Object.keys( variation?.settings?.color ).length,
Expand Down
Expand Up @@ -20,20 +20,22 @@ import { IconWithCurrentColor } from './icon-with-current-color';
import FontFamilies from './font-families';
import ScreenHeader from './header';
import { NavigationButtonAsItem } from './navigation-button';
import useThemeStyleVariationsByProperty from './use-theme-style-variations-by-property';
import { useCurrentMergeThemeStyleVariationsWithUserConfig } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';

function ScreenTypography() {
const fontLibraryEnabled = useSelect(
( select ) =>
select( editorStore ).getEditorSettings().fontLibraryEnabled,
[]
);
const typographyVariations = useThemeStyleVariationsByProperty( {
styleProperty: 'typography',
filter: ( variation ) =>
variation?.settings?.typography?.fontFamilies &&
Object.keys( variation?.settings?.typography?.fontFamilies ).length,
} );
const typographyVariations =
useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'typography',
filter: ( variation ) =>
variation?.settings?.typography?.fontFamilies &&
Object.keys( variation?.settings?.typography?.fontFamilies )
.length,
} );

return (
<>
Expand Down

This file was deleted.

Expand Up @@ -21,7 +21,7 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { mergeBaseAndUserConfigs } from './global-styles-provider';
import { unlock } from '../../lock-unlock';
import { getFamilyPreviewStyle } from './font-library-modal/utils/preview-styles';
import useThemeStyleVariationsByProperty from './use-theme-style-variations-by-property';
import { useCurrentMergeThemeStyleVariationsWithUserConfig } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
import Subtitle from './subtitle';

const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock(
Expand Down Expand Up @@ -166,12 +166,14 @@ function TypographyVariation( { variation } ) {
}

export default function TypographyVariations() {
const typographyVariations = useThemeStyleVariationsByProperty( {
styleProperty: 'typography',
filter: ( variation ) =>
variation?.settings?.typography?.fontFamilies &&
Object.keys( variation?.settings?.typography?.fontFamilies ).length,
} );
const typographyVariations =
useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'typography',
filter: ( variation ) =>
variation?.settings?.typography?.fontFamilies &&
Object.keys( variation?.settings?.typography?.fontFamilies )
.length,
} );

const { base } = useContext( GlobalStylesContext );
const uniqueTypographyVariations = [];
Expand Down
@@ -1,13 +1,47 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useContext, useMemo } from '@wordpress/element';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { mergeBaseAndUserConfigs } from '../../components/global-styles/global-styles-provider';
import cloneDeep from '../../utils/clone-deep';
import { unlock } from '../../lock-unlock';

const { GlobalStylesContext } = unlock( blockEditorPrivateApis );

/**
* Fetches the current theme style variations, filters them by the provided property,
* and merges with current user-defined global style/settings object.
*
* @param {Object} props Object of hook args.
* @param {string} props.property The property to filter by.
* @param {Function} props.filter Optional. The filter function to apply to the variations.
* @return {Object[]|*} The merged object.
*/
export function useCurrentMergeThemeStyleVariationsWithUserConfig( {
property,
filter,
} ) {
const variations = useSelect( ( select ) => {
return select(
coreStore
).__experimentalGetCurrentThemeGlobalStylesVariations();
}, [] );
const { user: baseVariation } = useContext( GlobalStylesContext );

return useThemeStyleVariationsByProperty( {
variations,
property,
filter,
baseVariation,
} );
}

/**
* Returns a new object, with properties specified in `property`,
Expand Down

0 comments on commit 63b85db

Please sign in to comment.