@@ -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 ) ;
0 commit comments