Skip to content

Commit

Permalink
refactor: scroll-to-top
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Dec 4, 2024
1 parent 958326d commit 963a633
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./globals.css";
import { Footer } from "@/components/footer";
import GoogleAnalytics from "@/components/GoogleAnalytics";
import { Header } from "@/components/header";
import { ScrollToTop } from "@/components/scroll-to-top";
import { Toaster } from "@/components/ui/toaster";

const inter = Inter({ subsets: ["latin"], fallback: ["sans-serif"] });
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function RootLayout({
<Header />
<GoogleAnalytics />
{children}
<ScrollToTop />
<Footer />
<Toaster />
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import React, { useEffect, useState } from "react";
"use client";

import { useCallback, useEffect, useState } from "react";
import { cn } from "@/lib/utils";
import { ChevronUpIcon } from "lucide-react";

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

const toggleVisibility = () => {
const toggleVisibility = useCallback(() => {
if (window.scrollY > 700) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
}, []);

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

useEffect(() => {
window.addEventListener("scroll", toggleVisibility);

return () => {
window.removeEventListener("scroll", toggleVisibility);
};
}, []);
}, [toggleVisibility]);

return (
<div
className={`${
isVisible
? "visible opacity-60 hover:w-40 hover:opacity-100"
: "invisible opacity-0"
} group fixed bottom-4 right-4 z-50 flex h-11 w-11 cursor-pointer items-center justify-center rounded-full bg-primary px-3 transition-all`}
className={cn(
"group fixed bottom-4 right-4 z-50 flex h-11 w-11 cursor-pointer items-center justify-center rounded-full bg-primary px-3 transition-all",
!isVisible
? "invisible opacity-0"
: "visible opacity-60 hover:w-40 hover:opacity-100"
)}
onClick={scrollToTop}
>
<div className="flex items-center justify-center">
Expand All @@ -45,6 +50,4 @@ const ScrollToTop = () => {
</div>
</div>
);
};

export default ScrollToTop;
}
2 changes: 0 additions & 2 deletions src/components/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { queryDatabase } from "../../lib/utils/query-db";
import { ToastAction } from "../ui/toast";
import { useToast } from "../ui/use-toast";
import { SearchFilter } from "./filter/search-filter";
import ScrollToTop from "./ScrollToTop";
import { SearchSelect } from "./SearchSelect";

export interface CourseObject {
Expand Down Expand Up @@ -328,7 +327,6 @@ const Search = () => {
ge={ge}
/>
)}
<ScrollToTop />
</div>
</div>
</div>
Expand Down

0 comments on commit 963a633

Please sign in to comment.