Skip to content

Commit cbf319a

Browse files
authored
add option for toc scroll wrapper to be set to another element. (#357)
* add option for toc scroll wrapper to be overriden * update version in readme and dist files
1 parent 865be00 commit cbf319a

File tree

7 files changed

+40
-29
lines changed

7 files changed

+40
-29
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ OR
4040
Include the script at the bottom of the page before the closing body tag.
4141

4242
```html
43-
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.27.4/tocbot.min.js"></script>
43+
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.30.0/tocbot.min.js"></script>
4444
```
4545

4646

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

5151
```html
52-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.27.4/tocbot.css">
52+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.30.0/tocbot.css">
5353
```
5454

5555
OR
@@ -240,6 +240,11 @@ basePath: '',
240240
// Only takes affect when `tocSelector` is scrolling,
241241
// keep the toc scroll position in sync with the content.
242242
disableTocScrollSync: false,
243+
// If this is null then just use `tocElement` or `tocSelector` instead
244+
// assuming `disableTocScrollSync` is set to false. This allows for
245+
// scrolling an outer element (like a nav panel w/ search) containing the toc.
246+
// Please pass an element, not a selector here.
247+
tocScrollingWrapper: null,
243248
// Offset for the toc scroll (top) position when scrolling the page.
244249
// Only effective if `disableTocScrollSync` is false.
245250
tocScrollOffset: 0,

data/README.json

+2-2
Large diffs are not rendered by default.

dist/tocbot.cjs

+10-8
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ __webpack_require__.r(__webpack_exports__);
457457
// Only takes affect when `tocSelector` is scrolling,
458458
// keep the toc scroll position in sync with the content.
459459
disableTocScrollSync: false,
460+
// If this is null then just use `tocElement` or `tocSelector` instead
461+
// assuming `disableTocScrollSync` is set to false. This allows for
462+
// scrolling an outer element (like a nav panel w/ search) containing the toc.
463+
tocScrollingWrapper: null,
460464
// Offset for the toc scroll (top) position when scrolling the page.
461465
// Only effective if `disableTocScrollSync` is false.
462466
tocScrollOffset: 0,
@@ -1075,21 +1079,19 @@ __webpack_require__.r(__webpack_exports__);
10751079
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
10761080
/* harmony export */ "default": () => (/* binding */ updateTocScroll)
10771081
/* harmony export */ });
1078-
/* eslint no-var: off */
1079-
10801082
const SCROLL_LEEWAY = 30
10811083
function updateTocScroll (options) {
1082-
var toc = options.tocElement || document.querySelector(options.tocSelector)
1084+
const toc = options.tocScrollingWrapper || options.tocElement || document.querySelector(options.tocSelector)
10831085
if (toc && toc.scrollHeight > toc.clientHeight) {
1084-
var activeItem = toc.querySelector('.' + options.activeListItemClass)
1086+
const activeItem = toc.querySelector(`.${options.activeListItemClass}`)
10851087
if (activeItem) {
10861088
// Determine container top and bottom
1087-
var cTop = toc.scrollTop
1088-
var cBottom = cTop + toc.clientHeight
1089+
const cTop = toc.scrollTop
1090+
const cBottom = cTop + toc.clientHeight
10891091

10901092
// Determine element top and bottom
1091-
var eTop = activeItem.offsetTop
1092-
var eBottom = eTop + activeItem.clientHeight
1093+
const eTop = activeItem.offsetTop
1094+
const eBottom = eTop + activeItem.clientHeight
10931095

10941096
// Check if out of view
10951097
// Above scroll view

dist/tocbot.js

+10-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tocbot.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/default-options.js

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export default {
102102
// Only takes affect when `tocSelector` is scrolling,
103103
// keep the toc scroll position in sync with the content.
104104
disableTocScrollSync: false,
105+
// If this is null then just use `tocElement` or `tocSelector` instead
106+
// assuming `disableTocScrollSync` is set to false. This allows for
107+
// scrolling an outer element (like a nav panel w/ search) containing the toc.
108+
tocScrollingWrapper: null,
105109
// Offset for the toc scroll (top) position when scrolling the page.
106110
// Only effective if `disableTocScrollSync` is false.
107111
tocScrollOffset: 0,

src/js/update-toc-scroll.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
/* eslint no-var: off */
2-
31
const SCROLL_LEEWAY = 30
42
export default function updateTocScroll (options) {
5-
var toc = options.tocElement || document.querySelector(options.tocSelector)
3+
const toc = options.tocScrollingWrapper || options.tocElement || document.querySelector(options.tocSelector)
64
if (toc && toc.scrollHeight > toc.clientHeight) {
7-
var activeItem = toc.querySelector('.' + options.activeListItemClass)
5+
const activeItem = toc.querySelector(`.${options.activeListItemClass}`)
86
if (activeItem) {
97
// Determine container top and bottom
10-
var cTop = toc.scrollTop
11-
var cBottom = cTop + toc.clientHeight
8+
const cTop = toc.scrollTop
9+
const cBottom = cTop + toc.clientHeight
1210

1311
// Determine element top and bottom
14-
var eTop = activeItem.offsetTop
15-
var eBottom = eTop + activeItem.clientHeight
12+
const eTop = activeItem.offsetTop
13+
const eBottom = eTop + activeItem.clientHeight
1614

1715
// Check if out of view
1816
// Above scroll view

0 commit comments

Comments
 (0)