Skip to content

Commit

Permalink
[site] Clarify some magic numbers from pagetoc.js
Browse files Browse the repository at this point in the history
Signed-off-by: Harry Callahan <hcallahan@lowrisc.org>
  • Loading branch information
hcallahan-lowrisc authored and marnovandermaas committed May 31, 2023
1 parent c6990b6 commit 652d91e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion site/book-theme/css/general.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ h6:target::before {
content: '';
width: 100%;
left: 0%;
margin-top: -1rem;
margin-top: -10px;
height: var(--target-height);
background-color: #d0966f4a;
z-index: -10;
Expand Down
17 changes: 12 additions & 5 deletions site/book-theme/pagetoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ var updateDynamicHighlight = function() {
var id;
let elements = document.getElementsByClassName("header");
// Set id == the highest "header" element visible in the window.
// Define an offset to account for the menubar, and bump the decision-point a
// bit further down the page, which makes the behaviour feel more natural.
const highestVisibleHeaderOffset = 150; // px
Array.prototype.forEach.call(elements, function(el) {
if ((window.pageYOffset + 150) >= el.offsetTop) {
if ((window.pageYOffset + highestVisibleHeaderOffset) >= el.offsetTop) {
id = el;
}
});
Expand Down Expand Up @@ -62,11 +65,13 @@ var set_target_highlight = function(event) {
// "hashchange" event
newurl = event.newURL;
}
// Along with "margin-top: -10px;" applied to the :target selector style, this gives the
// highlight a top and bottom margin of 10px around the heading.
const targetMarginTopBottom = 20; // px
Array.prototype.forEach.call(document.getElementsByClassName("header"), function(el) {
if (new URL(el.href).hash == new URL(newurl).hash) {
document.documentElement.style.setProperty(
'--target-height',
el.getBoundingClientRect().height + 20 + "px"
'--target-height', (el.getBoundingClientRect().height + targetMarginTopBottom) + "px"
);
}
});
Expand All @@ -80,8 +85,10 @@ window.addEventListener("load", set_target_highlight);
var set_pagetoc_height = function() {
let el = document.getElementsByClassName("pagetoc")[0];
el.style.height = "auto";
let pagetoc_height = el.getBoundingClientRect().height;
let window_height = window.innerHeight - document.documentElement.style.getPropertyValue('--menu-bar-height') - 200;
// Add some extra margin-bottom to the pagetoc, to keep away from the bottom of the page.
const pagetocMarginBottom = 200; // px
let pagetoc_height = el.getBoundingClientRect().height + pagetocMarginBottom;
let window_height = window.innerHeight - document.documentElement.style.getPropertyValue('--menu-bar-height');
if (pagetoc_height < window_height) {
el.style.height = "auto";
} else {
Expand Down

0 comments on commit 652d91e

Please sign in to comment.