V1.0

Base Components

Breadcrumb

Guiding users by displaying their path within an app or website.

Blogs
The Power of Minimalism in Design

Installation

Install the following dependencies:

terminal
npm install @radix-ui/react-slot

Create a breadcrumb.tsx file and paste the following code into it.

/components/ui/breadcrumb.tsx
// AlignUI Breadcrumb v0.0.0
 
import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
 
import { cnExt } from '@/utils/cn';
import { PolymorphicComponentProps } from '@/utils/polymorphic';
 
const BREADCRUMB_ROOT_NAME = 'BreadcrumbRoot';
const BREADCRUMB_ITEM_NAME = 'BreadcrumbItem';
const BREADCRUMB_ICON_NAME = 'BreadcrumbIcon';
const BREADCRUMB_ARROW_NAME = 'BreadcrumbArrow';
 
type BreadcrumbRootProps = React.HTMLAttributes<HTMLDivElement> & {
  asChild?: boolean;
};
 
function BreadcrumbRoot({
  asChild,
  children,
  className,
  ...rest
}: BreadcrumbRootProps) {
  const Component = asChild ? Slot : 'div';
 
  return (
    <Component className={cnExt('flex flex-wrap gap-1.5', className)} {...rest}>
      {children}
    </Component>
  );
}
BreadcrumbRoot.displayName = BREADCRUMB_ROOT_NAME;
 
type BreadcrumbItemProps = React.HTMLAttributes<HTMLDivElement> & {
  asChild?: boolean;
  active?: boolean;
};
 
const BreadcrumbItem = React.forwardRef<HTMLDivElement, BreadcrumbItemProps>(
  ({ asChild, children, className, active, ...rest }, forwardedRef) => {
    const Component = asChild ? Slot : 'div';
 
    return (
      <Component
        ref={forwardedRef}
        className={cnExt(
          // base
          'flex items-center gap-1.5 transition-colors duration-200 ease-out',
          'text-label-sm text-text-sub-600',
          {
            // not active
            'underline decoration-transparent': !active,
            // hover
            'hover:text-text-strong-950 hover:decoration-current': !active,
            // active
            'text-text-strong-950': active,
          },
          className,
        )}
        {...rest}
      >
        {children}
      </Component>
    );
  },
);
BreadcrumbItem.displayName = BREADCRUMB_ITEM_NAME;
 
function BreadcrumbItemIcon<T extends React.ElementType>({
  className,
  as,
  ...rest
}: PolymorphicComponentProps<T>) {
  const Component = as || 'div';
 
  return <Component className={cnExt('size-5', className)} {...rest} />;
}
BreadcrumbItemIcon.displayName = BREADCRUMB_ICON_NAME;
 
function BreadcrumbItemArrowIcon<T extends React.ElementType>({
  className,
  as,
  ...rest
}: PolymorphicComponentProps<T>) {
  const Component = as || 'div';
 
  return (
    <Component
      className={cnExt(
        'flex size-5 select-none items-center justify-center text-text-disabled-300',
        className,
      )}
      {...rest}
    />
  );
}
BreadcrumbItemArrowIcon.displayName = BREADCRUMB_ARROW_NAME;
 
export {
  BreadcrumbRoot as Root,
  BreadcrumbItem as Item,
  BreadcrumbItemIcon as Icon,
  BreadcrumbItemArrowIcon as ArrowIcon,
};

Update the import paths to match your project setup.

Examples

SettingsNotifications
Email Notifications

Slash Separator

Settings
/
Notifications
/
Email Notifications

API Reference

This component is based on the <div> element and supports all of its props. And adds:

PropTypeDefault
asChild
boolean

Individual breadcrumb item. This component is based on the <div> element initially and supports all of its props. And adds:

PropTypeDefault
active
boolean
asChild
boolean

Icon used within a breadcrumb item. The Breadcrumb.Icon component is polymorphic, allowing you to change the underlying HTML element using the as prop.

PropTypeDefault
as
React.ElementType
div

Icon used to separate breadcrumb items, usually an arrow. The Breadcrumb.ArrowIcon component is polymorphic, allowing you to change the underlying HTML element using the as prop.

PropTypeDefault
as
React.ElementType
div
© 2024 AlignUI Design System. All rights reserved.