Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions projects/packages/wp-build-polyfills/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Keep the dashboard frame steady while it loads and when switching admin pages.
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
</style>
<?php
}

/**
* Print the script that samples the admin menu background.
* Hold the first render of a wp-build page until the document has parsed, so Core's
* cross-document view transitions capture it with its admin menu.
*
* Held on every WordPress 7.0+ wp-build page, reduced motion included: PHP cannot see the media
* query Core gates the transition on. Never a module: Firefox then drops wp-admin's footer import map.
*
* @return void
*/
public static function print_render_hold() {
if (
! wp_style_is( 'wp-view-transitions-admin', 'enqueued' )
|| ! preg_grep( '/-wp-admin-prerequisites$/', wp_scripts()->queue )
) {
return;
}

// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- Not enqueueable: the script APIs pass `src` through esc_url(), which strips data: URLs.
echo '<script id="wp-build-admin-frame-render-hold" src="data:text/javascript," defer blocking="render"></script>' . "\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.
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 ] ) {
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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( '#<script\b[^>]*\bsrc="data:text/javascript,"#', $html );
$this->assertMatchesRegularExpression( '#<script\b[^>]*\sdefer[\s>=]#', $html );
$this->assertStringContainsString( 'blocking="render"', $html );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Activity Log: Stop the frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Activity Log: Stop the frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Dashboards: Stop the frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Stop the dashboard frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Activity Log: Stop the frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Activity Log: Stop the frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Stop the dashboard frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Stop the dashboard frame from flashing while loading and when switching admin pages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Dashboards: Stop the frame from flashing while loading and when switching admin pages.
Loading