Skip to content

Commit c842b5d

Browse files
committed
Fix sidebar animation and other behavior
This fixes several issues with how the sidebar was behaving: - Manually resizing the sidebar was incorrectly applying transition animations to the page-wrapper causing awkward movement. - Clicking the sidebar toggle caused the menu bar to behave differently compared to loading a page with the sidebar visible or hidden. - page-wrapper animation wasn't working when JS was disabled. - RTL sidebar animation was broken. Most of these issues stem from #2454 which moved `js` and `sidebar-visible` classes from `<body>` to `<html>`, but failed to update some of the JS and CSS code that was still assuming it was on the body. #1641 previously moved `js` from `<html>` to `<body>` with the reasoning "This will be necessary for using CSS selectors on root attributes.". However, I don't see how that is absolutely necessary, since selectors like `[dir=rtl].js` should work to select the root element.
1 parent 0ac89dd commit c842b5d

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/front-end/css/general.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ h6:target::before {
8686
box-sizing: border-box;
8787
background-color: var(--bg);
8888
}
89-
.no-js .page-wrapper,
89+
html:not(.js) .page-wrapper,
9090
.js:not(.sidebar-resizing) .page-wrapper {
9191
transition: margin-left 0.3s ease, transform 0.3s ease; /* Animation: slide away */
9292
}
93-
[dir=rtl] .js:not(.sidebar-resizing) .page-wrapper {
93+
[dir=rtl]:not(.js) .page-wrapper,
94+
[dir=rtl].js:not(.sidebar-resizing) .page-wrapper {
9495
transition: margin-right 0.3s ease, transform 0.3s ease; /* Animation: slide away */
9596
}
9697

src/front-end/js/book.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,6 @@ aria-label="Show hidden lines"></button>';
515515
})();
516516

517517
(function sidebar() {
518-
const body = document.querySelector('body');
519518
const sidebar = document.getElementById('sidebar');
520519
const sidebarLinks = document.querySelectorAll('#sidebar a');
521520
const sidebarToggleButton = document.getElementById('sidebar-toggle');
@@ -548,7 +547,7 @@ aria-label="Show hidden lines"></button>';
548547
});
549548

550549
function showSidebar() {
551-
body.classList.add('sidebar-visible');
550+
document.documentElement.classList.add('sidebar-visible');
552551
Array.from(sidebarLinks).forEach(function(link) {
553552
link.setAttribute('tabIndex', 0);
554553
});
@@ -562,7 +561,7 @@ aria-label="Show hidden lines"></button>';
562561
}
563562

564563
function hideSidebar() {
565-
body.classList.remove('sidebar-visible');
564+
document.documentElement.classList.remove('sidebar-visible');
566565
Array.from(sidebarLinks).forEach(function(link) {
567566
link.setAttribute('tabIndex', -1);
568567
});
@@ -594,14 +593,14 @@ aria-label="Show hidden lines"></button>';
594593
function initResize() {
595594
window.addEventListener('mousemove', resize, false);
596595
window.addEventListener('mouseup', stopResize, false);
597-
body.classList.add('sidebar-resizing');
596+
document.documentElement.classList.add('sidebar-resizing');
598597
}
599598
function resize(e) {
600599
let pos = e.clientX - sidebar.offsetLeft;
601600
if (pos < 20) {
602601
hideSidebar();
603602
} else {
604-
if (!body.classList.contains('sidebar-visible')) {
603+
if (!document.documentElement.classList.contains('sidebar-visible')) {
605604
showSidebar();
606605
}
607606
pos = Math.min(pos, window.innerWidth - 100);
@@ -610,7 +609,7 @@ aria-label="Show hidden lines"></button>';
610609
}
611610
//on mouseup remove windows functions mousemove & mouseup
612611
function stopResize() {
613-
body.classList.remove('sidebar-resizing');
612+
document.documentElement.classList.remove('sidebar-resizing');
614613
window.removeEventListener('mousemove', resize, false);
615614
window.removeEventListener('mouseup', stopResize, false);
616615
}

0 commit comments

Comments
 (0)