Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a reusable breadcrumb component #364

Closed
wants to merge 4 commits into from
Closed
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
35 changes: 35 additions & 0 deletions app/components/Breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react";
import BreadcrumbItem from "./BreadcrumbItem";

Check failure on line 2 in app/components/Breadcrumb/Breadcrumb.tsx

View workflow job for this annotation

GitHub Actions / eslint

Insert `⏎`

Check failure on line 2 in app/components/Breadcrumb/Breadcrumb.tsx

View workflow job for this annotation

GitHub Actions / eslint

Insert `⏎`
import BreadcrumbSeparator from "./BreadcrumbSeparator";

interface Page {
name: string;
link: string;
}

interface BreadcrumbProperties {
pages: Page[];
}

const Breadcrumb: React.FC<BreadcrumbProperties> = ({ 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="mx-2 flex items-center">
<BreadcrumbSeparator />
</span>
)}
</React.Fragment>
))}
</nav>
);
};

export default Breadcrumb;
39 changes: 39 additions & 0 deletions app/components/Breadcrumb/BreadcrumbItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Link } from "@remix-run/react";
import React from "react";

interface BreadcrumbItemProperties {
name: string;
link: string;
isCurrent: boolean;
}

const BreadcrumbItem: React.FC<BreadcrumbItemProperties> = ({
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;
24 changes: 24 additions & 0 deletions app/components/Breadcrumb/BreadcrumbProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useLocation } from "@remix-run/react";
import React from "react";
import Breadcrumb from "./Breadcrumb";

Check failure on line 3 in app/components/Breadcrumb/BreadcrumbProvider.tsx

View workflow job for this annotation

GitHub Actions / eslint

Insert `⏎`

Check failure on line 3 in app/components/Breadcrumb/BreadcrumbProvider.tsx

View workflow job for this annotation

GitHub Actions / eslint

Insert `⏎`

const BreadcrumbProvider: React.FC = () => {
const location = useLocation();
const pathnames = location.pathname.split("/").filter(Boolean);

const breadcrumbPages = pathnames.map((pathname: string, index: number) => {
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;
18 changes: 18 additions & 0 deletions app/components/Breadcrumb/BreadcrumbSeparator.tsx
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;

Check failure on line 18 in app/components/Breadcrumb/BreadcrumbSeparator.tsx

View workflow job for this annotation

GitHub Actions / eslint

Insert `⏎`

Check failure on line 18 in app/components/Breadcrumb/BreadcrumbSeparator.tsx

View workflow job for this annotation

GitHub Actions / eslint

Insert `⏎`
2 changes: 2 additions & 0 deletions app/components/Breadcrumb/index.ts
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";
Loading