Skip to content

Commit 6f07dc9

Browse files
Implementar scroll-driven header
O header agora esconde para cima conforme a barra de rolagem desce, e reaparece quando a barra de rolagem sobe.
1 parent 8dcfa68 commit 6f07dc9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/components/Header.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DropdownMenuTrigger,
77
} from "@/components/ui/dropdown-menu";
88
import { useIsMobile } from "@/hooks/use-mobile";
9+
import { useScrollDirection } from "@/hooks/use-scroll-direction";
910
import { Github, Menu, Moon, Play, Sun } from "lucide-react";
1011
import { useTheme } from "next-themes";
1112
import { Link, useLocation, useNavigate } from "react-router-dom";
@@ -15,6 +16,7 @@ const Header = () => {
1516
const navigate = useNavigate();
1617
const location = useLocation();
1718
const isMobile = useIsMobile();
19+
const scrollDirection = useScrollDirection();
1820

1921
const isActiveLink = (path: string) => {
2022
return location.pathname === path || location.pathname.startsWith(path + "/");
@@ -26,7 +28,9 @@ const Header = () => {
2628

2729
return (
2830
<header
29-
className="border-b border-border backdrop-blur-sm sticky top-0 z-50"
31+
className={`border-b border-border backdrop-blur-sm sticky top-0 z-50 transition-transform duration-300 ease-in-out ${
32+
scrollDirection === "down" ? "-translate-y-full" : "translate-y-0"
33+
}`}
3034
style={{ backgroundColor: "hsl(var(--header-bg))" }}
3135
>
3236
<div className="container mx-auto px-6 py-4">

src/hooks/use-scroll-direction.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useEffect, useState } from "react";
2+
3+
export function useScrollDirection() {
4+
const [scrollDirection, setScrollDirection] = useState<"up" | "down">("up");
5+
const [lastScrollY, setLastScrollY] = useState(0);
6+
7+
useEffect(() => {
8+
const updateScrollDirection = () => {
9+
const scrollY = window.scrollY;
10+
const direction = scrollY > lastScrollY ? "down" : "up";
11+
12+
if (direction !== scrollDirection && Math.abs(scrollY - lastScrollY) > 10) {
13+
setScrollDirection(direction);
14+
}
15+
16+
setLastScrollY(scrollY > 0 ? scrollY : 0);
17+
};
18+
19+
window.addEventListener("scroll", updateScrollDirection);
20+
return () => window.removeEventListener("scroll", updateScrollDirection);
21+
}, [scrollDirection, lastScrollY]);
22+
23+
return scrollDirection;
24+
}

0 commit comments

Comments
 (0)