Skip to content

Commit 74b28a8

Browse files
committed
Add the body class the header's full-width rule keys on
Core removes #wpcontent's left padding on the two screens that use this header, keyed on a body class. Without it the header sits 20px in from the menu instead of spanning the screen — reported as "the margin on the left not pulling the header across". The class name comes from PageHeader::body_class() rather than being written here, so the rule and the class cannot drift apart.
1 parent cac9fab commit 74b28a8

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎src/SettingFields.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function __construct( string $id, array $config ) {
128128
add_action( 'admin_menu', [ $this, 'register_menu' ] );
129129
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] );
130130
add_filter( 'screen_settings', [ $this, 'render_screen_tools' ], 10, 2 );
131+
add_filter( 'admin_body_class', [ $this, 'body_class' ] );
131132
add_action( 'admin_post_' . $this->action_slug( 'export' ), [ $this, 'handle_export' ] );
132133
add_action( 'admin_post_' . $this->action_slug( 'import' ), [ $this, 'handle_import' ] );
133134
add_action( 'admin_post_' . $this->action_slug( 'reset' ), [ $this, 'handle_reset' ] );
@@ -243,6 +244,26 @@ public function register_menu(): void {
243244
}
244245
}
245246

247+
/**
248+
* Mark the screen as one using the kit's page header.
249+
*
250+
* The header only spans the screen if `#wpcontent`'s left padding is
251+
* removed, and core does that with a body class rather than on the header
252+
* itself. The class name comes from the kit so the rule and the class
253+
* cannot drift apart.
254+
*
255+
* @param string $classes Existing body classes.
256+
*
257+
* @return string
258+
*/
259+
public function body_class( string $classes ): string {
260+
if ( '' === $this->hook_suffix || get_current_screen()?->id !== $this->hook_suffix ) {
261+
return $classes;
262+
}
263+
264+
return trim( $classes . ' ' . PageHeader::body_class() );
265+
}
266+
246267
/**
247268
* Register the help tabs.
248269
*

‎tests/SettingFieldsTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,29 @@ public function test_the_header_renders_outside_the_wrap(): void {
698698
$this->assertLessThan( $wrap, strpos( $html, 'wp-header-end' ) );
699699
}
700700

701+
/**
702+
* The screen carries the body class the header's rule keys on.
703+
*
704+
* Without it #wpcontent keeps its 20px left padding and the header sits
705+
* inset from the menu instead of spanning the screen.
706+
*/
707+
public function test_the_screen_carries_the_header_body_class(): void {
708+
$page = $this->page();
709+
$page->register_menu();
710+
711+
$GLOBALS['sf_screen'] = new \SF_Screen( $page->get_hook_suffix() );
712+
713+
$this->assertStringContainsString(
714+
\ArrayPress\FieldKit\Support\PageHeader::body_class(),
715+
$page->body_class( 'existing-class' )
716+
);
717+
718+
// And another screen is left exactly as it was.
719+
$GLOBALS['sf_screen'] = new \SF_Screen( 'edit-post' );
720+
721+
$this->assertSame( 'existing-class', $page->body_class( 'existing-class' ) );
722+
}
723+
701724
/**
702725
* The tools are not in the header.
703726
*

0 commit comments

Comments
 (0)