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 Oct 3, 2022
1 parent c4b0f71 commit 830fb7b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 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,8 @@ 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( 'raf-throttle', get_stylesheet_directory_uri() . '/js/rafThrottle.min.js', array(), filemtime( __DIR__ . '/js/rafThrottle.min.js' ), true );
wp_enqueue_script( 'wporg-table-of-contents', get_stylesheet_directory_uri() . '/js/table-of-contents.js', array('raf-throttle'), filemtime( __DIR__ . '/js/table-of-contents.js' ), true );
}

/**
Expand Down

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

57 changes: 57 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,57 @@
/**
* Table of contents JS.
*
* Stops the sticky table of contents on wide screens from overlapping the footer
* by positioning it slightly above
* Uses global rafThrottle.min.js - https://github.com/wuct/raf-throttle
*/

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

if ( ! toc ) {
return;
}

const footer = document.querySelector( '.global-footer' );
const tocTop = parseInt( window.getComputedStyle( toc ).top, 10 );
let windowHeight = window.innerHeight;

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

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

const tocHeight = toc.offsetHeight;
const tocBottom = tocTop + tocHeight;
const footerTopWithClearSpace = parseInt( footer.getBoundingClientRect().top, 10 ) - 30;

// if the bottom of the toc is below the top of the footer's clear space move it above
if ( tocBottom > footerTopWithClearSpace ) {
toc.style.bottom = windowHeight - footerTopWithClearSpace + 'px';
toc.style.top = 'initial';
} else {
resetToCPosition();
}
};

setToCPosition();

// eslint-disable-next-line no-undef
window.addEventListener( 'scroll', rafThrottle( setToCPosition ) );
window.addEventListener(
'resize',
// eslint-disable-next-line no-undef
rafThrottle( () => {
windowHeight = window.innerHeight;
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 830fb7b

Please sign in to comment.