@@ -25,14 +25,11 @@ export default function DocsLayout({
2525 const handleScroll = ( ) => {
2626 // The header with logo is approximately 72px tall (p-4 = 16px top/bottom + content height)
2727 // Fix the sidebar when the user scrolls past the header
28- if ( window . scrollY > 72 ) {
29- setIsFixed ( true )
30- } else {
31- setIsFixed ( false )
32- }
28+ const shouldBeFixed = window . scrollY > 72
29+ setIsFixed ( ( prev ) => ( prev !== shouldBeFixed ? shouldBeFixed : prev ) )
3330 }
3431
35- window . addEventListener ( 'scroll' , handleScroll )
32+ window . addEventListener ( 'scroll' , handleScroll , { passive : true } )
3633 return ( ) => window . removeEventListener ( 'scroll' , handleScroll )
3734 } , [ ] )
3835
@@ -46,14 +43,16 @@ export default function DocsLayout({
4643 const isAtTop = scrollTop === 0
4744 const isAtBottom = scrollTop + clientHeight >= scrollHeight - 1
4845
49- setShowTopFade ( ! isAtTop )
50- setShowBottomFade ( ! isAtBottom )
46+ setShowTopFade ( ( prev ) => ( prev !== ! isAtTop ? ! isAtTop : prev ) )
47+ setShowBottomFade ( ( prev ) => ( prev !== ! isAtBottom ? ! isAtBottom : prev ) )
5148 }
5249
53- // Check initial state
54- handleScroll ( )
50+ // Use requestAnimationFrame to check initial state after layout
51+ requestAnimationFrame ( ( ) => {
52+ handleScroll ( )
53+ } )
5554
56- sidebarElement . addEventListener ( 'scroll' , handleScroll )
55+ sidebarElement . addEventListener ( 'scroll' , handleScroll , { passive : true } )
5756 return ( ) => sidebarElement . removeEventListener ( 'scroll' , handleScroll )
5857 } , [ ] )
5958
0 commit comments