From 107e0c50c42a09db3e79fed04b6c389cf307ac95 Mon Sep 17 00:00:00 2001 From: Ramon Date: Mon, 27 May 2024 15:46:18 +1000 Subject: [PATCH] Background image: add functionality to remove a theme's default, site-wide background image in the editor by settings the value to `background-image: none` (#61998) Co-authored-by: ramonjd Co-authored-by: tellthemachines --- .../global-styles/background-panel.js | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/block-editor/src/components/global-styles/background-panel.js b/packages/block-editor/src/components/global-styles/background-panel.js index b8c3b6358ab5f3..2e73f52564362e 100644 --- a/packages/block-editor/src/components/global-styles/background-panel.js +++ b/packages/block-editor/src/components/global-styles/background-panel.js @@ -83,6 +83,8 @@ export function hasBackgroundSizeValue( style ) { export function hasBackgroundImageValue( style ) { return ( !! style?.background?.backgroundImage?.id || + // Supports url() string values in theme.json. + 'string' === typeof style?.background?.backgroundImage || !! style?.background?.backgroundImage?.url ); } @@ -279,6 +281,23 @@ function BackgroundImageToolsPanelItem( { const hasValue = hasBackgroundImageValue( style ); + const closeAndFocus = () => { + const [ toggleButton ] = focus.tabbable.find( + replaceContainerRef.current + ); + // Focus the toggle button and close the dropdown menu. + // This ensures similar behaviour as to selecting an image, where the dropdown is + // closed and focus is redirected to the dropdown toggle button. + toggleButton?.focus(); + toggleButton?.click(); + }; + + const onRemove = () => + onChange( + setImmutably( style, [ 'background', 'backgroundImage' ], 'none' ) + ); + const canRemove = ! hasValue && hasBackgroundImageValue( inheritedValue ); + return ( + { canRemove && ( + { + closeAndFocus(); + onRemove(); + } } + > + { __( 'Remove' ) } + + ) } { hasValue && ( { - const [ toggleButton ] = focus.tabbable.find( - replaceContainerRef.current - ); - // Focus the toggle button and close the dropdown menu. - // This ensures similar behaviour as to selecting an image, where the dropdown is - // closed and focus is redirected to the dropdown toggle button. - toggleButton?.focus(); - toggleButton?.click(); + closeAndFocus(); resetBackgroundImage(); } } >