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

Unable to add CSS class names to editor iframe #56831

Open
markhowellsmead opened this issue Dec 6, 2023 · 3 comments
Open

Unable to add CSS class names to editor iframe #56831

markhowellsmead opened this issue Dec 6, 2023 · 3 comments
Labels
[Package] Editor /packages/editor [Type] Bug An existing feature does not function as intended

Comments

@markhowellsmead
Copy link

Description

I'm attempting to add CSS class names to the new iframe in the editor using JavaScript. The aim is to apply a user-selected accent colour in the editor.

I've only been able to do so using an overly-complicated combination of MutationObserver (to detect when the iframe becomes available) and a setInterval to repeatedly try to apply the class name until it sticks. This seems to be unnecessarily complex. Is there a better solution?

I've tried to add the class names using the admin_body_class hook in PHP, but this doesn't feel right (as it's PHP) and it also only appears to work on the main body (which doesn't help, as it's hidden by the iframe).

Step-by-step reproduction instructions

See the code example.

Screenshots, screen recording, code snippet

https://gist.github.com/markhowellsmead/01c8c29937162e93a11e21c8209872d1

Environment info

  • WordPress 6.4
  • No plugins.

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@markhowellsmead markhowellsmead added the [Type] Bug An existing feature does not function as intended label Dec 6, 2023
@markhowellsmead
Copy link
Author

See #17854 and

// Ideally ALL classes that are added through get_body_class should
// be added in the editor too, which we'll somehow have to get from
// the server in the future (which will run the PHP filters).
setBodyClasses(
Array.from( ownerDocument.body.classList ).filter(
( name ) =>
name.startsWith( 'admin-color-' ) ||
name.startsWith( 'post-type-' ) ||
name === 'wp-embed-responsive'
.

The solution is to use the admin_body_class hook in PHP to add class names to the main body. By prefixing the class names with admin-color-, the class names will be automatically copied over to the editor iframe.

@markhowellsmead
Copy link
Author

Reopening as there should be a more logical way to do this.

@jordesign jordesign added the [Package] Editor /packages/editor label Dec 6, 2023
@ramonjd
Copy link
Member

ramonjd commented Dec 7, 2023

I'm just throwing this out there without much thought, so it might be irrelevant or useless.

The comment above says:

// Ideally ALL classes that are added through get_body_class should
// be added in the editor too, which we'll somehow have to get from
// the server in the future (which will run the PHP filters).

I wonder if it'd be okay to inject the return value of body_class() in get_block_editor_settings, whose output is rendered in the editor HTML — and therefore accessible to the block editor applications — via wp.[editSite | editPost].initializeEditor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Editor /packages/editor [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants