Button
Buttons allow users to take actions, and make choices, with a single tap.
Example
Variants
Use the variant prop to change the visual style of the button.
<Button variant="primary">Primary</Button>
<Button variant="secondary">Secondary</Button>
<Button variant="outline">Outline</Button>
<Button variant="ghost">Ghost</Button>
<Button variant="danger">Danger</Button> Icons
You can easily pass icons into the button via slot, and even use it as an icon-only button by using the iconOnly property.
<Button variant="primary">
<Icon name="save" />
Save
</Button>
<Button variant="outline">
Delete
<Icon name="trash-2" />
</Button>
<Button variant="secondary" size="icon" aria-label="Settings">
<Icon name="settings" />
</Button>
<Button variant="outline" size="icon" shape="circle" aria-label="Notifications" hasBadge>
<Icon name="bell" />
<Badge intent="danger" shape="notification" variant="solid" class="font-bold">+99</Badge>
</Button> Sizes
Buttons come in different sizes to fit your needs.
<Button size="sm">Small</Button>
<Button size="md">Medium</Button>
<Button size="lg">Large</Button> Disabled State
Use the disabled attribute to make a button inactive.
<Button disabled>Disabled Primary</Button>
<Button variant="outline" disabled>Disabled Outline</Button> Ghost Button
Ghost button will make button background transparent, it is commonly used in colored background.
<Button variant="ghost">Ghost Button</Button> Danger Button
You can use the danger variant to mark button as danger status.
<Button variant="danger">Danger Button</Button>
<Button variant="outline" class="border-red-600 text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30">Danger Outline</Button>
<Button variant="ghost" class="text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30">Danger Ghost</Button> Block Button
The w-full class will make the button fit to its parent width.
<Button class="w-full">Block Button</Button> Shapes
Buttons come in different shapes: rounded, pill, square, and circle.
<Button shape="rounded">Rounded</Button>
<Button shape="pill">Pill</Button>
<Button shape="square">Square</Button>
<Button shape="circle" iconOnly aria-label="Circle">
<Icon name="search" />
</Button> Loading
A loading indicator can be added to a button by including a loader component.
<Button disabled>
<Loader variant="spinner" size="sm" class="text-current" />
Loading
</Button>
<Button variant="outline" disabled>
<Loader variant="spinner" size="sm" class="text-current" />
Loading
</Button> Button Group
Use the ButtonGroup component to group multiple buttons together.
<ButtonGroup>
<Button variant="outline">Daily</Button>
<Button variant="outline">Weekly</Button>
<Button variant="outline">Monthly</Button>
</ButtonGroup> Orientation and Attachment
Vertical Attached
Horizontal Detached
<ButtonGroup orientation="vertical">
<Button variant="outline">Option A</Button>
<Button variant="outline">Option B</Button>
<Button variant="outline">Option C</Button>
</ButtonGroup>
<ButtonGroup attached={false}>
<Button variant="primary">Day</Button>
<Button variant="outline">Week</Button>
<Button variant="outline">Month</Button>
</ButtonGroup> Actions and Interactivity
Buttons support standard onclick events as well as declarative server actions via fetch attributes.
<!-- Standard client-side event -->
<Button onclick="alert('Button clicked!')">Click me</Button>
<!-- Declarative fetch system (Server action) -->
<Button
action="/api/submit"
method="post"
target="#result-container"
swap="inner"
>
Save Changes
</Button>
<!-- The above translates to these HTML attributes internally: -->
<!-- data-action="/api/submit" data-method="post" data-target="#result-container" data-swap="inner" --> How to use
Import the button component and use it in your Astro files.
---
import Button from '@/components/general/Button.astro';
---
<Button
variant="primary"
size="md"
type="submit"
onclick="alert('Hello!')"
>
Submit Form
</Button> API Reference
| Property | Description | Type | Default |
|---|---|---|---|
| variant | The visual style variant of the button. | 'primary' | 'secondary' | 'outline' | 'ghost' | 'danger' | 'primary' |
| iconOnly | Whether the button should only contain an icon. | boolean | false |
| size | The size of the button. | 'sm' | 'md' | 'lg' | 'md' |
| shape | The border radius shape of the button. | 'rounded' | 'pill' | 'square' | 'circle' | 'rounded' |
| action | URL for declarative fetch action. | string | - |
| method | HTTP method for the declarative action. | 'get' | 'post' | 'put' | 'delete' | 'get' |
| target | CSS selector for the target element to update. | string | - |
| swap | How to swap the content in the target element. | 'inner' | 'outer' | 'prepend' | 'append' | 'inner' |
| class | Additional CSS classes to apply. | string | - |