Skip to content

Commit 363c8c9

Browse files
authored
fix: scroll bug due to incorrect toggle state (#186)
* fix: add mobile menu overlay to toggle body scroll * chore: update styles
1 parent 088e9f7 commit 363c8c9

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

assets/css/compiled/main.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ video {
499499
.pointer-events-none {
500500
pointer-events: none;
501501
}
502+
.fixed {
503+
position: fixed;
504+
}
502505
.absolute {
503506
position: absolute;
504507
}
@@ -508,6 +511,9 @@ video {
508511
.sticky {
509512
position: sticky;
510513
}
514+
.inset-0 {
515+
inset: 0px;
516+
}
511517
.inset-x-0 {
512518
left: 0px;
513519
right: 0px;
@@ -543,6 +549,9 @@ video {
543549
.top-full {
544550
top: 100%;
545551
}
552+
.z-10 {
553+
z-index: 10;
554+
}
546555
.z-20 {
547556
z-index: 20;
548557
}
@@ -966,6 +975,9 @@ video {
966975
--tw-border-opacity: 1;
967976
border-color: rgb(254 249 195 / var(--tw-border-opacity));
968977
}
978+
.bg-black\/80 {
979+
background-color: rgb(0 0 0 / 0.8);
980+
}
969981
.bg-black\/\[\.05\] {
970982
background-color: rgb(0 0 0 / .05);
971983
}
@@ -1399,6 +1411,9 @@ video {
13991411
.\[text-underline-position\:from-font\] {
14001412
text-underline-position: from-font;
14011413
}
1414+
.\[transition\:background-color_1\.5s_ease\] {
1415+
transition: background-color 1.5s ease;
1416+
}
14021417
.\[word-break\:break-word\] {
14031418
word-break: break-word;
14041419
}
@@ -2990,6 +3005,9 @@ body {
29903005
:is(html[class~="dark"] .dark\:border-yellow-200\/30) {
29913006
border-color: rgb(254 240 138 / 0.3);
29923007
}
3008+
:is(html[class~="dark"] .dark\:bg-black\/60) {
3009+
background-color: rgb(0 0 0 / 0.6);
3010+
}
29933011
:is(html[class~="dark"] .dark\:bg-blue-900\/30) {
29943012
background-color: rgb(30 58 138 / 0.3);
29953013
}

assets/js/menu.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
// Hamburger menu for mobile navigation
22

3-
const menu = document.querySelector('.hamburger-menu');
4-
5-
menu.addEventListener('click', (e) => {
6-
e.preventDefault();
3+
document.addEventListener('DOMContentLoaded', function () {
4+
const menu = document.querySelector('.hamburger-menu');
5+
const overlay = document.querySelector('.mobile-menu-overlay');
76
const sidebarContainer = document.querySelector('.sidebar-container');
87

9-
// Toggle the hamburger menu
10-
menu.querySelector('svg').classList.toggle('open');
8+
// Initialize the overlay
9+
const overlayClasses = ['fixed', 'inset-0', 'z-10', 'bg-black/80', 'dark:bg-black/60'];
10+
overlay.classList.add('bg-transparent');
11+
overlay.classList.remove("hidden", ...overlayClasses);
12+
13+
function toggleMenu() {
14+
// Toggle the hamburger menu
15+
menu.querySelector('svg').classList.toggle('open');
16+
17+
// When the menu is open, we want to show the navigation sidebar
18+
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,-100%,0)]');
19+
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,0,0)]');
20+
21+
// When the menu is open, we want to prevent the body from scrolling
22+
document.body.classList.toggle('overflow-hidden');
23+
document.body.classList.toggle('md:overflow-auto');
24+
}
25+
26+
menu.addEventListener('click', (e) => {
27+
e.preventDefault();
28+
toggleMenu();
29+
30+
if (overlay.classList.contains('bg-transparent')) {
31+
// Show the overlay
32+
overlay.classList.add(...overlayClasses);
33+
overlay.classList.remove('bg-transparent');
34+
} else {
35+
// Hide the overlay
36+
overlay.classList.remove(...overlayClasses);
37+
overlay.classList.add('bg-transparent');
38+
}
39+
});
1140

12-
// When the menu is open, we want to show the navigation sidebar
13-
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,-100%,0)]');
14-
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,0,0)]');
41+
overlay.addEventListener('click', (e) => {
42+
e.preventDefault();
43+
toggleMenu();
1544

16-
// When the menu is open, we want to prevent the body from scrolling
17-
document.body.classList.toggle('overflow-hidden');
18-
document.body.classList.toggle('md:overflow-auto');
45+
// Hide the overlay
46+
overlay.classList.remove(...overlayClasses);
47+
overlay.classList.add('bg-transparent');
48+
});
1949
});

exampleSite/hugo_stats.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"[counter-reset:step]",
6868
"[hyphens:auto]",
6969
"[text-underline-position:from-font]",
70+
"[transition:background-color_1.5s_ease]",
7071
"[word-break:break-word]",
7172
"absolute",
7273
"active:bg-gray-400/20",
@@ -90,6 +91,7 @@
9091
"before:pointer-events-none",
9192
"before:transition-transform",
9293
"before:w-px",
94+
"bg-black/80",
9395
"bg-black/[.05]",
9496
"bg-blue-100",
9597
"bg-clip-text",
@@ -159,6 +161,7 @@
159161
"cursor-pointer",
160162
"dark:before:bg-neutral-800",
161163
"dark:before:invert",
164+
"dark:bg-black/60",
162165
"dark:bg-blue-900/30",
163166
"dark:bg-dark",
164167
"dark:bg-dark/50",
@@ -235,6 +238,7 @@
235238
"ease-in",
236239
"filename",
237240
"first:mt-0",
241+
"fixed",
238242
"flex",
239243
"flex-col",
240244
"focus:bg-white",
@@ -311,6 +315,7 @@
311315
"inline",
312316
"inline-block",
313317
"inline-flex",
318+
"inset-0",
314319
"inset-x-0",
315320
"inset-y-0",
316321
"items-center",
@@ -401,6 +406,7 @@
401406
"min-w-full",
402407
"ml-1",
403408
"ml-4",
409+
"mobile-menu-overlay",
404410
"mr-2",
405411
"mt-1",
406412
"mt-1.5",
@@ -585,6 +591,7 @@
585591
"w-screen",
586592
"whitespace-nowrap",
587593
"xl:block",
594+
"z-10",
588595
"z-20",
589596
"z-[-1]"
590597
],

layouts/partials/sidebar.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{{- $pageURL := $context.RelPermalink -}}
1111

1212

13+
<div class="mobile-menu-overlay [transition:background-color_1.5s_ease] fixed inset-0 z-10 bg-black/80 dark:bg-black/60 hidden"></div>
1314
<aside class="sidebar-container flex flex-col print:hidden md:top-16 md:shrink-0 md:w-64 md:self-start max-md:[transform:translate3d(0,-100%,0)] {{ $sidebarClass }}">
1415
<!-- Search bar on small screen -->
1516
<div class="px-4 pt-4 md:hidden">

0 commit comments

Comments
 (0)