Skip to content

Commit 9aca655

Browse files
authored
Merge pull request #48273 from wildan-m/wildan/fix/42600-fix-back-help-docs
Fix Back Navigation on Help.Expensify.com and Improve LHN Highlight Accuracy
2 parents 7e0bf3d + 625076f commit 9aca655

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

docs/assets/js/main.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ window.addEventListener('load', () => {
165165
insertElementAfter(searchInput, searchLabel);
166166
});
167167

168+
const FIXED_HEADER_HEIGHT = 80;
169+
168170
const tocbotOptions = {
169171
// Where to render the table of contents.
170172
tocSelector: '.article-toc',
@@ -188,14 +190,51 @@ const tocbotOptions = {
188190
activeLinkClass: 'selected-article',
189191

190192
// Headings offset between the headings and the top of the document (requires scrollSmooth enabled)
191-
headingsOffset: 80,
192-
scrollSmoothOffset: -80,
193+
headingsOffset: FIXED_HEADER_HEIGHT,
194+
scrollSmoothOffset: -FIXED_HEADER_HEIGHT,
193195
scrollSmooth: true,
194196

195197
// If there is a fixed article scroll container, set to calculate titles' offset
196198
scrollContainer: 'content-area',
199+
200+
onClick: (e) => {
201+
e.preventDefault();
202+
const hashText = e.target.href.split('#').pop();
203+
// Append hashText to the current URL without saving to history
204+
const newUrl = `${window.location.pathname}#${hashText}`;
205+
history.replaceState(null, '', newUrl);
206+
},
197207
};
198208

209+
// Define the media query string for the mobile breakpoint
210+
const mobileBreakpoint = window.matchMedia('(max-width: 799px)');
211+
212+
// Function to update tocbot options and refresh
213+
function updateTocbotOptions(headingsOffset, scrollSmoothOffset) {
214+
tocbotOptions.headingsOffset = headingsOffset;
215+
tocbotOptions.scrollSmoothOffset = scrollSmoothOffset;
216+
window.tocbot.refresh({
217+
...tocbotOptions,
218+
});
219+
}
220+
221+
function handleBreakpointChange() {
222+
const isMobile = mobileBreakpoint.matches;
223+
const headingsOffset = isMobile ? FIXED_HEADER_HEIGHT : 0;
224+
const scrollSmoothOffset = isMobile ? -FIXED_HEADER_HEIGHT : 0;
225+
226+
// Update tocbot options only if there is a change in offsets
227+
if (tocbotOptions.headingsOffset !== headingsOffset || tocbotOptions.scrollSmoothOffset !== scrollSmoothOffset) {
228+
updateTocbotOptions(headingsOffset, scrollSmoothOffset);
229+
}
230+
}
231+
232+
// Add listener for changes to the media query status using addEventListener
233+
mobileBreakpoint.addEventListener('change', handleBreakpointChange);
234+
235+
// Initial check
236+
handleBreakpointChange();
237+
199238
function selectNewExpensify(newExpensifyTab, newExpensifyContent, expensifyClassicTab, expensifyClassicContent) {
200239
newExpensifyTab.classList.add('active');
201240
newExpensifyContent.classList.remove('hidden');

0 commit comments

Comments
 (0)