Skip to content

Commit

Permalink
Sidebar container: Fix sticky scrolling behaviour
Browse files Browse the repository at this point in the history
Recent changes to the global header and footer caused the floating sidebar containers on Dev Hub and Docs to not become sticky when scrolling.
This commit adjusts the sticky position, and makes the footer element selector more robust, using a classname rather than a tagname.
  • Loading branch information
adamwoodnz committed Nov 19, 2024
1 parent adbb562 commit bab8c5a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 0 additions & 1 deletion mu-plugins/blocks/sidebar-container/postcss/style.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

/* Account for local nav height on larger screens where it becomes fixed. */
@media (min-width: 890px) {
--local--nav--offset: 0;
--local--offset-top: calc(var(--wp-admin--admin-bar--height, 0px) + var(--wp--custom--local-navigation-bar--spacing--height, 60px));
}

Expand Down
2 changes: 1 addition & 1 deletion mu-plugins/blocks/sidebar-container/src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "Sidebar Container",
"icon": "align-pull-right",
"category": "layout",
"description": "A sticky container to be used in 2-column layouts.",
"description": "A sticky container to be used in column layouts.",
"textdomain": "wporg",
"attributes": {
"hasBackToTop": {
Expand Down
10 changes: 6 additions & 4 deletions mu-plugins/blocks/sidebar-container/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ let containers;
let main;
let footer;
let adminBarHeight;
let globalNavHeight;
const scrollHandlers = [];

/**
Expand Down Expand Up @@ -46,7 +45,7 @@ function createScrollHandler( container ) {

// Toggle the fixed position based on whether main has reached the local nav.
// This assumes that main and the sidebar are top aligned.
const shouldFix = mainTop <= globalNavHeight + localNavHeight;
const shouldFix = mainTop <= localNavHeight + adminBarHeight;
container.classList.toggle( 'is-fixed-sidebar', shouldFix );

// If the sidebar is fixed and the footer is visible in the viewport, reduce the height to stop overlap.
Expand All @@ -72,7 +71,6 @@ function onResize() {
window.getComputedStyle( document.documentElement ).getPropertyValue( 'margin-top' ),
10
);
globalNavHeight = getCustomPropValue( '--wp-global-header-height' ) || 90;

containers.forEach( ( container ) => {
// Toggle the floating class based on the configured breakpoint.
Expand All @@ -88,10 +86,14 @@ function onResize() {
scrollHandlers.forEach( ( handler ) => handler() );
}

/**
* Initialize the sidebar container.
* Three elements are required: main, a sidebar container (or multiple), and a footer.
*/
function init() {
main = document.querySelector( 'main' );
containers = document.querySelectorAll( '.wp-block-wporg-sidebar-container' );
footer = document.querySelector( 'footer.wp-block-template-part' );
footer = document.querySelector( '.wporg-footer' );

if ( main && containers.length && footer ) {
containers.forEach( ( container ) => {
Expand Down

0 comments on commit bab8c5a

Please sign in to comment.