Skip to content

Commit

Permalink
Background image: add functionality to remove a theme's default, site…
Browse files Browse the repository at this point in the history
…-wide background image in the editor by settings the value to `background-image: none` (WordPress#61998)

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org>
  • Loading branch information
3 people authored and patil-vipul committed Jun 17, 2024
1 parent e1df0a7 commit 107e0c5
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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 (
<ToolsPanelItem
className="single-column"
Expand Down Expand Up @@ -311,17 +330,20 @@ function BackgroundImageToolsPanelItem( {
}
variant="secondary"
>
{ canRemove && (
<MenuItem
onClick={ () => {
closeAndFocus();
onRemove();
} }
>
{ __( 'Remove' ) }
</MenuItem>
) }
{ hasValue && (
<MenuItem
onClick={ () => {
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();
} }
>
Expand Down

0 comments on commit 107e0c5

Please sign in to comment.