1- import React , { useRef , useEffect } from 'react'
1+ import React , { useState , useEffect , useCallback } from 'react'
22import { AppBar , Toolbar , Typography , Container , Box , IconButton , Link , Button } from '@mui/material'
33import GitHubIcon from '@mui/icons-material/GitHub'
44import SearchIcon from '@mui/icons-material/Search'
@@ -12,39 +12,41 @@ export function Layout({ children }: LayoutProps): React.ReactNode {
1212 const navigate = useNavigate ( )
1313 const location = useLocation ( )
1414 const isHomePage = location . pathname === '/' || location . pathname === ''
15- const appBarRef = useRef < HTMLDivElement > ( null )
15+ const [ scrolled , setScrolled ] = useState ( false )
1616
1717 useEffect ( ( ) => {
1818 if ( ! isHomePage ) return
1919
2020 const handleScroll = ( ) => {
21- if ( ! appBarRef . current ) return
22- const scrolled = window . scrollY > 60
23- appBarRef . current . style . backgroundColor = scrolled ? '#262626' : 'transparent'
24- appBarRef . current . style . boxShadow = scrolled ? '0 2px 8px rgba(0,0,0,0.15)' : 'none'
21+ setScrolled ( window . scrollY > 60 )
2522 }
2623
2724 window . addEventListener ( 'scroll' , handleScroll , { passive : true } )
2825 handleScroll ( )
29- return ( ) => window . removeEventListener ( 'scroll' , handleScroll )
26+ return ( ) => {
27+ window . removeEventListener ( 'scroll' , handleScroll )
28+ setScrolled ( false )
29+ }
3030 } , [ isHomePage ] )
3131
32- const handleSearchClick = ( ) => {
32+ const handleSearchClick = useCallback ( ( ) => {
3333 if ( isHomePage ) {
3434 window . scrollTo ( { top : 0 , behavior : 'smooth' } )
3535 } else {
3636 navigate ( '/' )
3737 }
38- }
38+ } , [ isHomePage , navigate ] )
39+
40+ const showSolidHeader = ! isHomePage || scrolled
3941
4042 return (
4143 < Box sx = { { display : 'flex' , flexDirection : 'column' , minHeight : '100vh' } } >
4244 < AppBar
43- ref = { appBarRef }
4445 position = "fixed"
4546 elevation = { 0 }
4647 sx = { {
47- backgroundColor : isHomePage ? 'transparent' : '#262626' ,
48+ backgroundColor : showSolidHeader ? '#262626' : 'transparent' ,
49+ boxShadow : showSolidHeader ? '0 2px 8px rgba(0,0,0,0.15)' : 'none' ,
4850 transition : 'background-color 0.3s ease, box-shadow 0.3s ease' ,
4951 zIndex : 20 ,
5052 } }
@@ -86,7 +88,7 @@ export function Layout({ children }: LayoutProps): React.ReactNode {
8688 </ Toolbar >
8789 </ AppBar >
8890
89- < Box component = "main" sx = { { flex : 1 , pt : 'var(--header-height)' } } >
91+ < Box component = "main" sx = { { flex : 1 , pt : isHomePage ? 0 : 'var(--header-height)' } } >
9092 { isHomePage ? (
9193 children
9294 ) : (
0 commit comments