Skip to content

Commit

Permalink
Add script to position fixed toc above footer is it overlaps
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Sep 14, 2022
1 parent c4b0f71 commit f340d58
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/wp-content/themes/wporg-developer/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ function theme_scripts_styles() {
wp_enqueue_script( 'wporg-developer-navigation', get_stylesheet_directory_uri() . '/js/navigation.js', array(), filemtime( __DIR__ . '/js/navigation.js' ), true );
wp_enqueue_script( 'wporg-developer-chapters', get_stylesheet_directory_uri() . '/js/chapters.js', array( 'jquery' ), filemtime( __DIR__ . '/js/chapters.js' ) );
wp_enqueue_script( 'wporg-developer-menu', get_stylesheet_directory_uri() . '/js/menu.js', array( 'jquery' ), filemtime( __DIR__ . '/js/menu.js' ), true );
wp_enqueue_script( 'wporg-table-of-contents', get_stylesheet_directory_uri() . '/js/table-of-contents.js', array(), filemtime( __DIR__ . '/js/table-of-contents.js' ), true );
}

/**
Expand Down
46 changes: 46 additions & 0 deletions source/wp-content/themes/wporg-developer/js/table-of-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Table of content JS.
*
* Stops the sticky table of contents on wide screens from overlapping the footer
* by positioning it slightly above
*/

document.body.onload = function () {
const toc = document.querySelector( '.table-of-contents' );

if ( ! toc ) {
return;
}

const resetToCPosition = () => {
if ( toc.style ) {
toc.removeAttribute( 'style' );
}
};

const tocBottom = toc.getBoundingClientRect().bottom;
const footer = document.querySelector( '.global-footer' );
const windowHeight = window.innerHeight;

const setToCPosition = () => {
if ( window.getComputedStyle( toc ).position !== 'fixed' ) {
resetToCPosition();
return;
}

const footerTop = footer.getBoundingClientRect().top;
const exposedFooter = windowHeight - footerTop + 30;
// if the bottom of the toc is less than the exposed footer move it to that position
if ( windowHeight - tocBottom < exposedFooter ) {
toc.style.bottom = exposedFooter + 'px';
toc.style.top = 'initial';
} else {
resetToCPosition();
}
};

setToCPosition();

window.addEventListener( 'scroll', setToCPosition );
window.addEventListener( 'resize', setToCPosition );
};
1 change: 1 addition & 0 deletions source/wp-content/themes/wporg-developer/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,7 @@ body.responsive-show {
.site-main .table-of-contents {
position: fixed;
width: 15vw;
margin-bottom: 0;

.code-reference & {
margin-left: 0;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f340d58

Please sign in to comment.