x
This commit is contained in:
1 parent
f64fee1347
commit
48aebb2ec9
1 file changed
+29
@@ -49,11 +49,13 @@ export default function Navbar({ categories = [], initialMenu = [], settings = n
|
|||||||
const [langSheetOpen, setLangSheetOpen] = useState(false)
|
const [langSheetOpen, setLangSheetOpen] = useState(false)
|
||||||
const [searchQuery, setSearchQuery] = useState('')
|
const [searchQuery, setSearchQuery] = useState('')
|
||||||
const [scrolled, setScrolled] = useState(false)
|
const [scrolled, setScrolled] = useState(false)
|
||||||
|
const [scrollingDown, setScrollingDown] = useState(false)
|
||||||
const [isClientReady, setIsClientReady] = useState(false)
|
const [isClientReady, setIsClientReady] = useState(false)
|
||||||
const [canRenderDesktopMenu, setCanRenderDesktopMenu] = useState(false)
|
const [canRenderDesktopMenu, setCanRenderDesktopMenu] = useState(false)
|
||||||
// Seed from server-fetched initialMenu — no client flash
|
// Seed from server-fetched initialMenu — no client flash
|
||||||
const [megaMenu] = useState<MegaMenuPayload[]>(initialMenu)
|
const [megaMenu] = useState<MegaMenuPayload[]>(initialMenu)
|
||||||
const navRef = useRef<HTMLElement>(null)
|
const navRef = useRef<HTMLElement>(null)
|
||||||
|
const previousScrollY = useRef(0)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const pathname = usePathname()
|
const pathname = usePathname()
|
||||||
|
|
||||||
@@ -97,6 +99,27 @@ export default function Navbar({ categories = [], initialMenu = [], settings = n
|
|||||||
return () => window.removeEventListener('scroll', onScroll)
|
return () => window.removeEventListener('scroll', onScroll)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onScroll = () => {
|
||||||
|
const currentScrollY = window.scrollY
|
||||||
|
const delta = currentScrollY - previousScrollY.current
|
||||||
|
|
||||||
|
if (currentScrollY <= 20) {
|
||||||
|
setScrollingDown(false)
|
||||||
|
} else if (delta > 4) {
|
||||||
|
setScrollingDown(true)
|
||||||
|
} else if (delta < -4) {
|
||||||
|
setScrollingDown(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
previousScrollY.current = currentScrollY
|
||||||
|
}
|
||||||
|
|
||||||
|
previousScrollY.current = window.scrollY
|
||||||
|
window.addEventListener('scroll', onScroll, { passive: true })
|
||||||
|
return () => window.removeEventListener('scroll', onScroll)
|
||||||
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let animationFrameId: number;
|
let animationFrameId: number;
|
||||||
const navElement = navRef.current;
|
const navElement = navRef.current;
|
||||||
@@ -156,6 +179,10 @@ export default function Navbar({ categories = [], initialMenu = [], settings = n
|
|||||||
|
|
||||||
const isHome = getPathWithoutLocale(pathname) === '/'
|
const isHome = getPathWithoutLocale(pathname) === '/'
|
||||||
const isTransparent = isHome && !scrolled && !mobileOpen && !searchOpen && !langSheetOpen
|
const isTransparent = isHome && !scrolled && !mobileOpen && !searchOpen && !langSheetOpen
|
||||||
|
const pathWithoutLocale = getPathWithoutLocale(pathname)
|
||||||
|
const isCommercePath = ['/shop', '/products', '/category', '/cart', '/checkout', '/wishlist', '/profile', '/order', '/track']
|
||||||
|
.some((route) => pathWithoutLocale === route || pathWithoutLocale.startsWith(`${route}/`))
|
||||||
|
const hideDesktopHeader = !isCommercePath && scrollingDown && !mobileOpen && !searchOpen && !langSheetOpen
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsClientReady(true)
|
setIsClientReady(true)
|
||||||
@@ -175,6 +202,8 @@ export default function Navbar({ categories = [], initialMenu = [], settings = n
|
|||||||
ref={navRef}
|
ref={navRef}
|
||||||
data-scrolled={scrolled}
|
data-scrolled={scrolled}
|
||||||
className={`global-navbar fixed top-0 z-50 w-full border-b px-4 py-3.5 transition-all duration-300 md:px-6 md:py-4 ${
|
className={`global-navbar fixed top-0 z-50 w-full border-b px-4 py-3.5 transition-all duration-300 md:px-6 md:py-4 ${
|
||||||
|
hideDesktopHeader ? 'md:pointer-events-none md:-translate-y-full' : 'translate-y-0'
|
||||||
|
} ${
|
||||||
scrolled
|
scrolled
|
||||||
? 'border-black/5 bg-surface-page/95 shadow-lg shadow-black/5 backdrop-blur-xl'
|
? 'border-black/5 bg-surface-page/95 shadow-lg shadow-black/5 backdrop-blur-xl'
|
||||||
: isTransparent
|
: isTransparent
|
||||||
|
|||||||
Reference in new issue
Block a user