Skip to content

Commit

Permalink
Restore instanceId and avoid passing id to useStyleOverride
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 2, 2023
1 parent 7060c16 commit 9a302fa
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 61 deletions.
59 changes: 23 additions & 36 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
} from '@wordpress/blocks';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { useMemo, useEffect } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -34,11 +33,9 @@ import {
} from '../components/duotone/utils';
import { getBlockCSSSelector } from '../components/global-styles/get-block-css-selector';
import { scopeSelector } from '../components/global-styles/utils';
import { useBlockSettings } from './utils';
import { useBlockSettings, useStyleOverride } from './utils';
import { default as StylesFiltersPanel } from '../components/global-styles/filters-panel';
import { useBlockEditingMode } from '../components/block-editing-mode';
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';

const EMPTY_ARRAY = [];

Expand Down Expand Up @@ -274,38 +271,28 @@ function DuotoneStyles( {

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

const { setStyleOverride, deleteStyleOverride } = unlock(
useDispatch( blockEditorStore )
useStyleOverride(
isValidFilter
? {
css:
colors !== 'unset'
? getDuotoneStylesheet( selector, filterId )
: getDuotoneUnsetStylesheet( selector ),
__unstableType: 'presets',
}
: undefined
);
useStyleOverride(
isValidFilter
? {
assets:
colors !== 'unset'
? getDuotoneFilter( filterId, colors )
: '',
__unstableType: 'svgs',
}
: undefined
);

useEffect( () => {
if ( ! isValidFilter ) return;

setStyleOverride( filterId, {
css:
colors !== 'unset'
? getDuotoneStylesheet( selector, filterId )
: getDuotoneUnsetStylesheet( selector ),
__unstableType: 'presets',
} );
setStyleOverride( `duotone-${ filterId }`, {
assets:
colors !== 'unset' ? getDuotoneFilter( filterId, colors ) : '',
__unstableType: 'svgs',
} );

return () => {
deleteStyleOverride( filterId );
deleteStyleOverride( `duotone-${ filterId }` );
};
}, [
isValidFilter,
colors,
selector,
filterId,
setStyleOverride,
deleteStyleOverride,
] );

return null;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createHigherOrderComponent } from '@wordpress/compose';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
Expand All @@ -17,7 +17,6 @@ import {
PanelBody,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useId } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -361,7 +360,7 @@ export const withLayoutStyles = createHigherOrderComponent(
} );
const shouldRenderLayoutStyles =
blockSupportsLayout && ! disableLayoutStyles;
const id = useId();
const id = useInstanceId( BlockListBlock );
const { layout } = attributes;
const { default: defaultBlockLayout } =
getBlockSupport( name, layoutBlockSupportKey ) || {};
Expand Down Expand Up @@ -433,7 +432,7 @@ export const withChildLayoutStyles = createHigherOrderComponent(
const shouldRenderChildLayoutStyles =
hasChildLayout && ! disableLayoutStyles;

const id = useId();
const id = useInstanceId( BlockListBlock );
const selector = `.wp-container-content-${ id }`;

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

useStyleOverride( { id, css } );
useStyleOverride( { css } );

return <BlockListBlock { ...props } className={ className } />;
},
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/hooks/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
BaseControl,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { createHigherOrderComponent } from '@wordpress/compose';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useMemo, Platform, useId } from '@wordpress/element';
import { useMemo, Platform } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

/**
Expand Down Expand Up @@ -359,7 +359,7 @@ export const withPositionStyles = createHigherOrderComponent(
const allowPositionStyles =
hasPositionBlockSupport && ! isPositionDisabled;

const id = useId();
const id = useInstanceId( BlockListBlock );

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

useStyleOverride( { id, css } );
useStyleOverride( { css } );

return <BlockListBlock { ...props } className={ className } />;
},
Expand Down
13 changes: 6 additions & 7 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useMemo, useId } from '@wordpress/element';
import { useMemo } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import {
getBlockSupport,
hasBlockSupport,
__EXPERIMENTAL_ELEMENTS as ELEMENTS,
} from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { getCSSRules, compileCSS } from '@wordpress/style-engine';

/**
Expand Down Expand Up @@ -400,7 +400,9 @@ const elementTypes = [
*/
const withElementsStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const blockElementsContainerIdentifier = `wp-elements-${ useId() }`;
const blockElementsContainerIdentifier = `wp-elements-${ useInstanceId(
BlockListBlock
) }`;

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

useStyleOverride( {
id: blockElementsContainerIdentifier,
css: styles,
} );
useStyleOverride( { css: styles } );

return (
<BlockListBlock
Expand Down
12 changes: 7 additions & 5 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { getBlockSupport } from '@wordpress/blocks';
import { useMemo, useEffect } from '@wordpress/element';
import { useMemo, useEffect, useId } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';

/**
Expand Down Expand Up @@ -118,17 +118,19 @@ export function shouldSkipSerialization( blockType, featureSet, feature ) {
return skipSerialization;
}

export function useStyleOverride( { id, css } ) {
export function useStyleOverride( style ) {
const { setStyleOverride, deleteStyleOverride } = unlock(
useDispatch( blockEditorStore )
);
const fallbackId = useId();
useEffect( () => {
if ( ! id || ! css ) return;
setStyleOverride( id, { css } );
if ( ! style ) return;
const id = style.id || fallbackId;
setStyleOverride( id, style );
return () => {
deleteStyleOverride( id );
};
}, [ id, css, setStyleOverride, deleteStyleOverride ] );
}, [ fallbackId, style, setStyleOverride, deleteStyleOverride ] );
}

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/block-library/src/gallery/gap-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export default function GapStyles( { blockGap, clientId } ) {
gap: ${ gapValue }
}`;

useStyleOverride( {
id: `gallery-gap-${ clientId }`,
css: gap,
} );
useStyleOverride( { css: gap } );

return null;
}

0 comments on commit 9a302fa

Please sign in to comment.