@@ -519,12 +519,35 @@ aria-label="Show hidden lines"></button>';
519
519
const sidebar = document . getElementById ( 'sidebar' ) ;
520
520
const sidebarLinks = document . querySelectorAll ( '#sidebar a' ) ;
521
521
const sidebarToggleButton = document . getElementById ( 'sidebar-toggle' ) ;
522
- const sidebarToggleAnchor = document . getElementById ( 'sidebar-toggle-anchor' ) ;
523
522
const sidebarResizeHandle = document . getElementById ( 'sidebar-resize-handle' ) ;
523
+ const sidebarCheckbox = document . getElementById ( 'sidebar-toggle-anchor' ) ;
524
524
let firstContact = null ;
525
525
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
+
526
550
function showSidebar ( ) {
527
- body . classList . remove ( 'sidebar-hidden' ) ;
528
551
body . classList . add ( 'sidebar-visible' ) ;
529
552
Array . from ( sidebarLinks ) . forEach ( function ( link ) {
530
553
link . setAttribute ( 'tabIndex' , 0 ) ;
@@ -540,7 +563,6 @@ aria-label="Show hidden lines"></button>';
540
563
541
564
function hideSidebar ( ) {
542
565
body . classList . remove ( 'sidebar-visible' ) ;
543
- body . classList . add ( 'sidebar-hidden' ) ;
544
566
Array . from ( sidebarLinks ) . forEach ( function ( link ) {
545
567
link . setAttribute ( 'tabIndex' , - 1 ) ;
546
568
} ) ;
@@ -554,8 +576,8 @@ aria-label="Show hidden lines"></button>';
554
576
}
555
577
556
578
// Toggle sidebar
557
- sidebarToggleAnchor . addEventListener ( 'change' , function sidebarToggle ( ) {
558
- if ( sidebarToggleAnchor . checked ) {
579
+ sidebarCheckbox . addEventListener ( 'change' , function sidebarToggle ( ) {
580
+ if ( sidebarCheckbox . checked ) {
559
581
const current_width = parseInt (
560
582
document . documentElement . style . getPropertyValue ( '--sidebar-target-width' ) , 10 ) ;
561
583
if ( current_width < 150 ) {
@@ -579,7 +601,7 @@ aria-label="Show hidden lines"></button>';
579
601
if ( pos < 20 ) {
580
602
hideSidebar ( ) ;
581
603
} else {
582
- if ( body . classList . contains ( 'sidebar-hidden ' ) ) {
604
+ if ( ! body . classList . contains ( 'sidebar-visible ' ) ) {
583
605
showSidebar ( ) ;
584
606
}
585
607
pos = Math . min ( pos , window . innerWidth - 100 ) ;
@@ -765,7 +787,7 @@ aria-label="Show hidden lines"></button>';
765
787
let scrollTop = document . scrollingElement . scrollTop ;
766
788
let prevScrollTop = scrollTop ;
767
789
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).
769
791
menu . style . top = scrollTop + 'px' ;
770
792
// Same as parseInt(menu.style.top.slice(0, -2), but faster
771
793
let topCache = menu . style . top . slice ( 0 , - 2 ) ;
0 commit comments