Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: breadcrumbs components

Brad Barbin04cb0a9359d2
diff --git a/apps/carbon/app/components/Breadcrumb.tsx b/apps/carbon/app/components/Breadcrumb.tsx
new file mode 100644
index 0000000000..2c0692ceb5
--- /dev/null
+++ b/apps/carbon/app/components/Breadcrumb.tsx
@@ -0,0 +1,78 @@
+import { Button, cn, getValidChildren } from "@carbon/react";
+import type { LinkProps } from "@remix-run/react";
+import { Link } from "@remix-run/react";
+import type { ComponentProps } from "react";
+import { cloneElement, forwardRef } from "react";
+
+import { LuChevronRight } from "react-icons/lu";
+
+const Breadcrumbs = forwardRef<
+ HTMLElement,
+ ComponentProps<"nav"> & {
+ useReactRouter?: boolean;
+ }
+>(({ className, children, useReactRouter = true, ...props }, ref) => {
+ const validChildren = getValidChildren(children);
+ const count = validChildren.length;
+ const clones = validChildren.map((child, index) =>
+ cloneElement(child, {
+ isFirstChild: index === 0,
+ isLastChild: index === count - 1,
+ })
+ );
+ return (
+ <nav
+ aria-label="Breadcrumb"
+ ref={ref}
+ className={cn("reset flex", className)}
+ {...props}
+ >
+ <ol className="inline-flex items-center">{clones}</ol>
+ </nav>
+ );
+});
+Breadcrumbs.displayName = "Breadcrumbs";
+
+const BreadcrumbItem = forwardRef<
+ HTMLLIElement,
+ ComponentProps<"li"> & {
+ isFirstChild?: boolean;
+ isLastChild?: boolean;
+ }
+>(({ className, children, isFirstChild, isLastChild, ...props }, ref) => (
+ <li
+ ref={ref}
+ className={cn("inline-flex items-center", className)}
+ {...props}
+ >
+ {!isFirstChild && (
+ <LuChevronRight className="text-muted-foreground h-3 w-3" />
+ )}
+ {children}
+ </li>
+));
+BreadcrumbItem.displayName = "BreadcrumbItem";
+
+const BreadcrumbLink = forwardRef<
+ HTMLAnchorElement,
+ LinkProps & {
+ isCurrentPage?: boolean;
+ }
+>(({ className, children, isCurrentPage, ...props }, ref) => {
+ return (
+ <Button variant="link" className={cn("px-2", className)} asChild>
+ {isCurrentPage ? (
+ <span aria-current="page" ref={ref} {...props}>
+ {children}
+ </span>
+ ) : (
+ <Link ref={ref} {...props}>
+ {children}
+ </Link>
+ )}
+ </Button>
+ );
+});
+BreadcrumbLink.displayName = "BreadcrumbLink";
+
+export { BreadcrumbItem, BreadcrumbLink, Breadcrumbs };
diff --git a/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx b/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx
index aed9b246b6..e7ebbfc4b8 100644
--- a/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx
+++ b/apps/carbon/app/components/Layout/Topbar/Breadcrumbs.tsx
@@ -1,7 +1,11 @@
-import { HStack, useColor } from "@carbon/react";
-import { Breadcrumb, BreadcrumbItem, BreadcrumbLink } from "@chakra-ui/react";
-import { Link, useMatches } from "@remix-run/react";
+import { HStack } from "@carbon/react";
+import { useMatches } from "@remix-run/react";
import { z } from "zod";
+import {
+ BreadcrumbItem,
+ BreadcrumbLink,
+ Breadcrumbs as BreadcrumbsBase,
+} from "~/components";
export const BreadcrumbHandle = z.object({
breadcrumb: z.any(),
@@ -15,7 +19,6 @@ const BreadcrumbHandleMatch = z.object({
const Breadcrumbs = () => {
const matches = useMatches();
- const linkColor = useColor("gray.800");
const breadcrumbs = matches
.map((m) => {
@@ -30,22 +33,19 @@ const Breadcrumbs = () => {
.filter(Boolean);
return (
- <HStack className="items-center h-full">
- <Breadcrumb noOfLines={1}>
+ <HStack className="items-center h-full" spacing={0}>
+ <BreadcrumbsBase className="line-clamp-1">
{breadcrumbs.map((breadcrumb, i) => (
<BreadcrumbItem key={i}>
<BreadcrumbLink
- fontSize="sm"
- fontWeight={500}
- color={linkColor}
- as={Link}
- to={breadcrumb?.to}
+ isCurrentPage={!breadcrumb?.to}
+ to={breadcrumb?.to ?? ""}
>
{breadcrumb?.breadcrumb}
</BreadcrumbLink>
</BreadcrumbItem>
))}
- </Breadcrumb>
+ </BreadcrumbsBase>
</HStack>
);
};
diff --git a/apps/carbon/app/components/index.ts b/apps/carbon/app/components/index.ts
index 790859306d..0af7473cc1 100644
--- a/apps/carbon/app/components/index.ts
+++ b/apps/carbon/app/components/index.ts
@@ -1,5 +1,6 @@
import Address from "./Address";
import Avatar from "./Avatar";
+import { BreadcrumbItem, BreadcrumbLink, Breadcrumbs } from "./Breadcrumb";
import Combobox from "./Combobox";
import Contact from "./Contact";
import CreatableCombobox from "./CreateableCombobox";
@@ -10,6 +11,9 @@ import Table from "./Table";
export {
Address,
Avatar,
+ BreadcrumbItem,
+ BreadcrumbLink,
+ Breadcrumbs,
Combobox,
Contact,
CreatableCombobox,
diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsx
index 56d224bd2f..c238bc02fb 100644
--- a/packages/react/src/index.tsx
+++ b/packages/react/src/index.tsx
@@ -196,6 +196,7 @@ import {
useOutsideClick,
} from "./hooks";
import { cn } from "./utils/cn";
+import { getValidChildren } from "./utils/react";
export {
ActionMenu,
@@ -364,6 +365,7 @@ export {
Tr,
VStack,
cn,
+ getValidChildren,
multiSelectTriggerVariants,
theme,
useColor,