# Timeline

Events in order along an axis, such as an order history or an activity log, each with a marker and your content.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-timeline`
- Source: [libs/ui/src/lib/molecules/timeline/timeline.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/timeline/timeline.ts)

```typescript title="Import"
import { Timeline, TimelineContent } from '@vigdx/ui/timeline';
```

## Basic

Pass `items` and an `ng-template vigTimelineContent`; bind the same items to `vigTimelineContentOf` so `let-event` is typed. The context also has `index`, `first`, `last` and `side`. Put dates in `<time datetime>` elements.

```typescript title="timeline-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Timeline, TimelineContent } from '@vigdx/ui/timeline';
import { ORDER_EVENTS } from './events';

@Component({
  selector: 'app-timeline-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Timeline, TimelineContent],
  template: `
    <vig-timeline [items]="events">
      <ng-template vigTimelineContent [vigTimelineContentOf]="events" let-event>
        <p class="font-semibold">{{ event.status }}</p>
        <p class="text-sm text-muted-foreground">
          <time [attr.datetime]="event.iso">{{ event.date }}</time> · {{ event.detail }}
        </p>
      </ng-template>
    </vig-timeline>
  `,
})
export class TimelineBasicDemo {
  protected readonly events = ORDER_EVENTS;
}
```

## Alignment and opposite content

`align` puts the content after the axis (`end`, default), before it (`start`) or on alternating sides (`alternate`); sides are logical, so RTL mirrors them. A `vigTimelineOpposite` template fills the other side, e.g. with the date. The timeline measures its own width: below 32rem a two-sided timeline collapses to one side, the opposite content above the content. Being a size container, it fills its parent's width (`w-full`) instead of growing with its content: give it a width inside a flex row or an inline-block.

```typescript title="timeline-align.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { type SelectOption } from '@vigdx/ui/select';
import { SelectButton } from '@vigdx/ui/select-button';
import {
  Timeline,
  TimelineContent,
  TimelineOpposite,
  type TimelineAlign,
} from '@vigdx/ui/timeline';
import { ORDER_EVENTS } from './events';

@Component({
  selector: 'app-timeline-align',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Timeline, TimelineContent, TimelineOpposite, SelectButton],
  template: `
    <vig-select-button label="Content side" [options]="aligns" [(value)]="align" />
    <vig-timeline [items]="events" [align]="align() ?? 'end'">
      <ng-template vigTimelineOpposite [vigTimelineOppositeOf]="events" let-event>
        <time class="text-sm" [attr.datetime]="event.iso">{{ event.date }}</time>
      </ng-template>
      <ng-template vigTimelineContent [vigTimelineContentOf]="events" let-event>
        <p class="font-semibold">{{ event.status }}</p>
        <p class="text-sm text-muted-foreground">{{ event.detail }}</p>
      </ng-template>
    </vig-timeline>
  `,
})
export class TimelineAlignDemo {
  protected readonly events = ORDER_EVENTS;
  protected readonly aligns: SelectOption<TimelineAlign>[] = [
    { value: 'end', label: 'End' },
    { value: 'start', label: 'Start' },
    { value: 'alternate', label: 'Alternate' },
  ];
  protected readonly align = signal<TimelineAlign | null>('alternate');
}
```

## Horizontal

`orientation="horizontal"` shares the width between the events; the sides line up across events (a CSS subgrid). Below 32rem it turns vertical, so it never scrolls sideways.

```typescript title="timeline-horizontal.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Timeline, TimelineContent, TimelineOpposite } from '@vigdx/ui/timeline';
import { ORDER_EVENTS } from './events';

@Component({
  selector: 'app-timeline-horizontal',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Timeline, TimelineContent, TimelineOpposite],
  template: `
    <vig-timeline [items]="events" orientation="horizontal">
      <ng-template vigTimelineOpposite [vigTimelineOppositeOf]="events" let-event>
        <time class="text-sm" [attr.datetime]="event.iso">{{ event.date }}</time>
      </ng-template>
      <ng-template vigTimelineContent [vigTimelineContentOf]="events" let-event>
        <p class="font-semibold">{{ event.status }}</p>
      </ng-template>
    </vig-timeline>
  `,
})
export class TimelineHorizontalDemo {
  protected readonly events = ORDER_EVENTS;
}
```

## Custom markers

A `vigTimelineMarker` template replaces the default dot, e.g. with an icon; it is centered on the axis, a track `--vig-timeline-marker-size` wide (1.5em; set it to your markers' width so wide ones do not crowd the content), and kept visible to screen readers, so give a marker that carries meaning a text alternative.

```typescript title="timeline-marker.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Card } from '@vigdx/ui/card';
import { Icon } from '@vigdx/ui/icon';
import { Timeline, TimelineContent, TimelineMarker } from '@vigdx/ui/timeline';
import { ORDER_EVENTS } from './events';

@Component({
  selector: 'app-timeline-marker',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Timeline, TimelineContent, TimelineMarker, Icon, Card],
  template: `
    <vig-timeline [items]="events" align="alternate">
      <ng-template vigTimelineMarker [vigTimelineMarkerOf]="events" let-event let-last="last">
        <span
          class="inline-flex size-[2em] items-center justify-center rounded-full border-2"
          [class]="
            last
              ? 'border-success bg-success text-success-foreground'
              : 'border-primary bg-background text-primary'
          "
        >
          <vig-icon [name]="event.icon" />
        </span>
      </ng-template>
      <ng-template vigTimelineContent [vigTimelineContentOf]="events" let-event>
        <vig-card [heading]="event.status" [headingLevel]="3">
          <p class="text-sm text-muted-foreground">
            <time [attr.datetime]="event.iso">{{ event.date }}</time>
          </p>
          <p class="text-sm">{{ event.detail }}</p>
        </vig-card>
      </ng-template>
    </vig-timeline>
  `,
})
export class TimelineMarkerDemo {
  protected readonly events = ORDER_EVENTS;
}
```

## 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="timeline"][data-part="root"] { … }`: 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).

Scope: `[data-scope="timeline"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The `<ol role="list">`. |   |
| `item` | One `<li>` event. |   |
| `opposite` | The `vigTimelineOpposite` content, across the axis. |   |
| `separator` | The axis cell: marker and connector. |   |
| `marker` | The default dot, or the wrapper of `vigTimelineMarker`. |   |
| `connector` | The line to the next event (a border). |   |
| `content` | The `vigTimelineContent` content. |   |

## Accessibility

- An ordered list: screen readers announce the number of events and each one's position. Opposite content comes first in each item, then the content, whatever side they are drawn on.
- The default dot and the connectors are decorative and hidden from screen readers. They are borders in theme tokens (`--color-primary`, `--color-border-strong`), which forced-colors mode paints in CanvasText.
- The timeline has no keyboard behaviour of its own; links and buttons in the content are reached with Tab.

## API

### Timeline

Selector: `vig-timeline` · Kind: component

Events in order along an axis, each with a marker and a content template, and optionally opposite content such as its date.

**Example :**

```html
<vig-timeline [items]="events">
  <ng-template vigTimelineContent [vigTimelineContentOf]="events" let-event>
    <h3>{{ event.title }}</h3>
  </ng-template>
  <ng-template vigTimelineOpposite [vigTimelineOppositeOf]="events" let-event>
    <time [attr.datetime]="event.iso">{{ event.date }}</time>
  </ng-template>
</vig-timeline>
```

An ordered list (`<ol>`): screen readers announce the count and each event's position. Markers and connectors are drawn with borders in theme tokens, which forced-colors mode repaints in system colors. Sides are logical, so the timeline mirrors under RTL. The timeline is a size container: below 32rem of its own width, two-sided layouts (`alternate`, or opposite content) collapse to one side with the opposite content above the content, and a horizontal timeline turns vertical. As a size container it does not size to its content: it fills the width of its parent (`w-full`), so give it a width in a shrink-to-fit parent (a flex row, an inline-block). The axis is `--vig-timeline-marker-size` wide (1.5em), so markers of any width stay centered on one line; set it to the width of custom markers.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `align` | `TimelineAlign` | `'end'` | Side of the axis the content takes: `end` (default), `start` or `alternate`. |
| `items` | `readonly T[]` | `[]` | The events, in order. |
| `orientation` | `TimelineOrientation` | `'vertical'` | `vertical` (default) or `horizontal`; a horizontal timeline narrower than 32rem turns vertical. |

### TimelineContent

Selector: `ng-template[vigTimelineContent]` · Kind: directive

Content of each event of `vig-timeline`. Bind the items to `vigTimelineContentOf` to type `let-event`:

**Example :**

```html
<ng-template vigTimelineContent [vigTimelineContentOf]="events" let-event>
  {{ event.title }}
</ng-template>
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `vigTimelineContentOf` | `readonly T[] \| null \| undefined` | `undefined` | Type-only: the items the template renders, so `let-event` is typed. Not read at runtime. |

### TimelineOpposite

Selector: `ng-template[vigTimelineOpposite]` · Kind: directive

Content on the other side of the axis, such as the date of each event. Bind the items to `vigTimelineOppositeOf` to type `let-event`.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `vigTimelineOppositeOf` | `readonly T[] \| null \| undefined` | `undefined` | Type-only: the items the template renders, so `let-event` is typed. Not read at runtime. |

### TimelineMarker

Selector: `ng-template[vigTimelineMarker]` · Kind: directive

Custom marker on the axis (an icon, an avatar) in place of the default dot. Bind the items to `vigTimelineMarkerOf` to type `let-event`.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `vigTimelineMarkerOf` | `readonly T[] \| null \| undefined` | `undefined` | Type-only: the items the template renders, so `let-event` is typed. Not read at runtime. |
