Skip to content

Commit 6f46113

Browse files
committed
refactor: improve toc popup module
1 parent 03e302c commit 6f46113

File tree

4 files changed

+49
-34
lines changed

4 files changed

+49
-34
lines changed

_javascript/modules/components/toc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const desktopMode = matchMedia('(min-width: 1200px)');
55

66
function refresh(e) {
77
if (e.matches) {
8-
mobile.hidePopup();
8+
if (mobile.popupOpened) {
9+
mobile.hidePopup();
10+
}
11+
912
desktop.refresh();
1013
} else {
1114
mobile.refresh();

_javascript/modules/components/toc/toc-mobile.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const SCROLL_LOCK = 'overflow-hidden';
1212
const CLOSING = 'closing';
1313

1414
export class TocMobile {
15-
static invisible = true;
16-
static barHeight = 16 * 3; // 3rem
15+
static #invisible = true;
16+
static #barHeight = 16 * 3; // 3rem
1717

1818
static options = {
1919
tocSelector: '#toc-popup-content',
@@ -23,7 +23,7 @@ export class TocMobile {
2323
orderedList: false,
2424
scrollSmooth: false,
2525
collapseDepth: 4,
26-
headingsOffset: this.barHeight
26+
headingsOffset: this.#barHeight
2727
};
2828

2929
static initBar() {
@@ -33,44 +33,40 @@ export class TocMobile {
3333
$tocBar.classList.toggle('invisible', entry.isIntersecting);
3434
});
3535
},
36-
{ rootMargin: `-${this.barHeight}px 0px 0px 0px` }
36+
{ rootMargin: `-${this.#barHeight}px 0px 0px 0px` }
3737
);
3838

3939
observer.observe($soloTrigger);
40-
this.invisible = false;
40+
this.#invisible = false;
4141
}
4242

4343
static listenAnchors() {
4444
const $anchors = document.getElementsByClassName('toc-link');
4545
[...$anchors].forEach((anchor) => {
46-
anchor.onclick = this.hidePopup;
46+
anchor.onclick = () => this.hidePopup();
4747
});
4848
}
4949

5050
static refresh() {
51-
if (this.invisible) {
51+
if (this.#invisible) {
5252
this.initComponents();
5353
}
5454
tocbot.refresh(this.options);
5555
this.listenAnchors();
5656
}
5757

58+
static get popupOpened() {
59+
return $popup.open;
60+
}
61+
5862
static showPopup() {
59-
TocMobile.lockScroll(true);
63+
this.lockScroll(true);
6064
$popup.showModal();
6165
const activeItem = $popup.querySelector('li.is-active-li');
6266
activeItem.scrollIntoView({ block: 'center' });
6367
}
6468

65-
static hidePopup(event) {
66-
if (event?.type === 'cancel') {
67-
event.preventDefault();
68-
}
69-
70-
if (!$popup.open) {
71-
return;
72-
}
73-
69+
static hidePopup() {
7470
$popup.toggleAttribute(CLOSING);
7571

7672
$popup.addEventListener(
@@ -82,7 +78,7 @@ export class TocMobile {
8278
{ once: true }
8379
);
8480

85-
TocMobile.lockScroll(false);
81+
this.lockScroll(false);
8682
}
8783

8884
static lockScroll(enable) {
@@ -91,26 +87,34 @@ export class TocMobile {
9187
}
9288

9389
static clickBackdrop(event) {
90+
if ($popup.hasAttribute(CLOSING)) {
91+
return;
92+
}
93+
9494
const rect = event.target.getBoundingClientRect();
9595
if (
9696
event.clientX < rect.left ||
9797
event.clientX > rect.right ||
9898
event.clientY < rect.top ||
9999
event.clientY > rect.bottom
100100
) {
101-
TocMobile.hidePopup();
101+
this.hidePopup();
102102
}
103103
}
104104

105105
static initComponents() {
106106
this.initBar();
107107

108108
[...$triggers].forEach((trigger) => {
109-
trigger.onclick = this.showPopup;
109+
trigger.onclick = () => this.showPopup();
110110
});
111111

112-
$popup.onclick = this.clickBackdrop;
113-
$btnClose.onclick = $popup.oncancel = this.hidePopup;
112+
$popup.onclick = (e) => this.clickBackdrop(e);
113+
$btnClose.onclick = () => this.hidePopup();
114+
$popup.oncancel = (e) => {
115+
e.preventDefault();
116+
this.hidePopup();
117+
};
114118
}
115119

116120
static init() {

_layouts/post.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ <h1 data-toc-skip>{{ page.title }}</h1>
100100
{% if enable_toc %}
101101
<div id="toc-bar" class="d-flex align-items-center justify-content-between invisible">
102102
<span class="label text-truncate">{{ page.title }}</span>
103-
<button type="button" class="toc-trigger btn btn-link me-1">
103+
<button type="button" class="toc-trigger btn me-1">
104104
<i class="fa-solid fa-list-ul fa-fw"></i>
105105
</button>
106106
</div>
@@ -113,8 +113,8 @@ <h1 data-toc-skip>{{ page.title }}</h1>
113113
<dialog id="toc-popup" class="p-0">
114114
<div class="header d-flex flex-row align-items-center justify-content-between">
115115
<div class="label text-truncate py-2 ms-4">{{- page.title -}}</div>
116-
<button id="toc-popup-close" type="button" class="btn btn-link">
117-
<i class="fas fa-close fa-fw"></i>
116+
<button id="toc-popup-close" type="button" class="btn mx-1 my-1 opacity-75">
117+
<i class="fas fa-close"></i>
118118
</button>
119119
</div>
120120
<div id="toc-popup-content" class="px-4 py-3 pb-4"></div>

_sass/layout/post.scss

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,13 @@ header {
380380
$slide-in: slide-in 0.3s ease-out;
381381
$slide-out: slide-out 0.3s ease-out;
382382
$curtain-height: 2rem;
383+
$backdrop: blur(5px);
383384

384385
border-color: var(--toc-popup-border-color);
385386
border-width: 1px;
386387
border-radius: $radius-lg;
387388
color: var(--text-color);
388-
background: var(--main-bg);
389+
background: var(--card-bg);
389390
margin-top: $topbar-height;
390391
min-width: 20rem;
391392
font-size: 1.05rem;
@@ -422,8 +423,15 @@ header {
422423
}
423424
}
424425

425-
button:focus-visible {
426-
box-shadow: none;
426+
button {
427+
> i {
428+
font-size: 1.25rem;
429+
vertical-align: middle;
430+
}
431+
432+
&:focus-visible {
433+
box-shadow: none;
434+
}
427435
}
428436

429437
ul {
@@ -461,20 +469,20 @@ header {
461469
}
462470

463471
&::-webkit-backdrop {
464-
-webkit-backdrop-filter: blur(5px);
465-
backdrop-filter: blur(5px);
472+
-webkit-backdrop-filter: $backdrop;
473+
backdrop-filter: $backdrop;
466474
}
467475

468476
&::backdrop {
469-
-webkit-backdrop-filter: blur(5px);
470-
backdrop-filter: blur(5px);
477+
-webkit-backdrop-filter: $backdrop;
478+
backdrop-filter: $backdrop;
471479
}
472480

473481
&::after {
474482
display: flex;
475483
content: '';
476484
position: relative;
477-
background: linear-gradient(transparent, var(--main-bg) 70%);
485+
background: linear-gradient(transparent, var(--card-bg) 70%);
478486
height: $curtain-height;
479487
}
480488

0 commit comments

Comments
 (0)