Skip to content

Commit a2352e6

Browse files
Hide the sidebar when collapsed to prevent browser search to find text from it
1 parent bd4b94b commit a2352e6

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/front-end/css/chrome.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ html:not(.sidebar-resizing) .sidebar {
530530
/* sidebar-hidden */
531531
#sidebar-toggle-anchor:not(:checked) ~ .sidebar {
532532
transform: translateX(calc(0px - var(--sidebar-width) - var(--sidebar-resize-indicator-width)));
533-
z-index: -1;
534533
}
535534
[dir=rtl] #sidebar-toggle-anchor:not(:checked) ~ .sidebar {
536535
transform: translateX(calc(var(--sidebar-width) + var(--sidebar-resize-indicator-width)));

src/front-end/js/book.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,29 @@ aria-label="Show hidden lines"></button>';
521521
const sidebarToggleButton = document.getElementById('sidebar-toggle');
522522
const sidebarToggleAnchor = document.getElementById('sidebar-toggle-anchor');
523523
const sidebarResizeHandle = document.getElementById('sidebar-resize-handle');
524+
const sidebarCheckbox = document.getElementById('sidebar-toggle-anchor');
524525
let firstContact = null;
525526

527+
528+
/* Because we cannot change the `display` using only CSS after/before the transition, we
529+
need JS to do it. We change the display to prevent the browsers search to find text inside
530+
the collapsed sidebar. */
531+
if (!document.documentElement.classList.contains('sidebar-visible')) {
532+
sidebar.style.display = 'none';
533+
}
534+
sidebar.addEventListener('transitionend', () => {
535+
/* We only change the display to "none" if we're collapsing the sidebar. */
536+
if (!sidebarCheckbox.checked) {
537+
sidebar.style.display = 'none';
538+
}
539+
});
540+
sidebarToggleButton.addEventListener('click', () => {
541+
/* To allow the sidebar expansion animation, we first need to put back the display. */
542+
if (!sidebarCheckbox.checked) {
543+
sidebar.style.display = '';
544+
}
545+
});
546+
526547
function showSidebar() {
527548
body.classList.add('sidebar-visible');
528549
Array.from(sidebarLinks).forEach(function(link) {
@@ -538,6 +559,7 @@ aria-label="Show hidden lines"></button>';
538559
}
539560

540561
function hideSidebar() {
562+
console.trace();
541563
body.classList.remove('sidebar-visible');
542564
Array.from(sidebarLinks).forEach(function(link) {
543565
link.setAttribute('tabIndex', -1);
@@ -577,7 +599,7 @@ aria-label="Show hidden lines"></button>';
577599
if (pos < 20) {
578600
hideSidebar();
579601
} else {
580-
if (body.classList.contains('sidebar-hidden')) {
602+
if (!body.classList.contains('sidebar-visible')) {
581603
showSidebar();
582604
}
583605
pos = Math.min(pos, window.innerWidth - 100);

src/front-end/templates/index.hbs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@
117117
} else {
118118
sidebar = 'hidden';
119119
}
120-
sidebar_toggle.checked = sidebar === 'visible';
121-
html.classList.remove('sidebar-visible');
122-
html.classList.add("sidebar-" + sidebar);
120+
if (sidebar === 'visible') {
121+
sidebar_toggle.checked = true;
122+
} else {
123+
html.classList.remove('sidebar-visible');
124+
}
123125
</script>
124126

125127
<nav id="sidebar" class="sidebar" aria-label="Table of contents">

0 commit comments

Comments
 (0)