Skip to content
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

add option for toc scroll wrapper to be set to another element. #357

Merged
merged 2 commits into from
Oct 14, 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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ OR
Include the script at the bottom of the page before the closing body tag.

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.27.4/tocbot.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.30.0/tocbot.min.js"></script>
```


Expand All @@ -49,7 +49,7 @@ Include the script at the bottom of the page before the closing body tag.
CSS is used for expanding & collapsing groupings and some basic styling.

```html
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.27.4/tocbot.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.30.0/tocbot.css">
```

OR
Expand Down Expand Up @@ -240,6 +240,11 @@ basePath: '',
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync: false,
// 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,
Expand Down
4 changes: 2 additions & 2 deletions data/README.json

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions dist/tocbot.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ __webpack_require__.r(__webpack_exports__);
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync: false,
// 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.
tocScrollingWrapper: null,
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset: 0,
Expand Down Expand Up @@ -1075,21 +1079,19 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ updateTocScroll)
/* harmony export */ });
/* eslint no-var: off */

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

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

// Check if out of view
// Above scroll view
Expand Down
18 changes: 10 additions & 8 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.

4 changes: 4 additions & 0 deletions src/js/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export default {
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync: false,
// 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.
tocScrollingWrapper: null,
// Offset for the toc scroll (top) position when scrolling the page.
// Only effective if `disableTocScrollSync` is false.
tocScrollOffset: 0,
Expand Down
14 changes: 6 additions & 8 deletions src/js/update-toc-scroll.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/* eslint no-var: off */

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

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

// Check if out of view
// Above scroll view
Expand Down