Skip to content

Commit

Permalink
Implemented reusable breadcrumb component
Browse files Browse the repository at this point in the history
  • Loading branch information
Yimiika committed Jul 21, 2024
1 parent c6abc55 commit 4dbc608
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
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 `⏎`
import BreadcrumbSeparator from "./BreadcrumbSeparator";

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

interface BreadcrumbProps {

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

View workflow job for this annotation

GitHub Actions / eslint

The variable `BreadcrumbProps` should be named `BreadcrumbProperties`. A more descriptive name will do too
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">

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

View workflow job for this annotation

GitHub Actions / eslint

Replace `flex·items-center·mx-2` with `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 React from "react";

Check failure on line 1 in app/components/Breadcrumb/BreadcrumbItem.tsx

View workflow job for this annotation

GitHub Actions / eslint

Replace `React·from·"` with `{·Link·}·from·"@remix-run/`
import { Link } from "@remix-run/react";

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

View workflow job for this annotation

GitHub Actions / eslint

Replace `{·Link·}·from·"@remix-run/` with `React·from·"`

interface BreadcrumbItemProps {

Check failure on line 4 in app/components/Breadcrumb/BreadcrumbItem.tsx

View workflow job for this annotation

GitHub Actions / eslint

The variable `BreadcrumbItemProps` should be named `BreadcrumbItemProperties`. A more descriptive name will do too
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;
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 React from "react";

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

View workflow job for this annotation

GitHub Actions / eslint

Replace `import·React·from·"react";⏎import·{·useLocation·}·from·"@remix-run/react";` with `import·{·useLocation·}·from·"@remix-run/react";⏎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);

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

View workflow job for this annotation

GitHub Actions / eslint

arrow function is equivalent to `Boolean`. Use `Boolean` directly

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;
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;
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";

0 comments on commit 4dbc608

Please sign in to comment.