-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widget): toc expand on smooth scroll
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
(function (window, document) { | ||
function register($toc) { | ||
const currentInView = new Set(); | ||
const headingToMenu = new Map(); | ||
const $menus = Array.from($toc.querySelectorAll('.menu-list > li > a')); | ||
|
||
for (const $menu of $menus) { | ||
const elementId = $menu.getAttribute('href').trim().slice(1); | ||
const $heading = document.getElementById(elementId); | ||
if ($heading) { | ||
headingToMenu.set($heading, $menu); | ||
} | ||
} | ||
|
||
const $headings = Array.from(headingToMenu.keys()); | ||
|
||
const callback = (entries) => { | ||
for (const entry of entries) { | ||
if (entry.isIntersecting) { | ||
currentInView.add(entry.target); | ||
} else { | ||
currentInView.delete(entry.target); | ||
} | ||
} | ||
let $heading; | ||
if (currentInView.size) { | ||
// heading is the first in-view heading | ||
$heading = [...currentInView].sort(($el1, $el2) => $el1.offsetTop - $el2.offsetTop)[0]; | ||
} else if ($headings.length) { | ||
// heading is the closest heading above the viewport top | ||
$heading = $headings | ||
.filter(($heading) => $heading.offsetTop < window.scrollY) | ||
.sort(($el1, $el2) => $el2.offsetTop - $el1.offsetTop)[0]; | ||
} | ||
if ($heading && headingToMenu.has($heading)) { | ||
$menus.forEach(($menu) => $menu.classList.remove('is-active')); | ||
|
||
const $menu = headingToMenu.get($heading); | ||
$menu.classList.add('is-active'); | ||
let $menuList = $menu.parentElement.parentElement; | ||
while ( | ||
$menuList.classList.contains('menu-list') && | ||
$menuList.parentElement.tagName.toLowerCase() === 'li' | ||
) { | ||
$menuList.parentElement.children[0].classList.add('is-active'); | ||
$menuList = $menuList.parentElement.parentElement; | ||
} | ||
} | ||
}; | ||
const observer = new IntersectionObserver(callback, { threshold: 0 }); | ||
|
||
for (const $heading of $headings) { | ||
observer.observe($heading); | ||
// smooth scroll to the heading | ||
if (headingToMenu.has($heading)) { | ||
const $menu = headingToMenu.get($heading); | ||
$menu.setAttribute('data-href', $menu.getAttribute('href')); | ||
$menu.setAttribute('href', 'javascript:;'); | ||
$menu.addEventListener('click', () => { | ||
if (typeof $heading.scrollIntoView === 'function') { | ||
$heading.scrollIntoView({ behavior: 'smooth' }); | ||
} | ||
}); | ||
$heading.style.scrollMargin = '1em'; | ||
} | ||
} | ||
} | ||
|
||
if (typeof window.IntersectionObserver === 'undefined') { | ||
return; | ||
} | ||
|
||
document.querySelectorAll('#toc').forEach(register); | ||
})(window, document); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a42c10a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#10
a42c10a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ppoffice/hexo-theme-icarus#745