Skip to content
العربية
App patterns

Sidebar

App navigation in sections, with icons, badges and the current page marked; the mark slides to the new page (no slide with reduced motion). It collapses to an icon rail with tooltips. The collapse button shows its words ("Collapse sidebar") beside the icon while the sidebar is expanded; in the rail it is the icon alone, and the words are its tooltip and accessible name. toggleLabel={false} turns the words off. They come from the provider strings collapseSidebar and expandSidebar; an app overrides them with strings. Give PercentProvider the router's location, and a clicked link shows a pending bar at once, until the new page is there. An item can have children: it stays a link to its own href, and a chevron button beside it folds the child links (one level; the children of a child are not shown). The group starts open when it or a child is current, and it opens when the current page moves into it. defaultOpen opens it at first, and defaultOpen={false} keeps it folded even when the current page is inside; then the parent shows the selected look without aria-current. The button name is the label with the strings showSubItems and hideSubItems. The icon rail shows no children. An item without href is a plain row, for a status such as "Loading clients…": it is not a link, not focusable, never current, and has no children; it keeps the height and layout of a link row, in a quieter colour, so the list does not move when links replace it. The app gives the live role to the label (role="status") so a screen reader announces the text when it changes. A section can fold (collapsible): its title shows badge while folded, the choice is kept in the browser under id, and a current page inside opens it. An item can show a status dot (danger, warning) with statusLabel for screen readers and as a tooltip.

import { Sidebar, useSidebarRail } from "@percentsa/ui";

Examples

Sara Alotaibi
Code
export function SidebarExample() {
  const [collapsed, setCollapsed] = useState(false);
  const [current, setCurrent] = useState("#inbox");
  return (
    <div
      className="h-[28rem] overflow-hidden rounded-card border border-line"
      onClick={(e) => {
        const href = (e.target as Element).closest("nav a")?.getAttribute("href");
        if (!href) return;
        e.preventDefault(); // an app's router sets `current`; here a click does
        setCurrent(href);
      }}
    >
      <Sidebar
        sections={useSections(current)}
        collapsed={collapsed}
        onCollapsedChange={setCollapsed}
        header={<SidebarLogo collapsed={collapsed} />}
        footer={<User collapsed={collapsed} />}
      />
    </div>
  );
}

/** Items with `children` fold open to their links; the group with the current page starts open. Hosting and System fold from their titles. */
Code
export function SidebarNestedExample() {
  const t = useT();
  const [current, setCurrent] = useState("#al-noor-app");
  const dot = (href: string, label: string) => ({ href, label, current: href === current });
  const group = (href: string, label: string, children: { href: string; label: string }[]) => {
    const kids = children.map((c) => dot(c.href, c.label));
    return { href, label, current: href === current, children: kids };
  };
  const sections: SidebarSection[] = [
    {
      title: t("Clients", "العملاء"),
      items: [
        { ...group("#al-noor", t("Al Noor", "النور"), [
          { href: "#al-noor-app", label: t("Mobile app", "تطبيق الجوال") },
          { href: "#al-noor-site", label: t("Website", "الموقع") },
        ]), icon: <Users />, status: "danger" as const, statusLabel: t("Site down", "الموقع متوقف") },
        { ...group("#sahab", t("Sahab", "سحاب"), [{ href: "#sahab-portal", label: t("Portal", "البوابة") }]), icon: <Users />, status: "warning" as const, statusLabel: t("Restarting", "قيد إعادة التشغيل") },
        { ...group("#private", t("Private client", "عميل خاص"), [{ href: "#private-erp", label: t("ERP", "نظام ERP") }]), icon: <Lock /> },
      ],
    },
    {
      title: t("Hosting", "الاستضافة"),
      collapsible: true,
      id: "docs-hosting",
      badge: "2",
      items: [
        { href: "#servers", label: t("Servers", "الخوادم"), icon: <Server /> },
        { href: "#backups", label: t("Backups", "النسخ الاحتياطية"), icon: <DatabaseBackup /> },
      ],
    },
    {
      title: t("System", "النظام"),
      collapsible: true,
      id: "docs-system",
      badge: "1",
      items: [{ href: "#logs", label: t("Logs", "السجلات"), icon: <ScrollText /> }],
    },
  ];
  return (
    <div
      className="h-[28rem] overflow-hidden rounded-card border border-line"
      onClick={(e) => {
        const href = (e.target as Element).closest("nav a")?.getAttribute("href");
        if (!href) return;
        e.preventDefault();
        setCurrent(href);
      }}
    >
      <Sidebar sections={sections} />
    </div>
  );
}

/** An item without `href` is a plain row: here a loading status. The app puts `role="status"` on the label, so a screen reader announces the text when it changes. */
Code
export function SidebarPlainRowExample() {
  const t = useT();
  const sections: SidebarSection[] = [
    {
      title: t("Clients", "العملاء"),
      items: [
        {
          icon: <Loader2 aria-hidden className="motion-safe:animate-spin" />,
          label: <span role="status">{t("Loading clients…", "جارٍ تحميل العملاء…")}</span>,
        },
      ],
    },
  ];
  return (
    <div className="h-48 overflow-hidden rounded-card border border-line">
      <Sidebar sections={sections} />
    </div>
  );
}

/** Ctrl+K (⌘K on a Mac) opens it anywhere on this page. Try "انشاء فاتوره": Arabic letter forms match. */