diff --git a/projects/packages/wp-build-polyfills/README.md b/projects/packages/wp-build-polyfills/README.md
index 815ef18ea1f6..17187551d894 100644
--- a/projects/packages/wp-build-polyfills/README.md
+++ b/projects/packages/wp-build-polyfills/README.md
@@ -69,9 +69,15 @@ The version threshold for force-replacements can be overridden with a third para
WP_Build_Polyfills::register( 'my-plugin', array( 'wp-notices' ), '7.1' );
```
-## Admin frame backdrop
+## Admin frame
-`WP_Build_Admin_Frame` makes the `@wordpress/boot` single-page backdrop continue the wp-admin menu color. `@wordpress/admin-ui` only knows Core's color schemes and paints a near-black backdrop for WordPress.com and third-party ones, and on WordPress 7.0+ the boot module that runs is Core's bundled copy, so the override is applied from PHP: `WP_Build_Polyfills::register()` arms it, and it prints a stylesheet on `admin_head` plus a script on `in_admin_header` that samples the `#adminmenuback` background into `--wp-build-admin-menu-background`. It lives here because this package is the one runtime every wp-build page already loads, and it is temporary until wp-build or boot ship the same behavior.
+`WP_Build_Admin_Frame` reconciles the `@wordpress/boot` single-page layout with the wp-admin frame. On WordPress 7.0+ the boot module that runs is Core's bundled copy, so the fixes are applied from PHP around the page, and `WP_Build_Polyfills::register()` arms them:
+
+- **Backdrop color.** `@wordpress/admin-ui` only knows Core's color schemes and paints a near-black backdrop for WordPress.com and third-party ones. A script on `in_admin_header` samples the `#adminmenuback` background into `--wp-build-admin-menu-background`, which a stylesheet on `admin_head` applies.
+- **First paint.** The same stylesheet paints the backdrop and a stage-shaped panel on the empty app container, so the page does not flash white before boot mounts.
+- **Cross-document view transitions.** Where Core enables them, a render-blocking deferred script on `admin_head` holds a wp-build page's first render until its admin menu has parsed, and boot's surfaces are unnamed while the page leaves so they do not zoom or slide over the next one.
+
+It lives here because this package is the one runtime every wp-build page already loads, and it is temporary until wp-build or boot ship the same behavior.
## Boot module asset file
diff --git a/projects/packages/wp-build-polyfills/changelog/jetpack-2561-2574-frame-flash b/projects/packages/wp-build-polyfills/changelog/jetpack-2561-2574-frame-flash
new file mode 100644
index 000000000000..41de40f0b3d5
--- /dev/null
+++ b/projects/packages/wp-build-polyfills/changelog/jetpack-2561-2574-frame-flash
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fixed
+
+Keep the dashboard frame steady while it loads and when switching admin pages.
diff --git a/projects/packages/wp-build-polyfills/src/class-wp-build-admin-frame.php b/projects/packages/wp-build-polyfills/src/class-wp-build-admin-frame.php
index 7e467bb08036..acd8c34def7c 100644
--- a/projects/packages/wp-build-polyfills/src/class-wp-build-admin-frame.php
+++ b/projects/packages/wp-build-polyfills/src/class-wp-build-admin-frame.php
@@ -20,17 +20,21 @@
* printed on `in_admin_header` that samples the rendered menu background into
* a custom property on the root element.
*
- * Pages without a boot layout are unaffected: the selectors match nothing.
+ * The frame is also held steady: painted before boot mounts, and captured whole
+ * by the cross-document view transitions Core enables in wp-admin.
+ *
+ * Pages without a boot mount container are unaffected: the selectors match nothing.
*/
class WP_Build_Admin_Frame {
/**
- * Hook the stylesheet and the sampling script. Safe to call repeatedly.
+ * Hook the stylesheet, the render hold and the frame script. Safe to call repeatedly.
*
* @return void
*/
public static function register() {
add_action( 'admin_head', array( self::class, 'print_styles' ) );
+ add_action( 'admin_head', array( self::class, 'print_render_hold' ) );
add_action( 'in_admin_header', array( self::class, 'print_script' ) );
}
@@ -59,6 +63,11 @@ public static function print_styles() {
background: var(--wp-build-admin-menu-background, #fff);
}
+ /* Old-only boot surfaces zoom or slide over the next page, so let the root snapshot carry them. */
+ html.wp-build-admin-frame-leaving :is(.boot-layout--single-page, [class*="__layout-single-page"]) :is([class*="__stage"], [class*="__inspector"], [class*="__canvas"], .interface-interface-skeleton__header, .interface-interface-skeleton__sidebar) {
+ view-transition-name: none !important;
+ }
+
/*
* Boot's layout is absolutely positioned against `#wpbody`, which grows
* with the admin menu: a menu taller than the viewport stretches the app
@@ -70,19 +79,59 @@ public static function print_styles() {
* (WordPress/gutenberg#82114).
*/
@media (min-width: 783px) {
+ body.js:has([id$="-wp-admin-app"]:empty) #wpbody,
body:has(.boot-layout--single-page) #wpbody,
body:has([class*="__layout-single-page"]) #wpbody {
position: sticky;
top: var(--wp-admin--admin-bar--height, 32px);
height: calc(100vh - var(--wp-admin--admin-bar--height, 32px));
}
+
+ /*
+ * Paint boot's stage on the empty app container until it mounts, where the
+ * page template's critical CSS would otherwise leave the area white. Plain
+ * #fff: boot's theme provider whitens the stage, unlike the root surface token.
+ * A container that never mounts keeps the panel and passes for an empty app.
+ */
+ body.js:has([id$="-wp-admin-app"]:empty) {
+ background: var(--wp-build-admin-menu-background, #fff);
+ }
+ body.js [id$="-wp-admin-app"]:empty {
+ position: absolute;
+ inset-block: 0 8px;
+ inset-inline: 0 8px;
+ border-radius: var(--wpds-border-radius-xl, 12px);
+ background: #fff;
+ }
}
queue )
+ ) {
+ return;
+ }
+
+ // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- Not enqueueable: the script APIs pass `src` through esc_url(), which strips data: URLs.
+ echo '' . "\n";
+ }
+
+ /**
+ * Print the script that samples the admin menu background and marks the root
+ * while the page leaves for a cross-document view transition.
*
* Runs right after `#adminmenuback` is printed and before the layout
* mounts, which is why the property goes on the root element.
@@ -92,6 +141,16 @@ public static function print_styles() {
public static function print_script() {
$script = <<<'JS'
( function () {
+ window.addEventListener( 'pageswap', function ( event ) {
+ if ( event.viewTransition ) {
+ document.documentElement.classList.add( 'wp-build-admin-frame-leaving' );
+ }
+ } );
+ // A page restored from the back/forward cache reveals again with the class still set.
+ window.addEventListener( 'pagereveal', function () {
+ document.documentElement.classList.remove( 'wp-build-admin-frame-leaving' );
+ } );
+
var menu = document.getElementById( 'adminmenuback' );
if ( ! menu ) {
return;
diff --git a/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php b/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php
index 014383d0e30b..6d5baf6d41c0 100644
--- a/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php
+++ b/projects/packages/wp-build-polyfills/src/class-wp-build-polyfills.php
@@ -110,8 +110,8 @@ class WP_Build_Polyfills {
* load time. Those companions show up in get_consumers() under the
* requesting consumer's name.
*
- * Every call also arms WP_Build_Admin_Frame for the request, so the boot
- * single-page backdrop follows the wp-admin menu color on every wp-build page.
+ * Every call also arms WP_Build_Admin_Frame for the request, which keeps the boot
+ * single-page layout in step with the wp-admin frame on every wp-build page.
*
* @param string $consumer A unique identifier for the consumer (e.g. plugin slug).
* @param string[] $polyfills List of polyfill handles/module IDs to register.
diff --git a/projects/packages/wp-build-polyfills/tests/php/WP_Build_Admin_Frame_Test.php b/projects/packages/wp-build-polyfills/tests/php/WP_Build_Admin_Frame_Test.php
index 7e1f109d2b26..7c5454cbe1e9 100644
--- a/projects/packages/wp-build-polyfills/tests/php/WP_Build_Admin_Frame_Test.php
+++ b/projects/packages/wp-build-polyfills/tests/php/WP_Build_Admin_Frame_Test.php
@@ -16,6 +16,11 @@ class WP_Build_Admin_Frame_Test extends BaseTestCase {
*/
const HOOKS = array( 'admin_head', 'in_admin_header' );
+ /**
+ * Script the wp-build page template enqueues on its own page only.
+ */
+ const PREREQUISITES_HANDLE = 'example-dashboard-wp-admin-prerequisites';
+
/**
* Callbacks attached to HOOKS before the test, restored afterwards.
*
@@ -40,12 +45,16 @@ public function set_up() {
}
/**
- * Restore the admin header hooks.
+ * Restore the admin header hooks and drop the enqueued page assets.
*
* @after
*/
#[After]
public function tear_down() {
+ wp_dequeue_script( self::PREREQUISITES_HANDLE );
+ wp_deregister_script( self::PREREQUISITES_HANDLE );
+ wp_dequeue_style( 'wp-view-transitions-admin' );
+
global $wp_filter;
foreach ( self::HOOKS as $hook ) {
if ( null === $this->original_hooks[ $hook ] ) {
@@ -70,6 +79,33 @@ private function render_admin_header() {
return ob_get_clean();
}
+ /**
+ * Return the declarations of the rule whose selector list ends with `$selector`.
+ *
+ * @param string $css Stylesheet.
+ * @param string $selector Last selector before the rule's opening brace.
+ * @return string
+ */
+ private function css_block( $css, $selector ) {
+ $this->assertSame( 1, preg_match( '/' . preg_quote( $selector, '/' ) . ' \{([^}]*)\}/', $css, $matches ), "No rule for $selector" );
+ return $matches[1];
+ }
+
+ /**
+ * Enqueue the script the wp-build page template adds to its own page.
+ */
+ private function enqueue_wp_build_page() {
+ wp_register_script( self::PREREQUISITES_HANDLE, '', array(), '1.0', true );
+ wp_enqueue_script( self::PREREQUISITES_HANDLE );
+ }
+
+ /**
+ * Enqueue the stylesheet Core uses to turn on cross-document view transitions in wp-admin.
+ */
+ private function enqueue_core_view_transitions() {
+ wp_enqueue_style( 'wp-view-transitions-admin' );
+ }
+
/**
* Nothing is printed on requests where no consumer registered.
*/
@@ -134,6 +170,28 @@ public function test_styles_cap_the_admin_body_to_the_viewport() {
);
}
+ /**
+ * Before boot mounts, the stylesheet paints its frame on the empty app container.
+ */
+ public function test_styles_paint_the_frame_before_boot_mounts() {
+ ob_start();
+ WP_Build_Admin_Frame::print_styles();
+ $css = ob_get_clean();
+
+ $this->assertStringContainsString( 'body.js:has([id$="-wp-admin-app"]:empty) #wpbody,', $css );
+ $this->assertStringContainsString(
+ 'background: var(--wp-build-admin-menu-background, #fff);',
+ $this->css_block( $css, 'body.js:has([id$="-wp-admin-app"]:empty)' )
+ );
+
+ $panel = $this->css_block( $css, 'body.js [id$="-wp-admin-app"]:empty' );
+ $this->assertStringContainsString( 'position: absolute;', $panel );
+ $this->assertStringContainsString( 'inset-block: 0 8px;', $panel );
+ $this->assertStringContainsString( 'inset-inline: 0 8px;', $panel );
+ $this->assertStringContainsString( 'border-radius: var(--wpds-border-radius-xl, 12px);', $panel );
+ $this->assertStringContainsString( 'background: #fff;', $panel );
+ }
+
/**
* The script samples #adminmenuback into the custom property on the root element.
*/
@@ -152,4 +210,85 @@ public function test_script_samples_the_admin_menu_into_the_custom_property() {
);
$this->assertStringNotContainsString( 'jQuery', $js );
}
+
+ /**
+ * While leaving for a view transition, every boot surface with an old-only animation is unnamed.
+ */
+ public function test_styles_unname_boot_surfaces_while_leaving_the_page() {
+ ob_start();
+ WP_Build_Admin_Frame::print_styles();
+ $css = ob_get_clean();
+
+ $rule = $this->css_block(
+ $css,
+ 'html.wp-build-admin-frame-leaving :is(.boot-layout--single-page, [class*="__layout-single-page"]) :is([class*="__stage"], [class*="__inspector"], [class*="__canvas"], .interface-interface-skeleton__header, .interface-interface-skeleton__sidebar)'
+ );
+ $this->assertStringContainsString( 'view-transition-name: none !important;', $rule );
+ }
+
+ /**
+ * The script marks the root while leaving for a view transition and clears it on reveal.
+ */
+ public function test_script_marks_the_root_while_leaving_for_a_view_transition() {
+ ob_start();
+ WP_Build_Admin_Frame::print_script();
+ $js = ob_get_clean();
+
+ $this->assertStringContainsString( "window.addEventListener( 'pageswap'", $js );
+ $this->assertStringContainsString( 'event.viewTransition', $js );
+ $this->assertStringContainsString( "document.documentElement.classList.add( 'wp-build-admin-frame-leaving' )", $js );
+ $this->assertStringContainsString( "window.addEventListener( 'pagereveal'", $js );
+ $this->assertStringContainsString( "document.documentElement.classList.remove( 'wp-build-admin-frame-leaving' )", $js );
+ $this->assertStringNotContainsString( 'viewTransitionName', $js );
+ }
+
+ /**
+ * Registration holds the first render of a wp-build page, once.
+ */
+ public function test_registration_holds_the_first_render_on_wp_build_pages() {
+ $this->enqueue_wp_build_page();
+ $this->enqueue_core_view_transitions();
+ WP_Build_Admin_Frame::register();
+ // @phan-suppress-next-line PhanPluginDuplicateAdjacentStatement -- The second call must be a no-op.
+ WP_Build_Admin_Frame::register();
+
+ $this->assertSame( 1, substr_count( $this->render_admin_header(), 'blocking="render"' ) );
+ }
+
+ /**
+ * Admin pages that are not wp-build pages keep rendering as soon as they can.
+ */
+ public function test_render_hold_is_skipped_outside_wp_build_pages() {
+ ob_start();
+ WP_Build_Admin_Frame::print_render_hold();
+ $this->assertSame( '', ob_get_clean() );
+ }
+
+ /**
+ * Without Core's view transitions there is nothing to hold for, so first paint is not delayed.
+ */
+ public function test_render_hold_is_skipped_without_core_view_transitions() {
+ $this->enqueue_wp_build_page();
+
+ ob_start();
+ WP_Build_Admin_Frame::print_render_hold();
+ $this->assertSame( '', ob_get_clean() );
+ }
+
+ /**
+ * The hold is a deferred, render-blocking classic script, never a module.
+ */
+ public function test_render_hold_is_a_deferred_classic_script() {
+ $this->enqueue_wp_build_page();
+ $this->enqueue_core_view_transitions();
+
+ ob_start();
+ WP_Build_Admin_Frame::print_render_hold();
+ $html = ob_get_clean();
+
+ $this->assertStringNotContainsString( 'module', $html );
+ $this->assertMatchesRegularExpression( '#