Skip to content

Commit bcd4552

Browse files
GuillaumeGomezehuss
authored andcommitted
Hide the sidebar when collapsed to prevent browser search to find text from it
1 parent f942f38 commit bcd4552

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
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: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,30 @@ aria-label="Show hidden lines"></button>';
519519
const sidebar = document.getElementById('sidebar');
520520
const sidebarLinks = document.querySelectorAll('#sidebar a');
521521
const sidebarToggleButton = document.getElementById('sidebar-toggle');
522-
const sidebarToggleAnchor = document.getElementById('sidebar-toggle-anchor');
523522
const sidebarResizeHandle = document.getElementById('sidebar-resize-handle');
523+
const sidebarCheckbox = document.getElementById('sidebar-toggle-anchor');
524524
let firstContact = null;
525525

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

554574
// Toggle sidebar
555-
sidebarToggleAnchor.addEventListener('change', function sidebarToggle() {
556-
if (sidebarToggleAnchor.checked) {
575+
sidebarCheckbox.addEventListener('change', function sidebarToggle() {
576+
if (sidebarCheckbox.checked) {
557577
const current_width = parseInt(
558578
document.documentElement.style.getPropertyValue('--sidebar-target-width'), 10);
559579
if (current_width < 150) {
@@ -577,7 +597,7 @@ aria-label="Show hidden lines"></button>';
577597
if (pos < 20) {
578598
hideSidebar();
579599
} else {
580-
if (body.classList.contains('sidebar-hidden')) {
600+
if (!body.classList.contains('sidebar-visible')) {
581601
showSidebar();
582602
}
583603
pos = Math.min(pos, window.innerWidth - 100);

src/front-end/templates/index.hbs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@
116116
sidebar = sidebar || 'visible';
117117
} else {
118118
sidebar = 'hidden';
119+
sidebar_toggle.checked = false;
120+
}
121+
if (sidebar === 'visible') {
122+
sidebar_toggle.checked = true;
123+
} else {
124+
html.classList.remove('sidebar-visible');
119125
}
120-
sidebar_toggle.checked = sidebar === 'visible';
121-
html.classList.remove('sidebar-visible');
122-
html.classList.add("sidebar-" + sidebar);
123126
</script>
124127

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

0 commit comments

Comments
 (0)