-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Global styles: block background UI controls #60100
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
Changes from all commits
3a99461
72ffc2f
a686680
7988ba5
f000836
541ff26
a804a8c
ef149cc
5aba906
47a389a
d040811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/6836 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/60100 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -805,6 +805,7 @@ public static function get_style_variations( $scope = 'theme' ) { | |
| * as the value of `_link` object in REST API responses. | ||
| * | ||
| * @since 6.6.0 | ||
| * @since 6.7.0 Added support for resolving block styles. | ||
| * | ||
| * @param WP_Theme_JSON_Gutenberg $theme_json A theme json instance. | ||
| * @return array An array of resolved paths. | ||
|
|
@@ -818,10 +819,11 @@ public static function get_resolved_theme_uris( $theme_json ) { | |
|
|
||
| $theme_json_data = $theme_json->get_raw_data(); | ||
|
|
||
| // Top level styles. | ||
| $background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null; | ||
| // Using the same file convention when registering web fonts. See: WP_Font_Face_Resolver:: to_theme_file_uri. | ||
| $placeholder = 'file:./'; | ||
|
|
||
| // Top level styles. | ||
| $background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null; | ||
| if ( | ||
| isset( $background_image_url ) && | ||
| is_string( $background_image_url ) && | ||
|
|
@@ -840,6 +842,33 @@ public static function get_resolved_theme_uris( $theme_json ) { | |
| $resolved_theme_uris[] = $resolved_theme_uri; | ||
| } | ||
|
|
||
| // Block styles. | ||
| if ( ! empty( $theme_json_data['styles']['blocks'] ) ) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should abstract this logic. There'll be elements and other things later I imagine. Also add unit tests to cover this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What did you have in mind for the abstraction? A separate method, or function call to another file? Just curious how this part of the feature might evolve 🙂
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe once (if?) we support background images for elements, the whole
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point. Let's cross that bridge when we come to it.
I haven't really thought about it, but the obvious target would be around building the resolved array items. So the if block starting from The future will involve target specific styles — top-level, blocks, elements and, eventually, variations, the latter two requiring possibly further nested loops. Maybe once elements comes along it'll be clearer how the abstraction should look. |
||
| foreach ( $theme_json_data['styles']['blocks'] as $block_name => $block_styles ) { | ||
| if ( ! isset( $block_styles['background']['backgroundImage']['url'] ) ) { | ||
| continue; | ||
| } | ||
| $background_image_url = $block_styles['background']['backgroundImage']['url'] ?? null; | ||
| if ( | ||
| isset( $background_image_url ) && | ||
| is_string( $background_image_url ) && | ||
| // Skip if the src doesn't start with the placeholder, as there's nothing to replace. | ||
| str_starts_with( $background_image_url, $placeholder ) ) { | ||
| $file_type = wp_check_filetype( $background_image_url ); | ||
| $src_url = str_replace( $placeholder, '', $background_image_url ); | ||
| $resolved_theme_uri = array( | ||
| 'name' => $background_image_url, | ||
| 'href' => sanitize_url( get_theme_file_uri( $src_url ) ), | ||
| 'target' => "styles.blocks.{$block_name}.background.backgroundImage.url", | ||
| ); | ||
| if ( isset( $file_type['type'] ) ) { | ||
| $resolved_theme_uri['type'] = $file_type['type']; | ||
| } | ||
| $resolved_theme_uris[] = $resolved_theme_uri; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $resolved_theme_uris; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ import { | |
| useHasBackgroundPanel, | ||
| hasBackgroundImageValue, | ||
| } from '../components/global-styles/background-panel'; | ||
| import { | ||
| globalStylesDataKey, | ||
| globalStylesLinksDataKey, | ||
| } from '../store/private-keys'; | ||
|
|
||
| export const BACKGROUND_SUPPORT_KEY = 'background'; | ||
|
|
||
|
|
@@ -134,10 +138,25 @@ export function BackgroundImagePanel( { | |
| setAttributes, | ||
| settings, | ||
| } ) { | ||
| const style = useSelect( | ||
| ( select ) => | ||
| select( blockEditorStore ).getBlockAttributes( clientId )?.style, | ||
| [ clientId ] | ||
| const { style, inheritedValue, _links } = useSelect( | ||
| ( select ) => { | ||
| const { getBlockAttributes, getSettings } = | ||
| select( blockEditorStore ); | ||
| const _settings = getSettings(); | ||
| return { | ||
| style: getBlockAttributes( clientId )?.style, | ||
| _links: _settings[ globalStylesLinksDataKey ], | ||
| /* | ||
| * @TODO 1. Pass inherited value down to all block style controls, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably doesn't need to happen any time soon, but might be useful for other block controls I imagine? |
||
| * See: packages/block-editor/src/hooks/style.js | ||
| * @TODO 2. Add support for block style variations, | ||
| * See implementation: packages/block-editor/src/hooks/block-style-variation.js | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if a variation has been applied to a block, we need to parse it here. This needs to happen in a follow up. I've added a note to #54336 cc @aaronrobertshaw just in case there are gotchas (TL;DR - I'm trying to pass down the theme styles applied to a block to the controls, so that the controls are aware of any defaults)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, at a glance, we'd need to detect which, if any, variation has been applied to the block, then merge the variation's data over the block type's data. The resulting set would tell us which value the control should get. |
||
| */ | ||
| inheritedValue: | ||
| _settings[ globalStylesDataKey ]?.blocks?.[ name ], | ||
| }; | ||
| }, | ||
| [ clientId, name ] | ||
| ); | ||
|
|
||
| if ( | ||
|
|
@@ -165,12 +184,14 @@ export function BackgroundImagePanel( { | |
|
|
||
| return ( | ||
| <StylesBackgroundPanel | ||
| inheritedValue={ inheritedValue } | ||
| as={ BackgroundInspectorControl } | ||
| panelId={ clientId } | ||
| defaultValues={ BACKGROUND_DEFAULT_VALUES } | ||
| settings={ updatedSettings } | ||
| onChange={ onChange } | ||
| value={ style } | ||
| themeFileURIs={ _links?.[ 'wp:theme-file' ] } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the magic that allows the background image preview to display where the image comes from theme.json |
||
| /> | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export const globalStylesDataKey = Symbol( 'globalStylesDataKey' ); | ||
| export const globalStylesLinksDataKey = Symbol( 'globalStylesLinks' ); | ||
| export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' ); | ||
| export const reusableBlocksSelectKey = Symbol( 'reusableBlocksSelect' ); |
Uh oh!
There was an error while loading. Please reload this page.