PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / form-builder / blocks / src / components / EditorBodyClass.js

EditorBodyClass.js in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.3, at form-builder/blocks/src/components/EditorBodyClass.js

63 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Stamp `wppb-fb-block-editor` on admin + canvas iframe bodies for scoped chrome CSS.
3 * Iframe mounts late / reloads — re-stamp on mutations and iframe load.
4 */
5
6 import { registerPlugin } from '@wordpress/plugins';
7 import { useSelect } from '@wordpress/data';
8 import { useEffect } from '@wordpress/element';
9
10 const PB_CPTS = [ 'wppb-rf-cpt', 'wppb-epf-cpt' ];
11 const BODY_CLASS = 'wppb-fb-block-editor';
12
13 /** The block-canvas document: the iframe's contentDocument, else null. */
14 function findCanvasDoc() {
15 const iframe = document.querySelector( 'iframe[name="editor-canvas"]' );
16 if ( iframe && iframe.contentDocument ) return iframe.contentDocument;
17 return null;
18 }
19
20 function stamp() {
21 if ( document.body ) document.body.classList.add( BODY_CLASS );
22 const canvasDoc = findCanvasDoc();
23 if ( canvasDoc && canvasDoc.body ) canvasDoc.body.classList.add( BODY_CLASS );
24 }
25
26 const EditorBodyClass = () => {
27 const isOurPostType = useSelect(
28 ( select ) => PB_CPTS.includes( select( 'core/editor' ).getCurrentPostType() ),
29 []
30 );
31
32 useEffect( () => {
33 if ( ! isOurPostType ) return undefined;
34
35 let frame = null;
36 const scheduleStamp = () => {
37 if ( frame ) return;
38 frame = requestAnimationFrame( () => {
39 frame = null;
40 stamp();
41 } );
42 };
43
44 stamp(); // immediate (the iframe may already be present)
45
46 const observer = new MutationObserver( scheduleStamp );
47 observer.observe( document.body, { childList: true, subtree: true } );
48
49 const iframe = document.querySelector( 'iframe[name="editor-canvas"]' );
50 if ( iframe ) iframe.addEventListener( 'load', scheduleStamp );
51
52 return () => {
53 observer.disconnect();
54 if ( frame ) cancelAnimationFrame( frame );
55 if ( iframe ) iframe.removeEventListener( 'load', scheduleStamp );
56 };
57 }, [ isOurPostType ] );
58
59 return null;
60 };
61
62 registerPlugin( 'wppb-fb-editor-body-class', { render: EditorBodyClass } );
63