---
title: Button for Astro
description: "Astro button components with shadcn styles, sizes, loading states, and link examples. Preview each variant and install the editable source."
seo:
    title: "Buttons for Astro with Shadcn UI & Tailwind CSS"
related:
    - /components/link
    - /components/button-group
    - /components/glow-button
    - /components/cta-button
sidebar:
    label: "Button"
---

```astro
---
import ArrowUpRight from "@lucide/astro/icons/arrow-up-right"

import { Button } from "@/components/ui/button"
---

<Button>Button</Button>
<Button size="icon">
  <ArrowUpRight />
</Button>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/button
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/button/button-variants.ts

```ts
import { cva, type VariantProps } from 'class-variance-authority'

export const buttonVariants = cva(
    "font-sans focus-visible:ring-ring focus-visible:ring-offset-background relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 rounded-lg border text-base font-medium whitespace-nowrap transition-shadow outline-none before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] focus-visible:ring-2 focus-visible:ring-offset-1 disabled:opacity-64 sm:text-sm pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 [&_svg]:pointer-events-none [&_svg]:-mx-0.5 [&_svg]:shrink-0 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 group/button disabled:pointer-events-none data-loading:text-transparent data-loading:select-none",
    {
        compoundVariants: [
            {
                variant: 'outline',
                size: ['icon', 'icon-xs', 'icon-sm', 'icon-lg', 'icon-xl'],
                class: "",
            },
        ],
        defaultVariants: {
            size: 'default',
            variant: 'default',
        },
        variants: {
            size: {
                default: "h-9 px-[calc(--spacing(3)-1px)] sm:h-8",
                icon: "size-9 sm:size-8",
                'icon-lg': "size-10 sm:size-9",
                'icon-sm': "size-8 sm:size-7",
                'icon-xl': "size-11 sm:size-10 [&_svg:not([class*='size-'])]:size-5 sm:[&_svg:not([class*='size-'])]:size-4.5",
                'icon-xs': "size-7 rounded-md before:rounded-[calc(var(--radius-md)-1px)] sm:size-6 not-in-data-[slot=input-group]:[&_svg:not([class*='size-'])]:size-4 sm:not-in-data-[slot=input-group]:[&_svg:not([class*='size-'])]:size-3.5",
                lg: "h-10 px-[calc(--spacing(3.5)-1px)] sm:h-9",
                sm: "h-8 gap-1.5 px-[calc(--spacing(2.5)-1px)] sm:h-7",
                xl: "h-11 px-[calc(--spacing(4)-1px)] text-lg sm:h-10 sm:text-base [&_svg:not([class*='size-'])]:size-5 sm:[&_svg:not([class*='size-'])]:size-4.5",
                xs: "h-7 gap-1 rounded-md px-[calc(--spacing(2)-1px)] text-sm before:rounded-[calc(var(--radius-md)-1px)] sm:h-6 sm:text-xs [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5",
            },
            variant: {
                glass: "border-white/24 border bg-white/14 text-white hover:bg-white/18 before:bg-white/0 relative backdrop-blur-sm before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:transition-[background-color] before:duration-300 before:ease-in-out hover:before:bg-white/8",
                surface: "border-border bg-card text-card-foreground hover:bg-accent",
                'destructive-outline':
                    "border-input bg-popover text-destructive-foreground hover:border-destructive/32 hover:bg-destructive/4 data-pressed:border-destructive/32 data-pressed:bg-destructive/4 *:data-[slot=button-loading-indicator]:text-foreground dark:bg-input/32 shadow-xs/5 not-dark:bg-clip-padding not-disabled:not-active:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:not-disabled:before:shadow-[0_-1px_--theme(--color-white/2%)] dark:not-disabled:not-active:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [:disabled,:active,[data-pressed]]:shadow-none",
                default: "border-primary bg-primary text-primary-foreground shadow-primary/24 hover:bg-primary/90 data-pressed:bg-primary/90 *:data-[slot=button-loading-indicator]:text-primary-foreground shadow-xs not-disabled:inset-shadow-[0_1px_--theme(--color-white/16%)] [:active,[data-pressed]]:inset-shadow-[0_1px_--theme(--color-black/8%)] [:disabled,:active,[data-pressed]]:shadow-none",
                destructive: "border-destructive bg-destructive shadow-destructive/24 hover:bg-destructive/90 data-pressed:bg-destructive/90 text-white shadow-xs not-disabled:inset-shadow-[0_1px_--theme(--color-white/16%)] *:data-[slot=button-loading-indicator]:text-white [:active,[data-pressed]]:inset-shadow-[0_1px_--theme(--color-black/8%)] [:disabled,:active,[data-pressed]]:shadow-none",
                ghost: "text-foreground hover:bg-accent data-pressed:bg-accent *:data-[slot=button-loading-indicator]:text-foreground border-transparent",
                link: "text-foreground *:data-[slot=button-loading-indicator]:text-foreground border-transparent underline-offset-4 hover:underline data-pressed:underline",
                outline: "border-input bg-popover text-foreground hover:bg-accent/50 data-pressed:bg-accent/50 *:data-[slot=button-loading-indicator]:text-foreground dark:bg-input/32 dark:data-pressed:bg-input/64 dark:hover:bg-input/64 shadow-xs/5 not-dark:bg-clip-padding not-disabled:not-active:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:not-disabled:before:shadow-[0_-1px_--theme(--color-white/2%)] dark:not-disabled:not-active:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [:disabled,:active,[data-pressed]]:shadow-none",
                secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/90 data-pressed:bg-secondary/90 *:data-[slot=button-loading-indicator]:text-secondary-foreground [:active,[data-pressed]]:bg-secondary/80 border-transparent",
            },
        },
    },
)

export type ButtonVariantProps = VariantProps<typeof buttonVariants>
```

#### src/components/ui/button/button.astro

```astro
---
import type { HTMLTag, Polymorphic } from "astro/types"
import { Spinner } from "../spinner"

import { cn } from "cn"

import { buttonVariants } from "./button-variants"
import type { ButtonProps } from "./types"

type Props<Tag extends HTMLTag = "button"> = Polymorphic<{ as: Tag }> &
  ButtonProps

const {
  as: Tag = "href" in Astro.props ? "a" : "button",
  class: className,
  variant = "default",
  size = "default",
  withLoader = false,
  ...props
} = Astro.props

// Props validates the selected tag at the call site. The render tag is dynamic.
const elementProps: Record<string, unknown> = {
  ...(Tag === "button" ? { type: "button" as const } : {}),
  ...props,
}
---

<Tag
  data-slot="button"
  data-variant={variant}
  data-size={size}
  class={cn(buttonVariants({ variant, size }), className)}
  {...elementProps}
>
  <slot />
  <slot name="button-loading-indicator">
    {withLoader && (
      <Spinner
        data-slot="button-loading-indicator"
        class="absolute not-in-data-loading:hidden"
      />
    )}
  </slot>
</Tag>
```

#### src/components/ui/button/index.ts

```ts
export { buttonVariants, type ButtonVariantProps } from "./button-variants"

export { default as Button } from "./button.astro"
```

#### src/components/ui/button/types.ts

```ts
import type { HTMLTag } from 'astro/types'

import type { ButtonVariantProps } from './button-variants'

export interface ButtonProps extends ButtonVariantProps {
    /**
     * The HTML tag to render the button as.
     *
     * @default 'button'
     */
    as?: HTMLTag
    /**
     * Add the built-in loading indicator. It will remain hidden until the button's `data-loading` attribute is set to `true`.
     *
     * @default false
     */
    withLoader?: boolean
}
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import { Button } from "@/components/ui/button"
```

You can also import `buttonVariants` to reuse button styles on other HTML elements.
Pass `size` and `variant` to the helper. It supplies the classes for the
project preset selected during installation. No extra data attributes are
needed to select those styles.
Keep a trigger's existing `data-slot` when you style another component this way.

```astro
---
import { buttonVariants } from "@/components/ui/button"
---

<button
    data-slot="button"
    aria-label="Button"
    type="button"
    class={buttonVariants({ variant: "default", size: "sm" })}
>
    Button
</button>
```

### Link

Use [Link](/components/link) for a page path, an external URL, a phone number, or an email address. `Button` can render an anchor when you only need button styles on a simple href.

```astro
---
import { Button } from "@/components/ui/button"
---

<Button as="a" href="/login">Login</Button>
```

### Polymorphic

`Button` is polymorphic via `as`. Pass `as` to change the rendered element and
use the relevant attributes for that element.

```astro
<Button as="button" type="button">Save</Button>
<Button as="a" href="/docs">Docs</Button>
<Button as="label" for="file-upload">Upload</Button>
```

## Examples

### Sizes

```astro
---
import ArrowUpRight from "@lucide/astro/icons/arrow-up-right"

import { Button } from "@/components/ui/button"
---

<Button variant="outline" size="sm">
  Small
</Button>
<Button variant="outline" size="icon-sm">
  <ArrowUpRight />
</Button>
<Button variant="outline">Save task</Button>
<Button variant="outline" size="icon">
  <ArrowUpRight />
</Button>
<Button variant="outline" size="lg">
  Large
</Button>
<Button variant="outline" size="icon-lg">
  <ArrowUpRight />
</Button>
```

### Default

```astro
---
import { Button } from "@/components/ui/button"
---

<Button>Button</Button>
```

### Ghost

```astro
---
import { Button } from "@/components/ui/button"
---

<Button variant="ghost">Add a note</Button>
```

### Outline

```astro
---
import { Button } from "@/components/ui/button"
---

<Button variant="outline">Open project</Button>
```

### Glass

The glass variant uses a translucent fill and a backdrop blur. Put the button on a photo so the image shows through the control.

```astro
---
import { Image } from "astro:assets"

import { Button } from "@/components/ui/button"
import city from "@/assets/images/futuristic-city.png"
---

<!--
  Keep the photo behind the control. The glass fill and blur need a detailed image.
-->
<figure class="relative w-full overflow-hidden rounded-xl">
  <Image
    src={city}
    alt="A coastal city at dusk, with lights along a curved road"
    widths={[640, 960, 1280]}
    sizes="(min-width: 40rem) 48rem, 100vw"
    class="aspect-video w-full object-cover object-[center_70%]"
  />
  <div class="absolute inset-0 grid place-items-center p-6">
    <Button variant="glass" size="lg">
      Explore the city
    </Button>
  </div>
</figure>
```

### Secondary

```astro
---
import { Button } from "@/components/ui/button"
---

<Button variant="secondary">Review notes</Button>
```

### Destructive

```astro
---
import { Button } from "@/components/ui/button"
---

<Button variant="destructive">Remove task</Button>
```

### Destructive Outline

```astro
---
import { Button } from "@/components/ui/button"
import Trash from "@lucide/astro/icons/trash"
---

<Button variant="destructive-outline">Delete</Button>
<Button variant="destructive-outline" size="icon">
  <Trash />
</Button>
```

### Link

```astro
---
import { Button } from "@/components/ui/button"
---

<Button variant="link">Read the brief</Button>
```

### With Icon

The spacing between the icon and the text is automatically adjusted based on the size of the button. You do not need any margin on the icon.

```astro
---
import GitBranch from "@lucide/astro/icons/git-branch"

import { Button } from "@/components/ui/button"
---

<Button variant="outline" size="sm">
  <GitBranch />
  New branch
</Button>
```

### Icon only

```astro
---
import Flame from "@lucide/astro/icons/flame"

import { Button } from "@/components/ui/button"
---

<Button variant="outline" size="icon" aria-label="Submit">
  <Flame />
</Button>
```

### Rounded

Use the `rounded-full` class to make the button rounded.

```astro
---
import ArrowUp from "@lucide/astro/icons/arrow-up"

import { Button } from "@/components/ui/button"
---

<Button class="rounded-full">
  Submit
  <ArrowUp />
</Button>
<Button class="rounded-full" size="icon">
  <ArrowUp />
</Button>
```

### Loading (Built-in)

Use the `withLoader` prop to show a built-in loading indicator. Then just set the `data-loading` attribute to show a loading indicator.

```astro
---
import { Button } from "@/components/ui/button"
---

<Button size="sm" variant="outline" withLoader data-loading-demo="button">
  Submit
</Button>
<script>
  document.addEventListener("click", (event) => {
    if (!(event.target instanceof Element)) return
    const button = event.target.closest<HTMLButtonElement>(
      '[data-loading-demo="button"]',
    )
    if (!button) return
    button.setAttribute("data-loading", "")
    setTimeout(() => {
      button.removeAttribute("data-loading")
    }, 1000)
  })
</script>
```

### Loading (Custom)

Pass a custom loading indicator by adding the `button-loading-indicator` slot.

:::note
To show a loading indicator without replacing the content, don't set a slot name.
:::

```astro
---
import { Button } from "@/components/ui/button"
import LoaderIcon from "@lucide/astro/icons/loader"
---

<Button size="sm" variant="outline" data-loading-demo="custom">
  Submit
  <LoaderIcon
    data-slot="button-loading-indicator"
    class="absolute animate-spin"
  />
</Button>
<script>
  document.addEventListener("click", (event) => {
    if (!(event.target instanceof Element)) return
    const button = event.target.closest<HTMLButtonElement>(
      '[data-loading-demo="custom"]',
    )
    if (!button) return
    button.setAttribute("data-loading", "")
    setTimeout(() => {
      button.removeAttribute("data-loading")
    }, 1000)
  })
</script>
```

## Sizes and states

```astro
---
import { Button, type ButtonVariantProps } from "@/components/ui/button"
const variants: ButtonVariantProps["variant"][] = [
  "default",
  "secondary",
  "outline",
  "ghost",
  "destructive",
  "destructive-outline",
  "link",
  "glass",
  "surface",
]
---

<div class="grid gap-4">
  {variants.map((variant) => (
    <div class="flex flex-wrap items-center gap-3">
      <Button variant={variant}>{variant}</Button>
      <Button variant={variant} disabled>
        {variant} disabled
      </Button>
    </div>
  ))}
</div>
```

## API Reference

### ButtonProps

<AutoTypeTable path="../../packages/ui/src/components/ui/button/types.ts" name="ButtonProps" />

Read the [installed source](/r/button.json) for the complete prop types.

## When to use Button

Use a button for a page action, such as saving a form. Use [Link](/components/link) for a destination. The default slot holds the label and optional icon. Set `type="button"` for actions inside a form that must not submit it.

## Keyboard and behavior

A native button supports Enter and Space. A link supports Enter and normal browser link actions. Give an icon-only button an `aria-label`. The `withLoader` option and `data-loading` attribute change the display; your code must start the work, prevent repeat requests when needed, and report the result.

## Theme and layout

The variants use the shared primary, secondary, and destructive tokens. Keep the default variant for the main action and use outline or ghost for nearby controls. A class override can change one button without changing the site palette. The component does not require a client controller for normal form submission or navigation.
