Breadcrumbs

A breadcrumb path at the top of a file card. It keeps its first and last levels and folds the rest into a "…" button, which opens the hidden levels as a small menu.

  • logo-mark.svg12 KB
  • logo-wordmark.svg18 KB
  • brand-guide.pdf2.4 MB

3 filesChanged 2 hours ago

Options

Size

Code

import { useEffect, useId, useRef, useState } from "react";
import "./breadcrumbs.css";

/* A breadcrumb path. When there are more than "max" levels, the middle ones
   fold into a "…" button that opens them as a small menu.

   <Breadcrumbs items={[{ label: "Home", href: "/" }, { label: "Logo files", href: "/files" }]} /> */
export function Breadcrumbs({ items, max = 3 }) {
  const id = useId();
  const more = useRef(null);
  const [open, setOpen] = useState(false);
  const fold = items.length > max;
  const folded = fold ? items.slice(1, items.length - (max - 1)) : [];
  const shown = fold ? items.slice(items.length - (max - 1)) : items.slice(1);
  const last = items[items.length - 1];

  useEffect(() => {
    const away = (event) => {
      if (more.current && !more.current.contains(event.target)) setOpen(false);
    };
    document.addEventListener("pointerdown", away);
    return () => document.removeEventListener("pointerdown", away);
  }, []);

  const keys = (event) => {
    const links = Array.from(more.current.querySelectorAll("ul a"));
    if (event.key === "Escape") {
      setOpen(false);
      more.current.querySelector("button").focus();
    }
    if (event.key === "ArrowDown" || event.key === "ArrowUp") {
      event.preventDefault();
      setOpen(true);
      const at = links.indexOf(document.activeElement);
      const step = event.key === "ArrowDown" ? 1 : -1;
      links[(at + step + links.length) % links.length].focus();
    }
  };

  const link = (item) => (
    <li key={item.label}>
      <a href={item.href} aria-current={item === last ? "page" : undefined}>
        {item.label}
      </a>
    </li>
  );

  return (
    <nav className="dh-crumbs" aria-label="Breadcrumb">
      <ol>
        {link(items[0])}
        {fold && (
          <li
            className="dh-crumbs-more"
            ref={more}
            onKeyDown={keys}
            onBlur={(event) => {
              if (!event.currentTarget.contains(event.relatedTarget)) setOpen(false);
            }}
          >
            <button
              type="button"
              aria-label={"Show " + folded.length + " more levels"}
              aria-expanded={open}
              aria-controls={id}
              onClick={() => setOpen(!open)}
            >
              …
            </button>
            <ul id={id}>{folded.map(link)}</ul>
          </li>
        )}
        {shown.map(link)}
      </ol>
    </nav>
  );
}

const file = "M4 1.500h4.500V5a1.500 1.500 0 0 0 1.500 1.500h3.500V13a1.500 1.500 0 0 1-1.500 1.500H4A1.500 1.500 0 0 1 2.500 13V3A1.500 1.500 0 0 1 4 1.500Zm6 .400 3.100 3.100H10V1.900Z";

export function Example() {
  const files = [
    ["logo-mark.svg", "12 KB"],
    ["logo-wordmark.svg", "18 KB"],
    ["brand-guide.pdf", "2.4 MB"],
  ];

  return (
    <div className="dh-crumbs-card">
      <Breadcrumbs
        items={[
          { label: "Home", href: "#" },
          { label: "Clients", href: "#" },
          { label: "Logoipsum", href: "#" },
          { label: "Projects", href: "#" },
          { label: "Brand refresh", href: "#" },
          { label: "Logo files", href: "#" },
        ]}
      />
      <ul className="dh-crumbs-files">
        {files.map(([name, size]) => (
          <li key={name}>
            <svg viewBox="0 0 16 16" aria-hidden="true">
              <path d={file} />
            </svg>
            <span>{name}</span>
            <em>{size}</em>
          </li>
        ))}
      </ul>
      <p className="dh-crumbs-foot">
        3 files<span>Changed 2 hours ago</span>
      </p>
    </div>
  );
}
  • logo-mark.svg12 KB
  • logo-wordmark.svg18 KB
  • brand-guide.pdf2.4 MB

3 filesChanged 2 hours ago