-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix CSS Custom Properties for presets in the site editor #36851
Conversation
@@ -71,34 +71,22 @@ function_exists( 'gutenberg_is_edit_site_page' ) && | |||
} | |||
|
|||
$new_global_styles = array(); | |||
$new_presets = array( | |||
array( | |||
'css' => 'variables', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer pass the CSS variables via the block editor settings and tell the editor not to wrap them. Instead, we enqueue them via the enqueue_block_editor_assets
filter.
@@ -23,23 +23,20 @@ import wrap from './transforms/wrap'; | |||
* @return {Array} converted rules. | |||
*/ | |||
const transformStyles = ( styles, wrapperClassName = '' ) => { | |||
return map( | |||
styles, | |||
( { css, baseURL, __experimentalNoWrapper = false } ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need the __experimentalNoWrapper
so this is an extra win.
Size Change: -28 B (0%) Total Size: 1.11 MB
ℹ️ View Unchanged
|
@@ -304,3 +302,5 @@ function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $ | |||
add_filter( 'force_filtered_html_on_import', 'gutenberg_global_styles_force_filtered_html_on_import_filter', 999 ); | |||
add_filter( 'safecss_filter_attr_allow_css', 'gutenberg_global_styles_include_support_for_wp_variables', 10, 2 ); | |||
// This filter needs to be executed last. | |||
|
|||
add_filter( 'enqueue_block_editor_assets', 'gutenberg_load_css_custom_properties' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was of loading styles is kind of deprecated to the iframing. cc @ellatrix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For them to load in the iframe, the styles need to be enqueued here:
gutenberg/lib/client-assets.php
Lines 710 to 780 in e523441
/** | |
* Sets the editor styles to be consumed by JS. | |
*/ | |
function gutenberg_extend_block_editor_styles_html() { | |
global $pagenow; | |
$script_handles = array(); | |
$style_handles = array( | |
'wp-block-editor', | |
'wp-block-library', | |
'wp-block-library-theme', | |
'wp-edit-blocks', | |
); | |
if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { | |
$style_handles[] = 'wp-widgets'; | |
$style_handles[] = 'wp-edit-widgets'; | |
} | |
$block_registry = WP_Block_Type_Registry::get_instance(); | |
foreach ( $block_registry->get_all_registered() as $block_type ) { | |
if ( ! empty( $block_type->style ) ) { | |
$style_handles[] = $block_type->style; | |
} | |
if ( ! empty( $block_type->editor_style ) ) { | |
$style_handles[] = $block_type->editor_style; | |
} | |
if ( ! empty( $block_type->script ) ) { | |
$script_handles[] = $block_type->script; | |
} | |
} | |
$style_handles = array_unique( $style_handles ); | |
$done = wp_styles()->done; | |
ob_start(); | |
// We do not need reset styles for the iframed editor. | |
wp_styles()->done = array( 'wp-reset-editor-styles' ); | |
wp_styles()->do_items( $style_handles ); | |
wp_styles()->done = $done; | |
$styles = ob_get_clean(); | |
$script_handles = array_unique( $script_handles ); | |
$done = wp_scripts()->done; | |
ob_start(); | |
wp_scripts()->done = array(); | |
wp_scripts()->do_items( $script_handles ); | |
wp_scripts()->done = $done; | |
$scripts = ob_get_clean(); | |
$editor_assets = wp_json_encode( | |
array( | |
'styles' => $styles, | |
'scripts' => $scripts, | |
) | |
); | |
echo "<script>window.__editorAssets = $editor_assets</script>"; | |
} | |
add_action( 'admin_footer-appearance_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' ); | |
add_action( 'admin_footer-post.php', 'gutenberg_extend_block_editor_styles_html' ); | |
add_action( 'admin_footer-post-new.php', 'gutenberg_extend_block_editor_styles_html' ); | |
add_action( 'admin_footer-widgets.php', 'gutenberg_extend_block_editor_styles_html' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ellatrix I've pushed the changes to use the function you mention, but it looks like it enqueues the given styles within the iframe. Or perhaps I need to do more things?
In this PR I'm actually trying to do the opposite: enqueue the styles outside the iframe, so they're available for the global styles sidebar.
This is the setup:
- Styles for the content, within the iframe: CSS vars + block styles. This is working fine and it's done at
gutenberg_experimental_global_styles_settings
(adds them to editor settings' styles key). - Styles for the sidebar: only the CSS vars. So far, we've been using the same method than the content's styles but with an additional flag (
__experimentalNoWrapper
) so they weren't wrapped by.editor-styles-wrapper
and so targeted the sidebar. This is what I'm struggling to see how to do in an iframed editor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talked with ella in private and can confirm that using enqueue_block_editor_assets
is fine. So, switching this back to the original direction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enqueue_block_editor_assets
is for the editor UI only, not the iframe. If you need it to load in both, you'll need to use both.
For me this issue is larger than just "loading styles". Basically we render these "previews" (the small circles) for gradients/colors in a lot of places, and we always relied on the fact that the previous use the actual value and are not dependent of a global CSS variable. So while the current PR solves the issue for the case where the custom CSS variable originates from theme.json, it doesn't solve if if the value of the gradient relied on a variable that is loaded in an editor styles for instance. So this makes me wonder whether a potential fix could be to actually consider these small circles as "block previews". Meaning render a circle inside an iframe and use the same mechanism used for the block canvas. Also, it's not just about the circles, there's a lot of places where we want a "preview": the global styles preview on the global styles sidebar, the typography previews as well. |
A similar but maybe simpler workaround to the proposed solution on this PR could be to "flag" (add some flag to the style object) the style and enqueue it outside the iframe as well. This would also only solve the issue if the custom CSS variable comes from theme.json. |
Oh, interesting. I'm not super familiar with how other parts of the editor interface work (previews, etc), although I see this approach is working and seems natural (3rd parties can use it as well). But I'll let others comment on the best direction to fix this. |
dcafb08
to
e5d91a6
Compare
e5d91a6
to
50e1019
Compare
This is ready for another review and/or 👍 |
For the record, I see |
This reverts commit e5d91a66932576f0699ab0df2e2893594734b34a.
50e1019
to
b481928
Compare
@youknowriad @ellatrix is there anything left here that I should implement? It's working as expected for me:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a decent fix but I think longer term it's probably better to consider these small "preview" as actual BlockPreviews and just render the whole styles like in any preview.
It appears this PR introduced a regression #37292 |
Fixes #30642
This PR enqueues the CSS Custom Properties coming from the
theme.json
in a different way.Before
We enqueued them via the
styles
key in the block editor settings. By doing this the styles are wrapped with the.editor-styles-wrapper
class, for them to be scoped to the content canvas. This was problematic for the CSS Custom Properties, as they wouldn't be available in the sidebar, hence, we added__experimentalNoWrapper
to the styles transformation, a scape hatch for certain styles not to be wrapped via.editor-styles-wrapper
.After
We enqueue the vars via the
enqueue_block_editor_assets
that is unaffected by the.editor-styles-wrapper
mechanism.How to test
See testing instructions in the issue.
In addition to testing that the gradients are visible, I've also tested that, in the site editor, modifications to the solid colors are reflected in the own component (sidebar) and in the content canvas. I've also applied this PR on top of #36820 to make sure gradients work as expected as well.