@molecule/app-breadcrumb-angular

Feature · breadcrumb · App (browser) · v1.1.1 · Apache-2.0

Angular breadcrumb navigation component

npm install @molecule/app-breadcrumb-angular

npm · Source on GitHub

How it works

@molecule/app-breadcrumb-angular is a ready-made breadcrumb feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component } from '@angular/core'
import { Breadcrumb } from '@molecule/app-breadcrumb-angular'

@Component({
  selector: 'app-trail',
  standalone: true,
  imports: [Breadcrumb],
  template: `
    <nav
      mol-breadcrumb
      [items]="[
        { label: 'Home', to: '/' },
        { label: 'Projects', to: '/projects' },
        { label: 'Apollo Redesign' },
      ]"
      [onNavigate]="navigate"
    ></nav>
  `,
})
export class Trail {
  navigate = (to: string): void => {
    this.router.navigate([to])
  }
}

Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular breadcrumb navigation component.

Exports Breadcrumb — the Angular counterpart of @molecule/app-breadcrumb-react with the same props and the same DOM: a list of crumbs where each but the last is a link. The final crumb renders as plain text with aria-current="page". Pass onNavigate to intercept clicks and hand off to a router.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component } from '@angular/core'
import { Breadcrumb } from '@molecule/app-breadcrumb-angular'

@Component({
  selector: 'app-trail',
  standalone: true,
  imports: [Breadcrumb],
  template: `
    <nav
      mol-breadcrumb
      [items]="[
        { label: 'Home', to: '/' },
        { label: 'Projects', to: '/projects' },
        { label: 'Apollo Redesign' },
      ]"
      [onNavigate]="navigate"
    ></nav>
  `,
})
export class Trail {
  navigate = (to: string): void => {
    this.router.navigate([to])
  }
}

Type

feature

Installation

npm install @molecule/app-breadcrumb-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

Describes a single crumb entry in a breadcrumb trail. Node props (label, icon) are projected or templated content in Angular (an input cannot carry a node), so they are typed unknown: pass plain strings or <ng-template> refs.

interface BreadcrumbItem {
  /** Display text or template for the crumb. */
  label?: unknown
  /**
   * Optional `to` target. When absent the crumb renders as plain text
   * (typically the current page).
   */
  to?: string
  /** Optional leading icon. */
  icon?: unknown
}

Props accepted by Breadcrumb — the same props @molecule/app-breadcrumb-react accepts. Node props (separator) are projected content in Angular, so they are typed unknown: pass them in the host template as [slot=separator], not as values.

interface BreadcrumbProps {
  /** Ordered list of crumbs, last one usually the current page. */
  items?: BreadcrumbItem[]
  /**
   * Click handler invoked with the target `to` when an item is clicked.
   * Useful when wiring to a router. When omitted the component renders
   * items with an anchor `<a href={to}>`.
   */
  onNavigate?: (to: string) => void
  /** Separator between crumbs — defaults to a `/`; projected as `[slot=separator]`. */
  separator?: unknown
  /** Extra classes on the `<nav>` element. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string
}

Classes

Breadcrumb component.

Breadcrumb navigation: each item before the last is a link; the last item is plain text with aria-current="page". The host IS the <nav> (nav[mol-breadcrumb]), matching React's root exactly. With onNavigate, linked crumbs render as <button type="button">s that call it; without, they render as plain <a href> anchors.

The separator renders once per gap: the projected [slot=separator] content when given (cloned into every gap — Angular projects a node once, React renders the same element in every gap), else the default <span aria-hidden>/</span>.

The nav's accessible name flows through t() with the English default 'Breadcrumb', so it follows the app's locale.

@Component({
  selector: 'nav[mol-breadcrumb]',
  standalone: true,
  imports: [NgTemplateOutlet],
  inputs: ['items', 'onNavigate', 'className', 'dataMolId'],
  host: {
    '[class]': 'classes()',
    '[attr.aria-label]': 'navLabel()',
    '[attr.data-mol-id]': 'dataMolId ?? null',
  },
  template: `@for (item of items; track $index) {
      <span [class]="crumbClasses()">
        @if (isLast($index) || !item.to) {
          <span [attr.aria-current]="isLast($index) ? 'page' : null">
            <ng-container
              [ngTemplateOutlet]="crumbContent"
              [ngTemplateOutletContext]="contentContext(item)"
            />
          </span>
        } @else if (onNavigate) {
          <button type="button" [class]="linkClasses()" (click)="navigate(item)">
            <ng-container
              [ngTemplateOutlet]="crumbContent"
              [ngTemplateOutletContext]="contentContext(item)"
            />
          </button>
        } @else {
          <a [attr.href]="item.to" [class]="linkClasses()">
            <ng-container
              [ngTemplateOutlet]="crumbContent"
              [ngTemplateOutletContext]="contentContext(item)"
            />
          </a>
        }
        @if (!isLast($index)) {
          <span #gapSep></span>
        }
      </span>
    }
    <ng-template #crumbContent let-icon="icon" let-label="label">
      <span [class]="contentClasses()">
        @if (isTemplate(icon)) {
          <ng-container [ngTemplateOutlet]="icon" />
        } @else if (icon != null) {
          {{ icon }}
        }
        @if (isTemplate(label)) {
          <ng-container [ngTemplateOutlet]="label" />
        } @else {
          {{ label }}
        }
      </span>
    </ng-template>
    <span #sepSlot><ng-content select="[slot=separator]"></ng-content></span>`,
})
export class Breadcrumb implements AfterViewInit, AfterViewChecked {
  /** Ordered list of crumbs, last one usually the current page. */
  items: BreadcrumbItem[] = []
  /** Click handler invoked with the target `to` when a linked crumb is clicked. */
  onNavigate?: (to: string) => void
  /** Extra classes on the `<nav>` element. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string

  /** The separator slot capture span (moved out of the DOM once read). */
  @ViewChild('sepSlot') private sepSlot?: ElementRef<HTMLSpanElement>
  /** One empty span per gap, filled with the projected separator clones. */
  @ViewChildren('gapSep') private gapSeps?: QueryList<ElementRef<HTMLSpanElement>>

  /** The i18n handle (locale-reactive), read in an injection context. */
  private readonly i18n = useTranslation()

  /** The projected separator nodes, cloned into every gap but the first. */
  private originals: Node[] = []

  /** Whether the caller projected a separator (drives the per-gap default's `@if`). */
  readonly customSeparator = signal(false)

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The nav class list (React's
   * `cm.cn(cm.flex({ align, gap, wrap }), cm.textSize('sm'), className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(
      this.cm().flex({ align: 'center', gap: 'xs', wrap: 'wrap' }),
      this.cm().textSize('sm'),
      this.className,
    )
  }

  /**
   * The nav's accessible name (React's `aria-label`): prop-less `t()` with
   * the English default `'Breadcrumb'`, re-read on a locale switch (the `t`
   * call reads the locale notifications signal, so the host binding
   * re-evaluates on one).
   * @returns The translated nav label.
   */
  navLabel(): string {
    return this.i18n.t('breadcrumb.aria.breadcrumb', undefined, { defaultValue: 'Breadcrumb' })
  }

  /**
   * The crumb wrapper's class (`cm.flex({ align: 'center', gap: 'xs' })`).
   * @returns The class string.
   */
  crumbClasses(): string {
    return this.cm().flex({ align: 'center', gap: 'xs' })
  }

  /**
   * The icon/label row's class (`cm.flex({ align: 'center', gap: 'xs' })`).
   * @returns The class string.
   */
  contentClasses(): string {
    return this.cm().flex({ align: 'center', gap: 'xs' })
  }

  /**
   * The link crumb's class (`cm.link`), through `cn()` so a token that is a
   * function in a bonded map resolves to its string (Angular's classMap
   * binding rejects functions).
   * @returns The class string.
   */
  linkClasses(): string {
    return this.cm().cn(this.cm().link)
  }

  /**
   * Whether the crumb at the position is the current page.
   * @param index - The crumb's position.
   * @returns True for the last crumb.
   */
  isLast(index: number): boolean {
    return index === this.items.length - 1
  }

  /**
   * The icon/label context the shared crumb template renders.
   * @param item - The crumb.
   * @returns The template context.
   */
  contentContext(item: BreadcrumbItem): { icon: unknown; label: unknown } {
    return { icon: item.icon, label: item.label }
  }

  /**
   * Whether the value is a node template (a node-valued prop's Angular
   * representation). Checked structurally — `TemplateRef` is a type-only
   * import here, and the check must survive any bundler.
   * @param value - The prop value.
   * @returns True for a template ref.
   */
  isTemplate(value: unknown): value is TemplateRef<unknown> {
    return (
      typeof value === 'object' &&
      value !== null &&
      typeof (value as TemplateRef<unknown>).createEmbeddedView === 'function'
    )
  }

  /**
   * Activates a linked crumb (React's `onClick={() => onNavigate(item.to!)}`).
   * @param item - The crumb.
   */
  navigate(item: BreadcrumbItem): void {
    if (item.to != null && item.to !== '') this.onNavigate?.(item.to)
  }

  /**
   * Reads slot presence (view queries resolve only at view init), keeps the
   * projected separator nodes and removes the capture span — React renders
   * the separator bare, once per gap.
   */
  ngAfterViewInit(): void {
    const slot = this.sepSlot?.nativeElement
    if (!slot) return
    this.originals = Array.from(slot.childNodes).filter(hasRenderable)
    slot.remove()
    this.customSeparator.set(this.originals.length > 0)
  }

  /**
   * Fills every empty gap with the separator: the projected nodes in the
   * first gap and a fresh clone in the others when the caller projected a
   * separator (React renders the same element in every gap; Angular projects
   * a node once), else the default `/` (built here, because the gap count
   * follows the items, which a template cannot fill from outside the loop).
   */
  ngAfterViewChecked(): void {
    const gaps = this.gapSeps ?? []
    gaps.forEach((ref, i) => {
      const gap = ref.nativeElement
      if (gap.childNodes.length > 0) return
      if (!this.customSeparator()) {
        gap.appendChild(defaultSeparator())
        return
      }
      const nodes = i === 0 ? this.originals : this.originals.map((node) => node.cloneNode(true))
      for (const node of nodes) gap.appendChild(node)
    })
  }
}

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.5.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common

  • @angular/core

  • @angular/platform-browser

  • @molecule/app-angular

  • @molecule/app-ui

  • @molecule/app-ui-angular

  • Without onNavigate, crumbs render as plain <a href> (full page load in SPAs) — pass [onNavigate] to stay client-side; with it, crumbs render as <button>s. The last item (or any item without to) renders as plain text with aria-current="page". No router dependency is required by the component itself.

  • The separator renders once per gap: project [slot=separator] content to replace the default / (the projected nodes are cloned into every gap — Angular projects a node once, React renders the same element in every gap).

  • Node-valued fields (label, icon) accept <ng-template> refs or plain strings; a template renders in the crumb's icon/label row.

Translations

Translation strings are provided by @molecule/app-locales-breadcrumb.