-
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
Cannot access the blocks root element in the editor using JavaScript #55947
Comments
The Since we cannot access iframe's // A helper method that updates both main and iframed canvas body classes.
function updateBodyClasses( { newClass, prevClass } ) {
document.body.classList.remove( prevClass );
document.body.classList.add( newClass );
const iframe = document.querySelector( '[name="editor-canvas"]' );
if ( iframe ) {
iframe.contentDocument.body.classList.remove( prevClass );
iframe.contentDocument.body.classList.add( newClass );
}
}
// Example.
updateBodyClasses( { newClass: 'is-quinary-accent', prevClass: 'is-secondary-accent' } ); I assume you would use a similar logic as Wabi and add styles using Cc-ing @richtabor in case he wants to update the code for Wabi 😄 Footnotes
|
Terrific, thanks George. The combination of
|
Problem resolved! |
I'm glad my snippet was helpful! |
Description
The Block Editor is loaded in an iframe. The site is empty and has no plugins installed. The theme contains no customisations of any kind to the functionality: it contains nothing but an empty index.html template for the site editor.
I have now enqueued a JavaScript file using the
enqueue_block_editor_assets
hook, as described in #48286. If Iconsole.log
thedocument.body.classList
, this returns the CSS class names of the root element in the post.php file view, not the class names of the root element in the iframe. This would seem to indicate that the script is loading in the context of thewindow
, not theiframe
.(My actual goal is to add a custom CSS class name to the root element of the block editor when selecting a colour option through a custom
ToggleGroupControl
element provided byregisterPlugin
. In the same way which the Wabi and Wei themes work: this functionality in those themes is also no longer working, presumably because Rich's scripts use the same logic and don't work in theiframe
context.)Step-by-step reproduction instructions
Enqueue a JavaScript file containing
console.log(document.body.classlist);
using theenqueue_block_editor_assets
hook. Then load the edit page screen in the block editor.Screenshots, screen recording, code snippet
No response
Environment info
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
The text was updated successfully, but these errors were encountered: