Hide the sidebar when collapsed to prevent browser search to find text from it

This commit is contained in:
Guillaume Gomez 2025-06-05 16:27:47 +02:00 committed by Eric Huss
parent f942f3835e
commit bcd4552bdf
3 changed files with 30 additions and 8 deletions

View file

@ -530,7 +530,6 @@ html:not(.sidebar-resizing) .sidebar {
/* sidebar-hidden */
#sidebar-toggle-anchor:not(:checked) ~ .sidebar {
transform: translateX(calc(0px - var(--sidebar-width) - var(--sidebar-resize-indicator-width)));
z-index: -1;
}
[dir=rtl] #sidebar-toggle-anchor:not(:checked) ~ .sidebar {
transform: translateX(calc(var(--sidebar-width) + var(--sidebar-resize-indicator-width)));

View file

@ -519,10 +519,30 @@ aria-label="Show hidden lines"></button>';
const sidebar = document.getElementById('sidebar');
const sidebarLinks = document.querySelectorAll('#sidebar a');
const sidebarToggleButton = document.getElementById('sidebar-toggle');
const sidebarToggleAnchor = document.getElementById('sidebar-toggle-anchor');
const sidebarResizeHandle = document.getElementById('sidebar-resize-handle');
const sidebarCheckbox = document.getElementById('sidebar-toggle-anchor');
let firstContact = null;
/* Because we cannot change the `display` using only CSS after/before the transition, we
need JS to do it. We change the display to prevent the browsers search to find text inside
the collapsed sidebar. */
if (!document.documentElement.classList.contains('sidebar-visible')) {
sidebar.style.display = 'none';
}
sidebar.addEventListener('transitionend', () => {
/* We only change the display to "none" if we're collapsing the sidebar. */
if (!sidebarCheckbox.checked) {
sidebar.style.display = 'none';
}
});
sidebarToggleButton.addEventListener('click', () => {
/* To allow the sidebar expansion animation, we first need to put back the display. */
if (!sidebarCheckbox.checked) {
sidebar.style.display = '';
}
});
function showSidebar() {
body.classList.add('sidebar-visible');
Array.from(sidebarLinks).forEach(function(link) {
@ -552,8 +572,8 @@ aria-label="Show hidden lines"></button>';
}
// Toggle sidebar
sidebarToggleAnchor.addEventListener('change', function sidebarToggle() {
if (sidebarToggleAnchor.checked) {
sidebarCheckbox.addEventListener('change', function sidebarToggle() {
if (sidebarCheckbox.checked) {
const current_width = parseInt(
document.documentElement.style.getPropertyValue('--sidebar-target-width'), 10);
if (current_width < 150) {
@ -577,7 +597,7 @@ aria-label="Show hidden lines"></button>';
if (pos < 20) {
hideSidebar();
} else {
if (body.classList.contains('sidebar-hidden')) {
if (!body.classList.contains('sidebar-visible')) {
showSidebar();
}
pos = Math.min(pos, window.innerWidth - 100);

View file

@ -116,10 +116,13 @@
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
sidebar_toggle.checked = false;
}
sidebar_toggle.checked = sidebar === 'visible';
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
}
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">