Frontend School

Build a Reusable Breadcrumbs Component

Machine Coding
medium

Build a Reusable Breadcrumbs Component

Medium
React
Frontend
Breadcrumbs
Navigation
UI
Accessibility

Overview

Breadcrumbs are a secondary navigation aid that helps users understand their location within a website or application's hierarchy. They provide a clear, clickable path back to higher-level pages, improving discoverability and overall user experience. For an L3 candidate, building a robust and accessible breadcrumbs component demonstrates an understanding of fundamental UI component design, prop handling, and accessibility best practices in React.

What you need to do / Task

Your task is to implement a reusable Breadcrumbs React component.

  1. Component Definition: Create a functional React component named Breadcrumbs.
  2. Props Handling: The Breadcrumbs component should accept a prop named items, which will be an array of objects. Each object in the array will represent a segment of the path and should have the following structure:
    interface BreadcrumbItem {
      label: string; // The display text for the breadcrumb segment
      href: string;  // The URL path for the breadcrumb segment
    }
    
  3. Rendering Logic:
    • Render each BreadcrumbItem in the items array sequentially.
    • Each item (except the last one) should be rendered as a clickable link (<a> tag) using its href.
    • The last item in the path should be rendered as plain text (e.g., within a <span> tag), indicating the current page, and should not be a clickable link.
    • Include a visual separator (e.g., > or /) between each breadcrumb segment.

UI/UX expectations

  • Layout: Breadcrumbs should be displayed horizontally in a single line, from left to right.
  • Interactions: Hovering over clickable breadcrumb links should indicate interactivity (e.g., a slight text change or underline). Clicking a link should technically "navigate" (for this exercise, simply setting the href attribute is sufficient; no actual routing integration is needed).
  • Accessibility:
    • The entire breadcrumb navigation should be enclosed within a <nav> element with aria-label="Breadcrumb".
    • The current page (last item) should have the aria-current="page" attribute.
    • Ensure proper semantic HTML elements (e.g., <ol>, <li>) are used.

Acceptance criteria

  • A Breadcrumbs component is defined and rendered in App.tsx.
  • The component correctly accepts and processes the items prop, adhering to the BreadcrumbItem interface.
  • All items except the last one are rendered as clickable links (<a> tags).
  • The last item is rendered as non-clickable plain text (e.g., <span> tag).
  • A visual separator (e.g., > or /) is displayed between breadcrumb segments.
  • The overall structure is wrapped in a <nav aria-label="Breadcrumb"> element.
  • The last breadcrumb item includes the aria-current="page" attribute.
  • Basic CSS is applied to ensure a horizontal layout and readability of the breadcrumbs.

Constraints

Constraints & assumptions

  • You must use React (functional components).
  • No external UI libraries (e.g., Material UI, Ant Design) are allowed for the breadcrumbs component itself. You should build it from scratch.
  • The solution should be implemented within the provided Sandpack environment.
  • Assume the href values provided in the items prop are valid paths, even if they don't lead to actual pages in this simulated environment.
  • The maximum number of breadcrumb items will be reasonable (e.g., under 10).

Out of scope

  • Implementing a full-fledged routing solution (e.g., React Router). Simply setting the href attribute is sufficient.
  • Complex animations or transitions on hover/click.
  • Dynamic fetching of breadcrumb data from an API.
  • Extensive error handling for malformed items props.

What interviewers evaluate

  • Component Design: Ability to structure a reusable, maintainable React component.
  • Prop Handling & Rendering Logic: Correctly processing props and rendering UI elements based on conditional logic.
  • Semantic HTML & Accessibility: Using appropriate HTML tags and ARIA attributes for navigation components.
  • Code Quality: Readability, organization, and adherence to modern React practices.

Loading editor...