Skip to content

Commit

Permalink
Merge pull request #48273 from wildan-m/wildan/fix/42600-fix-back-hel…
Browse files Browse the repository at this point in the history
…p-docs

Fix Back Navigation on Help.Expensify.com and Improve LHN Highlight Accuracy
  • Loading branch information
Gonals authored Sep 2, 2024
2 parents 7e0bf3d + 625076f commit 9aca655
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions docs/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ window.addEventListener('load', () => {
insertElementAfter(searchInput, searchLabel);
});

const FIXED_HEADER_HEIGHT = 80;

const tocbotOptions = {
// Where to render the table of contents.
tocSelector: '.article-toc',
Expand All @@ -188,14 +190,51 @@ const tocbotOptions = {
activeLinkClass: 'selected-article',

// Headings offset between the headings and the top of the document (requires scrollSmooth enabled)
headingsOffset: 80,
scrollSmoothOffset: -80,
headingsOffset: FIXED_HEADER_HEIGHT,
scrollSmoothOffset: -FIXED_HEADER_HEIGHT,
scrollSmooth: true,

// If there is a fixed article scroll container, set to calculate titles' offset
scrollContainer: 'content-area',

onClick: (e) => {
e.preventDefault();
const hashText = e.target.href.split('#').pop();
// Append hashText to the current URL without saving to history
const newUrl = `${window.location.pathname}#${hashText}`;
history.replaceState(null, '', newUrl);
},
};

// Define the media query string for the mobile breakpoint
const mobileBreakpoint = window.matchMedia('(max-width: 799px)');

// Function to update tocbot options and refresh
function updateTocbotOptions(headingsOffset, scrollSmoothOffset) {
tocbotOptions.headingsOffset = headingsOffset;
tocbotOptions.scrollSmoothOffset = scrollSmoothOffset;
window.tocbot.refresh({
...tocbotOptions,
});
}

function handleBreakpointChange() {
const isMobile = mobileBreakpoint.matches;
const headingsOffset = isMobile ? FIXED_HEADER_HEIGHT : 0;
const scrollSmoothOffset = isMobile ? -FIXED_HEADER_HEIGHT : 0;

// Update tocbot options only if there is a change in offsets
if (tocbotOptions.headingsOffset !== headingsOffset || tocbotOptions.scrollSmoothOffset !== scrollSmoothOffset) {
updateTocbotOptions(headingsOffset, scrollSmoothOffset);
}
}

// Add listener for changes to the media query status using addEventListener
mobileBreakpoint.addEventListener('change', handleBreakpointChange);

// Initial check
handleBreakpointChange();

function selectNewExpensify(newExpensifyTab, newExpensifyContent, expensifyClassicTab, expensifyClassicContent) {
newExpensifyTab.classList.add('active');
newExpensifyContent.classList.remove('hidden');
Expand Down

0 comments on commit 9aca655

Please sign in to comment.