SideNav
In-page navigation for an area with many sections, such as an app's settings. Groups of router links, each with a small heading; the current page has aria-current="page" and the Sidebar's selected look. By default it is a compact column that fills its box: the app sets the width and puts the section next to it, for example grid gap-8 md:grid-cols-[15rem_minmax(0,1fr)]. sticky keeps the column in view while the page scrolls, under the AppShell header (--p-sticky-top); it scrolls on its own when it is taller than the screen, with its scrollbar hidden; wheel, touch and keyboard scrolling still work. For your own scroll box, the class scrollbar-none (from @percentsa/ui/styles.css) hides the scrollbar the same way (Tailwind's own scrollbar-none plus the WebKit rule). To show the scrollbar again, put scrollbar-auto in className. variant="list" is the same groups as a full-width list with each description under its label and a chevron at the end: the area's home on a phone (description shows only there). Show the column from 768 px and the list below it. Labels wrap; they are never cut. iconOnly (column only) turns the column into an icon column: each item is its icon in a 44 px box, the label is the tooltip and the link's accessible name, group headings stay for screen readers only, and a badge is a small dot (its value stays for screen readers). An item without an icon shows the first letter of its label (a label that is not text shows nothing, so give it an icon). iconOnly has no effect on variant="list". The part has no breakpoints of its own: for an icon column from 768 px and the full column from 1280 px, render it twice and show each with classes, hidden md:grid xl:hidden for the icon column and hidden xl:grid for the full one (see the second example). Use Sidebar for the app menu and Tabs for a few items in a row.
import { SideNav } from "@percentsa/ui";Examples
Profile
How this part of your account works.
Email me about changes
Code
export function SideNavExample() {
const t = useT();
const [current, setCurrent] = useState("#profile");
const groups = useSettingsGroups(current);
const title = groups.flatMap((g) => g.items).find((i) => i.current)?.label;
return (
<div
className="grid w-full gap-8 md:grid-cols-[15rem_minmax(0,1fr)]"
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);
}}
>
{/* Add `sticky` to keep the column in view while the page scrolls. Leave it out in a box that scrolls on its own. */}
<SideNav groups={groups} label={t("Settings", "الإعدادات")} />
<SettingsSection title={title} description={t("How this part of your account works.", "كيف يعمل هذا الجزء من حسابك.")} footer={<Button>{t("Save", "حفظ")}</Button>}>
<SettingsRow label={t("Display name", "الاسم الظاهر")} controlId="side-nav-name" control={<Input id="side-nav-name" defaultValue={t("Sara", "سارة")} />} />
<SettingsRow label={t("Email me about changes", "راسلني عند حدوث تغييرات")} control={<Switch defaultChecked label={t("On", "مفعّل")} />} />
</SettingsSection>
</div>
);
}
/** The settings home on a phone: the same groups as a full-width list with a description under each label. */Narrower than 768 px both columns hide: show the list variant there. From 768 px the icon column shows; from 1280 px the full column.
Code
export function SideNavIconsExample() {
const t = useT();
const [current, setCurrent] = useState("#profile");
const groups = useSettingsGroups(current);
return (
<div
className="flex w-full gap-8"
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);
}}
>
<SideNav groups={groups} label={t("Settings", "الإعدادات")} iconOnly className="hidden w-11 shrink-0 md:grid xl:hidden" />
<SideNav groups={groups} label={t("Settings", "الإعدادات")} className="hidden w-60 shrink-0 xl:grid" />
<p className="text-sm text-fg-3">{t("Narrower than 768 px both columns hide: show the list variant there. From 768 px the icon column shows; from 1280 px the full column.", "تحت 768 بكسل يختفي العمودان: اعرض نمط القائمة هناك. من 768 بكسل يظهر عمود الأيقونات؛ ومن 1280 بكسل العمود الكامل.")}</p>
</div>
);
}Code
export function SideNavListExample() {
const t = useT();
const groups = useSettingsGroups("");
return (
<div className="w-full max-w-sm" onClick={(e) => (e.target as Element).closest("nav a") && e.preventDefault()}>
<SideNav variant="list" groups={groups} label={t("Settings", "الإعدادات")} />
</div>
);
}
/** An icon column from 768 px, the full column from 1280 px: two renders, each shown by responsive classes. Below 768 px use `variant="list"`. Resize the window to see it. */