Skip to content

Commit 162ec39

Browse files
authored
Show pseudo state global styles on canvas when "Show state on canvas" is active (#78190)
* Show global styles on the canvas for pseudo states when Show state on canvas is active * Use existing merge helper * Improve accuracy of useMemo deps
1 parent 1011829 commit 162ec39

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

‎packages/block-editor/src/hooks/style.js‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
} from '@wordpress/components';
99
import { __ } from '@wordpress/i18n';
1010
import { addFilter } from '@wordpress/hooks';
11+
import { useSelect } from '@wordpress/data';
12+
import { mergeGlobalStyles } from '@wordpress/global-styles-engine';
1113
import {
1214
getBlockSupport,
1315
hasBlockSupport,
@@ -47,6 +49,8 @@ import { buildStateSelector, buildCanvasStateSelector } from './state-utils';
4749
import { BlockInspectorPreTabsFill } from '../components/block-inspector/inspector-pre-tabs-slot-fill';
4850
import { scopeSelector } from '../components/global-styles/utils';
4951
import { useBlockEditingMode } from '../components/block-editing-mode';
52+
import { store as blockEditorStore } from '../store';
53+
import { globalStylesDataKey } from '../store/private-keys';
5054

5155
const styleSupportKeys = [
5256
...TYPOGRAPHY_SUPPORT_KEYS,
@@ -347,6 +351,14 @@ function BlockStyleControls( {
347351
const blockEditingMode = useBlockEditingMode();
348352
const [ selectedState, setSelectedState ] = useState( 'default' );
349353
const [ showStateOnCanvas, setShowStateOnCanvas ] = useState( true );
354+
const globalBlockStyles = useSelect(
355+
( select ) =>
356+
select( blockEditorStore ).getSettings()[ globalStylesDataKey ]
357+
?.blocks?.[ name ],
358+
[ name ]
359+
);
360+
const globalStateValue = globalBlockStyles?.[ selectedState ];
361+
const instanceStateValue = style?.[ selectedState ];
350362

351363
// Inject state styles onto the editor canvas so the selected state is
352364
// visible while editing. Scoped to this block instance via data-block so
@@ -356,16 +368,34 @@ function BlockStyleControls( {
356368
if ( ! showStateOnCanvas || selectedState === 'default' ) {
357369
return undefined;
358370
}
359-
const stateValue = style?.[ selectedState ];
360-
if ( ! stateValue ) {
371+
let stateValue;
372+
373+
if ( globalStateValue && instanceStateValue ) {
374+
stateValue = mergeGlobalStyles(
375+
globalStateValue,
376+
instanceStateValue
377+
);
378+
} else if ( instanceStateValue ) {
379+
stateValue = instanceStateValue;
380+
} else if ( globalStateValue ) {
381+
stateValue = globalStateValue;
382+
} else {
361383
return undefined;
362384
}
385+
363386
const selector = buildCanvasStateSelector( clientId, name );
364387
const css = compileCSS( stateValue, { selector } );
365388
// Use !important to override utility classes (e.g. has-accent-3-color)
366389
// that the block's default color support generates with !important.
367390
return css ? css.replace( /;/g, ' !important;' ) : undefined;
368-
}, [ showStateOnCanvas, selectedState, style, clientId, name ] );
391+
}, [
392+
showStateOnCanvas,
393+
selectedState,
394+
globalStateValue,
395+
instanceStateValue,
396+
clientId,
397+
name,
398+
] );
369399
useStyleOverride( { css: canvasStateCSS } );
370400

371401
if ( blockEditingMode !== 'default' ) {

0 commit comments

Comments
 (0)