-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented reusable Breadcrumb component
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from "react"; | ||
import BreadcrumbItem from "./BreadcrumbItem"; | ||
import BreadcrumbSeparator from "./BreadcrumbSeparator"; | ||
|
||
interface Page { | ||
name: string; | ||
link: string; | ||
} | ||
|
||
interface BreadcrumbProps { | ||
pages: Page[]; | ||
} | ||
|
||
const Breadcrumb: React.FC<BreadcrumbProps> = ({ pages }) => { | ||
return ( | ||
<nav aria-label="breadcrumb" className="flex items-center"> | ||
{pages.map((page, index) => ( | ||
<React.Fragment key={index}> | ||
<BreadcrumbItem | ||
name={page.name} | ||
link={page.link} | ||
isCurrent={index === pages.length - 1} | ||
/> | ||
{index < pages.length - 1 && ( | ||
<span className="flex items-center mx-2"> | ||
<BreadcrumbSeparator /> | ||
</span> | ||
)} | ||
</React.Fragment> | ||
))} | ||
</nav> | ||
); | ||
}; | ||
|
||
export default Breadcrumb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from "react"; | ||
import { Link } from "@remix-run/react"; | ||
|
||
interface BreadcrumbItemProps { | ||
name: string; | ||
link: string; | ||
isCurrent: boolean; | ||
} | ||
|
||
const BreadcrumbItem: React.FC<BreadcrumbItemProps> = ({ | ||
name, | ||
link, | ||
isCurrent, | ||
}) => { | ||
const commonStyles = "text-[#222] font-roboto text-xs font-normal leading-4"; | ||
const itemStyles = "flex items-center py-1"; | ||
const marginRight = isCurrent ? "" : "mr-2"; | ||
|
||
if (isCurrent) { | ||
return ( | ||
<span | ||
className={`${commonStyles} text-[#6A6A6A] ${itemStyles} ${marginRight}`} | ||
> | ||
{name} | ||
</span> | ||
); | ||
} | ||
|
||
return ( | ||
<Link | ||
to={link} | ||
className={`${commonStyles} hover:underline ${itemStyles} ${marginRight}`} | ||
> | ||
{name} | ||
</Link> | ||
); | ||
}; | ||
|
||
export default BreadcrumbItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react"; | ||
import { useLocation } from "@remix-run/react"; | ||
import Breadcrumb from "./Breadcrumb"; | ||
|
||
const BreadcrumbProvider: React.FC = () => { | ||
const location = useLocation(); | ||
const pathnames = location.pathname.split("/").filter((x) => x); | ||
|
||
const breadcrumbPages = pathnames.map((pathname, index) => { | ||
const link = `/${pathnames.slice(0, index + 1).join("/")}`; | ||
return { | ||
name: pathname.charAt(0).toUpperCase() + pathname.slice(1), | ||
link, | ||
}; | ||
}); | ||
breadcrumbPages.unshift({ | ||
name: "Home", | ||
link: "/", | ||
}); | ||
|
||
return <Breadcrumb pages={breadcrumbPages} />; | ||
}; | ||
|
||
export default BreadcrumbProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from "react"; | ||
|
||
const BreadcrumbSeparator: React.FC = () => ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="12" | ||
height="12" | ||
viewBox="0 0 12 12" | ||
fill="none" | ||
> | ||
<path | ||
d="M4.5 1.5L8.7375 5.7375C8.80621 5.8076 8.8447 5.90184 8.8447 6C8.8447 6.09816 8.80621 6.1924 8.7375 6.2625L4.5 10.5" | ||
stroke="#6A6A6A" | ||
/> | ||
</svg> | ||
); | ||
|
||
export default BreadcrumbSeparator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as Breadcrumb } from "./Breadcrumb"; | ||
export { default as BreadcrumbItem } from "./BreadcrumbItem"; |