@@ -240,7 +240,11 @@ function addScrollTopBehavior() {
240240 var lastTopbarVisible = null ;
241241 var lastHeaderVisible = null ;
242242 var lastScrollY = window . scrollY || window . pageYOffset || 0 ;
243- var minDelta = 6 ; // px minimal upward movement to trigger show mid-page
243+ // Accumulated scroll thresholds (px)
244+ var UP_THRESHOLD = 14 ;
245+ var DOWN_THRESHOLD = 20 ;
246+ var accum = 0 ; // accumulated dy in the current direction
247+ var dirSign = 0 ; // 1 for up, -1 for down, 0 for none
244248 var lastVisible = null ; // persistent desired visibility state
245249
246250 function isTopbarMenuOpen ( ) {
@@ -251,8 +255,15 @@ function addScrollTopBehavior() {
251255 }
252256
253257 function isDocSidebarVisible ( ) {
258+ // Prefer layout state from .docs-main
259+ var main = document . querySelector ( '.docs-main' ) ;
260+ if ( main && main . classList . contains ( 'sidebar-hidden' ) ) {
261+ return false ;
262+ }
254263 var sidebar = document . querySelector ( '.docs-sidebar' ) ;
255264 if ( ! sidebar ) return false ;
265+ // If both 'hidden' and 'visible' are present, treat as hidden
266+ if ( sidebar . classList . contains ( 'hidden' ) ) return false ;
256267 return sidebar . classList . contains ( 'visible' ) ;
257268 }
258269
@@ -288,29 +299,45 @@ function addScrollTopBehavior() {
288299
289300 function computeDesiredVisibility ( ) {
290301 var pos = atTopOrBottom ( threshold ) ;
291- var dy = lastScrollY - pos . y ;
292- var directionUp = dy > minDelta ;
293- var directionDown = dy < - minDelta ;
302+ var dy = lastScrollY - pos . y ; // positive when scrolling up
294303
295304 // If menus are open, always visible
296305 if ( isTopbarMenuOpen ( ) || isDocSidebarVisible ( ) ) {
306+ // reset accumulation so we don't surprise after closing
307+ accum = 0 ; dirSign = 0 ;
297308 return { visible : true , atBottom : false , atTop : pos . atTop , y : pos . y } ;
298309 }
299310
300311 // At top or bottom within threshold => visible
301312 if ( pos . atTop || pos . atBottom ) {
313+ accum = 0 ; dirSign = 0 ;
302314 return { visible : true , atBottom : pos . atBottom , atTop : pos . atTop , y : pos . y } ;
303315 }
304316
305- // Mid-page behavior: show on upward scroll, hide on downward scroll
306- if ( directionUp ) {
317+ // Mid-page: accumulate movement until threshold reached
318+ var sign = dy > 0 ? 1 : ( dy < 0 ? - 1 : 0 ) ;
319+ if ( sign === 0 ) {
320+ // no movement: keep state, do not change accum
321+ return { visible : ( lastVisible !== null ? lastVisible : false ) , atBottom : false , atTop : false , y : pos . y } ;
322+ }
323+ if ( sign !== dirSign ) {
324+ // direction changed: reset accumulator to current delta
325+ accum = dy ;
326+ dirSign = sign ;
327+ } else {
328+ accum += dy ;
329+ }
330+
331+ if ( dirSign === 1 && accum >= UP_THRESHOLD ) {
332+ // show after sufficient upward scroll
307333 return { visible : true , atBottom : false , atTop : false , y : pos . y } ;
308334 }
309- if ( directionDown ) {
335+ if ( dirSign === - 1 && ( - accum ) >= DOWN_THRESHOLD ) {
336+ // hide after sufficient downward scroll
310337 return { visible : false , atBottom : false , atTop : false , y : pos . y } ;
311338 }
312339
313- // No significant movement: keep previous state
340+ // Not enough movement yet : keep previous
314341 return { visible : ( lastVisible !== null ? lastVisible : false ) , atBottom : false , atTop : false , y : pos . y } ;
315342 }
316343
0 commit comments