Skip to content

Commit 1476ec7

Browse files
authored
Merge pull request #2725 from GuillaumeGomez/search-collapsed
Hide the sidebar when collapsed to prevent browser search to find text from it
2 parents e6315bf + d1c0979 commit 1476ec7

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
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: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,35 @@ 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+
// Workaround for Safari skipping the animation when changing
544+
// `display` and a transform in the same event loop. This forces a
545+
// reflow after updating the display.
546+
sidebar.offsetHeight;
547+
}
548+
});
549+
526550
function showSidebar() {
527-
body.classList.remove('sidebar-hidden');
528551
body.classList.add('sidebar-visible');
529552
Array.from(sidebarLinks).forEach(function(link) {
530553
link.setAttribute('tabIndex', 0);
@@ -540,7 +563,6 @@ aria-label="Show hidden lines"></button>';
540563

541564
function hideSidebar() {
542565
body.classList.remove('sidebar-visible');
543-
body.classList.add('sidebar-hidden');
544566
Array.from(sidebarLinks).forEach(function(link) {
545567
link.setAttribute('tabIndex', -1);
546568
});
@@ -554,8 +576,8 @@ aria-label="Show hidden lines"></button>';
554576
}
555577

556578
// Toggle sidebar
557-
sidebarToggleAnchor.addEventListener('change', function sidebarToggle() {
558-
if (sidebarToggleAnchor.checked) {
579+
sidebarCheckbox.addEventListener('change', function sidebarToggle() {
580+
if (sidebarCheckbox.checked) {
559581
const current_width = parseInt(
560582
document.documentElement.style.getPropertyValue('--sidebar-target-width'), 10);
561583
if (current_width < 150) {
@@ -579,7 +601,7 @@ aria-label="Show hidden lines"></button>';
579601
if (pos < 20) {
580602
hideSidebar();
581603
} else {
582-
if (body.classList.contains('sidebar-hidden')) {
604+
if (!body.classList.contains('sidebar-visible')) {
583605
showSidebar();
584606
}
585607
pos = Math.min(pos, window.innerWidth - 100);
@@ -765,7 +787,7 @@ aria-label="Show hidden lines"></button>';
765787
let scrollTop = document.scrollingElement.scrollTop;
766788
let prevScrollTop = scrollTop;
767789
const minMenuY = -menu.clientHeight - 50;
768-
// When the script loads, the page can be at any scroll (e.g. if you reforesh it).
790+
// When the script loads, the page can be at any scroll (e.g. if you refresh it).
769791
menu.style.top = scrollTop + 'px';
770792
// Same as parseInt(menu.style.top.slice(0, -2), but faster
771793
let topCache = menu.style.top.slice(0, -2);

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">

tests/gui/sidebar.goml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,39 @@ set-window-size: (1100, 600)
77
reload:
88

99
store-value: (content_indent, 308)
10+
store-value: (sidebar_storage_value, "mdbook-sidebar")
11+
store-value: (sidebar_storage_hidden_value, "hidden")
12+
store-value: (sidebar_storage_displayed_value, "visible")
1013

1114
define-function: (
1215
"hide-sidebar",
1316
[],
1417
block {
15-
// The content should be "moved" to the right because of the sidebar.
16-
assert-css: ("#sidebar", {"transform": "none"})
1718
assert-position: ("#page-wrapper", {"x": |content_indent|})
1819

1920
// We now hide the sidebar.
2021
click: "#sidebar-toggle"
21-
wait-for: "body.sidebar-hidden"
22-
// `transform` is 0.3s so we need to wait a bit (0.5s) to ensure the animation is done.
23-
wait-for: 5000
24-
assert-css-false: ("#sidebar", {"transform": "none"})
25-
// The page content should now be on the left.
26-
assert-position: ("#page-wrapper", {"x": 0})
22+
wait-for-css: ("#sidebar", {"display": "none"})
23+
assert-local-storage: {|sidebar_storage_value|: |sidebar_storage_hidden_value|}
2724
},
2825
)
2926

3027
define-function: (
3128
"show-sidebar",
3229
[],
3330
block {
34-
// The page content should be on the left and the sidebar "moved out".
35-
assert-css: ("#sidebar", {"transform": "matrix(1, 0, 0, 1, -308, 0)"})
31+
assert-css: ("#sidebar", {"display": "none"})
3632
assert-position: ("#page-wrapper", {"x": 0})
3733

3834
// We expand the sidebar.
3935
click: "#sidebar-toggle"
40-
wait-for: "body.sidebar-visible"
36+
wait-for-css-false: ("#sidebar", {"display": "none"})
4137
// `transform` is 0.3s so we need to wait a bit (0.5s) to ensure the animation is done.
4238
wait-for: 5000
4339
assert-css-false: ("#sidebar", {"transform": "matrix(1, 0, 0, 1, -308, 0)"})
4440
// The page content should be moved to the right.
4541
assert-position: ("#page-wrapper", {"x": |content_indent|})
42+
assert-local-storage: {|sidebar_storage_value|: |sidebar_storage_displayed_value|}
4643
},
4744
)
4845

@@ -54,3 +51,13 @@ set-window-size: (900, 600)
5451
reload:
5552
call-function: ("show-sidebar", {})
5653
call-function: ("hide-sidebar", {})
54+
55+
// We now test that if the sidebar is considered open and we reload the page, since
56+
// the width is small, it will still be collapsed.
57+
set-local-storage: {|sidebar_storage_value|: |sidebar_storage_displayed_value|}
58+
reload:
59+
// The stored value shouldn't have changed.
60+
assert-local-storage: {|sidebar_storage_value|: |sidebar_storage_displayed_value|}
61+
// But the sidebar should be hidden anyway.
62+
assert-css: ("#sidebar", {"display": "none"})
63+
assert-position: ("#page-wrapper", {"x": 0})

0 commit comments

Comments
 (0)