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/buttonnpx shadcn@latest add @gnostic/buttonpnpm dlx shadcn@latest add @gnostic/buttonCopy the files
src/components/ui/button/button-variants.tssrc/components/ui/button/button.astrosrc/components/ui/button/index.tssrc/components/ui/button/types.ts
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"
---
<div
data-slot="button"
role="button"
tabindex="-1"
aria-label="Open Menu"
class={buttonVariants({ variant: "ghost", size: "sm" })}
>
<Icon name="menu" />
</div>
Link
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.
Secondary
Subtle
Destructive
Destructive Outline
Link
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)
Put the icon in the button-loading-indicator slot. The example uses
hidden in-data-loading:block to show it while the button has data-loading.
Sizes and states
API Reference
Button slots
button-loading-indicator?Astro content
Supplies a custom loading indicator. Set data-slot="button-loading-indicator" on its root to apply the Button loading styles.
Astro contentSpinner when withLoader is trueButtonProps
as?HTMLTag
The HTML tag to render the button as.
HTMLTag'button'withLoader?boolean
Add the built-in loading indicator. It will remain hidden until the button's `data-loading` attribute is set to `true`.
booleanfalsevariant?"outline" | "default" | "glass" | "surface" | "destructive-outline" | "destructive" | "ghost" | "link" | "secondary" | "subtle" | null | undefined
"outline" | "default" | "glass" | "surface" | "destructive-outline" | "destructive" | "ghost" | "link" | "secondary" | "subtle" | null | undefinedsize?"icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "default" | "lg" | "sm" | "xl" | "xs" | null | undefined
"icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "default" | "lg" | "sm" | "xl" | "xs" | null | undefinedRead 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.
