Skip to content

Commit 2e87041

Browse files
committed
feat: added BackButton component for going to previous page
1 parent 5fe756d commit 2e87041

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/components/ui/BackButton.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use client";
2+
3+
import { cn } from "@/lib/utils";
4+
import { useRouter } from "next/navigation";
5+
6+
7+
export default function BackButton({ className, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) {
8+
const router = useRouter();
9+
10+
const hasHistory =
11+
(typeof window !== "undefined" &&
12+
window.history?.length &&
13+
window.history.length > 1) ||
14+
typeof window === "undefined";
15+
16+
if (!hasHistory) {
17+
return null;
18+
}
19+
20+
return (
21+
<button
22+
className={
23+
cn(
24+
"text-slate-600 dark:text-slate-300 hover:text-black dark:hover:text-white",
25+
"cursor-pointer hover:underline hover:underline-offset-4",
26+
className
27+
)
28+
}
29+
onClick={router.back}
30+
{...props}
31+
/>
32+
);
33+
}

0 commit comments

Comments
 (0)