# Ribbon

> An Office-style ribbon built from Tabs whose panels each hold a Toolbar of labelled groups, with multi-size commands that step down before they overflow, group launchers, a File menu, contextual tabs, the simplified layout and a collapsed ribbon.

Canonical: https://salmonui.dev/docs/navigation/ribbon

```tsx
import { Button } from '@salmonui/buttons/button';
import { MenuButton } from '@salmonui/buttons/menu';
import { Tab, TabList, TabPanel, Tabs } from '@salmonui/navigation/tabs';
import { Toolbar, ToolbarGroup, ToolbarItem } from '@salmonui/navigation/toolbar';
import '@salmonui/buttons/button/styles.css';
import '@salmonui/buttons/menu/styles.css';
import '@salmonui/navigation/tabs/styles.css';
import '@salmonui/navigation/toolbar/styles.css';

<Tabs value={tab} onValueChange={setTab}>
  <div style={{ gridArea: 'list', display: 'flex', alignItems: 'center' }}>
    <MenuButton variant="ghost" items={fileItems}>File</MenuButton>
    <TabList aria-label="Ribbon tabs">
      <Tab value="home">Home</Tab>
      <Tab value="insert">Insert</Tab>
    </TabList>
  </div>
  <TabPanel value="home" tabIndex={-1}>
    <Toolbar aria-label="Home commands">
      <ToolbarGroup label="Clipboard" action={<Button variant="ghost" size="sm" iconOnly aria-label="Clipboard options"><LaunchIcon /></Button>}>
        <ToolbarItem label="Paste" sizes={['lg', 'md', 'sm']} overflowPriority={3}>
          {(size) =>
            size === 'sm' ? (
              <Button variant="ghost" size="sm" iconOnly aria-label="Paste" onClick={paste}><PasteIcon /></Button>
            ) : (
              <Button variant="ghost" size={size === 'lg' ? 'md' : 'sm'} iconStart={<PasteIcon />} onClick={paste}>Paste</Button>
            )
          }
        </ToolbarItem>
      </ToolbarGroup>
    </Toolbar>
  </TabPanel>
  <TabPanel value="insert" tabIndex={-1}>...</TabPanel>
</Tabs>
```

A ribbon is a set of tabs whose panels are toolbars, so it is built from
[Tabs](https://salmonui.dev/docs/navigation/tabs) and [Toolbar](https://salmonui.dev/docs/navigation/toolbar): the
tab row selects a panel, each panel holds one `Toolbar`, each group of
commands is a `ToolbarGroup` with a visible `label`, and each command is a
`ToolbarItem`. Tabs, groups and commands are all JSX, so the application
adds, removes, hides and disables commands by rendering.

## Structure

| Ribbon part | Salmon UI |
| --- | --- |
| The ribbon | `Tabs` around a tab row and one `TabPanel` per tab |
| A tab | `Tab` in the `TabList`; its `TabPanel` holds a `Toolbar` |
| A group | `ToolbarGroup label="..."`: the label shows under the group's items and names the group (`role="group"`) |
| A collection of items in a group | A run of items inside the group (items flow in one row) |
| A command | `ToolbarItem` around any control |
| The selected tab | `Tabs value` / `defaultValue` / `onValueChange`; `onBeforeValueChange` cancels (`event.preventDefault()`) |
| A group launcher | `ToolbarGroup action`: a `Button` next to the group label with its own `onClick` |
| A minimized ribbon | Application state: see [Minimize](https://salmonui.dev/docs/navigation/ribbon#minimize) |
| Classic and simplified layouts | Application state that changes the items' `sizes`: see [Layouts](https://salmonui.dev/docs/navigation/ribbon#layouts) |
| Contextual tabs | Conditionally rendered `Tab` and `TabPanel`: see [Contextual tabs](https://salmonui.dev/docs/navigation/ribbon#contextual-tabs) |
| The File menu | A `MenuButton` placed before the `TabList` |

Give each `TabPanel` `tabIndex={-1}`: the toolbar inside it is the panel's
tab stop, so the panel itself does not need one.

## Commands and sizes

A command is a `ToolbarItem` with `sizes`, the sizes it may take, largest
first. Its child can be a function of the current size, so the command
renders differently at each one; the recipe uses three:

| Size | Ribbon look | In the demo |
| --- | --- | --- |
| `'lg'` | Large: icon over the label | A `Button` with `iconStart` and its label; the toolbar stacks a button's icon over its label in a `'lg'` item |
| `'md'` | Medium: icon and label in a row | The same `Button` at `size="sm"` |
| `'sm'` | Small: icon only | `Button iconOnly` with `aria-label` |

The current size is also `data-size` on `.salmon-toolbar-item`, for your own
CSS. The server and the first client render use `sizes[0]`; the toolbar
measures after mount.

Render the `ToolbarItem` itself inside the group (call a helper function
that returns it, as the demo does): a component of your own that returns a
`ToolbarItem` would be wrapped in a second, measured item, because the
toolbar wraps every direct child that is not a toolbar part.

### Item types

Any Salmon control works as an item:

| Command kind | Salmon UI control in a `ToolbarItem` |
| --- | --- |
| Button | `Button` (or `ToggleButton` for a toggled command) |
| Checkbox | `Checkbox` |
| Color picker | `ColorPicker` |
| Combo box | `Combobox` |
| Drop-down | `Select`, or `MenuButton` for a command menu |
| Split button | `SplitButton` |
| Button group | `ToggleButtonGroup` (single or multiple selection) |
| Custom content | Any content inside `ToolbarItem` |

Import the controls you use. Form fields (selects, comboboxes, inputs)
default to `overflow="never"`, because a field cannot be a menu entry;
buttons and checkboxes overflow into the menu, and choosing their entry
clicks the hidden control.

### Gallery

A style gallery is a `Listbox` with
`layout="grid"` inside a `Popover` launched from a `ToolbarItem`:
the tiles are grid cells, arrow keys move across rows and columns, Enter or a
click applies a style, and Escape closes the popover with focus back on its
trigger. Keep the item `overflow="never"` (the popover cannot be a menu
entry), or put a "Styles" `MenuButton` with the same choices in the overflow.
`Popover` comes from `@salmonui/overlays/popover` and `Listbox` from
`@salmonui/dropdowns/listbox`; import their `styles.css` as well.

```tsx
<ToolbarGroup label="Styles">
  <ToolbarItem overflow="never">
    <Popover placement="bottomStart">
      <Popover.Trigger render={(props) => <Button {...props} variant="ghost" />}>Styles</Popover.Trigger>
      <Popover.Content aria-label="Style gallery">
        <Listbox aria-label="Styles" layout="grid" columns={3} data={styles} onValueChange={applyStyle} />
      </Popover.Content>
    </Popover>
  </ToolbarItem>
</ToolbarGroup>
```

## Adaptive resizing

When the ribbon gets narrower, commands step down before anything leaves
the bar:

1. The command with the lowest `overflowPriority` (ties: the last one in
   the bar) steps down one size at a time, `'lg'` to `'md'` to `'sm'`, then
   the next command.
2. Only when every multi-size command is at its last size do items move
   into the overflow menu behind the "more" button, in the same order.
3. Widening reverses it.

Give the commands you want to keep large a higher `overflowPriority`. Item
widths are measured once per size and cached, so a resize is a read of the
container; React commits only when a size or the overflow set changes. The
stepping code is a separate chunk that loads only when an item declares more
than one size.

### Group overflow popups

Items that overflowed from a labelled group appear in the overflow menu
under the group's label, in bar order, so the menu keeps the ribbon's
grouping; the overflow menu's open state tells when it shows. A group whose items
have all overflowed is hidden together with its label and launcher.

## Layouts

The classic layout is the default above. The simplified layout is a single
row of small commands: switch every command's `sizes` to `['sm']` from
application state, and render the layout switch where you like, for example after the `TabList`:

```tsx
const sizes = layout === 'classic' ? ['lg', 'md', 'sm'] : ['sm'];

<Button aria-pressed={layout === 'simplified'} onClick={() => setLayout(layout === 'classic' ? 'simplified' : 'classic')}>
  Simplified
</Button>
```

A one-entry `sizes` list fixes the size, so the toolbar only moves items
into the menu. No refresh call is needed: the toolbar re-measures on resize
and after every render.

## Minimize

Keep a `collapsed` flag in application state. While it is set, hide the
`TabPanel`s and show the selected tab's toolbar in a [Popover](https://salmonui.dev/docs/layout/popover)
anchored to the ribbon when a tab is clicked:

```tsx
<TabPanel value={value} tabIndex={-1} style={collapsed ? { display: 'none' } : {}}>
  {collapsed ? null : panel(value)}
</TabPanel>

<Popover open={collapsed && popup} onOpenChange={setPopup} anchor={ribbonRef} placement="bottomStart">
  <Popover.Content aria-label="Ribbon commands">{panel(tab)}</Popover.Content>
</Popover>
```

Open the popover from `Tabs onValueChange` and from each `Tab onClick` (a
click on the selected tab does not change the value). Escape and an outside
press close it. Collapsing and expanding are your own state changes, so
any code that reacts to them belongs where you set the flag.

## Contextual tabs

Render a contextual tab only while its context applies, for example while a
table is selected, and move the selection back when it disappears:

```tsx
const current = tab === 'table' && !tableSelected ? 'home' : tab;

<TabList aria-label="Ribbon tabs">
  <Tab value="home">Home</Tab>
  {tableSelected ? <Tab value="table">Table Design</Tab> : null}
</TabList>
```

## File menu and Backstage

The File menu is a `MenuButton` placed before the `TabList` in the tab row.
Backstage (a full-screen File view) is not built: open a full-screen
[Dialog](https://salmonui.dev/docs/layout/dialog) with a vertical `Tabs` list from the File menu
instead. The help pane is any element after the
`TabList` in the same row.

## Tooltips

Wrap a command in the Layout category's [Tooltip](https://salmonui.dev/docs/layout/tooltip) for a
title and a description. Use its `render` form so the `Button` stays the
item's direct child (the toolbar styles a large item's button directly):

```tsx
<Tooltip
  content={<span><strong>Paste</strong><br />Add content from the clipboard.</span>}
  render={(trigger) => <Button {...trigger} variant="ghost" iconStart={<PasteIcon />}>Paste</Button>}
/>
```

The tooltip opens on hover and keyboard focus and describes the button
(`aria-describedby`).

## Keyboard and accessibility

The ribbon is three kinds of tab stops, each with its own APG keys:

| Part | Keys |
| --- | --- |
| File `MenuButton` | Enter, Space or ArrowDown open the menu; arrows move; Escape closes and returns focus |
| `TabList` (APG Tabs) | One tab stop; ArrowLeft / ArrowRight move and select (mirrored under `dir="rtl"`), Home / End |
| Panel `Toolbar` (APG Toolbar) | One tab stop that returns to the last focused command; ArrowLeft / ArrowRight move through the commands, the group launchers and the "more" button, Home / End |
| Overflow menu | ArrowDown on "more" opens it on the first entry; ArrowUp / ArrowDown move through all groups; Enter runs the command; Escape closes and returns focus |

Each group is a `role="group"` named by its label; the launcher is a button
with its own `aria-label`. KeyTips are not built:
put `aria-keyshortcuts` on commands that have application shortcuts.

## Not provided

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

- Backstage: an Office-style full-screen File view that existing
  parts already cover. Put the Backstage items into a full-screen `Dialog`
  with a vertical `Tabs` list, opened from the File `MenuButton`.
- KeyTips: they need a global key-sequence layer over every item,
  which a composition of separate parts has no single owner for. Register
  your shortcuts in your application and announce them with
  `aria-keyshortcuts`; the tabs and toolbar keyboard models stay available.
- A help pane slot: in a composition it is simply an element.
  Render the help pane content after the `TabList` in the tab row.
- Imperative methods that add, remove, update, show, hide, enable or
  disable tabs, groups, collections and items: the composition is
  declarative, so tabs, groups and items exist because you render them.
  Make each change a state change: render, hide (conditional rendering) or
  disable (`disabled`) the controls.
- A built-in layout switcher button and a collapse button: render your own
  (the demo puts both after the `TabList`).

## Server rendering

The ribbon is a `Tabs` and a set of `Toolbar`s, so it renders on the server
and hydrates without warnings: every command renders at `sizes[0]` on the
server, and the toolbar steps sizes and fills the overflow menu only after
mount. Popovers, menus and tooltips mount on the client.

## API reference

The ribbon is built from the parts in
[`@salmonui/navigation/tabs`](https://salmonui.dev/docs/reference/navigation/tabs),
[`@salmonui/navigation/toolbar`](https://salmonui.dev/docs/reference/navigation/toolbar),
[`@salmonui/buttons/menu`](https://salmonui.dev/docs/reference/buttons/menu),
[`@salmonui/overlays/popover`](https://salmonui.dev/docs/reference/overlays/popover) and
[`@salmonui/dropdowns/listbox`](https://salmonui.dev/docs/reference/dropdowns/listbox).

### Live demo: Ribbon

Live demo: https://salmonui.dev/demos/navigation/ribbon-recipe

Tabs whose panels each hold a Toolbar of labelled groups with multi-size commands, a group launcher, a File menu, a contextual tab, the simplified layout and a collapsed ribbon in a Popover.

`Demo.tsx`:

```tsx
'use client';
import { useRef, useState, type ReactNode } from 'react';
import { Button, ToggleButton, ToggleButtonGroup } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { ColorPicker } from '@salmonui/inputs/color-picker';
import { Combobox } from '@salmonui/dropdowns/combobox';
import { MenuButton, SplitButton } from '@salmonui/buttons/menu';
import { Listbox } from '@salmonui/dropdowns/listbox';
import { Popover } from '@salmonui/overlays/popover';
import { Select } from '@salmonui/dropdowns/select';
import { Slider } from '@salmonui/inputs/slider';
import { Tab, TabList, TabPanel, Tabs } from '@salmonui/navigation/tabs';
import { Toolbar, ToolbarGroup, ToolbarItem, type ToolbarItemSize } from '@salmonui/navigation/toolbar';
import { Tooltip, type TooltipTriggerProps } from '@salmonui/overlays/tooltip';

const GALLERY = ['Normal', 'Title', 'Heading 1', 'Heading 2', 'Quote', 'Code'].map((label) => ({ value: label.toLowerCase().replace(' ', '-'), label }));

// A ribbon (/docs/navigation/ribbon): Tabs whose panels each hold a
// Toolbar of labelled groups. Commands are multi-size ToolbarItems; the
// classic layout allows lg, md and sm, the simplified layout only sm.

const icon = (d: string): ReactNode => (
  <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    <path d={d} />
  </svg>
);
const ICONS = {
  paste: icon('M5 2h6v2H5zM3 4h10v10H3z'),
  cut: icon('M4 4l8 8M12 4l-8 8'),
  copy: icon('M5 5h8v8H5zM3 11V3h8'),
  find: icon('M7 3a4 4 0 1 0 0 8a4 4 0 1 0 0-8M10 10l4 4'),
  table: icon('M2 3h12v10H2zM2 7h12M6 3v10'),
  picture: icon('M2 3h12v10H2zM2 11l4-4 3 3 2-2 3 3'),
  bold: icon('M4 3h5a3 3 0 0 1 0 6H4zM4 9h6a3 3 0 0 1 0 6H4z'),
  launch: icon('M6 10l6-6M8 4h4v4'),
};

type Layout = 'classic' | 'simplified';
const SIZES: Record<Layout, readonly ToolbarItemSize[]> = { classic: ['lg', 'md', 'sm'], simplified: ['sm'] };

interface Command {
  readonly id: string;
  readonly label: string;
  readonly glyph: ReactNode;
  readonly tip?: string;
  readonly priority?: number;
}

/**
 * One command as a multi-size ToolbarItem. Called as a function (not as a
 * component), so the ToolbarItem is a direct child of its ToolbarGroup.
 */
function command(c: Command, sizes: readonly ToolbarItemSize[], run: (id: string) => void) {
  const button = (size: ToolbarItemSize, trigger?: TooltipTriggerProps) =>
    size === 'sm' ? (
      <Button {...trigger} variant="ghost" iconOnly aria-label={c.label} onClick={() => run(c.id)}>
        {c.glyph}
      </Button>
    ) : (
      <Button {...trigger} variant="ghost" size={size === 'lg' ? 'md' : 'sm'} iconStart={c.glyph} onClick={() => run(c.id)}>
        {c.label}
      </Button>
    );
  return (
    <ToolbarItem key={c.id} label={c.label} icon={c.glyph} sizes={sizes} overflowPriority={c.priority}>
      {(size) =>
        c.tip ? (
          <Tooltip
            content={
              <span>
                <strong>{c.label}</strong>
                <br />
                {c.tip}
              </span>
            }
            render={(trigger) => button(size, trigger)}
          />
        ) : (
          button(size)
        )
      }
    </ToolbarItem>
  );
}

export default function RibbonRecipeDemo() {
  const [tab, setTab] = useState('home');
  const [layout, setLayout] = useState<Layout>('classic');
  const [collapsed, setCollapsed] = useState(false);
  const [popup, setPopup] = useState(false);
  const [tableSelected, setTableSelected] = useState(false);
  const [width, setWidth] = useState(900);
  const [last, setLast] = useState('nothing yet');
  const frame = useRef<HTMLDivElement>(null);
  const sizes = SIZES[layout];
  const run = (id: string) => setLast(id);
  // A contextual tab that disappears takes the selection back to Home.
  const current = tab === 'table' && !tableSelected ? 'home' : tab;

  const panel = (value: string): ReactNode => {
    if (value === 'insert')
      return (
        <Toolbar aria-label="Insert commands">
          <ToolbarGroup label="Tables">{command({ id: 'table', label: 'Table', glyph: ICONS.table }, layout === 'classic' ? ['lg', 'sm'] : ['sm'], run)}</ToolbarGroup>
          <ToolbarGroup label="Illustrations">
            {command({ id: 'picture', label: 'Picture', glyph: ICONS.picture, tip: 'Insert a picture from this device.' }, layout === 'classic' ? ['lg', 'sm'] : ['sm'], run)}
            <ToolbarItem overflow="never">
              <Combobox aria-label="Shape" data={['Circle', 'Square', 'Arrow']} placeholder="Shape" />
            </ToolbarItem>
          </ToolbarGroup>
        </Toolbar>
      );
    if (value === 'table')
      return (
        <Toolbar aria-label="Table design commands">
          <ToolbarGroup label="Table style options">
            <ToolbarItem label="Header row">
              <Checkbox defaultChecked>Header row</Checkbox>
            </ToolbarItem>
            <ToolbarItem label="Banded rows">
              <Checkbox>Banded rows</Checkbox>
            </ToolbarItem>
          </ToolbarGroup>
        </Toolbar>
      );
    return (
      <Toolbar aria-label="Home commands">
        <ToolbarGroup
          label="Clipboard"
          action={
            <Button variant="ghost" iconOnly aria-label="Clipboard options" onClick={() => run('clipboard options')}>
              {ICONS.launch}
            </Button>
          }
        >
          {command({ id: 'paste', label: 'Paste', glyph: ICONS.paste, tip: 'Add content from the clipboard.', priority: 3 }, sizes, run)}
          {command({ id: 'cut', label: 'Cut', glyph: ICONS.cut, tip: 'Remove the selection and put it on the clipboard.', priority: 1 }, sizes, run)}
          {command({ id: 'copy', label: 'Copy', glyph: ICONS.copy, tip: 'Put a copy of the selection on the clipboard.', priority: 1 }, sizes, run)}
        </ToolbarGroup>
        <ToolbarGroup label="Font">
          <ToolbarItem overflow="never">
            <Select aria-label="Font" data={['Inter', 'Georgia', 'Menlo']} defaultValue="Inter" />
          </ToolbarItem>
          <ToolbarItem overflow="never">
            <ToggleButtonGroup aria-label="Text style" selectionMode="multiple" name="style">
              <ToggleButton value="bold" iconOnly aria-label="Bold">
                {ICONS.bold}
              </ToggleButton>
              <ToggleButton value="italic" iconOnly aria-label="Italic">
                <em>I</em>
              </ToggleButton>
            </ToggleButtonGroup>
          </ToolbarItem>
          <ToolbarItem overflow="never">
            <ColorPicker aria-label="Font color" defaultValue="#c2410c" />
          </ToolbarItem>
        </ToolbarGroup>
        <ToolbarGroup label="Styles">
          <ToolbarItem overflow="never">
            {/* Gallery: a Listbox grid of tiles in a Popover. */}
            <Popover placement="bottomStart">
              <Popover.Trigger render={(props) => <Button {...props} variant="ghost" />}>Styles</Popover.Trigger>
              <Popover.Content aria-label="Style gallery">
                <Listbox aria-label="Styles" layout="grid" columns={3} data={GALLERY} onValueChange={(value) => run(`style:${String(value)}`)} styles={{ option: { blockSize: 48 } }} />
              </Popover.Content>
            </Popover>
          </ToolbarItem>
        </ToolbarGroup>
        <ToolbarGroup label="Editing">
          {command({ id: 'find', label: 'Find', glyph: ICONS.find }, sizes, run)}
          <ToolbarItem label="Replace">
            <SplitButton variant="ghost" items={[{ id: 'replace-all', label: 'Replace all' }]} onClick={() => run('replace')} onAction={(event) => run(event.id)}>
              Replace
            </SplitButton>
          </ToolbarItem>
        </ToolbarGroup>
      </Toolbar>
    );
  };

  const tabs = ['home', 'insert', ...(tableSelected ? ['table'] : [])];
  return (
    <div>
      <div className="demo-toolbar">
        {/* Inline like the checkbox, so the row's controls share one axis. */}
        <label>
          Width
          <Slider min={240} max={1100} value={width} onValueChange={setWidth} style={{ inlineSize: 240 }} />
        </label>
        <Checkbox checked={tableSelected} onCheckedChange={(checked) => setTableSelected(checked)}>
          A table is selected (contextual tab)
        </Checkbox>
      </div>
      <div ref={frame} style={{ inlineSize: width, maxInlineSize: '100%', border: '1px solid var(--salmon-border-subtle)', borderRadius: 'var(--salmon-radius-lg)' }}>
        <Tabs
          value={current}
          onValueChange={(next) => {
            setTab(next);
            if (collapsed) setPopup(true);
          }}
          panelMount="active"
        >
          {/* The tab row: the File menu before the TabList, layout switches after it (the help pane slot). */}
          <div style={{ gridArea: 'list', display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: 4, paddingInline: 4 }}>
            <MenuButton
              variant="ghost"
              style={{ flex: 'none' }}
              items={[
                { id: 'new', label: 'New' },
                { id: 'open', label: 'Open…' },
                { id: 'save', label: 'Save' },
              ]}
              onAction={(event) => run(`file: ${event.id}`)}
            >
              File
            </MenuButton>
            <TabList aria-label="Ribbon tabs" style={{ flex: '1 1 auto' }}>
              {tabs.map((value) => (
                <Tab key={value} value={value} onClick={() => collapsed && setPopup(true)}>
                  {value === 'home' ? 'Home' : value === 'insert' ? 'Insert' : 'Table Design'}
                </Tab>
              ))}
            </TabList>
            <Button variant="ghost" style={{ flex: 'none' }} aria-pressed={layout === 'simplified'} onClick={() => setLayout(layout === 'classic' ? 'simplified' : 'classic')}>
              Simplified
            </Button>
            <Button
              variant="ghost"
              style={{ flex: 'none' }}
              aria-pressed={collapsed}
              onClick={() => {
                setCollapsed(!collapsed);
                setPopup(false);
              }}
            >
              Collapse
            </Button>
          </div>
          {tabs.map((value) => (
            <TabPanel key={value} value={value} tabIndex={-1} style={collapsed ? { display: 'none' } : {}}>
              {collapsed ? null : panel(value)}
            </TabPanel>
          ))}
        </Tabs>
        {/* Minimized: the selected tab's commands open in a Popover under the tab row. */}
        <Popover open={collapsed && popup} onOpenChange={setPopup} anchor={frame} placement="bottomStart">
          <Popover.Content aria-label="Ribbon commands" style={{ inlineSize: width, maxInlineSize: '100vw' }}>
            {panel(current)}
          </Popover.Content>
        </Popover>
      </div>
      <p className="demo-status">Last command: {last}</p>
    </div>
  );
}
```
