# Button

Triggers an action, submits a form or — with an `href` — navigates while looking like a button. Seven variants, three appearances and five sizes, all drawn from theme tokens.

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

```typescript title="Import"
import { Button, ButtonIcon } from '@vigdx/ui/button';
```

## Variants

`variant` picks the hue from the shared set: `primary` (default), `secondary`, `ghost` and the four status colors.

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

@Component({
  selector: 'app-button-variants',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Primary" />
      <vig-button label="Secondary" variant="secondary" />
      <vig-button label="Ghost" variant="ghost" />
      <vig-button label="Info" variant="info" />
      <vig-button label="Success" variant="success" />
      <vig-button label="Warning" variant="warning" />
      <vig-button label="Error" variant="error" />
    </div>
  `,
})
export class ButtonVariantsDemo {}
```

## Appearances

`appearance` sets the emphasis: `solid` (default), `outlined` and `text`. Combine it with any variant.

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

@Component({
  selector: 'app-button-appearances',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Solid" />
      <vig-button label="Outlined" appearance="outlined" />
      <vig-button label="Text" appearance="text" />
    </div>
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Delete" variant="error" />
      <vig-button label="Delete" variant="error" appearance="outlined" />
      <vig-button label="Delete" variant="error" appearance="text" />
    </div>
  `,
})
export class ButtonAppearancesDemo {}
```

## Sizes

`size` runs from `xs` (24px) to `xl` (48px); `md` (34px) is the default control height.

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

@Component({
  selector: 'app-button-sizes',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Extra small" size="xs" />
      <vig-button label="Small" size="sm" />
      <vig-button label="Medium" size="md" />
      <vig-button label="Large" size="lg" />
      <vig-button label="Extra large" size="xl" />
    </div>
  `,
})
export class ButtonSizesDemo {}
```

## Icons

Project any 1em icon marked with `vigButtonIcon`. Without a `label` the button turns square — give it an `ariaLabel`; `rounded` makes it a circle.

```typescript title="button-icons.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { Icon } from '@vigdx/ui/icon';
import { Tooltip } from '@vigdx/ui/tooltip';

@Component({
  selector: 'app-button-icons',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Icon, Tooltip],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="New invoice">
        <vig-icon vigButtonIcon name="plus" />
      </vig-button>
      <vig-button label="Upload" variant="secondary">
        <vig-icon vigButtonIcon name="upload" />
      </vig-button>

      <!-- Icon-only: give it an accessible name, and a tooltip for sighted users. -->
      <vig-button variant="secondary" ariaLabel="Search" vigTooltip="Search">
        <vig-icon vigButtonIcon name="search" />
      </vig-button>
      <vig-button appearance="text" variant="secondary" ariaLabel="More actions" vigTooltip="More">
        <vig-icon vigButtonIcon name="more-horizontal" />
      </vig-button>
      <vig-button [rounded]="true" ariaLabel="Add" vigTooltip="Add">
        <vig-icon vigButtonIcon name="plus" />
      </vig-button>
    </div>
  `,
})
export class ButtonIconsDemo {}
```

## Loading and disabled

`loading` swaps the icon for a spinner, sets `aria-busy` and blocks clicks while keeping the variant colors and focus. `disabled` greys the button out.

```typescript title="button-loading.ts"
import { ChangeDetectionStrategy, Component, DestroyRef, inject, signal } from '@angular/core';
import { Button } from '@vigdx/ui/button';

@Component({
  selector: 'app-button-loading',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button
        [label]="saving() ? 'Saving…' : 'Save changes'"
        [loading]="saving()"
        (click)="save()"
      />
      <vig-button label="Disabled" [disabled]="true" />
      <vig-button label="Disabled" variant="secondary" [disabled]="true" />
    </div>
  `,
})
export class ButtonLoadingDemo {
  protected readonly saving = signal(false);
  private timer: ReturnType<typeof setTimeout> | undefined;

  constructor() {
    inject(DestroyRef).onDestroy(() => clearTimeout(this.timer));
  }

  protected save(): void {
    this.saving.set(true);
    clearTimeout(this.timer);
    this.timer = setTimeout(() => this.saving.set(false), 1500);
  }
}
```

## Links

With an `href` the button renders an `<a>`. `target="_blank"` adds `rel="noopener noreferrer"`; a disabled link drops its `href` and leaves the tab order.

```typescript title="button-links.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button, ButtonIcon } from '@vigdx/ui/button';
import { Icon } from '@vigdx/ui/icon';

@Component({
  selector: 'app-button-links',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button, ButtonIcon, Icon],
  template: `
    <div class="flex flex-wrap items-center gap-3">
      <vig-button label="Read the docs" href="/docs/getting-started" />
      <vig-button label="Angular" variant="secondary" href="https://angular.dev" target="_blank">
        <vig-icon vigButtonIcon name="external-link" />
      </vig-button>
      <vig-button label="Unavailable" appearance="outlined" href="/pricing" [disabled]="true" />
    </div>
  `,
})
export class ButtonLinksDemo {}
```

## Full width

`fullWidth` stretches the button across its container, e.g. in narrow forms.

```typescript title="button-full-width.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button } from '@vigdx/ui/button';

@Component({
  selector: 'app-button-full-width',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="mx-auto flex w-full max-w-sm flex-col gap-3">
      <vig-button label="Create account" size="lg" [fullWidth]="true" />
      <vig-button label="Sign in instead" variant="secondary" size="lg" [fullWidth]="true" />
    </div>
  `,
})
export class ButtonFullWidthDemo {}
```

## All variants

Every variant in every appearance, plus the disabled state.

```typescript title="button-all-variants.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Button, type ButtonAppearance, type ButtonVariant } from '@vigdx/ui/button';

@Component({
  selector: 'app-button-all-variants',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Button],
  template: `
    <div class="overflow-x-auto">
      <table class="w-full border-separate border-spacing-2 text-sm">
        <caption class="sr-only">
          Every variant in every appearance
        </caption>
        <thead>
          <tr>
            <th scope="col" class="text-start font-medium text-muted-foreground">Variant</th>
            @for (appearance of appearances; track appearance) {
              <th scope="col" class="text-start font-medium text-muted-foreground">
                {{ appearance }}
              </th>
            }
            <th scope="col" class="text-start font-medium text-muted-foreground">disabled</th>
          </tr>
        </thead>
        <tbody>
          @for (variant of variants; track variant) {
            <tr>
              <th scope="row" class="text-start font-mono text-xs font-normal">{{ variant }}</th>
              @for (appearance of appearances; track appearance) {
                <td>
                  <vig-button
                    [label]="variant"
                    [variant]="variant"
                    [appearance]="appearance"
                    size="sm"
                  />
                </td>
              }
              <td>
                <vig-button [label]="variant" [variant]="variant" size="sm" [disabled]="true" />
              </td>
            </tr>
          }
        </tbody>
      </table>
    </div>
  `,
})
export class ButtonAllVariantsDemo {
  protected readonly variants: ButtonVariant[] = [
    'primary',
    'secondary',
    'ghost',
    'info',
    'success',
    'warning',
    'error',
  ];
  protected readonly appearances: ButtonAppearance[] = ['solid', 'outlined', 'text'];
}
```

## 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="button"][data-part="control"] { … }`: 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="button"]`

| Part | Element | data-state |
| --- | --- | --- |
| `control` | The native `<button>`, or `<a>` with `href`. |   |
| `spinner` | The busy ring while `loading`. |   |
| `icon` | Wrapper of the `[vigButtonIcon]` slot. |   |

## Accessibility

- Renders a native `<button>` (`type="button"` by default, so it never submits a form by accident) or, with `href`, a native `<a>`. Both activate with Enter; buttons also with Space.
- Icon-only buttons have no visible text: always pass `ariaLabel`. A `vigTooltip` with the same text helps sighted mouse users.
- `loading` sets `aria-busy="true"` and blocks activation, but keeps the button focusable (`aria-disabled="true"` instead of `disabled`), so a focused submit button that starts loading does not drop focus to the page; a disabled link gets `aria-disabled="true"` and `tabindex="-1"`.
- Focus is always visible: a `--shadow-focus` halo on filled and outlined buttons, the offset `--shadow-focus-ring` on borderless ones.
- Every variant meets WCAG AA contrast against its fill in the light and dark palettes.

## API

### Button

Selector: `vig-button` · Kind: component

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `appearance` | `ButtonAppearance` | `'solid'` | — |
| `ariaLabel` | `string \| undefined` | `undefined` | Accessible name — required for icon-only buttons, overridable for i18n. |
| `autofocus` | `boolean` | `false` | Sets `autofocus` on the inner control, so a native `<dialog>` focuses it when it opens — e.g. the safe action of a destructive confirmation. |
| `disabled` | `boolean` | `false` | — |
| `fullWidth` | `boolean` | `false` | Stretches the button across its container (e.g. form submit rows). |
| `href` | `string \| undefined` | `undefined` | Renders a link (`<a>`) with the button's look instead of a `<button>`. `disabled` then drops the `href` and marks the link `aria-disabled`. |
| `label` | `string \| undefined` | `undefined` | — |
| `loading` | `boolean` | `false` | Shows a spinner in place of the icon and blocks activation. The button keeps focus while busy: it is marked `aria-disabled` rather than natively disabled. |
| `rel` | `string \| undefined` | `undefined` | Link `rel`; defaults to `noopener noreferrer` for `target="_blank"`. |
| `rounded` | `boolean` | `false` | Pill shape; combined with an icon-only button it renders a circle. |
| `size` | `ButtonSize` | `'md'` | — |
| `target` | `string \| undefined` | `undefined` | Link target, e.g. `_blank`; only applies with `href`. |
| `type` | `"button" \| "submit" \| "reset"` | `'button'` | — |
| `variant` | `ButtonVariant` | `'primary'` | — |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigButtonIcon]` | `ButtonIcon` | Marks projected content as the button's leading icon. Any icon source works (icon font `<i>`, inline `<svg>`) as long as it sizes at 1em. A button with an icon and no `label` renders square (icon-only) and must be given an `ariaLabel`. |
