Skip to content

Instantly share code, notes, and snippets.

@devDoubleH
Last active March 3, 2024 07:55
Show Gist options
  • Save devDoubleH/41872d265c3c2f8e2ac4322c6ca42523 to your computer and use it in GitHub Desktop.
Save devDoubleH/41872d265c3c2f8e2ac4322c6ca42523 to your computer and use it in GitHub Desktop.
Handmade shadcn/ui Breadcrumb
import * as React from "react";
import { ChevronRightIcon } from "@radix-ui/react-icons";
interface BreadcrumbItemProps extends React.HTMLAttributes<HTMLAnchorElement> {
href: string;
}
const Breadcrumb = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, children, ...props }, ref) => {
const items = React.Children.toArray(children);
return (
<div
ref={ref}
className={`flex items-center space-x-2 text-sm ${className}`}
{...props}
>
{items.map((item, index) => (
<React.Fragment key={index}>
{item}
{index < items.length - 1 && (
<ChevronRightIcon className="w-4 h-4 text-foreground" />
)}
</React.Fragment>
))}
</div>
);
});
Breadcrumb.displayName = "Breadcrumb";
const BreadcrumbItem = React.forwardRef<HTMLAnchorElement, BreadcrumbItemProps>(
({ className, href, children, ...props }, ref) => (
<a
ref={ref}
href={href}
className={`text-gray-500 hover:text-gray-700 ${className}`}
{...props}
>
{children}
</a>
)
);
BreadcrumbItem.displayName = "BreadcrumbItem";
export { Breadcrumb, BreadcrumbItem };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment