Skip to content

update to simplify scroll sync behavior #358

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 2 commits into from
Nov 9, 2024
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ disableTocScrollSync: false,
tocScrollingWrapper: null,
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset: 0,
tocScrollOffset: 30,
// Enable the URL hash to update with the proper heading ID as
// a user scrolls the page.
enableUrlHashUpdateOnScroll: false
Expand Down
4 changes: 2 additions & 2 deletions data/README.json

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions dist/tocbot.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,11 @@ __webpack_require__.r(__webpack_exports__);
// If this is null then just use `tocElement` or `tocSelector` instead
// assuming `disableTocScrollSync` is set to false. This allows for
// scrolling an outer element (like a nav panel w/ search) containing the toc.
// Please pass an element, not a selector here.
tocScrollingWrapper: null,
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset: 0,
tocScrollOffset: 30,
// Enable the URL hash to update with the proper heading ID as
// a user scrolls the page.
enableUrlHashUpdateOnScroll: false
Expand Down Expand Up @@ -1079,28 +1080,19 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ updateTocScroll)
/* harmony export */ });
const SCROLL_LEEWAY = 30

function updateTocScroll (options) {
const toc = options.tocScrollingWrapper || options.tocElement || document.querySelector(options.tocSelector)
if (toc && toc.scrollHeight > toc.clientHeight) {
const activeItem = toc.querySelector(`.${options.activeListItemClass}`)
if (activeItem) {
// Determine container top and bottom
const cTop = toc.scrollTop
const cBottom = cTop + toc.clientHeight

// Determine element top and bottom
const eTop = activeItem.offsetTop
const eBottom = eTop + activeItem.clientHeight

// Check if out of view
// Above scroll view
if (eTop < cTop + options.tocScrollOffset) {
toc.scrollTop -= (cTop - eTop) + options.tocScrollOffset
// Below scroll view
} else if (eBottom > cBottom - options.tocScrollOffset - SCROLL_LEEWAY) {
toc.scrollTop += (eBottom - cBottom) + options.tocScrollOffset + (2 * SCROLL_LEEWAY)
}
const scrollAmount = eTop - options.tocScrollOffset
toc.scrollTop = scrollAmount > 0 ? scrollAmount : 0
}
}
}
Expand Down
18 changes: 5 additions & 13 deletions dist/tocbot.js

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

2 changes: 1 addition & 1 deletion dist/tocbot.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/js/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ export default {
// If this is null then just use `tocElement` or `tocSelector` instead
// assuming `disableTocScrollSync` is set to false. This allows for
// scrolling an outer element (like a nav panel w/ search) containing the toc.
// Please pass an element, not a selector here.
tocScrollingWrapper: null,
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset: 0,
tocScrollOffset: 30,
// Enable the URL hash to update with the proper heading ID as
// a user scrolls the page.
enableUrlHashUpdateOnScroll: false
Expand Down
15 changes: 3 additions & 12 deletions src/js/update-toc-scroll.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
const SCROLL_LEEWAY = 30

export default function updateTocScroll (options) {
const toc = options.tocScrollingWrapper || options.tocElement || document.querySelector(options.tocSelector)
if (toc && toc.scrollHeight > toc.clientHeight) {
const activeItem = toc.querySelector(`.${options.activeListItemClass}`)
if (activeItem) {
// Determine container top and bottom
const cTop = toc.scrollTop
const cBottom = cTop + toc.clientHeight

// Determine element top and bottom
const eTop = activeItem.offsetTop
const eBottom = eTop + activeItem.clientHeight

// Check if out of view
// Above scroll view
if (eTop < cTop + options.tocScrollOffset) {
toc.scrollTop -= (cTop - eTop) + options.tocScrollOffset
// Below scroll view
} else if (eBottom > cBottom - options.tocScrollOffset - SCROLL_LEEWAY) {
toc.scrollTop += (eBottom - cBottom) + options.tocScrollOffset + (2 * SCROLL_LEEWAY)
}
Comment on lines -17 to -22
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not always ideal and felt buggy since it would just barely keep the items in view.

const scrollAmount = eTop - options.tocScrollOffset
toc.scrollTop = scrollAmount > 0 ? scrollAmount : 0
Comment on lines +12 to +13
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new way is more similar to how Stripe does it:
https://docs.stripe.com/api

}
}
}