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

Local navigation block: Hide page title before collapsing menu #641

Merged
merged 3 commits into from
Jul 22, 2024
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
8 changes: 7 additions & 1 deletion mu-plugins/blocks/local-navigation-bar/postcss/style.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
}
}

@media ( max-width: 550px ) {
@media ( max-width: 599px ) {
& .wp-block-group p:not(.wp-block-site-title) {
display: none;
}
Expand Down Expand Up @@ -341,6 +341,12 @@
display: none;
}

&.wporg-hide-page-title {
& .wp-block-group p:not(.wp-block-site-title) {
display: none;
}
}

&.wporg-show-collapsed-nav {
& .wp-block-navigation {
display: none;
Expand Down
46 changes: 37 additions & 9 deletions mu-plugins/blocks/local-navigation-bar/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function init() {

// Bail early on small screens, the visible nav block is already mobile.
if ( window.innerWidth < 600 ) {
container.classList.remove( 'wporg-show-collapsed-nav' );
container.classList.remove( 'wporg-hide-page-title', 'wporg-show-collapsed-nav' );
navElement.classList.add( 'wporg-is-mobile-nav' );
return;
}
Expand All @@ -72,6 +72,26 @@ function init() {
container.dataset.navWidth = Math.ceil( navWidth );
}

const titleElement = container.querySelector( '.wp-block-site-title, div.wp-block-group' );
if ( ! titleElement ) {
return;
}

// Get the initial full width, before any elements are hidden.
let fullTitleWidth = titleElement.dataset.fullWidth;
if ( ! fullTitleWidth ) {
// Like navWidth, get this by the individual items to get the
// non-wrapped width.
fullTitleWidth =
[ ...titleElement.children ].reduce(
( acc, current ) => ( acc += current.getBoundingClientRect().width ),
0
) +
10 * ( titleElement.children.length - 1 ); // 10px margin between items.

titleElement.dataset.fullWidth = Math.ceil( fullTitleWidth );
}

const {
paddingInlineStart = '0px',
paddingInlineEnd = '0px',
Expand All @@ -85,18 +105,26 @@ function init() {
parseInt( gap, 10 ) -
20; // 20px right padding is added when the collapsed nav is hidden.

const titleElement = container.querySelector( '.wp-block-site-title, div.wp-block-group' );
if ( ! titleElement ) {
return;
// If the title area is not a group block, use the same width for
// short and full (as there is no page title to hide).
let soloTitleWidth = fullTitleWidth;
if ( titleElement.classList.contains( 'wp-block-group' ) ) {
soloTitleWidth = titleElement.children[ 0 ].getBoundingClientRect().width;
}
const { width: titleWidth } = titleElement.getBoundingClientRect();

const usedWidth = Math.ceil( titleWidth ) + Math.ceil( navWidth );
const usedFullWidth = Math.ceil( fullTitleWidth ) + Math.ceil( navWidth );
const usedShortWidth = Math.ceil( soloTitleWidth ) + Math.ceil( navWidth );

if ( usedWidth > availableWidth ) {
container.classList.add( 'wporg-show-collapsed-nav' );
} else {
if ( availableWidth > usedFullWidth ) {
// Screen is large enough for everything, show all.
container.classList.remove( 'wporg-show-collapsed-nav', 'wporg-hide-page-title' );
} else if ( availableWidth > usedShortWidth ) {
// Menu and title will collide, hide page title.
container.classList.add( 'wporg-hide-page-title' );
container.classList.remove( 'wporg-show-collapsed-nav' );
} else {
// Menu and title will collide even with only site title, use collapsed nav.
container.classList.add( 'wporg-hide-page-title', 'wporg-show-collapsed-nav' );
}
};

Expand Down
Loading