@@ -394,46 +394,42 @@ class Theme {
394394 }
395395
396396 initRelated ( ) {
397- const $relatedStatic = document . getElementById ( 'related-static' ) ;
398- if ( ! $relatedStatic ) return ;
399-
400- if ( this . util . isTocStatic ( ) || $relatedStatic . getAttribute ( 'data-kept' ) === 'true' ) {
401- if ( this . _relatedOnScroll ) this . scrollEventSet . delete ( this . _relatedOnScroll ) ;
402- return ;
403- }
404-
405397 const $relatedAuto = document . getElementById ( 'related-auto' ) ;
406- const $relatedContent = document . getElementById ( 'related-content-static' ) ;
407-
408- if ( $relatedContent && $relatedAuto ) {
409- $relatedAuto . querySelector ( '.related-content' ) . appendChild ( $relatedContent ) ;
410- } else {
411- return ;
398+ if ( ! $relatedAuto ) return ;
399+
400+ const $relatedContentStatic = document . getElementById ( 'related-content-static' ) ;
401+ if ( $relatedContentStatic ) {
402+ $relatedAuto . querySelector ( '.related-content' ) . appendChild ( $relatedContentStatic ) ;
412403 }
413404
414405 const $toc = document . getElementById ( 'toc-auto' ) ;
415-
416- if ( $toc && getComputedStyle ( $toc ) . visibility !== 'hidden' && getComputedStyle ( $toc ) . display !== 'none' ) {
417- // Sychronize position with the TOC
418- const tocRect = $toc . getBoundingClientRect ( ) ;
419- $relatedAuto . style . top = `${ window . scrollY + tocRect . top } px` ;
420- $relatedAuto . style . left = `20px` ;
421- $relatedAuto . style . visibility = 'visible' ;
422- } else {
423- $relatedAuto . style . visibility = 'hidden' ;
424- return ;
425- }
426-
427- const headerIsFixed = document . body . getAttribute ( 'data-header-desktop' ) !== 'normal' ;
428- const headerHeight = document . getElementById ( 'header-desktop' ) . offsetHeight ;
429- const TOP_SPACING = 20 + ( headerIsFixed ? headerHeight : 0 ) ;
430- const minRelatedTop = $relatedAuto . offsetTop ;
431- const minScrollTop = minRelatedTop - TOP_SPACING + ( headerIsFixed ? 0 : headerHeight ) ;
406+ if ( ! $toc ) return ;
432407
408+ // This function ONLY handles vertical scrolling adjustments.
433409 this . _relatedOnScroll = this . _relatedOnScroll || ( ( ) => {
410+ // If the panel's container is hidden by CSS, do nothing.
411+ if ( getComputedStyle ( $relatedAuto . parentElement ) . display === 'none' ) {
412+ return ;
413+ }
414+
415+ const headerIsFixed = document . body . getAttribute ( 'data-header-desktop' ) !== 'normal' ;
416+ const headerHeight = document . getElementById ( 'header-desktop' ) . offsetHeight ;
417+ const TOP_SPACING = 20 + ( headerIsFixed ? headerHeight : 0 ) ;
418+
419+ // Use the TOC's position as the reliable source of truth for vertical alignment.
420+ const minRelatedTop = $toc . offsetTop ;
421+ const minScrollTop = minRelatedTop - TOP_SPACING ;
422+
434423 const footerTop = document . getElementById ( 'post-footer' ) . offsetTop ;
435424 const maxRelatedTop = footerTop - $relatedAuto . getBoundingClientRect ( ) . height ;
436- const maxScrollTop = maxRelatedTop - TOP_SPACING + ( headerIsFixed ? 0 : headerHeight ) ;
425+ const maxScrollTop = maxRelatedTop - TOP_SPACING ;
426+
427+ // Set initial position before scrolling begins
428+ if ( $relatedAuto . style . position !== 'fixed' ) {
429+ $relatedAuto . style . position = 'absolute' ;
430+ $relatedAuto . style . top = `${ minRelatedTop } px` ;
431+ }
432+
437433 if ( this . newScrollTop < minScrollTop ) {
438434 $relatedAuto . style . position = 'absolute' ;
439435 $relatedAuto . style . top = `${ minRelatedTop } px` ;
@@ -445,8 +441,12 @@ class Theme {
445441 $relatedAuto . style . top = `${ TOP_SPACING } px` ;
446442 }
447443 } ) ;
444+
445+ // Run once on load and add to the scroll listener.
448446 this . _relatedOnScroll ( ) ;
449447 this . scrollEventSet . add ( this . _relatedOnScroll ) ;
448+ // Also run on resize to catch layout shifts.
449+ this . resizeEventSet . add ( this . _relatedOnScroll ) ;
450450 }
451451
452452 initToc ( ) {
0 commit comments