# Common behavior

> Rules every Navigation component shares - right-to-left layouts, localeText, className and style passthrough, icons, children and render props, accessibility, server rendering, and the options left out by design.

Canonical: https://salmonui.dev/docs/navigation/common-behavior

Accordion, Breadcrumbs, Carousel, ContextMenu, Menubar, SidebarLayout,
Tabs, Toolbar (and the App Bar), TreeView, Stepper and Pager follow
the same rules, so what you learn on one page holds on the others.

## Right-to-left

Set `dir="rtl"` on the page or any container:
every stylesheet uses CSS logical properties, and arrow keys, submenus,
overflow menus, separators, the Carousel's slide direction and the Pager's
icons mirror with it.

```tsx
<div dir="rtl">
  <Tabs defaultValue="a">…</Tabs>
</div>
```

## Texts

Every built-in string comes from a `localeText` prop: a partial object over
the exported, frozen English defaults (`TABS_DEFAULT_LOCALE_TEXT`,
`TOOLBAR_DEFAULT_LOCALE_TEXT`, `CAROUSEL_DEFAULT_LOCALE_TEXT`,
`PAGER_DEFAULT_LOCALE_TEXT`, ...). Pass only the keys you translate; the
strings serialize, so the server and the client render the same text.
Accordion has no built-in text, so it has no `localeText`.

```tsx
<Pager totalCount={120} localeText={{ nextPage: 'Nächste Seite', previousPage: 'Vorherige Seite' }} />
```

## Classes, styles and attributes

`className` and `style` land on the root after the system class, and every
other DOM prop (`id`, `aria-*`, `data-*`, handlers) spreads onto the
element that owns the behavior. Parts are components of their own
(`AccordionItem`, `BreadcrumbItem`, `Tab`, `TabPanel`, `ToolbarItem`,
`Step`) and take the same props; internal parts carry
`.salmon-<component>-<part>` classes and state in `data-*` attributes.
`width` and `height` are plain `style` values.

## Icons

Icons are `ReactNode`s, never class names or image URLs: `icon` on
`AccordionItem`, `BreadcrumbItem`, `Tab`, `ToolbarItem`, `Step` and menu
items, `getItemIcon` on TreeView, and `icons` on the Pager for its built-in
buttons. They
render decoratively (`aria-hidden`); the label names the item.

## Templates

Content is children. Where a component renders an element for you, a
`render*` prop receives a stable context and returns a node: `render` on
`BreadcrumbItem`, `MenubarItem`, `SidebarMain` and `SidebarTrigger` (router
links), `renderStep`, `renderIndicator` and the Carousel button props,
`renderPager` / `renderInfo` / `renderPageSize`, and TreeView `renderItem`.
Per-item hooks (`renderItem`, `getItemProps`) are pure and run at
render time for rendered items only, including TreeView's virtual window;
they never touch the DOM. Toolbar and Accordion items are your
own children, so customizing an item means rendering it.

## Accessibility

Each component implements its WAI-ARIA APG pattern (accordion, breadcrumb,
carousel, menu and menubar, tabs, toolbar, tree view; ordered list with
`aria-current="step"` for the Stepper; a navigation landmark for the
Pager), with one tab stop and arrow keys where the pattern asks for it.
axe-core and ARIA snapshots run for every component in light, dark and
right-to-left.

## Server rendering

Every component renders on the server (Next.js App Router, Vite SSR) and
hydrates without warnings. Layout that needs measuring (overflow menus,
`collapseBelow`, the Carousel's position) starts from CSS in the server
markup and only changes classes after mount; popups mount on the client.

## Options left out by design

Not offered, each with the reason and what to use:

- Built-in state persistence: a component that writes
  `localStorage` on its own breaks server rendering (the server cannot read the stored value,
  so the first client render would differ) and hides state from the
  application. Every persisted state is a controlled prop with a change
  event (`Accordion value`, `Tabs value`, `TreeView expanded` / `selected`
  / `checked`, `SidebarLayout open` / `collapsed`, `Pager page` /
  `pageSize`, `Stepper value`, `Carousel index`): store the value from the
  change event (in `localStorage` or the URL) and pass it back after mount.
- HTML string content and templates, with their sanitizing and encoding
  options: content is `ReactNode`, so nothing is parsed from strings and
  there is nothing to sanitize; string templates would also need a runtime
  template compiler that breaks a strict CSP. Move HTML content into JSX;
  for trusted HTML, call `dangerouslySetInnerHTML` inside your own element.
- Lifecycle events and a teardown method: React already exposes
  mount and unmount. Run setup logic in `useEffect` or a ref callback, and
  remove a component with conditional rendering.
- `animation` settings: the theme has no motion input and motion must
  stop under `prefers-reduced-motion`, so each component has one fixed CSS
  transition with no duration or easing API. If a different speed is
  required, override the transition in CSS on the component's class.
- Imperative collection methods that add, hide, enable, select or refresh
  items: the item collection belongs to the application, and
  methods that mutate an internal copy make the UI and the application
  state disagree and cannot run on the server. Add an item by inserting it
  into the array the children render from, disable it with `disabled`, hide
  it with conditional rendering and select it with the `value` prop.
  The `ref` keeps only what props cannot express (TreeView `scrollToItem`,
  `expandAll`, `startRename`; Carousel `next`, `play`), and overflow
  re-measures by itself.

### Live demo: Navigation common behavior

Live demo: https://salmonui.dev/demos/navigation/navigation-common

Breadcrumbs, Tabs and a Pager sharing the category rules - a right-to-left switch, English or German localeText, className, style and data attributes on the root, a ReactNode icon, render props, keyboard support and server rendering.

`Demo.tsx`:

```tsx
'use client';
import { useState, useSyncExternalStore } from 'react';
import { BreadcrumbItem, Breadcrumbs, type BreadcrumbItemRenderProps } from '@salmonui/navigation/breadcrumbs';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Pager } from '@salmonui/navigation/pager';
import { Select } from '@salmonui/dropdowns/select';
import { Tab, TabList, TabPanel, Tabs } from '@salmonui/navigation/tabs';

// Placeholder links target this demo's own title (no dangling links in the static export).
const DEMO_ANCHOR = '#demo-navigation-common-title';

const HOME = (
  <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
    <path d="M8 1 1 7h2v7h4v-4h2v4h4V7h2z" />
  </svg>
);

const TEXT = {
  en: {
    breadcrumbs: { label: 'Breadcrumb' },
    tabs: { scrollBackward: 'Scroll tabs backward', scrollForward: 'Scroll tabs forward' },
    pager: { label: 'Pagination', nextPage: 'Next page', previousPage: 'Previous page', pagerInfo: '{page} of {pageCount} pages' },
    crumbs: ['Home', 'Reports', 'Sales'],
    tabNames: ['Overview', 'Regions', 'Products', 'Channels', 'Forecast', 'Returns', 'Archive'],
    showing: 'Showing page',
  },
  de: {
    breadcrumbs: { label: 'Brotkrumen' },
    tabs: { scrollBackward: 'Tabs zurück blättern', scrollForward: 'Tabs vor blättern' },
    pager: { label: 'Seitennavigation', nextPage: 'Nächste Seite', previousPage: 'Vorherige Seite', pagerInfo: 'Seite {page} von {pageCount}' },
    crumbs: ['Start', 'Berichte', 'Umsatz'],
    tabNames: ['Übersicht', 'Regionen', 'Produkte', 'Kanäle', 'Prognose', 'Retouren', 'Archiv'],
    showing: 'Seite',
  },
} as const;

const LANGUAGES = [
  { value: 'en', label: 'English' },
  { value: 'de', label: 'Deutsch' },
];

const noopSubscribe = () => () => {};

/** Stands in for a router link: `render` hands over the computed props. */
function RouterLink(props: BreadcrumbItemRenderProps) {
  return <a {...props} data-router-link="" />;
}

export default function NavigationCommonDemo() {
  const [rtl, setRtl] = useState(false);
  const [lang, setLang] = useState<'en' | 'de'>('en');
  // Server snapshot false, client snapshot true: flips right after hydration, no mismatch.
  const hydrated = useSyncExternalStore(noopSubscribe, () => true, () => false);
  const t = TEXT[lang];
  return (
    <div style={{ display: 'grid', gap: 16 }}>
      <div className="demo-toolbar" style={{ marginBlockEnd: 0 }}>
        <Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
          Right to left
        </Checkbox>
        <label>
          Language{' '}
          <Select aria-label="Language" data={LANGUAGES} value={lang} onValueChange={(value) => value != null && setLang(value as 'en' | 'de')} />
        </label>
        <span>
          Render: <output>{hydrated ? 'server HTML, hydrated' : 'server HTML'}</output>
        </span>
      </div>
      {/* The components switch direction and language; the demo controls stay left to right. */}
      <div dir={rtl ? 'rtl' : 'ltr'} lang={lang} style={{ display: 'grid', gap: 16 }}>
        <Breadcrumbs
          localeText={t.breadcrumbs}
          className="demo-crumbs"
          style={{ fontSize: 14 }}
          data-section="reports"
          onItemClick={(event) => event.preventDefault()}
        >
          <BreadcrumbItem href={DEMO_ANCHOR} icon={HOME}>
            {t.crumbs[0]}
          </BreadcrumbItem>
          <BreadcrumbItem href={DEMO_ANCHOR} render={(props) => <RouterLink {...props} />}>
            {t.crumbs[1]}
          </BreadcrumbItem>
          <BreadcrumbItem>{t.crumbs[2]}</BreadcrumbItem>
        </Breadcrumbs>
        <div style={{ maxWidth: 380 }}>
          <Tabs defaultValue="t0" localeText={t.tabs}>
            <TabList aria-label={t.crumbs[2]}>
              {t.tabNames.map((name, index) => (
                <Tab key={index} value={`t${index}`}>
                  {name}
                </Tab>
              ))}
            </TabList>
            {t.tabNames.map((name, index) => (
              <TabPanel key={index} value={`t${index}`}>
                {name}
              </TabPanel>
            ))}
          </Tabs>
        </div>
        <Pager
          totalCount={240}
          defaultPage={3}
          locale={lang}
          localeText={t.pager}
          renderInfo={(ctx) => (
            <span>
              {t.showing} <strong>{ctx.page}</strong> / {ctx.pageCount}
            </span>
          )}
        />
      </div>
    </div>
  );
}
```
