# Tabs

Switches between related panels that share one place on the page, one panel at a time. Use tabs for peer views of the same object, not for steps of a flow.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-tabs`, `vig-tab`, `[vigTabLabel]`, `[vigTabContent]`
- Source: [libs/ui/src/lib/organisms/tabs/tabs.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/organisms/tabs/tabs.ts)

```typescript title="Import"
import { Tabs, Tab, TabLabel, TabContent } from '@vigdx/ui/tabs';
```

## Basic

Each `vig-tab` has a unique `value` and a `label`; its content is the panel. The first enabled tab is active by default. Name the tab list with `ariaLabel`.

```typescript title="tabs-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Tab, Tabs } from '@vigdx/ui/tabs';

@Component({
  selector: 'app-tabs-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Tab, Tabs],
  template: `
    <vig-tabs ariaLabel="Park settings">
      <vig-tab value="season" label="Season">
        <p class="p-4 text-sm">The park opens on 1 April and closes on 31 October.</p>
      </vig-tab>
      <vig-tab value="pricing" label="Pricing">
        <p class="p-4 text-sm">Day tickets cost €39; children under six enter free.</p>
      </vig-tab>
      <vig-tab value="hours" label="Opening hours">
        <p class="p-4 text-sm">Weekdays 10:00–18:00, weekends 09:00–20:00.</p>
      </vig-tab>
    </vig-tabs>
  `,
})
export class TabsBasicDemo {}
```

## Custom labels and disabled

An `ng-template vigTabLabel` inside a tab replaces its text label, e.g. with an icon and a count. A `disabled` tab cannot be selected and is skipped by the arrow keys.

```typescript title="tabs-custom-labels.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Badge } from '@vigdx/ui/badge';
import { Icon } from '@vigdx/ui/icon';
import { Tab, TabLabel, Tabs } from '@vigdx/ui/tabs';

@Component({
  selector: 'app-tabs-custom-labels',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Badge, Icon, Tab, TabLabel, Tabs],
  template: `
    <vig-tabs ariaLabel="Mailbox">
      <vig-tab value="inbox">
        <ng-template vigTabLabel>
          <vig-icon name="inbox" />
          Inbox
          <vig-badge label="12" variant="info" />
        </ng-template>
        <p class="p-4 text-sm">12 unread messages.</p>
      </vig-tab>
      <vig-tab value="files">
        <ng-template vigTabLabel>
          <vig-icon name="file" />
          Files
        </ng-template>
        <p class="p-4 text-sm">Attachments shared with you.</p>
      </vig-tab>
      <vig-tab value="archive" label="Archive" [disabled]="true">
        <p class="p-4 text-sm">Not available on your plan.</p>
      </vig-tab>
    </vig-tabs>
  `,
})
export class TabsCustomLabelsDemo {}
```

## Controlled

Two-way bind `[(value)]` to preselect a tab, read the active one or switch tabs from elsewhere on the page.

```typescript title="tabs-controlled.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';
import { Tab, Tabs } from '@vigdx/ui/tabs';

@Component({
  selector: 'app-tabs-controlled',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, Tab, Tabs],
  template: `
    <vig-tabs ariaLabel="Order" [(value)]="active">
      <vig-tab value="details" label="Details">
        <p class="p-4 text-sm">Order #1042 · 3 items · €128.40</p>
      </vig-tab>
      <vig-tab value="shipping" label="Shipping">
        <p class="p-4 text-sm">Express delivery to Berlin, arriving Thursday.</p>
      </vig-tab>
      <vig-tab value="history" label="History">
        <p class="p-4 text-sm">Placed, paid and packed.</p>
      </vig-tab>
    </vig-tabs>
    <div class="flex flex-wrap items-center gap-3">
      <vig-button
        label="Track shipment"
        variant="secondary"
        size="sm"
        (click)="active.set('shipping')"
      />
      <span class="text-sm text-muted-foreground">Active tab: {{ active() }}</span>
    </div>
  `,
})
export class TabsControlledDemo {
  protected readonly active = signal('shipping');
}
```

## Deferred content

Projected panel content is created with the page and stays in the DOM while hidden, so the browser's find-in-page can reach it. For a heavy panel, put its content in an `ng-template vigTabContent`: it is created on the tab's first activation and keeps its state when the user switches away, but find-in-page cannot see it until then.

```typescript title="tabs-lazy.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Tab, TabContent, Tabs } from '@vigdx/ui/tabs';

import { ReportPanel } from './report-panel';

@Component({
  selector: 'app-tabs-lazy',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Tab, TabContent, Tabs, ReportPanel],
  template: `
    <vig-tabs ariaLabel="Report">
      <vig-tab value="summary" label="Summary">
        <p class="p-4 text-sm">Revenue is up 12% on last quarter.</p>
      </vig-tab>
      <vig-tab value="charts" label="Charts">
        <ng-template vigTabContent>
          <div class="p-4 text-sm"><app-tabs-report-panel name="Charts" /></div>
        </ng-template>
      </vig-tab>
      <vig-tab value="raw" label="Raw data">
        <ng-template vigTabContent>
          <div class="p-4 text-sm"><app-tabs-report-panel name="Raw data" /></div>
        </ng-template>
      </vig-tab>
    </vig-tabs>
  `,
})
export class TabsLazyDemo {}
```

```typescript title="report-panel.ts"
import {
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  inject,
  input,
  signal,
} from '@angular/core';
import { Skeleton } from '@vigdx/ui/skeleton';

/** Stands in for a heavy panel that fetches its data when it is created. */
@Component({
  selector: 'app-tabs-report-panel',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Skeleton],
  template: `
    @if (loaded()) {
      <p>{{ name() }} loaded. Switch tabs and back: it keeps its state.</p>
    } @else {
      <vig-skeleton [lines]="2" [ariaLabel]="'Loading ' + name()" />
    }
  `,
})
export class ReportPanel {
  readonly name = input.required<string>();
  protected readonly loaded = signal(false);

  constructor() {
    const timer = setTimeout(() => this.loaded.set(true), 800);
    inject(DestroyRef).onDestroy(() => clearTimeout(timer));
  }
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="tabs"][data-part="list"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

### Tabs `[data-scope="tabs"]`

| Part | Element | data-state |
| --- | --- | --- |
| `list` | The `role="tablist"` row. |   |
| `tab` | A tab button (`aria-selected`). | `active, inactive` |

### Tab `[data-scope="tabs"]`

| Part | Element | data-state |
| --- | --- | --- |
| `panel` | The `role="tabpanel"`. | `active, inactive` |

## Accessibility

- Follows the WAI-ARIA APG tabs pattern: a `role="tablist"` of `role="tab"` buttons with `aria-selected`, and a `role="tabpanel"` labelled by its tab.
- The tab list is a single tab stop on the active tab. ArrowLeft and ArrowRight move and activate (automatic activation, wrapping, mirrored in right-to-left layouts); Home and End jump to the first and last enabled tab.
- When the active panel holds no focusable content it becomes a tab stop (`tabindex="0"`), so Tab moves from the tab list straight into it; a panel with its own controls leaves Tab to them.
- Inactive panels are hidden with `hidden="until-found"`: the browser's find-in-page and text-fragment links can reveal their text, which selects their tab. Where `until-found` is not supported they are simply hidden. Panels of disabled tabs and not-yet-created `vigTabContent` templates are not searchable.
- Give the tab list an accessible name with `ariaLabel` whenever the page holds more than one set of tabs.
- The selected tab is marked by its underline, not by color alone, and only that tab keeps an underline in forced-colors mode.
- Custom `vigTabLabel` templates render inside the tab button: keep them to text, icons and badges, never nested buttons or links.

## API

### Tabs

Selector: `vig-tabs` · Kind: component

Tabbed view following the APG tabs pattern: one roving tab stop on the tablist, arrow keys move and activate. Inactive panels stay in the DOM as `hidden="until-found"`, so the browser's find-in-page (and text-fragment links) can reveal them, which selects their tab; browsers without `until-found` treat them as plain `hidden`. Wrap heavy panel content in a `vigTabContent` template to create it on first activation instead.

**Example :**

```html
<vig-tabs [(value)]="active">
  <vig-tab value="general" label="General">…panel…</vig-tab>
  <vig-tab value="pricing">
    <ng-template vigTabLabel><i class="pi pi-dollar"></i> Pricing</ng-template>
    …panel…
  </vig-tab>
</vig-tabs>
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `ariaLabel` | `string \| undefined` | `undefined` | — |
| `value` (model) | `string \| undefined` | `undefined` | The active tab's value; two-way bindable. Defaults to the first tab. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `valueChange` | `string \| undefined` | The active tab's value; two-way bindable. Defaults to the first tab. |

#### Methods

| Signature | Description |
| --- | --- |
| `panelId(value: string): string` | — |
| `tabId(value: string): string` | — |

### Tab

Selector: `vig-tab` · Kind: component

One tab: `value` identifies it, `label` (or a vigTabLabel template) names it, and the projected content is the panel — hidden (but findable) while inactive. A `vigTabContent` template is created on first activation.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | `false` | — |
| `label` | `string` | `''` | — |
| `value` (required) | `string` | — | — |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigTabLabel]` | `TabLabel` | Marks an ng-template as a tab's custom header content (icon, badge…). |
| `ng-template[vigTabContent]` | `TabContent` | Marks an ng-template as a tab's deferred panel content: it is created on the tab's first activation and kept afterwards (state survives switching away). Until then find-in-page cannot reach it, so use it only for heavy panels; plain projected content stays searchable while hidden. **Example :** `<vig-tab value="charts" label="Charts"> <ng-template vigTabContent><app-charts /></ng-template> </vig-tab>` |
