-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show toc on mobile screens (#1964)
- Loading branch information
Showing
13 changed files
with
430 additions
and
30 deletions.
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,10 @@ | ||
{% comment %} | ||
Determine TOC state and return it through variable "enable_toc" | ||
{% endcomment %} | ||
|
||
{% assign enable_toc = false %} | ||
{% if site.toc and page.toc %} | ||
{% if page.content contains '<h2' or page.content contains '<h3' %} | ||
{% assign enable_toc = true %} | ||
{% endif %} | ||
{% endif %} |
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
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 |
---|---|---|
@@ -1,15 +1,30 @@ | ||
export function toc() { | ||
if (document.querySelector('main h2, main h3')) { | ||
// see: https://github.com/tscanlin/tocbot#usage | ||
tocbot.init({ | ||
tocSelector: '#toc', | ||
contentSelector: '.content', | ||
ignoreSelector: '[data-toc-skip]', | ||
headingSelector: 'h2, h3, h4', | ||
orderedList: false, | ||
scrollSmooth: false | ||
}); | ||
|
||
document.getElementById('toc-wrapper').classList.remove('d-none'); | ||
import { TocMobile as mobile } from './toc/toc-mobile'; | ||
import { TocDesktop as desktop } from './toc/toc-desktop'; | ||
|
||
const desktopMode = matchMedia('(min-width: 1200px)'); | ||
|
||
function refresh(e) { | ||
if (e.matches) { | ||
mobile.hidePopup(); | ||
desktop.refresh(); | ||
} else { | ||
mobile.refresh(); | ||
} | ||
} | ||
|
||
function init() { | ||
if (document.querySelector('main>article[data-toc="true"]') === null) { | ||
return; | ||
} | ||
|
||
// Avoid create multiple instances of Tocbot. Ref: <https://github.com/tscanlin/tocbot/issues/203> | ||
if (desktopMode.matches) { | ||
desktop.init(); | ||
} else { | ||
mobile.init(); | ||
} | ||
|
||
desktopMode.onchange = refresh; | ||
} | ||
|
||
export { init as initToc }; |
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,22 @@ | ||
export class TocDesktop { | ||
/* Tocbot options Ref: https://github.com/tscanlin/tocbot#usage */ | ||
static options = { | ||
tocSelector: '#toc', | ||
contentSelector: '.content', | ||
ignoreSelector: '[data-toc-skip]', | ||
headingSelector: 'h2, h3, h4', | ||
orderedList: false, | ||
scrollSmooth: false, | ||
headingsOffset: 16 * 2 // 2rem | ||
}; | ||
|
||
static refresh() { | ||
tocbot.refresh(this.options); | ||
} | ||
|
||
static init() { | ||
if (document.getElementById('toc-wrapper')) { | ||
tocbot.init(this.options); | ||
} | ||
} | ||
} |
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,117 @@ | ||
/** | ||
* TOC button, topbar and popup for mobile devices | ||
*/ | ||
|
||
const $tocBar = document.getElementById('toc-bar'); | ||
const $soloTrigger = document.getElementById('toc-solo-trigger'); | ||
const $triggers = document.getElementsByClassName('toc-trigger'); | ||
const $popup = document.getElementById('toc-popup'); | ||
const $btnClose = document.getElementById('toc-popup-close'); | ||
|
||
const SCROLL_LOCK = 'overflow-hidden'; | ||
const CLOSING = 'closing'; | ||
|
||
export class TocMobile { | ||
static invisible = true; | ||
static barHeight = 16 * 3; // 3rem | ||
|
||
static options = { | ||
tocSelector: '#toc-popup-content', | ||
contentSelector: '.content', | ||
ignoreSelector: '[data-toc-skip]', | ||
headingSelector: 'h2, h3, h4', | ||
orderedList: false, | ||
scrollSmooth: false, | ||
collapseDepth: 4, | ||
headingsOffset: this.barHeight | ||
}; | ||
|
||
static initBar() { | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach((entry) => { | ||
$tocBar.classList.toggle('invisible', entry.isIntersecting); | ||
}); | ||
}, | ||
{ rootMargin: `-${this.barHeight}px 0px 0px 0px` } | ||
); | ||
|
||
observer.observe($soloTrigger); | ||
this.invisible = false; | ||
} | ||
|
||
static listenAnchors() { | ||
const $anchors = document.getElementsByClassName('toc-link'); | ||
[...$anchors].forEach((anchor) => { | ||
anchor.onclick = this.hidePopup; | ||
}); | ||
} | ||
|
||
static refresh() { | ||
if (this.invisible) { | ||
this.initComponents(); | ||
} | ||
tocbot.refresh(this.options); | ||
this.listenAnchors(); | ||
} | ||
|
||
static showPopup() { | ||
TocMobile.lockScroll(true); | ||
$popup.showModal(); | ||
const activeItem = $popup.querySelector('li.is-active-li'); | ||
activeItem.scrollIntoView({ block: 'center' }); | ||
} | ||
|
||
static hidePopup() { | ||
if (!$popup.open) { | ||
return; | ||
} | ||
|
||
$popup.toggleAttribute(CLOSING); | ||
|
||
$popup.addEventListener( | ||
'animationend', | ||
() => { | ||
$popup.toggleAttribute(CLOSING); | ||
$popup.close(); | ||
}, | ||
{ once: true } | ||
); | ||
|
||
TocMobile.lockScroll(false); | ||
} | ||
|
||
static lockScroll(enable) { | ||
document.documentElement.classList.toggle(SCROLL_LOCK, enable); | ||
document.body.classList.toggle(SCROLL_LOCK, enable); | ||
} | ||
|
||
static clickBackdrop(event) { | ||
const rect = event.target.getBoundingClientRect(); | ||
if ( | ||
event.clientX < rect.left || | ||
event.clientX > rect.right || | ||
event.clientY < rect.top || | ||
event.clientY > rect.bottom | ||
) { | ||
TocMobile.hidePopup(); | ||
} | ||
} | ||
|
||
static initComponents() { | ||
this.initBar(); | ||
|
||
[...$triggers].forEach((trigger) => { | ||
trigger.onclick = this.showPopup; | ||
}); | ||
|
||
$popup.onclick = this.clickBackdrop; | ||
$btnClose.onclick = $popup.oncancel = this.hidePopup; | ||
} | ||
|
||
static init() { | ||
tocbot.init(this.options); | ||
this.listenAnchors(); | ||
this.initComponents(); | ||
} | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.