Cover: Add automatic sticky header height detection#74477
Cover: Add automatic sticky header height detection#74477JosVelasco wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Implements automatic detection and adjustment for Cover blocks with min-height: 100vh when sticky headers are present on the page. The implementation: - Detects sticky Group blocks containing header template parts - Calculates total sticky header height including margins - Sets CSS custom property --wp-sticky-header-height - Applies calc(100vh - var(--wp-sticky-header-height)) to Cover blocks - Updates dynamically on resize and layout changes - Works in both editor and frontend Detection logic supports: - Template parts with data-area="header" - Semantic <header> elements with template part class - Any template part inside sticky groups (broader compatibility) Fixes WordPress#51015
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @JosVelasco! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
PR Comment: Testing NotesTesting Note for ReviewersImportant: WordPress Core vs Gutenberg PluginWhen testing this PR locally, please note that WordPress Core may load its built-in Cover block before the Gutenberg plugin version, which will prevent these changes from taking effect. SymptomsIf you see this error when testing: This means WordPress Core is loading first and conflicting with the Gutenberg plugin version. How to Test This PROption 1: Disable Core Cover Block (Recommended) Temporarily prevent WordPress Core from registering the Cover block by adding this to your theme's add_action( 'init', function() {
if ( class_exists( 'WP_Block_Type_Registry' ) ) {
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'core/cover' ) ) {
$registry->unregister( 'core/cover' );
}
}
}, 5 ); // Priority 5 to run before Gutenberg registers at priority 10This ensures the Gutenberg plugin version loads instead. Option 2: Test with Gutenberg as Plugin Only Use a WordPress installation where Gutenberg is loaded purely as a plugin, not embedded in Core. This is typically:
Option 3: Modify Core File for Testing For quick local testing, you can apply the same changes to WordPress Core's version at: However, this is only for testing purposes - the actual fix should go through this PR to eventually be merged into Core. Why This HappensWordPress Core includes a version of the Cover block at This is a known challenge when developing Gutenberg features that need to override Core blocks. Verification After SetupOnce you've disabled the Core version, verify the fix is working by running this in the browser console: const scripts = Array.from(document.querySelectorAll('script'));
const hasCoverScript = scripts.some(s => s.textContent.includes('getStickyHeaderHeight'));
const cssVar = getComputedStyle(document.documentElement).getPropertyValue('--wp-sticky-header-height');
console.log('Gutenberg Cover script loaded:', hasCoverScript ? '✅' : '❌');
console.log('CSS variable value:', cssVar || '(not set)');
if (hasCoverScript && parseFloat(cssVar) > 0) {
console.log('✅ Fix is working correctly!');
}You should see:
Long-term SolutionOnce this PR is merged into Gutenberg and eventually synced to WordPress Core, this conflict will resolve naturally as both will have the same implementation. Let me know if you encounter any issues testing this PR! |
What?
Implements automatic detection and adjustment for Cover blocks set to 100vh height when sticky headers are present on the page.
Why?
Closes #51015
When a Cover block is set to 100vh and there's a sticky header on the page, content at the bottom of the Cover block gets hidden behind the sticky header. This creates a poor user experience where important content is inaccessible.
How?
The solution automatically detects sticky Group blocks containing header template parts and subtracts their height from Cover blocks set to 100vh.
Detection Logic
The implementation searches for:
position: sticky(via inline style or.is-position-stickyclass)data-area="header"attribute (explicit headers)<header>elements with template part classImplementation Details
sticky-header-utils.js): Calculates total sticky header height including margins--wp-sticky-header-heighton document rootstyle.scss): Appliescalc(100vh - var(--wp-sticky-header-height))to Cover blocks with inlinemin-height: 100vhedit/index.js): useEffect hook ensures detection runs in the editorindex.php): Inline script runs on DOMContentLoaded for frontendTechnical Approach
!importantflag ensures inline styles are overriddenTesting Instructions
Setup
position: sticky(Styles → Position → Sticky)100vh(Dimensions → Min height → 100 → vh)Expected Behavior
Test Scenarios