Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global styles: add option to remove site-wide theme background image #61998

Merged
merged 1 commit into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ||
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so we can detect strings values with url()

	"styles": {
		"background": {
			"backgroundImage": "url('https://images.pexels.com/photos/22484288/pexels-photo-22484288/free-photo-of-the-circular-stone-terraces-of-the-inca-ruins.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2')"
		}
	}

!! style?.background?.backgroundImage?.url
);
}
Expand Down Expand Up @@ -279,6 +281,23 @@ function BackgroundImageToolsPanelItem( {

const hasValue = hasBackgroundImageValue( style );

const closeAndFocus = () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An abstraction since we need this in two places

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' ) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "Remove" clear enough?

It should only appear where there's a theme.json value AND there's no user style defined - basically so users can remove the default theme background image and nothing else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good question. I think visually it's pretty clear when you open the dropdown by clicking on tile with the image name and thumbnail; in context "Remove" could only be related to that image. But a quick test with VoiceOver shows the experience is pretty flaky if you don't have the visual context: you tab past a menu popup button named "Background options" (this is the only clue you get that you're in a section related to background), then you tab into another menu popup button labeled with the the image file name, plus "Selected image: Untitled". That's a wider problem than this PR addresses though, so I'm not suggesting tying to solve it here 😅

Really tools panels were never accessible to begin with, which is the main issue, but perhaps for this particular instance we could improve the experience by switching the order of file name and "Selected image: Untitled", and maybe changing "Selected image" to "Site background image" (I'm not sure where the "Untitled" is coming from? but maybe we could dispense with it altogether?). But given this is an existing issue it should probably be addressed in a follow-up.

</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
Loading