-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Dynamic editor styles and inline CSS without targetting .editor-styles-wrapper
#18571
Comments
I agree, a purposeful function to allow inline styles for editor styles would be a good addition. I've had to do similar work-arounds with the Gutenberg-optimized themes I've build as well. |
Thanks for creating this issue! It's important for the work on iframing the content to get this right. The way it is done in Twenty Twenty will be copied over a lot by other theme authors. :/ I will probably have two go through stylesheets in JS, see if the target |
There is a shorter workaround, that should pass TRT guidelines, doesn't write a file, and avoids the need for duplicate CSS with different selectors. We can short-circuit
|
I add a theme style that is automatically adjusted to match inside the |
I think we should make |
Definitely, we need some universal functionality to load static and dynamic styles to all contexts of the editor. Additionally, to give more freedom, we need next improvements
|
A very elegant solution to this problem is to use the Example: add_filter(
'block_editor_settings_all',
function($settings) {
$settings['styles'][] = array(
'css' => ':root {--test: red;}',
'__unstableType' => 'theme',
'source' => 'blocksy'
);
return $settings;
}
); const settings = window.wp.data.select("core/editor").getEditorSettings();
wp.data.dispatch("core/editor").updateEditorSettings({
...settings,
styles: settings.styles.map((s) => {
if (s.source !== "blocksy") {
return s;
}
return {
...s,
css: ":root {--test: blue;}",
};
}),
}); Demo: CleanShot.2023-08-08.at.19.23.15.mp4For us in Blocksy, it works wonders. PR in core where a more general solution for the plugins is being explored: #52767 Props to @ellatrix for the hint. |
Twenty Twenty includes color settings in the Customizer for background color and accent color, and based on these settings, it generates ten different colors that it applies to different elements in the theme. In order to give the user a good idea of how their content will look when published, these colors also need to be applied to style elements in the block editor interface.
This ticket was created after a conversation with @andersnoren who added most of the technical context here.
The
add_editor_style()
function doesn’t support appending inline styles to the stylesheet.Because of this, Twenty Twenty uses
wp_enqueue_style()
andwp_style_add_data()
in a function hooked to theenqueue_block_editor_assets
action:(Twenty Twenty also sets different font families depending on which locale is active, as seen in the line beneath ”Add inline style for non-latin fonts.”)
As a result of this, some of those inline styles explicitly target
.editor-styles-wrapper
, in order to apply to only the right blocks and places.The primary purpose of
add_editor_style
is for theme developers to avoid having to write custom CSS just for the editor, or more precisely to avoid having to target.editor-styles-wrapper
at all, as that increases specificity unnecessarily and could break in future updates to the block editor structure, for example a change that necessitates a different classname or a new element.So, ideally, the stylesheet enqueues above could be replaced with
add_editor_style
. This is currently not possible – at least not while following theme directory guidelines. A workaround could be to write the dynamic colors directly to a CSS file, which is then added usingadd_editor_style( 'editor-style.css' )
, but themes on the WordPress.org theme directory are (fortunately) not allowed to write to files.There might be a usecase for a function that has the same relationship to
add_editor_style()
aswp_add_inline_style()
has towp_enqueue_style()
, allowing themes to append inline styles to the editor stylesheet. The code block above could then be partially replaced by something like:...allowing themes to enqueue dynamic block editor styles while still using
add_editor_style
.Is there a better way?
For now, the above is posted as an opener for a discussion about how to achieve what TwentyTwenty does with dynamic editor styles. Is there a way to do it today that avoids targetting
.editor-styles-wrapper
?Or is there a use case for a new function, like
add_editor_style
, which accomplishes the same?Thoughts welcome!
The text was updated successfully, but these errors were encountered: