Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scroll to top button #34

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/components/layout/scroll-to-top-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useEffect, useState } from "react";
import { Button } from "../ui/button";
import { ArrowUpIcon } from "lucide-react";
import { cn } from "@/lib/utils";

export function ScrollToTopButton() {
const [isVisible, setIsVisible] = useState(false);

const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};

useEffect(() => {
const toggleVisibility = () => {
setIsVisible(window.scrollY > 300);
};
window.addEventListener("scroll", toggleVisibility);
return () => window.removeEventListener("scroll", toggleVisibility);
}, []);

return (
<div
className={cn("fixed bottom-14 right-4 z-50 transition-opacity duration-300", {
"opacity-100": isVisible,
"pointer-events-none z-0 opacity-0": !isVisible,
})}
>
<Button onClick={scrollToTop} size="icon">
<ArrowUpIcon className="h-6 w-6" />
</Button>
</div>
);
}
2 changes: 2 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import "./styles/global.css";
import { useAuthStore } from "./stores/auth";
import { Loader2 } from "lucide-react";
import { ScrollToTopButton } from "./components/layout/scroll-to-top-button";

const queryClient = new QueryClient({
defaultOptions: {
Expand Down Expand Up @@ -87,6 +88,7 @@ export function App() {
)}
<RouterProvider router={router} context={routerContext} />
<Toaster />
<ScrollToTopButton />
</QueryClientProvider>
);
}
Expand Down
Loading