# Button

Button with primary, secondary, ghost, and danger variants.

- Category: Form
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/button.json
- CLI: `npx markasui@latest add button`

## When to use

Primary or secondary actions in forms and pages.

- One primary button per area
- Use ghost for toolbar actions
- Use danger only for destructive or irreversible actions

## Code

### Blade

Save at `resources/views/components/markas/button.blade.php`. No extra dependencies.

```blade
@props([
    'variant' => 'primary',
    'size' => 'md',
])

@php
    $base = 'inline-flex items-center justify-center gap-2 font-medium whitespace-nowrap transition-colors duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none';
    $variants = [
        'primary' => 'bg-primary text-on-primary hover:bg-primary-strong',
        'secondary' => 'border border-line bg-surface text-ink hover:bg-subtle',
        'ghost' => 'text-ink hover:bg-subtle',
        'danger' => 'bg-danger text-on-primary hover:bg-danger/90',
    ];
    $sizes = [
        'sm' => 'h-8 rounded-md px-3 text-sm',
        'md' => 'h-10 rounded-lg px-4 text-sm',
        'lg' => 'h-12 rounded-lg px-6 text-base',
    ];
@endphp

<button {{ $attributes->merge(['type' => 'button'])->class([$base, $variants[$variant], $sizes[$size]]) }}>
    {{ $slot }}
</button>
```

```blade
<x-markas.button>Save</x-markas.button>
<x-markas.button variant="secondary" size="sm">Cancel</x-markas.button>
<x-markas.button variant="danger" type="submit">Delete account</x-markas.button>
```

### React

Save at `src/components/markas/Button.tsx`. No extra dependencies.

```tsx
import type { ComponentProps } from "react";

const base =
  "inline-flex items-center justify-center gap-2 font-medium whitespace-nowrap transition-colors duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none";
const variants = {
  primary: "bg-primary text-on-primary hover:bg-primary-strong",
  secondary: "border border-line bg-surface text-ink hover:bg-subtle",
  ghost: "text-ink hover:bg-subtle",
  danger: "bg-danger text-on-primary hover:bg-danger/90",
};
const sizes = {
  sm: "h-8 rounded-md px-3 text-sm",
  md: "h-10 rounded-lg px-4 text-sm",
  lg: "h-12 rounded-lg px-6 text-base",
};
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type ButtonProps = ComponentProps<"button"> & {
  variant?: keyof typeof variants;
  size?: keyof typeof sizes;
};

export function Button({
  variant = "primary",
  size = "md",
  type = "button",
  className,
  ...props
}: ButtonProps) {
  return (
    <button
      type={type}
      className={cx(base, variants[variant], sizes[size], className)}
      {...props}
    />
  );
}
```

```tsx
import { Button } from "@/components/markas/Button";

<Button>Save</Button>
<Button variant="secondary" size="sm">Cancel</Button>
<Button variant="danger" type="submit">Delete account</Button>
```

### Vue

Save at `src/components/markas/Button.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    variant?: "primary" | "secondary" | "ghost" | "danger";
    size?: "sm" | "md" | "lg";
  }>(),
  { variant: "primary", size: "md" },
);

const base =
  "inline-flex items-center justify-center gap-2 font-medium whitespace-nowrap transition-colors duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none";
const variants = {
  primary: "bg-primary text-on-primary hover:bg-primary-strong",
  secondary: "border border-line bg-surface text-ink hover:bg-subtle",
  ghost: "text-ink hover:bg-subtle",
  danger: "bg-danger text-on-primary hover:bg-danger/90",
};
const sizes = {
  sm: "h-8 rounded-md px-3 text-sm",
  md: "h-10 rounded-lg px-4 text-sm",
  lg: "h-12 rounded-lg px-6 text-base",
};
</script>

<template>
  <button
    type="button"
    :class="[base, variants[props.variant], sizes[props.size]]"
  >
    <slot />
  </button>
</template>
```

```vue
<script setup lang="ts">
import Button from "@/components/markas/Button.vue";
</script>

<template>
  <Button>Save</Button>
  <Button variant="secondary" size="sm">Cancel</Button>
  <Button variant="danger" type="submit">Delete account</Button>
</template>
```

### Svelte

Save at `src/lib/components/markas/Button.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLButtonAttributes } from "svelte/elements";

  const base =
    "inline-flex items-center justify-center gap-2 font-medium whitespace-nowrap transition-colors duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none";
  const variants = {
    primary: "bg-primary text-on-primary hover:bg-primary-strong",
    secondary: "border border-line bg-surface text-ink hover:bg-subtle",
    ghost: "text-ink hover:bg-subtle",
    danger: "bg-danger text-on-primary hover:bg-danger/90",
  };
  const sizes = {
    sm: "h-8 rounded-md px-3 text-sm",
    md: "h-10 rounded-lg px-4 text-sm",
    lg: "h-12 rounded-lg px-6 text-base",
  };

  type Props = HTMLButtonAttributes & {
    variant?: keyof typeof variants;
    size?: keyof typeof sizes;
    children?: Snippet;
  };

  let {
    variant = "primary",
    size = "md",
    type = "button",
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

<button
  {type}
  class={[base, variants[variant], sizes[size], className]}
  {...rest}
>
  {@render children?.()}
</button>
```

```svelte
<script lang="ts">
  import Button from "$lib/components/markas/Button.svelte";
</script>

<Button>Save</Button>
<Button variant="secondary" size="sm">Cancel</Button>
<Button variant="danger" type="submit">Delete account</Button>
```

### Astro

Save at `src/components/markas/Button.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const base =
  "inline-flex items-center justify-center gap-2 font-medium whitespace-nowrap transition-colors duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none";
const variants = {
  primary: "bg-primary text-on-primary hover:bg-primary-strong",
  secondary: "border border-line bg-surface text-ink hover:bg-subtle",
  ghost: "text-ink hover:bg-subtle",
  danger: "bg-danger text-on-primary hover:bg-danger/90",
};
const sizes = {
  sm: "h-8 rounded-md px-3 text-sm",
  md: "h-10 rounded-lg px-4 text-sm",
  lg: "h-12 rounded-lg px-6 text-base",
};

type Props = HTMLAttributes<"button"> & {
  variant?: keyof typeof variants;
  size?: keyof typeof sizes;
};

const {
  variant = "primary",
  size = "md",
  type = "button",
  class: className,
  ...rest
} = Astro.props;
---

<button
  type={type}
  class:list={[base, variants[variant], sizes[size], className]}
  {...rest}
>
  <slot />
</button>
```

```astro
---
import Button from "@/components/markas/Button.astro";
---

<Button>Save</Button>
<Button variant="secondary" size="sm">Cancel</Button>
<Button variant="danger" type="submit">Delete account</Button>
```

### Angular

Save at `src/app/markas/button.component.ts`. No extra dependencies.

```ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from "@angular/core";

const base =
  "inline-flex items-center justify-center gap-2 font-medium whitespace-nowrap transition-colors duration-150 ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none";
const variants = {
  primary: "bg-primary text-on-primary hover:bg-primary-strong",
  secondary: "border border-line bg-surface text-ink hover:bg-subtle",
  ghost: "text-ink hover:bg-subtle",
  danger: "bg-danger text-on-primary hover:bg-danger/90",
};
const sizes = {
  sm: "h-8 rounded-md px-3 text-sm",
  md: "h-10 rounded-lg px-4 text-sm",
  lg: "h-12 rounded-lg px-6 text-base",
};

export type ButtonVariant = keyof typeof variants;
export type ButtonSize = keyof typeof sizes;

/**
 * Attribute selector on the native button: disabled, aria-*, name, value, and the user's
 * class stay on the button itself, so nothing has to be forwarded.
 */
@Component({
  selector: "button[markas-button]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "[attr.type]": "type()", "[class]": "classes()" },
  template: `<ng-content />`,
})
export class Button {
  readonly variant = input<ButtonVariant>("primary");
  readonly size = input<ButtonSize>("md");
  readonly type = input<"button" | "submit" | "reset">("button");

  protected readonly classes = computed(
    () => `${base} ${variants[this.variant()]} ${sizes[this.size()]}`,
  );
}
```

```ts
import { Component } from "@angular/core";
import { Button } from "./markas/button.component";

@Component({
  selector: "app-actions",
  imports: [Button],
  template: `
    <button markas-button>Save</button>
    <button markas-button variant="secondary" size="sm">Cancel</button>
    <button markas-button variant="danger" type="submit">Delete account</button>
  `,
})
export class Actions {}
```

Angular uses an attribute selector on the native button, so `disabled`, `aria-*`, and `class` stay on the button.

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `'primary' \| 'secondary' \| 'ghost' \| 'danger'` | primary | Visual style of the button. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | Height and padding. |
| `type` | `'button' \| 'submit' \| 'reset'` | button | Native type attribute. Defaults to button so it never submits a form by accident. |

## Accessibility

- Uses the native button element, so Enter and Space work out of the box.
- Keyboard focus shows a 2px primary outline.
- An icon-only button needs an aria-label.

## Notes

- `type` defaults to `button`. Set `type="submit"` for a form submit button.
- Extra classes are appended last. There is no `tailwind-merge`, so conflicting classes (two background colors, for example) depend on Tailwind's CSS order. Prefer the `variant` and `size` props.
