# Breadcrumb

Jejak lokasi halaman dalam hierarki situs, dengan halaman saat ini ditandai aria-current.

- Kategori: Navigasi
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/breadcrumb.json
- CLI: `npx markasui@latest add breadcrumb`

## Kapan dipakai

Halaman yang berada beberapa tingkat di dalam hierarki, misalnya detail produk di dalam kategori.

- Item terakhir selalu halaman saat ini, jangan beri href
- Jangan memakai breadcrumb untuk langkah proses atau wizard

## Kode

### Blade

Simpan di `resources/views/components/markas/breadcrumb.blade.php`. Tanpa dependensi tambahan.

```blade
@props([
    'items' => [],
    'navLabel' => 'Breadcrumb',
])

@php
    $list = 'flex flex-wrap items-center gap-1.5 text-sm text-muted';
    $item = 'inline-flex items-center gap-1.5';
    $link = 'rounded-md transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none';
    $current = 'font-medium text-ink';
    $separator = 'size-3.5 shrink-0 text-faint';

    $lastIndex = count($items) - 1;

    // Href berskema selain http, https, mailto, dan tel (misalnya javascript:) diganti "#".
    $safeHref = function (string $href): string {
        $value = ltrim(str_replace(["\t", "\n", "\r"], '', $href), "\x00..\x20");
        $hasScheme = preg_match('/^[a-z][a-z0-9+.\-]*:/i', $value) === 1;

        return $hasScheme && preg_match('/^(https?|mailto|tel):/i', $value) !== 1 ? '#' : $href;
    };
@endphp

<nav {{ $attributes->merge(['aria-label' => $navLabel]) }}>
    <ol class="{{ $list }}">
        @foreach (array_values($items) as $index => $crumb)
            <li class="{{ $item }}">
                @if ($index === $lastIndex)
                    <span aria-current="page" class="{{ $current }}">{{ $crumb['label'] }}</span>
                @elseif (! empty($crumb['href']))
                    <a href="{{ $safeHref((string) $crumb['href']) }}" class="{{ $link }}">{{ $crumb['label'] }}</a>
                @else
                    <span>{{ $crumb['label'] }}</span>
                @endif
                @if ($index !== $lastIndex)
                    <svg class="{{ $separator }}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 18 6-6-6-6" /></svg>
                @endif
            </li>
        @endforeach
    </ol>
</nav>
```

```blade
<x-markas.breadcrumb nav-label="Jejak navigasi" :items="[
    ['label' => 'Beranda', 'href' => '/'],
    ['label' => 'Komponen', 'href' => '/komponen'],
    ['label' => 'Breadcrumb'],
]" />
```

Tidak butuh Alpine.js.

### React

Simpan di `src/components/markas/Breadcrumb.tsx`. Tanpa dependensi tambahan.

```tsx
import type { ComponentProps, ReactNode } from "react";

const list = "flex flex-wrap items-center gap-1.5 text-sm text-muted";
const item = "inline-flex items-center gap-1.5";
const link =
  "rounded-md transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const current = "font-medium text-ink";
const separator = "size-3.5 shrink-0 text-faint";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Href berskema selain http, https, mailto, dan tel (misalnya javascript:) diganti "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};

export type BreadcrumbItem = {
  label: ReactNode;
  href?: string;
};

export type BreadcrumbProps = ComponentProps<"nav"> & {
  items: BreadcrumbItem[];
  /** Accessible label of the nav element. */
  navLabel?: string;
};

export function Breadcrumb({
  items,
  navLabel = "Breadcrumb",
  ...props
}: BreadcrumbProps) {
  const lastIndex = items.length - 1;

  return (
    <nav aria-label={navLabel} {...props}>
      <ol className={list}>
        {items.map((crumb, index) => (
          <li key={index} className={item}>
            {index === lastIndex ? (
              <span aria-current="page" className={current}>
                {crumb.label}
              </span>
            ) : crumb.href ? (
              <a href={safeHref(crumb.href)} className={link}>
                {crumb.label}
              </a>
            ) : (
              <span>{crumb.label}</span>
            )}
            {index !== lastIndex && (
              <svg
                className={separator}
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                strokeWidth="1.5"
                strokeLinecap="round"
                strokeLinejoin="round"
                aria-hidden="true"
              >
                <path d="m9 18 6-6-6-6" />
              </svg>
            )}
          </li>
        ))}
      </ol>
    </nav>
  );
}
```

```tsx
import { Breadcrumb } from "@/components/markas/Breadcrumb";

<Breadcrumb
  navLabel="Jejak navigasi"
  items={[
    { label: "Beranda", href: "/" },
    { label: "Komponen", href: "/komponen" },
    { label: "Breadcrumb" },
  ]}
/>;
```

### Vue

Simpan di `src/components/markas/Breadcrumb.vue`. Tanpa dependensi tambahan.

```vue
<script setup lang="ts">
export type BreadcrumbItem = { label: string; href?: string };

const props = withDefaults(
  defineProps<{
    items: BreadcrumbItem[];
    navLabel?: string;
  }>(),
  { navLabel: "Breadcrumb" },
);

const list = "flex flex-wrap items-center gap-1.5 text-sm text-muted";
const item = "inline-flex items-center gap-1.5";
const link =
  "rounded-md transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const current = "font-medium text-ink";
const separator = "size-3.5 shrink-0 text-faint";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Href berskema selain http, https, mailto, dan tel (misalnya javascript:) diganti "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};
</script>

<template>
  <nav :aria-label="props.navLabel">
    <ol :class="list">
      <li v-for="(crumb, index) in props.items" :key="index" :class="item">
        <span
          v-if="index === props.items.length - 1"
          aria-current="page"
          :class="current"
          >{{ crumb.label }}</span
        >
        <a v-else-if="crumb.href" :href="safeHref(crumb.href)" :class="link">{{
          crumb.label
        }}</a>
        <span v-else>{{ crumb.label }}</span>
        <svg
          v-if="index !== props.items.length - 1"
          :class="separator"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="m9 18 6-6-6-6" />
        </svg>
      </li>
    </ol>
  </nav>
</template>
```

```vue
<template>
  <Breadcrumb
    nav-label="Jejak navigasi"
    :items="[
      { label: 'Beranda', href: '/' },
      { label: 'Komponen', href: '/komponen' },
      { label: 'Breadcrumb' },
    ]"
  />
</template>
```

### Svelte

Simpan di `src/lib/components/markas/Breadcrumb.svelte`. Tanpa dependensi tambahan.

```svelte
<script lang="ts" module>
  export type BreadcrumbItem = { label: string; href?: string };
</script>

<script lang="ts">
  import type { HTMLAttributes } from "svelte/elements";

  const list = "flex flex-wrap items-center gap-1.5 text-sm text-muted";
  const item = "inline-flex items-center gap-1.5";
  const link =
    "rounded-md transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
  const current = "font-medium text-ink";
  const separator = "size-3.5 shrink-0 text-faint";

  const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
  const SAFE_SCHEME = /^(https?|mailto|tel):/i;

  /** Hrefs with a scheme other than http, https, mailto, and tel (such as javascript:) become "#". */
  const safeHref = (href: string) => {
    const cleaned = href.replace(/[\t\n\r]/g, "");
    const start = [...cleaned].findIndex((char) => char > " ");
    const value = cleaned.slice(Math.max(start, 0));
    return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
  };

  type Props = HTMLAttributes<HTMLElement> & {
    items: BreadcrumbItem[];
    /** Accessible label of the nav element. */
    navLabel?: string;
  };

  let { items, navLabel = "Breadcrumb", ...rest }: Props = $props();
  const lastIndex = $derived(items.length - 1);
</script>

<nav aria-label={navLabel} {...rest}>
  <ol class={list}>
    {#each items as crumb, index (index)}
      <li class={item}>
        {#if index === lastIndex}
          <span aria-current="page" class={current}>{crumb.label}</span>
        {:else if crumb.href}
          <a href={safeHref(crumb.href)} class={link}>{crumb.label}</a>
        {:else}
          <span>{crumb.label}</span>
        {/if}
        {#if index !== lastIndex}
          <svg
            class={separator}
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="m9 18 6-6-6-6" />
          </svg>
        {/if}
      </li>
    {/each}
  </ol>
</nav>
```

```svelte
<script lang="ts">
  import Breadcrumb from "$lib/components/markas/Breadcrumb.svelte";
</script>

<Breadcrumb
  navLabel="Jejak navigasi"
  items={[
    { label: "Beranda", href: "/" },
    { label: "Komponen", href: "/komponen" },
    { label: "Breadcrumb" },
  ]}
/>
```

### Astro

Simpan di `src/components/markas/Breadcrumb.astro`. Tanpa dependensi tambahan.

```astro
---
import type { HTMLAttributes } from "astro/types";

export type BreadcrumbItem = { label: string; href?: string };

type Props = HTMLAttributes<"nav"> & {
  items: BreadcrumbItem[];
  /** Accessible label of the nav element. */
  navLabel?: string;
};

const list = "flex flex-wrap items-center gap-1.5 text-sm text-muted";
const item = "inline-flex items-center gap-1.5";
const link =
  "rounded-md transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const current = "font-medium text-ink";
const separator = "size-3.5 shrink-0 text-faint";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Hrefs with a scheme other than http, https, mailto, and tel (such as javascript:) become "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};

const { items, navLabel = "Breadcrumb", ...rest } = Astro.props;
const lastIndex = items.length - 1;
---

<nav aria-label={navLabel} {...rest}>
  <ol class={list}>
    {
      items.map((crumb, index) => (
        <li class={item}>
          {index === lastIndex ? (
            <span aria-current="page" class={current}>
              {crumb.label}
            </span>
          ) : crumb.href ? (
            <a href={safeHref(crumb.href)} class={link}>
              {crumb.label}
            </a>
          ) : (
            <span>{crumb.label}</span>
          )}
          {index !== lastIndex && (
            <svg
              class={separator}
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="1.5"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="m9 18 6-6-6-6" />
            </svg>
          )}
        </li>
      ))
    }
  </ol>
</nav>
```

```astro
---
import Breadcrumb from "@/components/markas/Breadcrumb.astro";
---

<Breadcrumb
  navLabel="Jejak navigasi"
  items={[
    { label: "Beranda", href: "/" },
    { label: "Komponen", href: "/komponen" },
    { label: "Breadcrumb" },
  ]}
/>
```

Tidak ada JavaScript yang dikirim.

### Angular

Simpan di `src/app/markas/breadcrumb.component.ts`. Tanpa dependensi tambahan.

```ts
import { ChangeDetectionStrategy, Component, input } from "@angular/core";

const list = "flex flex-wrap items-center gap-1.5 text-sm text-muted";
const item = "inline-flex items-center gap-1.5";
const link =
  "rounded-md transition-colors duration-150 ease-out hover:text-ink focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none";
const current = "font-medium text-ink";
const separator = "size-3.5 shrink-0 text-faint";

const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
const SAFE_SCHEME = /^(https?|mailto|tel):/i;

/** Hrefs with a scheme other than http, https, mailto, and tel (such as javascript:) become "#". */
const safeHref = (href: string) => {
  const cleaned = href.replace(/[\t\n\r]/g, "");
  const start = [...cleaned].findIndex((char) => char > " ");
  const value = cleaned.slice(Math.max(start, 0));
  return HAS_SCHEME.test(value) && !SAFE_SCHEME.test(value) ? "#" : href;
};

export type BreadcrumbItem = { label: string; href?: string };

/** Attribute selector on the native nav: id, class, and other attributes stay on the nav itself. */
@Component({
  selector: "nav[markas-breadcrumb]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "[attr.aria-label]": "navLabel()" },
  template: `
    <ol [class]="list">
      @for (crumb of items(); track $index; let last = $last) {
        <li [class]="item">
          @if (last) {
            <span aria-current="page" [class]="current">{{ crumb.label }}</span>
          } @else if (crumb.href) {
            <a [attr.href]="safeHref(crumb.href)" [class]="link">{{
              crumb.label
            }}</a>
          } @else {
            <span>{{ crumb.label }}</span>
          }
          @if (!last) {
            <svg
              [class]="separator"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="1.5"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="m9 18 6-6-6-6" />
            </svg>
          }
        </li>
      }
    </ol>
  `,
})
export class Breadcrumb {
  readonly items = input.required<BreadcrumbItem[]>();
  /** Accessible label of the nav element. */
  readonly navLabel = input("Breadcrumb");

  protected readonly list = list;
  protected readonly item = item;
  protected readonly link = link;
  protected readonly current = current;
  protected readonly separator = separator;
  protected readonly safeHref = safeHref;
}
```

```ts
import { Component } from "@angular/core";
import { Breadcrumb } from "./markas/breadcrumb.component";

@Component({
  selector: "app-jejak-halaman",
  imports: [Breadcrumb],
  template: `<nav
    markas-breadcrumb
    navLabel="Jejak navigasi"
    [items]="items"
  ></nav>`,
})
export class JejakHalaman {
  readonly items = [
    { label: "Beranda", href: "/" },
    { label: "Komponen", href: "/komponen" },
    { label: "Breadcrumb" },
  ];
}
```

## Props

| Nama | Tipe | Default | Keterangan |
| --- | --- | --- | --- |
| `items` | `{ label, href? }[]` | [] | Urutan halaman dari akar ke halaman saat ini. Item terakhir dianggap halaman saat ini dan tidak diberi link. |
| `navLabel` | `string` | Breadcrumb | Label aksesibel elemen nav. Isi "Jejak navigasi" untuk situs berbahasa Indonesia. |

## Aksesibilitas

- Dibungkus nav dengan label dari navLabel (default Breadcrumb) dan daftar berurutan ol.
- Item terakhir memakai aria-current=page.
- Pemisah chevron diberi aria-hidden sehingga tidak dibacakan.

## Catatan

- Item terakhir selalu dirender sebagai teks dengan `aria-current="page"`, walau diberi `href`.
- Label nav default dalam bahasa Inggris ("Breadcrumb"). Isi `navLabel` (Blade: `nav-label`) dengan "Jejak navigasi" untuk situs berbahasa Indonesia.
