From 8ba1ae20c98258bfc9ff13b3a19cee7879025334 Mon Sep 17 00:00:00 2001 From: ramon Date: Mon, 24 Jun 2024 19:49:15 +1000 Subject: [PATCH] Fetching overrides in the hook --- .../src/hooks/block-style-variation.js | 17 +++++++++-------- .../edit-site/src/components/revisions/index.js | 16 +++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/block-editor/src/hooks/block-style-variation.js b/packages/block-editor/src/hooks/block-style-variation.js index fd180d5516ea56..3ffa903a8a3d8f 100644 --- a/packages/block-editor/src/hooks/block-style-variation.js +++ b/packages/block-editor/src/hooks/block-style-variation.js @@ -16,6 +16,7 @@ import { import { useStyleOverride } from './utils'; import { store as blockEditorStore } from '../store'; import { globalStylesDataKey } from '../store/private-keys'; +import { unlock } from '../lock-unlock'; const VARIATION_PREFIX = 'is-style-'; @@ -64,20 +65,20 @@ function getVariationNameFromClass( className, registeredStyles = [] ) { * based on an incoming theme config. If a matching style is found in the config, * a new override is created and returned. * - * @param {Object} config A global styles object, containing settings and styles. - * @param {Array} variationOverrides An array of existing block variation overrides. + * @param {Object} config A global styles object, containing settings and styles. * @return {Array} An array of new block variation overrides. */ -export function useUpdateBlockVariationOverridesWithConfig( - config, - variationOverrides = [] -) { +export function useUpdateBlockVariationOverridesWithConfig( config ) { + const overrides = useSelect( + ( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(), + [] + ); const { getBlockStyles } = useSelect( blocksStore ); const { getBlockName } = useSelect( blockEditorStore ); return useMemo( () => { const newOverrides = []; - for ( const [ id, override ] of variationOverrides ) { + for ( const [ id, override ] of overrides ) { if ( override?.variation && override?.clientId ) { const blockName = getBlockName( override.clientId ); const configStyles = @@ -135,7 +136,7 @@ export function useUpdateBlockVariationOverridesWithConfig( } } return newOverrides; - }, [ config, variationOverrides ] ); + }, [ config, overrides ] ); } function useBlockStyleVariation( name, variation, clientId ) { diff --git a/packages/edit-site/src/components/revisions/index.js b/packages/edit-site/src/components/revisions/index.js index fabaab3f92f2c5..cb210c09e4af43 100644 --- a/packages/edit-site/src/components/revisions/index.js +++ b/packages/edit-site/src/components/revisions/index.js @@ -34,15 +34,8 @@ function isObjectEmpty( object ) { } function RevisionStyles( { styles, config } ) { - const variationOverrides = useSelect( - ( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(), - [] - ); - - const mergedOverrides = useUpdateBlockVariationOverridesWithConfig( - config, - variationOverrides - ); + const mergedOverrides = + useUpdateBlockVariationOverridesWithConfig( config ); return ; } @@ -102,6 +95,11 @@ function Revisions( { userConfig, blocks } ) { settings={ settings } > + { /* + * Styles are printed at the end of the document, + * so they can access any registered style overrides, + * which are only stored after the block list is rendered. + */ }