Skip to content
Open
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
7 changes: 6 additions & 1 deletion client/app/main/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import Navbar from "@/components/Navbar";
import Feed from "@/components/feed/Feed";
import ScrollToTopButton from "@/components/ui/ScrollToTopButton";
import { Suspense } from "react";
import { useRef } from "react";
import MainQueryHandler from "./MainQueryHandler";

export default function Home() {
const pageRef = useRef<HTMLDivElement>(null);

return (
<div className="page-scroll">
<div ref={pageRef} className="page-scroll">
<Navbar />
<Suspense fallback={null}>
<MainQueryHandler />
</Suspense>
<Feed />
<ScrollToTopButton scrollContainerRef={pageRef} />
</div>
);
}
39 changes: 39 additions & 0 deletions client/components/ui/ScrollToTopButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use client";

import { ArrowUp } from "lucide-react";
import { RefObject, useEffect, useState } from "react";

type ScrollToTopButtonProps = {
scrollContainerRef: RefObject<HTMLElement | null>;
};

export default function ScrollToTopButton({
scrollContainerRef,
}: ScrollToTopButtonProps) {
const [visible, setVisible] = useState(false);

useEffect(() => {
const container = scrollContainerRef.current;
if (!container) return;

const handleScroll = () => setVisible(container.scrollTop > 400);
container.addEventListener("scroll", handleScroll, { passive: true });
handleScroll();

return () => container.removeEventListener("scroll", handleScroll);
}, [scrollContainerRef]);

if (!visible) return null;

return (
<button
type="button"
aria-label="Scroll to top"
title="Scroll to top"
onClick={() => scrollContainerRef.current?.scrollTo({ top: 0, behavior: "smooth" })}
className="fixed bottom-6 right-6 z-40 rounded-full border border-border bg-card p-3 text-foreground shadow-lg transition hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<ArrowUp className="h-5 w-5" aria-hidden="true" />
</button>
);
}