Skip to content

Commit 9a302fa

Browse files
committed
Restore instanceId and avoid passing id to useStyleOverride
1 parent 7060c16 commit 9a302fa

File tree

6 files changed

+45
-61
lines changed

6 files changed

+45
-61
lines changed

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

+23-36
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
} from '@wordpress/blocks';
1616
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
1717
import { addFilter } from '@wordpress/hooks';
18-
import { useMemo, useEffect } from '@wordpress/element';
19-
import { useDispatch } from '@wordpress/data';
18+
import { useMemo } from '@wordpress/element';
2019

2120
/**
2221
* Internal dependencies
@@ -34,11 +33,9 @@ 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';
40-
import { store as blockEditorStore } from '../store';
41-
import { unlock } from '../lock-unlock';
4239

4340
const EMPTY_ARRAY = [];
4441

@@ -274,38 +271,28 @@ function DuotoneStyles( {
274271

275272
const isValidFilter = Array.isArray( colors ) || colors === 'unset';
276273

277-
const { setStyleOverride, deleteStyleOverride } = unlock(
278-
useDispatch( blockEditorStore )
274+
useStyleOverride(
275+
isValidFilter
276+
? {
277+
css:
278+
colors !== 'unset'
279+
? getDuotoneStylesheet( selector, filterId )
280+
: getDuotoneUnsetStylesheet( selector ),
281+
__unstableType: 'presets',
282+
}
283+
: undefined
284+
);
285+
useStyleOverride(
286+
isValidFilter
287+
? {
288+
assets:
289+
colors !== 'unset'
290+
? getDuotoneFilter( filterId, colors )
291+
: '',
292+
__unstableType: 'svgs',
293+
}
294+
: undefined
279295
);
280-
281-
useEffect( () => {
282-
if ( ! isValidFilter ) return;
283-
284-
setStyleOverride( filterId, {
285-
css:
286-
colors !== 'unset'
287-
? getDuotoneStylesheet( selector, filterId )
288-
: getDuotoneUnsetStylesheet( selector ),
289-
__unstableType: 'presets',
290-
} );
291-
setStyleOverride( `duotone-${ filterId }`, {
292-
assets:
293-
colors !== 'unset' ? getDuotoneFilter( filterId, colors ) : '',
294-
__unstableType: 'svgs',
295-
} );
296-
297-
return () => {
298-
deleteStyleOverride( filterId );
299-
deleteStyleOverride( `duotone-${ filterId }` );
300-
};
301-
}, [
302-
isValidFilter,
303-
colors,
304-
selector,
305-
filterId,
306-
setStyleOverride,
307-
deleteStyleOverride,
308-
] );
309296

310297
return null;
311298
}

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
/**
@@ -359,7 +359,7 @@ export const withPositionStyles = createHigherOrderComponent(
359359
const allowPositionStyles =
360360
hasPositionBlockSupport && ! isPositionDisabled;
361361

362-
const id = useId();
362+
const id = useInstanceId( BlockListBlock );
363363

364364
// Higher specificity to override defaults in editor UI.
365365
const positionSelector = `.wp-container-${ id }.wp-container-${ id }`;
@@ -383,7 +383,7 @@ export const withPositionStyles = createHigherOrderComponent(
383383
!! attributes?.style?.position?.type,
384384
} );
385385

386-
useStyleOverride( { id, css } );
386+
useStyleOverride( { css } );
387387

388388
return <BlockListBlock { ...props } className={ className } />;
389389
},

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)