Frontend School

Dynamic Breadcrumb Navigation Component

Machine Coding
medium

Dynamic Breadcrumb Navigation Component

Medium
Breadcrumbs
UI
React
Frontend
Machine Coding

Overview

In complex web applications like a financial dashboard or an e-commerce platform, users often navigate through multiple levels of content. A clear breadcrumb navigation helps users understand their current location within the application hierarchy and provides an intuitive way to backtrack. This problem focuses on building a robust and accessible Breadcrumbs component that dynamically adapts to path length.

What you need to do / Task

Your task is to create a reusable Breadcrumbs React functional component with the following features:

  1. Component Definition: Create a Breadcrumbs component that accepts a path prop. The path prop will be an array of objects, each representing a segment: { label: string; href?: string; }. The href property is optional for the last item (current page) and for the ellipsis.
  2. Segment Rendering: Render each path segment. All segments, except the last one, should be clickable links. The last segment represents the current page and should be displayed as plain text, visually distinct, and not clickable.
  3. Separators: Display a clear visual separator (e.g., / or >) between path segments.
  4. Path Truncation: Implement a truncation mechanism for long paths. If the path contains more than a specified number of maxVisibleItems (default to 4), the component should display the first item, followed by an ellipsis (...), and then the last maxVisibleItems - 2 items. The ellipsis itself should not be clickable. For example, if maxVisibleItems is 4 and the path is [A, B, C, D, E, F], it should render as A > ... > E > F.
  5. Accessibility: Ensure the component is accessible by utilizing appropriate ARIA attributes and semantic HTML for navigation.
  6. Edge Cases: Handle empty paths (render nothing) and single-item paths gracefully.

UI/UX expectations

  • The breadcrumbs should be laid out horizontally.
  • There should be a clear visual distinction between clickable links and the current page item (e.g., different color, font-weight).
  • Hover states for clickable links should be present.
  • The separator should be visually clear and consistent.
  • When truncation occurs, the ellipsis (...) should be clearly visible and not clickable.

Acceptance criteria

  • The Breadcrumbs component renders correctly based on the provided path data.
  • Path segments (excluding the last one and the ellipsis) are rendered as functional <a> links.
  • A visible separator (e.g., /) is correctly placed between all visible path segments.
  • The last path segment is rendered as plain text, is visually distinct from links, and is not clickable.
  • Long paths (exceeding maxVisibleItems) are truncated as specified: first item, ellipsis, and the last maxVisibleItems - 2 items.
  • The component includes appropriate ARIA attributes (e.g., aria-label="Breadcrumb" on nav, aria-current="page" on the last item) for accessibility.
  • The component gracefully handles an empty path array (renders nothing) and a path array with a single item (renders only the current item).

Constraints

Constraints & assumptions

  • You must build a React functional component using TypeScript.
  • Styling should be done with plain CSS in styles.css. No CSS-in-JS or CSS pre-processors are required.
  • Assume href values are simple strings and do not require integration with a client-side routing library (e.g., React Router).
  • The default value for maxVisibleItems should be 4.

Out of scope

  • Integration with a client-side routing library (e.g., React Router).
  • Dynamic generation of the path array based on the current URL.
  • Complex responsive design beyond the specified truncation logic.
  • Server-side rendering (SSR) considerations.

What interviewers evaluate

  • Component Design: How well the component is structured, its props interface, and internal state management (if any).
  • Functional Correctness: Accuracy in implementing all specified requirements, including path rendering, linking, and truncation logic.
  • UI/UX & Styling: Fidelity to UI/UX expectations, effective use of CSS for layout and visual distinctions.
  • Accessibility: Proper use of semantic HTML and ARIA attributes to ensure the component is usable by everyone.
  • Code Quality: Readability, maintainability, type safety (TypeScript), and adherence to React best practices.

Loading editor...