Skip to content

Commit c6293ac

Browse files
ellatrixandrewserong
authored andcommitted
Restore instanceId and avoid passing id to useStyleOverride
1 parent b9e897b commit c6293ac

File tree

6 files changed

+45
-58
lines changed

6 files changed

+45
-58
lines changed

packages/block-editor/src/hooks/duotone.js

+23-33
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
1717
import { addFilter } from '@wordpress/hooks';
1818
import { useMemo, useEffect } from '@wordpress/element';
19-
import { useDispatch } from '@wordpress/data';
2019

2120
/**
2221
* Internal dependencies
@@ -34,12 +33,10 @@ import {
3433
} from '../components/duotone/utils';
3534
import { getBlockCSSSelector } from '../components/global-styles/get-block-css-selector';
3635
import { scopeSelector } from '../components/global-styles/utils';
37-
import { useBlockSettings } from './utils';
36+
import { useBlockSettings, useStyleOverride } from './utils';
3837
import { default as StylesFiltersPanel } from '../components/global-styles/filters-panel';
3938
import { useBlockEditingMode } from '../components/block-editing-mode';
4039
import { __unstableUseBlockElement as useBlockElement } from '../components/block-list/use-block-props/use-block-refs';
41-
import { store as blockEditorStore } from '../store';
42-
import { unlock } from '../lock-unlock';
4340

4441
const EMPTY_ARRAY = [];
4542

@@ -291,28 +288,34 @@ function DuotoneStyles( {
291288

292289
const isValidFilter = Array.isArray( colors ) || colors === 'unset';
293290

294-
const { setStyleOverride, deleteStyleOverride } = unlock(
295-
useDispatch( blockEditorStore )
291+
useStyleOverride(
292+
isValidFilter
293+
? {
294+
css:
295+
colors !== 'unset'
296+
? getDuotoneStylesheet( selector, filterId )
297+
: getDuotoneUnsetStylesheet( selector ),
298+
__unstableType: 'presets',
299+
}
300+
: undefined
301+
);
302+
useStyleOverride(
303+
isValidFilter
304+
? {
305+
assets:
306+
colors !== 'unset'
307+
? getDuotoneFilter( filterId, colors )
308+
: '',
309+
__unstableType: 'svgs',
310+
}
311+
: undefined
296312
);
297313

298314
const blockElement = useBlockElement( clientId );
299315

300316
useEffect( () => {
301317
if ( ! isValidFilter ) return;
302318

303-
setStyleOverride( filterId, {
304-
css:
305-
colors !== 'unset'
306-
? getDuotoneStylesheet( selector, filterId )
307-
: getDuotoneUnsetStylesheet( selector ),
308-
__unstableType: 'presets',
309-
} );
310-
setStyleOverride( `duotone-${ filterId }`, {
311-
assets:
312-
colors !== 'unset' ? getDuotoneFilter( filterId, colors ) : '',
313-
__unstableType: 'svgs',
314-
} );
315-
316319
// Safari does not always update the duotone filter when the duotone colors
317320
// are changed. When using Safari, force the block element to be repainted by
318321
// the browser to ensure any changes are reflected visually. This logic matches
@@ -329,20 +332,7 @@ function DuotoneStyles( {
329332
blockElement.offsetHeight;
330333
blockElement.style.display = display;
331334
}
332-
333-
return () => {
334-
deleteStyleOverride( filterId );
335-
deleteStyleOverride( `duotone-${ filterId }` );
336-
};
337-
}, [
338-
isValidFilter,
339-
blockElement,
340-
colors,
341-
selector,
342-
filterId,
343-
setStyleOverride,
344-
deleteStyleOverride,
345-
] );
335+
}, [ isValidFilter, blockElement ] );
346336

347337
return null;
348338
}

packages/block-editor/src/hooks/layout.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import classnames from 'classnames';
66
/**
77
* WordPress dependencies
88
*/
9-
import { createHigherOrderComponent } from '@wordpress/compose';
9+
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
1010
import { addFilter } from '@wordpress/hooks';
1111
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
1212
import { useSelect } from '@wordpress/data';
@@ -17,7 +17,6 @@ import {
1717
PanelBody,
1818
} from '@wordpress/components';
1919
import { __ } from '@wordpress/i18n';
20-
import { useId } from '@wordpress/element';
2120

2221
/**
2322
* Internal dependencies
@@ -361,7 +360,7 @@ export const withLayoutStyles = createHigherOrderComponent(
361360
} );
362361
const shouldRenderLayoutStyles =
363362
blockSupportsLayout && ! disableLayoutStyles;
364-
const id = useId();
363+
const id = useInstanceId( BlockListBlock );
365364
const { layout } = attributes;
366365
const { default: defaultBlockLayout } =
367366
getBlockSupport( name, layoutBlockSupportKey ) || {};
@@ -433,7 +432,7 @@ export const withChildLayoutStyles = createHigherOrderComponent(
433432
const shouldRenderChildLayoutStyles =
434433
hasChildLayout && ! disableLayoutStyles;
435434

436-
const id = useId();
435+
const id = useInstanceId( BlockListBlock );
437436
const selector = `.wp-container-content-${ id }`;
438437

439438
let css = '';
@@ -455,7 +454,7 @@ export const withChildLayoutStyles = createHigherOrderComponent(
455454
shouldRenderChildLayoutStyles && !! css, // Only attach a container class if there is generated CSS to be attached.
456455
} );
457456

458-
useStyleOverride( { id, css } );
457+
useStyleOverride( { css } );
459458

460459
return <BlockListBlock { ...props } className={ className } />;
461460
},

packages/block-editor/src/hooks/position.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
BaseControl,
1313
privateApis as componentsPrivateApis,
1414
} from '@wordpress/components';
15-
import { createHigherOrderComponent } from '@wordpress/compose';
15+
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
1616
import { useSelect } from '@wordpress/data';
17-
import { useMemo, Platform, useId } from '@wordpress/element';
17+
import { useMemo, Platform } from '@wordpress/element';
1818
import { addFilter } from '@wordpress/hooks';
1919

2020
/**
@@ -361,7 +361,7 @@ export const withPositionStyles = createHigherOrderComponent(
361361
const allowPositionStyles =
362362
hasPositionBlockSupport && ! isPositionDisabled;
363363

364-
const id = useId();
364+
const id = useInstanceId( BlockListBlock );
365365

366366
// Higher specificity to override defaults in editor UI.
367367
const positionSelector = `.wp-container-${ id }.wp-container-${ id }`;
@@ -385,7 +385,7 @@ export const withPositionStyles = createHigherOrderComponent(
385385
!! attributes?.style?.position?.type,
386386
} );
387387

388-
useStyleOverride( { id, css } );
388+
useStyleOverride( { css } );
389389

390390
return <BlockListBlock { ...props } className={ className } />;
391391
},

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import classnames from 'classnames';
66
/**
77
* WordPress dependencies
88
*/
9-
import { useMemo, useId } from '@wordpress/element';
9+
import { useMemo } from '@wordpress/element';
1010
import { addFilter } from '@wordpress/hooks';
1111
import {
1212
getBlockSupport,
1313
hasBlockSupport,
1414
__EXPERIMENTAL_ELEMENTS as ELEMENTS,
1515
} from '@wordpress/blocks';
16-
import { createHigherOrderComponent } from '@wordpress/compose';
16+
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
1717
import { getCSSRules, compileCSS } from '@wordpress/style-engine';
1818

1919
/**
@@ -400,7 +400,9 @@ const elementTypes = [
400400
*/
401401
const withElementsStyles = createHigherOrderComponent(
402402
( BlockListBlock ) => ( props ) => {
403-
const blockElementsContainerIdentifier = `wp-elements-${ useId() }`;
403+
const blockElementsContainerIdentifier = `wp-elements-${ useInstanceId(
404+
BlockListBlock
405+
) }`;
404406

405407
// The .editor-styles-wrapper selector is required on elements styles. As it is
406408
// added to all other editor styles, not providing it causes reset and global
@@ -481,10 +483,7 @@ const withElementsStyles = createHigherOrderComponent(
481483
: undefined;
482484
}, [ baseElementSelector, blockElementStyles, props.name ] );
483485

484-
useStyleOverride( {
485-
id: blockElementsContainerIdentifier,
486-
css: styles,
487-
} );
486+
useStyleOverride( { css: styles } );
488487

489488
return (
490489
<BlockListBlock

packages/block-editor/src/hooks/utils.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* WordPress dependencies
33
*/
44
import { getBlockSupport } from '@wordpress/blocks';
5-
import { useMemo, useEffect } from '@wordpress/element';
5+
import { useMemo, useEffect, useId } from '@wordpress/element';
66
import { useDispatch } from '@wordpress/data';
77

88
/**
@@ -118,17 +118,19 @@ export function shouldSkipSerialization( blockType, featureSet, feature ) {
118118
return skipSerialization;
119119
}
120120

121-
export function useStyleOverride( { id, css } ) {
121+
export function useStyleOverride( style ) {
122122
const { setStyleOverride, deleteStyleOverride } = unlock(
123123
useDispatch( blockEditorStore )
124124
);
125+
const fallbackId = useId();
125126
useEffect( () => {
126-
if ( ! id || ! css ) return;
127-
setStyleOverride( id, { css } );
127+
if ( ! style ) return;
128+
const id = style.id || fallbackId;
129+
setStyleOverride( id, style );
128130
return () => {
129131
deleteStyleOverride( id );
130132
};
131-
}, [ id, css, setStyleOverride, deleteStyleOverride ] );
133+
}, [ fallbackId, style, setStyleOverride, deleteStyleOverride ] );
132134
}
133135

134136
/**

packages/block-library/src/gallery/gap-styles.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ export default function GapStyles( { blockGap, clientId } ) {
4040
gap: ${ gapValue }
4141
}`;
4242

43-
useStyleOverride( {
44-
id: `gallery-gap-${ clientId }`,
45-
css: gap,
46-
} );
43+
useStyleOverride( { css: gap } );
4744

4845
return null;
4946
}

0 commit comments

Comments
 (0)