# Menu navigasi

> Menu sidebar untuk produk dengan banyak halaman, berisi bagian berjudul, sub menu yang terbuka di tempat, dan hitungan.

Produk dengan banyak halaman, seperti admin Loka Musik, mengelompokkan menunya ke dalam bagian berjudul dan melipat halaman yang berkaitan di bawah satu induk. Menu ini ada di sidebar kerangka [Dasbor](https://design.sosialoka.tech/id/patterns/dashboard/); halaman ini mengatur cara menu itu tumbuh.

## Kapan dipakai

- Tambahkan bagian kalau menu sudah lebih dari sekitar tujuh entri. Beri nama sesuai pekerjaan di dalamnya, misalnya "Keuangan & Royalti".
- Tambahkan induk kalau ada tiga halaman atau lebih yang saling berkaitan, misalnya laporan. Dua halaman yang berkaitan tetap jadi halaman biasa.
- Produk dengan lima sampai tujuh halaman tetap memakai daftar biasa di kerangka Dasbor.

### Lakukan dan jangan

- **Lakukan:** Batasi menu dua tingkat: induk dan halaman-halamannya.
- **Jangan:** Jangan taruh sub menu di dalam sub menu; jadikan tingkat itu tab di halamannya.

_Pratinjau langsung versi React dan Vue: https://design.sosialoka.tech/id/patterns/navigation-menu/_

## Anatomi

1. Judul bagian
2. Halaman dengan hitungan
3. Induk, terbuka
4. Sub menu dengan garis pandu
5. Halaman saat ini

Menu ada di permukaan `sidebar`. Judul bagian memakai `muted-foreground` ukuran `text-xs`. Halaman saat ini, termasuk yang ada di sub menu, memakai `sidebar-accent`. Sub menu menggantung dari induknya dengan garis pandu `border`.

## Aturan

- **Maksimal dua tingkat:** induk dan halaman-halamannya. Area yang lebih dalam dijadikan tab di halamannya.
- **Induk bukan halaman.** Induk hanya membuka dan menutup sub menunya, dan tidak punya `href`. Kalau areanya butuh ringkasan, jadikan halaman pertama, misalnya "Ringkasan".
- **Terbuka saat dikunjungi:** induk terbuka sendiri kalau salah satu halamannya sedang dibuka, dan judulnya jadi semibold.
- **Boleh terbuka lebih dari satu:** membuka satu induk tidak menutup yang lain.
- **Hitungan:** badge menunjukkan berapa yang menunggu, misalnya tiket yang masih terbuka. Tampilkan di halamannya; di induk yang tertutup, tampilkan jumlahnya. Jangan tampilkan nol.
- **Urutan:** bagian berurutan dari pekerjaan harian ke pengaturan: ringkasan, operasional, klien, keuangan, lalu sistem. Pengaturan dan administrasi paling akhir.
- **Ikon:** halaman dan induk di tingkat pertama punya ikon; halaman di sub menu tidak.
- **Hanya ikon:** saat sidebar diciutkan jadi ikon, induk membuka halamannya di menu di samping rail.
- **HP:** seluruh menu pindah ke sheet, dengan bagian dan sub menu yang sama.

## Props

`nav` pada blok Dasbor menerima halaman, induk, dan bagian:

| Entri | Bentuk | Kegunaan |
| --- | --- | --- |
| Halaman | `{ title, href, icon?, active?, badge? }` | Satu halaman. `active` menandai halaman saat ini; `badge` berisi hitungan. |
| Induk | `{ title, icon?, items, badge? }` | Membuka sub menu berisi halaman. Tanpa `href`. |
| Bagian | `{ label, items }` | Bagian menu berjudul, berisi halaman dan induk. |

Isi dengan daftar bagian, atau daftar halaman dan induk untuk menu tanpa judul. Vue memakai bentuk yang sama; Flutter menyusun menunya dari widget.

## Kode

**React**
```tsx
import {
  BuildingIcon,
  CoinsIcon,
  FileTextIcon,
  LayoutDashboardIcon,
  LifeBuoyIcon,
  MegaphoneIcon,
  MusicIcon,
  PercentIcon,
  SettingsIcon,
  ShieldIcon,
  TruckIcon,
} from "lucide-react"
import { DashboardShell, type DashboardNavSection } from "@/components/blocks/dashboard-shell"
import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"

// The Loka Musik admin menu: titled sections, pages, and parents that open a sub menu.
const nav: DashboardNavSection[] = [
  {
    label: "Overview & Support",
    items: [
      { title: "Dasbor", href: "#dasbor", icon: LayoutDashboardIcon },
      { title: "Tiket Support", href: "#tiket", icon: LifeBuoyIcon, badge: 3 },
      { title: "Pengumuman", href: "#pengumuman", icon: MegaphoneIcon },
    ],
  },
  {
    label: "Katalog & Operasional",
    items: [
      { title: "Pengiriman", href: "#pengiriman", icon: TruckIcon, badge: 12 },
      {
        title: "Laporan",
        icon: FileTextIcon,
        items: [
          { title: "Rilisan", href: "#laporan-rilisan" },
          { title: "Laporan Pengiriman", href: "#laporan-pengiriman" },
          { title: "Laporan Trek", href: "#laporan-trek" },
        ],
      },
    ],
  },
  {
    label: "Manajemen Klien & Artis",
    items: [
      { title: "Klien", href: "#klien", icon: BuildingIcon },
      {
        title: "Artis",
        icon: MusicIcon,
        items: [
          { title: "Artis", href: "#artis" },
          { title: "Klien Artis", href: "#klien-artis" },
          { title: "Kandidat Premium", href: "#kandidat" },
        ],
      },
    ],
  },
  {
    label: "Keuangan & Royalti",
    items: [
      {
        title: "Royalti",
        icon: CoinsIcon,
        items: [
          { title: "Ringkasan", href: "#royalti" },
          { title: "Laporan Royalti", href: "#laporan-royalti", active: true },
          { title: "Unggah Laporan", href: "#unggah" },
          { title: "Penarikan", href: "#penarikan", badge: 5 },
        ],
      },
      {
        title: "Pajak",
        icon: PercentIcon,
        items: [
          { title: "Klien PPh21", href: "#pph21" },
          { title: "Pengaturan PPh21", href: "#pph21-pengaturan" },
        ],
      },
    ],
  },
  {
    label: "Sistem & Master Data",
    items: [
      {
        title: "Pengaturan",
        icon: SettingsIcon,
        items: [
          { title: "Opsi", href: "#opsi" },
          { title: "Template Email", href: "#template-email" },
        ],
      },
      {
        title: "Administrasi",
        icon: ShieldIcon,
        items: [
          { title: "Manajemen Staf", href: "#staf" },
          { title: "Log Aktivitas", href: "#log" },
        ],
      },
    ],
  },
]

export default function NavigationMenuDemo() {
  return (
    <DashboardShell
      nav={nav}
      labels={{ search: "Cari rilis, artis, atau dokumen", empty: "Tidak ada yang cocok.", account: "Akun {name}", menu: "Buka menu" }}
      logo={
        <span className="flex items-center gap-2 font-semibold">
          <span className="size-7 rounded-md bg-primary" />
          Loka Musik Admin
        </span>
      }
      account={{
        name: "Raka Pratama",
        initials: "RP",
        detail: "raka@contoh.id",
        menu: [{ label: "Profil" }, { label: "Keluar", destructive: true }],
      }}
      search={{ groups: [{ heading: "Menu", items: [{ label: "Laporan Royalti" }, { label: "Penarikan" }] }] }}
    >
      <h1 className="text-2xl font-semibold">Laporan Royalti</h1>
      <p className="mt-1 text-sm text-muted-foreground">Laporan dari aggregator yang sudah diunggah.</p>
      <div className="mt-6 grid gap-4 md:grid-cols-3">
        {[
          ["Laporan bulan ini", "18"],
          ["Menunggu diproses", "4"],
          ["Total pendapatan", "Rp182.430.000,00"],
        ].map(([label, value]) => (
          <Card key={label}>
            <CardHeader>
              <CardDescription>{label}</CardDescription>
              <CardTitle className="text-2xl tabular-nums">{value}</CardTitle>
            </CardHeader>
          </Card>
        ))}
      </div>
    </DashboardShell>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Building, Coins, FileText, LayoutDashboard, LifeBuoy, Megaphone, Music, Percent, Settings, Shield, Truck } from "@lucide/vue"
import type { DashboardNavSection } from "@/components/blocks/dashboard-shell"
import { DashboardShell } from "@/components/blocks/dashboard-shell"
import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"

// The Loka Musik admin menu: titled sections, pages, and parents that open a sub menu.
const nav: DashboardNavSection[] = [
  {
    label: "Overview & Support",
    items: [
      { title: "Dasbor", href: "#dasbor", icon: LayoutDashboard },
      { title: "Tiket Support", href: "#tiket", icon: LifeBuoy, badge: 3 },
      { title: "Pengumuman", href: "#pengumuman", icon: Megaphone },
    ],
  },
  {
    label: "Katalog & Operasional",
    items: [
      { title: "Pengiriman", href: "#pengiriman", icon: Truck, badge: 12 },
      {
        title: "Laporan",
        icon: FileText,
        items: [
          { title: "Rilisan", href: "#laporan-rilisan" },
          { title: "Laporan Pengiriman", href: "#laporan-pengiriman" },
          { title: "Laporan Trek", href: "#laporan-trek" },
        ],
      },
    ],
  },
  {
    label: "Manajemen Klien & Artis",
    items: [
      { title: "Klien", href: "#klien", icon: Building },
      {
        title: "Artis",
        icon: Music,
        items: [
          { title: "Artis", href: "#artis" },
          { title: "Klien Artis", href: "#klien-artis" },
          { title: "Kandidat Premium", href: "#kandidat" },
        ],
      },
    ],
  },
  {
    label: "Keuangan & Royalti",
    items: [
      {
        title: "Royalti",
        icon: Coins,
        items: [
          { title: "Ringkasan", href: "#royalti" },
          { title: "Laporan Royalti", href: "#laporan-royalti", active: true },
          { title: "Unggah Laporan", href: "#unggah" },
          { title: "Penarikan", href: "#penarikan", badge: 5 },
        ],
      },
      {
        title: "Pajak",
        icon: Percent,
        items: [
          { title: "Klien PPh21", href: "#pph21" },
          { title: "Pengaturan PPh21", href: "#pph21-pengaturan" },
        ],
      },
    ],
  },
  {
    label: "Sistem & Master Data",
    items: [
      {
        title: "Pengaturan",
        icon: Settings,
        items: [
          { title: "Opsi", href: "#opsi" },
          { title: "Template Email", href: "#template-email" },
        ],
      },
      {
        title: "Administrasi",
        icon: Shield,
        items: [
          { title: "Manajemen Staf", href: "#staf" },
          { title: "Log Aktivitas", href: "#log" },
        ],
      },
    ],
  },
]

const account = {
  name: "Raka Pratama",
  initials: "RP",
  detail: "raka@contoh.id",
  menu: [{ label: "Profil" }, { label: "Keluar", destructive: true }],
}
const search = { groups: [{ heading: "Menu", items: [{ label: "Laporan Royalti" }, { label: "Penarikan" }] }] }
const labels = { search: "Cari rilis, artis, atau dokumen", empty: "Tidak ada yang cocok.", account: "Akun {name}", menu: "Buka menu" }
const stats = [
  ["Laporan bulan ini", "18"],
  ["Menunggu diproses", "4"],
  ["Total pendapatan", "Rp182.430.000,00"],
]
</script>

<template>
  <DashboardShell :nav="nav" :account="account" :search="search" :labels="labels">
    <template #logo>
      <span class="flex items-center gap-2 font-semibold">
        <span class="size-7 rounded-md bg-primary" />
        Loka Musik Admin
      </span>
    </template>
    <h1 class="text-2xl font-semibold">Laporan Royalti</h1>
    <p class="mt-1 text-sm text-muted-foreground">Laporan dari aggregator yang sudah diunggah.</p>
    <div class="mt-6 grid gap-4 md:grid-cols-3">
      <Card v-for="[label, value] in stats" :key="label">
        <CardHeader>
          <CardDescription>{{ label }}</CardDescription>
          <CardTitle class="text-2xl tabular-nums">{{ value }}</CardTitle>
        </CardHeader>
      </Card>
    </div>
  </DashboardShell>
</template>
```

**Flutter**
```dart
// The navigation menu of a product with many pages, such as the Loka Musik admin: titled sections, pages,
// and parents that open a sub menu in place. A parent is not a page; it opens on its own when one of its
// pages is the current page. Put this column in the sidebar of the dashboard frame.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:sosialoka_ui/sosialoka_ui.dart';

class _Page {
  const _Page(this.title, {this.icon, this.active = false, this.badge});
  final String title;
  final IconData? icon;
  final bool active;
  final int? badge;
}

class _Parent {
  const _Parent(this.title, this.icon, this.pages);
  final String title;
  final IconData icon;
  final List<_Page> pages;
}

const _sections = <(String, List<Object>)>[
  ('Overview & Support', [
    _Page('Dasbor', icon: LucideIcons.layoutDashboard),
    _Page('Tiket Support', icon: LucideIcons.lifeBuoy, badge: 3),
  ]),
  ('Katalog & Operasional', [
    _Page('Pengiriman', icon: LucideIcons.truck, badge: 12),
    _Parent('Laporan', LucideIcons.fileText, [_Page('Rilisan'), _Page('Laporan Pengiriman'), _Page('Laporan Trek')]),
  ]),
  ('Keuangan & Royalti', [
    _Parent('Royalti', LucideIcons.coins, [
      _Page('Ringkasan'),
      _Page('Laporan Royalti', active: true),
      _Page('Unggah Laporan'),
      _Page('Penarikan', badge: 5),
    ]),
    _Parent('Pajak', LucideIcons.percent, [_Page('Klien PPh21'), _Page('Pengaturan PPh21')]),
  ]),
];

class NavigationMenuDemo extends StatelessWidget {
  const NavigationMenuDemo({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    return Container(
      width: 256,
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: theme.colorScheme.card,
        border: Border(right: BorderSide(color: theme.colorScheme.border)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisSize: MainAxisSize.min,
        children: [
          for (final (label, entries) in _sections) ...[
            Padding(
              padding: const EdgeInsets.fromLTRB(8, 12, 8, 4),
              child: Text(label, style: theme.textTheme.small.copyWith(color: theme.colorScheme.mutedForeground, fontSize: 12)),
            ),
            for (final entry in entries)
              if (entry is _Parent) _ParentEntry(parent: entry) else _PageEntry(page: entry as _Page),
          ],
        ],
      ),
    );
  }
}

class _PageEntry extends StatelessWidget {
  const _PageEntry({required this.page, this.indent = false});

  final _Page page;
  final bool indent;

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final c = context.sosialoka;
    return Semantics(
      selected: page.active,
      child: ShadButton.ghost(
        height: indent ? 32 : 36,
        mainAxisAlignment: MainAxisAlignment.start,
        backgroundColor: page.active ? c.primarySoft : null,
        foregroundColor: page.active ? c.primaryText : theme.colorScheme.foreground,
        leading: page.icon == null ? null : Icon(page.icon, size: 16),
        trailing: page.badge == null ? null : Text('${page.badge}', style: theme.textTheme.small.copyWith(fontSize: 12)),
        onPressed: () {},
        child: Expanded(child: Text(page.title, overflow: TextOverflow.ellipsis)),
      ),
    );
  }
}

class _ParentEntry extends StatefulWidget {
  const _ParentEntry({required this.parent});

  final _Parent parent;

  @override
  State<_ParentEntry> createState() => _ParentEntryState();
}

class _ParentEntryState extends State<_ParentEntry> {
  // A parent opens on its own when one of its pages is the current page.
  late bool _open = widget.parent.pages.any((p) => p.active);

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Semantics(
          expanded: _open,
          child: ShadButton.ghost(
            mainAxisAlignment: MainAxisAlignment.start,
            foregroundColor: theme.colorScheme.foreground,
            leading: Icon(widget.parent.icon, size: 16),
            trailing: AnimatedRotation(
              turns: _open ? 0.25 : 0,
              duration: const Duration(milliseconds: 200),
              child: const Icon(LucideIcons.chevronRight, size: 16),
            ),
            onPressed: () => setState(() => _open = !_open),
            child: Expanded(child: Text(widget.parent.title)),
          ),
        ),
        if (_open)
          Container(
            margin: const EdgeInsets.only(left: 15),
            padding: const EdgeInsets.only(left: 9),
            decoration: BoxDecoration(border: Border(left: BorderSide(color: theme.colorScheme.border))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [for (final page in widget.parent.pages) _PageEntry(page: page, indent: true)],
            ),
          ),
      ],
    );
  }
}
```

## Pasang

Menu ini bagian dari blok Dasbor; pasang dari halaman [Dasbor](https://design.sosialoka.tech/id/patterns/dashboard/#pasang).

## Aksesibilitas

- Induk berupa tombol dengan `aria-expanded` dan `aria-controls` yang menunjuk sub menunya.
- Halaman saat ini punya `aria-current="page"`, termasuk di dalam sub menu.
- Judul bagian menamai kelompok link di bawahnya.
- Hitungan dibacakan bersama halamannya, misalnya "Tiket Support 3".
