---
title: Gradient Button for Astro
description: A button with an animated gradient border or fill.
sidebar:
    label: Gradient Button
seo:
    title: Gradient Button for Astro | Shadcn & Tailwind CSS
related:
    - /components/button
    - /components/glow-button
---

```astro
---
import PlusIcon from "@lucide/astro/icons/plus"
import { GradientButton } from "@/components/ui/gradient-button"
---

<div class="flex flex-wrap items-center justify-center gap-4">
  <GradientButton>Preview</GradientButton>
  <GradientButton variant="filled">Save changes</GradientButton>
  <GradientButton variant="filled" size="icon" aria-label="Add item">
    <PlusIcon aria-hidden="true" />
  </GradientButton>
</div>
```

## Installation

### Command

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

### Manual

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

No extra packages are required.

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

```astro
---
import type { HTMLTag, Polymorphic } from "astro/types"
import { cn } from "cn"

import { Button } from "@/components/ui/button"
import type { GradientButtonProps } from "./types.ts"

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

const {
  variant = "outline",
  colors,
  class: className,
  style: styleProp,
  ...props
} = Astro.props
const stops = colors
  ? {
      "--gradient-button-from": colors.from,
      "--gradient-button-via": colors.via,
      "--gradient-button-to": colors.to,
    }
  : {}
const style =
  typeof styleProp === "string"
    ? Object.entries(stops)
        .map(([name, value]) => `${name}:${value};`)
        .join("") + styleProp
    : { ...stops, ...styleProp }
---

<Button
  {...props}
  variant={null}
  data-slot="gradient-button"
  data-variant={variant}
  style={style}
  class={cn(
    "border-2 border-transparent",
    variant === "filled" ? "text-black" : "text-foreground",
    className,
  )}
>
  <slot />
  {Astro.slots.has("button-loading-indicator") && (
    <slot name="button-loading-indicator" slot="button-loading-indicator" />
  )}
</Button>

<style>
  @property --gradient-button-phase {
    syntax: "<angle>";
    initial-value: 0deg;
    inherits: false;
  }

  [data-slot="gradient-button"] {
    --gradient-button-surface: var(--background);
    --gradient-button-angle: calc(12deg + var(--gradient-button-phase));
    --gradient-button-stop: calc(75% - 25% * cos(var(--gradient-button-phase)));
    --gradient-button-middle: var(
      --gradient-button-via,
      var(--color-teal-500, #00bba7)
    );

    /* Clip the solid surface to the padding box to expose the gradient border. */
    background-image:
      linear-gradient(
        var(--gradient-button-surface),
        var(--gradient-button-surface)
      ),
      linear-gradient(
        var(--gradient-button-angle) in lch shorter hue,
        color-mix(
          in srgb,
          var(--gradient-button-from, var(--color-indigo-500, #615fff)) 40%,
          transparent
        ),
        color-mix(in srgb, var(--gradient-button-middle) 50%, transparent)
          var(--gradient-button-stop),
        color-mix(
          in srgb,
          var(--gradient-button-to, var(--color-indigo-500, #615fff)) 50%,
          transparent
        )
      ),
      radial-gradient(
        in lch shorter hue,
        var(--gradient-button-from, var(--color-blue-500, #2b7fff)),
        var(--gradient-button-middle),
        var(--gradient-button-to, var(--color-blue-600, #155dfc))
      );
    background-origin: border-box;
    background-clip: padding-box, border-box, border-box;
    background-size:
      100% 100%,
      calc(100% + var(--gradient-button-stop)) 160%,
      200% 200%;
    background-position:
      center,
      left center,
      var(--gradient-button-stop) center;
    background-repeat: no-repeat;
  }

  [data-slot="gradient-button"][data-variant="filled"] {
    --gradient-button-surface: transparent;
  }

  @media (prefers-reduced-motion: no-preference) {
    [data-slot="gradient-button"]:not(:disabled) {
      /* Resume the second animation on hover or focus to increase the speed. */
      animation:
        gradient-button-flow 10s linear infinite,
        gradient-button-flow 10s linear infinite;
      animation-composition: add;
      animation-play-state: running, paused;
    }

    [data-slot="gradient-button"]:not(:disabled):is(:hover, :focus-visible) {
      animation-play-state: running, running;
    }
  }

  @keyframes gradient-button-flow {
    from {
      --gradient-button-phase: 0deg;
    }
    to {
      --gradient-button-phase: 1turn;
    }
  }
</style>
```

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

```ts
export { default as GradientButton } from './gradient-button.astro'

export type { GradientButtonProps } from './types.ts'
```

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

```ts
import type { ButtonProps } from '@/components/ui/button/types.ts'

/** Props for a button with an animated gradient. */
export interface GradientButtonProps extends Omit<ButtonProps, 'variant'> {
    /** Apply the gradient to the border or the full button. @default 'outline' */
    variant?: 'outline' | 'filled'
    /** CSS colors for the start, middle, and end of the gradient. */
    colors?: { from: string; via: string; to: string }
}
```

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

The default `outline` variant draws the gradient around the border. Use
`filled` to extend it across the button. Both variants use the standard
Button sizes and accept a text label or an icon.

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

<GradientButton href="#settings">View settings</GradientButton>
<GradientButton variant="filled" type="submit">Save changes</GradientButton>
```

An `href` selects a link. A button defaults to `type="button"`. Give an
icon-only button an accessible name with `aria-label`.

## Custom colors

Set `from`, `via`, and `to` to CSS colors or theme variables. Use `class` to
choose a text color that is readable against a custom fill.

```astro
<GradientButton colors={{ from: "#6d28d9", via: "#db2777", to: "#ea580c" }}>
  Edit colors
</GradientButton>
```

The gradient moves faster on hover and keyboard focus. It stays still when
the button is disabled or the user requests reduced motion.

## GradientButton slots

| Prop | Type | Default | Description |
| - | - | - | - |
| `button-loading-indicator?` | `Astro content` | `Spinner when withLoader is true` | Supplies a custom loading indicator. Set data-slot="button-loading-indicator" on its root to apply the Button loading styles. |

## Props

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

See the [installable source](/ui/r/gradient-button.json).
