Skip to content

Commit

Permalink
memo style
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and andrewserong committed Nov 9, 2023
1 parent c6293ac commit 7af4ec5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export const withLayoutStyles = createHigherOrderComponent(
layoutClasses
);

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

return (
<BlockListBlock
Expand Down
26 changes: 20 additions & 6 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,33 @@ export function shouldSkipSerialization( blockType, featureSet, feature ) {
return skipSerialization;
}

export function useStyleOverride( style ) {
export function useStyleOverride( { id, css, assets, __unstableType } = {} ) {
const { setStyleOverride, deleteStyleOverride } = unlock(
useDispatch( blockEditorStore )
);
const fallbackId = useId();
useEffect( () => {
if ( ! style ) return;
const id = style.id || fallbackId;
setStyleOverride( id, style );
// Unmount if there is CSS and assets are empty.
if ( ! css && ! assets ) return;
const _id = id || fallbackId;
setStyleOverride( _id, {
id,
css,
assets,
__unstableType,
} );
return () => {
deleteStyleOverride( id );
deleteStyleOverride( _id );
};
}, [ fallbackId, style, setStyleOverride, deleteStyleOverride ] );
}, [
id,
css,
assets,
__unstableType,
fallbackId,
setStyleOverride,
deleteStyleOverride,
] );
}

/**
Expand Down

0 comments on commit 7af4ec5

Please sign in to comment.