# Tabs

Switches between panels of content.

This is the HTML to build it with `dex.css`, for work outside the Dex codebase. Read https://brand.meetdex.ai/design.md first: it says how to set up a page and how to use the stylesheet. The page at https://brand.meetdex.ai/components/tabs shows the Dex app's own React component, which only works inside the Dex codebase.

## HTML

Three variants. Pick by where the tabs sit, not by how many there are.

- **Underline**, the default: a page's main sections, such as a candidate's tabs under their name. A row along a hairline the width of its container, with a 2px line under the active tab.

```html
<div class="dex-tabs" role="tablist" aria-label="Candidate">
  <button role="tab" id="tab-role" aria-selected="true" aria-controls="panel-role">Role fit</button>
  <button role="tab" id="tab-profile" aria-selected="false" aria-controls="panel-profile" tabindex="-1">Profile</button>
  <button role="tab" id="tab-cv" aria-selected="false" aria-controls="panel-cv" tabindex="-1">CV</button>
</div>
<div role="tabpanel" id="panel-role" aria-labelledby="tab-role" style="margin-top: 0.5rem">…</div>
<div role="tabpanel" id="panel-profile" aria-labelledby="tab-profile" hidden>…</div>
<div role="tabpanel" id="panel-cv" aria-labelledby="tab-cv" hidden>…</div>
```

- **Pill**: two or three tabs in a smaller space, such as a card header or a dialog. The tab list is a `dex-segmented`, the segmented control's muted track, with the active tab raised on it.

```html
<div class="dex-segmented" role="tablist" aria-label="CV">
  <button role="tab" aria-selected="true">Preview</button>
  <button role="tab" aria-selected="false" tabindex="-1">Edit</button>
</div>
```

- **Secondary**: a second row of tabs inside a page that already has one. `dex-tabs dex-tabs--secondary`: no track and no line; the active tab is a muted block.
- **Disabled tab**: `aria-disabled="true"` fades it to 50%, in all three variants. Leave it out of the arrow keys.
- **Icons**: a 16px Lucide `<svg aria-hidden="true">` before the word.

Switching takes a few lines of script: on a press, or an arrow key moving along the row, set `aria-selected` and `tabindex` on the tabs and `hidden` on the panels.

```html
<script>
  document.querySelectorAll('[role="tablist"]').forEach((list) => {
    const tabs = [...list.querySelectorAll('[role="tab"]:not([aria-disabled="true"])')]
    const choose = (tab) => {
      tabs.forEach((other) => {
        const isActive = other === tab
        other.setAttribute('aria-selected', String(isActive))
        other.tabIndex = isActive ? 0 : -1
        const panel = document.getElementById(other.getAttribute('aria-controls'))
        if (panel) panel.hidden = !isActive
      })
      tab.focus()
    }
    tabs.forEach((tab, index) => {
      tab.addEventListener('click', () => choose(tab))
      tab.addEventListener('keydown', (event) => {
        if (event.key === 'ArrowRight') choose(tabs[Math.min(index + 1, tabs.length - 1)])
        if (event.key === 'ArrowLeft') choose(tabs[Math.max(index - 1, 0)])
      })
    })
  })
</script>
```

## States

- The active tab's word is the text colour; the rest are muted and darken when pointed at. Secondary tabs also take a faint tint.
- Focus is the 75% edge inside the tab.
- In the app the underline and the pill slide to the new tab. In `dex.css` they move at once.

## Rules

- Use tabs when each option has its own panel of content. To switch a view or a setting with no panels, use a segmented control (https://brand.meetdex.ai/components/segmented-control.md).
- Let an underline row run the page's full width, and inset it by the page's gutter, so the first word lines up with the heading above.
- One variant per screen: tabs inside tabs take a different variant from the tabs around them.
- Tab names are one or two words in sentence case.
