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

Site Editor: Classic theme site preview flickering #69474

Open
3 of 6 tasks
t-hamano opened this issue Mar 6, 2025 · 4 comments · May be fixed by #69480
Open
3 of 6 tasks

Site Editor: Classic theme site preview flickering #69474

t-hamano opened this issue Mar 6, 2025 · 4 comments · May be fixed by #69480
Labels
[Feature] Style Book [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@t-hamano
Copy link
Contributor

t-hamano commented Mar 6, 2025

Description

In the classic theme, you will see the front-end preview in the site editor.

The admin bar will flash on initial load, likely due to a process to remove it so that it doesn’t appear.

We may want to investigate whether you can eliminate this flickering.

Step-by-step reproduction instructions

  • Activate a classic theme.
  • Go to Appearance > Design
  • Check the initial load of the preview, you can also check by reloading the browser.

Screenshots, screen recording, code snippet

9ffa9a973fbb1dc80156ac5301259b50.mp4

Environment info

No response

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

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Style Book labels Mar 6, 2025
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Mar 6, 2025
@stokesman
Copy link
Contributor

stokesman commented Mar 7, 2025

I’d noticed this and tinkered with some fixes. To me it would seem best to have the page served without an admin bar—using the show_admin_bar filter.

A rough proof of concept diff adding a GET parameter to the site url so it can be served without an admin bar and interactivity disabled.
diff --git a/gutenberg.php b/gutenberg.php
index 9d57e8d2bc..967df52478 100644
--- a/gutenberg.php
+++ b/gutenberg.php
@@ -76,3 +76,22 @@ function gutenberg_pre_init() {
 
 	require_once __DIR__ . '/lib/load.php';
 }
+
+add_action( 'init', function() {
+	if( isset( $_GET['wp-site-editor-preview'] ) ) {
+		add_filter( 'show_admin_bar', '__return_false' );
+		add_action( 'wp_print_footer_scripts', function() {
+			echo '<script>', <<<JS
+				// Make interactive elements unclickable.
+				const interactiveElements = document.querySelectorAll(
+					'a, button, input, details, audio'
+				);
+				interactiveElements.forEach( ( element ) => {
+					element.style.pointerEvents = 'none';
+					element.tabIndex = -1;
+					element.setAttribute( 'aria-hidden', 'true' );
+				} );
+			JS, '</script>';
+		} );
+     }
+} );
diff --git a/packages/edit-site/src/components/editor/site-preview.js b/packages/edit-site/src/components/editor/site-preview.js
index e53f87bc5b..378e4629f8 100644
--- a/packages/edit-site/src/components/editor/site-preview.js
+++ b/packages/edit-site/src/components/editor/site-preview.js
@@ -12,10 +12,10 @@ export default function SitePreview() {
 		return siteData?.home;
 	}, [] );
 
-	// If theme is block based, return the Editor, otherwise return the site preview.
 	return (
 		<iframe
-			src={ siteUrl }
+			tabIndex={ -1 }
+			src={ `${ siteUrl }?wp-site-editor-preview` }
 			title={ __( 'Site Preview' ) }
 			style={ {
 				display: 'block',
@@ -23,26 +23,6 @@ export default function SitePreview() {
 				height: '100%',
 				backgroundColor: '#fff',
 			} }
-			onLoad={ ( event ) => {
-				// Hide the admin bar in the front-end preview.
-				const document = event.target.contentDocument;
-				document.getElementById( 'wpadminbar' ).remove();
-				document
-					.getElementsByTagName( 'html' )[ 0 ]
-					.setAttribute( 'style', 'margin-top: 0 !important;' );
-				document
-					.getElementsByTagName( 'body' )[ 0 ]
-					.classList.remove( 'admin-bar' );
-				// Make interactive elements unclickable.
-				const interactiveElements = document.querySelectorAll(
-					'a, button, input, details, audio'
-				);
-				interactiveElements.forEach( ( element ) => {
-					element.style.pointerEvents = 'none';
-					element.tabIndex = -1;
-					element.setAttribute( 'aria-hidden', 'true' );
-				} );
-			} }
 		/>
 	);
 }

Is that a bad idea for some reason?

@t-hamano
Copy link
Contributor Author

t-hamano commented Mar 7, 2025

To me it would seem best to have the page served without an admin bar—using the show_admin_bar filter.

I agree. To handle this on the server side, we need an approach where one of the conditional statements in the is_admin_bar_showing() function returns false.

As far as I know, the only way to do this is to hook into it based on GET parameters. Maybe we can reuse the wp_theme_preview parameter for the block theme preview?

@t-hamano
Copy link
Contributor Author

t-hamano commented Mar 7, 2025

I have submitted a core ticket to discuss a backend approach: https://core.trac.wordpress.org/ticket/63076

@t-hamano
Copy link
Contributor Author

t-hamano commented Mar 8, 2025

Core PR: WordPress/wordpress-develop#8475

I will also submit a Gutenberg PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Style Book [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
Development

Successfully merging a pull request may close this issue.

2 participants