-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadcrumbs.tsx
More file actions
59 lines (52 loc) · 1.39 KB
/
Copy pathbreadcrumbs.tsx
File metadata and controls
59 lines (52 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use client";
import { Link } from "nextjs13-progress";
import { usePathname } from "next/navigation";
import { ChevronRight, Home } from "lucide-react";
import { buttonVariants } from "./ui/button";
import { capitalize } from "@/utils/string";
const Breadcrumbs = () => {
const pathname = usePathname();
const segments = pathname.split("/").filter((page) => page !== "");
let link = "";
const parsedPages = segments.map((segment) => {
const name = segment
.split("-")
.map((word) => {
if (word.length > 10) {
return capitalize(word.slice(0, 10) + "...");
}
return capitalize(word);
})
.join(" ");
link = `${link}/${segment}`;
return {
href: link,
name,
};
});
// don't render on the main page
if (segments.length === 0) return;
return (
<div className="mt-2 flex items-center">
<Link
href="/"
className={buttonVariants({ variant: "ghost", size: "xs" })}
>
<Home className="h-4 w-4 mr-1" /> Home
</Link>
{parsedPages.map((page) => (
<>
<ChevronRight className="h-4 w-4" />
<Link
key={page.href}
href={page.href}
className={buttonVariants({ variant: "ghost", size: "xs" })}
>
{page.name}
</Link>
</>
))}
</div>
);
};
export default Breadcrumbs;