Skip to content

Commit

Permalink
update to simplify scroll sync behavior (#358)
Browse files Browse the repository at this point in the history
* update to simplify scroll sync behavior to be simpler and easier to follow

* built files
  • Loading branch information
tscanlin authored Nov 9, 2024
1 parent 2c34656 commit 4e7d6c6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 43 deletions.
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)
}
const scrollAmount = eTop - options.tocScrollOffset
toc.scrollTop = scrollAmount > 0 ? scrollAmount : 0
}
}
}

0 comments on commit 4e7d6c6

Please sign in to comment.