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 13, 2023
1 parent 9c6c637 commit ffcb60c
Showing 1 changed file with 20 additions and 6 deletions.
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 ffcb60c

Please sign in to comment.