Skip to content

Button

The Button component provides a versatile and accessible way to trigger actions throughout your application. It supports multiple variants, sizes, types, and animations while maintaining excellent accessibility standards. Built with semantic HTML and proper ARIA attributes, it adapts seamlessly to both light and dark modes.

Use the Button component when you need to:

  • Trigger actions (submit forms, navigate, open modals)
  • Provide primary and secondary call-to-action elements
  • Create interactive elements with proper keyboard support
  • Display loading or processing states
  • Group related actions with consistent styling
  • Add engaging hover animations to enhance user experience
  • Ensure accessibility compliance in interactive elements

Try hovering over the rocket button to see the animation effect!

Learn how to implement the Button component in your project, from basic usage to advanced configurations with animations.

---
import { Button } from 'accessible-astro-components'
---
<Button>Default Button</Button>
<Button type="primary">Primary Action</Button>
<Button type="secondary">Secondary Action</Button>
<!-- Filled buttons (default) -->
<Button type="primary">Filled Primary</Button>
<Button type="success">Filled Success</Button>
<!-- Outlined buttons -->
<Button variant="outlined" type="primary">Outlined Primary</Button>
<Button variant="outlined" type="error">Outlined Error</Button>
<Button size="sm">Small Button</Button>
<Button size="md">Medium Button</Button>
<Button size="lg">Large Button</Button>
<!-- Different semantic types -->
<Button type="info">Information</Button>
<Button type="success">Success</Button>
<Button type="warning">Warning</Button>
<Button type="error">Danger</Button>
<!-- Button states -->
<Button disabled>Disabled Button</Button>
<Button htmlType="submit">Submit Form</Button>
<Button htmlType="reset">Reset Form</Button>
---
import { Button } from 'accessible-astro-components'
import { Icon } from 'astro-icon/components'
---
<!-- Icon with text -->
<Button type="primary">
<Icon name="ion:save" size="1rem" />
Save Changes
</Button>
<!-- Icon only (provide aria-label) -->
<Button
type="info"
ariaLabel="Search"
isCircular
>
<Icon name="ion:search" size="1rem" />
</Button>

Available animations are ‘rotate’, ‘boop’, ‘bouncing’, ‘nudge’.

<!-- Hover animations -->
<Button
type="primary"
animateOnHover
animationType="boop"
>
<Icon name="ion:heart" size="1rem" />
Like
</Button>
<Button
type="info"
animateOnHover
animationType="rotate"
animationIntensity={8}
>
<Icon name="ion:refresh" size="1rem" />
Refresh
</Button>
<!-- Pulsing button to draw attention -->
<Button
type="success"
pulse
>
New Feature!
</Button>
<form>
<Button htmlType="submit" type="primary">
Submit Form
</Button>
<Button htmlType="reset" variant="outlined">
Reset
</Button>
<Button htmlType="button" onclick="handleCancel()">
Cancel
</Button>
</form>

Configure the Button component using these available props to customize its behavior and appearance.

PropTypeDefaultDescription
htmlType'button' | 'submit' | 'reset''button'The HTML button type attribute
variant'filled' | 'outlined''filled'The visual variant of the button
type'default' | 'primary' | 'secondary' | 'info' | 'success' | 'warning' | 'error''default'The type affecting color scheme
size'sm' | 'md' | 'lg''md'The size of the button
disabledbooleanfalseWhether the button is disabled
animateOnHoverbooleanfalseWhether to animate icons on hover
animationType'rotate' | 'boop' | 'bouncing' | 'nudge' | 'none''boop'Type of animation to apply on hover
animationIntensitynumber5Animation intensity (1-10)
pulsebooleanfalseWhether to apply a pulsating animation
ariaLabelstringundefinedAccessible label for screen readers
ariaDescribedbystringundefinedID of element describing the button
idstringundefinedThe id attribute
classstringundefinedAdditional CSS classes to apply

Accessibility is built into the core of this component through semantic HTML elements and proper ARIA attributes. The Button component ensures excellent accessibility:

  • Uses semantic <button> element with proper type attributes
  • Supports all standard button behaviors (click, enter, space)
  • Maintains proper focus states with clear visual indicators
  • High contrast color combinations for all variants
  • Automatic light/dark mode adaptation
  • Respects user’s reduced-motion preferences for animations
  • Proper disabled state handling
  • Support for ARIA attributes (aria-label, aria-describedby)
  • Text underline animations provide additional interaction feedback

Make the Button component your own with custom styling while maintaining its accessibility features.

/* Option 1: Using :global() in your style block */
<style>
:global(.button) {
font-weight: 600;
letter-spacing: 0.025em;
border-radius: 0.75rem;
}
:global(.button.type-primary) {
border-color: light-dark(hsl(220 100% 50%), hsl(220 100% 60%));
background-color: light-dark(hsl(220 100% 50%), hsl(220 100% 60%));
color: white;
}
:global(.button:hover) {
transform: translateY(-2px);
box-shadow: 0 4px 12px hsl(0 0% 0% / 0.15);
}
:global(.button:focus-visible) {
outline: 2px solid currentColor;
outline-offset: 2px;
}
/* Custom animation keyframes */
@media (prefers-reduced-motion: no-preference) {
:global(.button.animation-pulse) {
animation: custom-pulse 2s infinite;
}
}
@keyframes custom-pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
</style>
/* Option 2: Using is:global on the style tag */
<style is:global>
.button {
font-weight: 600;
letter-spacing: 0.025em;
border-radius: 0.75rem;
}
.button.type-primary {
border-color: light-dark(hsl(220 100% 50%), hsl(220 100% 60%));
background-color: light-dark(hsl(220 100% 50%), hsl(220 100% 60%));
color: white;
}
</style>

See the Button component in action with these practical examples demonstrating different variants, animations, and styling options.

Filled

Outlined

Success

Warning

Error

Hover over the buttons to see different animation effects. The pulse button automatically animates.

Form buttons with proper HTML types for semantic form behavior.

Icon-only buttons with proper accessibility labels. Hover to see animations.

  • Link: For navigation and external links with button styling
  • Badge: For compact interactive elements and status indicators
  • Modal: Often used together for confirm/cancel actions