Skip to content

fix highlight issue after clicking and scrolling to the bottom #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Template/Tocbot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TOCBOT_OPTIONS = {
},
// headingsOffset: 40,
// scrollSmoothOffset: -40,
enableUrlHashUpdateOnScroll: true,
enableUrlHashUpdateOnScroll: false,
// skipRendering: true,
}

Expand Down
14 changes: 12 additions & 2 deletions src/js/build-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function (options) {
const SPACE_CHAR = " "
let tocElement
let currentlyHighlighting = true
let eventCount = 0

/**
* Create link and list elements.
Expand Down Expand Up @@ -183,7 +184,6 @@ export default function (options) {
}
// Get the top most heading currently visible on the page so we know what to highlight.
const headings = headingsArray

// This is needed for scroll events since document doesn't have getAttribute
const clickedHref = event?.target?.getAttribute
? event?.target?.getAttribute("href")
Expand All @@ -193,6 +193,9 @@ export default function (options) {
? getIsHeaderBottomMode(clickedHref.replace("#", ""))
: false
const shouldUpdate = currentlyHighlighting || isBottomMode
if (event && eventCount < 5) {
eventCount++
}

if (shouldUpdate && !!tocElement && headings.length > 0) {
const topHeader = getTopHeader(headings)
Expand All @@ -212,10 +215,17 @@ export default function (options) {
const isPageBottomMode = getIsPageBottomMode()
if (clickedHref && isBottomMode) {
activeId = clickedHref.replace("#", "")
} else if (hashId && hashId !== topHeaderId && isPageBottomMode) {
} else if (
hashId &&
hashId !== topHeaderId &&
isPageBottomMode &&
(getIsHeaderBottomMode(topHeaderId) || eventCount <= 2)
) {
// This is meant to handle the case
// of showing the items as highlighted when they
// are in bottom mode and cannot be scrolled to.
// Make sure that they stay highlighted on refresh
// too, not just when clicked.
activeId = hashId
}

Expand Down