Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Button for Astro

Astro button components with shadcn styles, sizes, loading states, and link examples. Preview each variant and install the editable source.

Installation

bunx --bun shadcn@latest add @gnostic/button
npx shadcn@latest add @gnostic/button
pnpm dlx shadcn@latest add @gnostic/button

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/button/button-variants.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
---
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
export { buttonVariants, type ButtonVariantProps } from "./button-variants"

export { default as Button } from "./button.astro"
src/components/ui/button/types.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
}

Usage

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.

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

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

Use 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.

---
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.

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

Examples

Sizes

Default

Ghost

Outline

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.

A coastal city at dusk, with lights along a curved road

Secondary

Destructive

Destructive Outline

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.

Icon only

Rounded

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

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.

Loading (Custom)

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

Sizes and states

API Reference

ButtonProps

PropType
as?HTMLTag

The HTML tag to render the button as.

TypeHTMLTag
Default'button'
withLoader?boolean

Add the built-in loading indicator. It will remain hidden until the button's `data-loading` attribute is set to `true`.

Typeboolean
Defaultfalse
variant?"outline" | "default" | "glass" | "surface" | "destructive-outline" | "destructive" | "ghost" | "link" | "secondary" | null | undefined
Type"outline" | "default" | "glass" | "surface" | "destructive-outline" | "destructive" | "ghost" | "link" | "secondary" | null | undefined
size?"icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "default" | "lg" | "sm" | "xl" | "xs" | null | undefined
Type"icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "default" | "lg" | "sm" | "xl" | "xs" | null | undefined

Read the installed source for the complete prop types.

When to use Button

Use a button for a page action, such as saving a form. Use 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.