SutraUI.Button (Sutra UI v0.3.0)
View SourceA versatile button component with multiple variants, sizes, and navigation support.
Buttons are the primary interactive element for triggering actions. They can render
as <button>, <a>, or Phoenix LiveView navigation links depending on the props.
Examples
# Basic button
<.button>Click me</.button>
# With variant and size
<.button variant="destructive" size="lg">Delete Account</.button>
# Navigation button
<.button navigate={~p"/dashboard"}>Go to Dashboard</.button>
# Icon button (requires aria-label)
<.button size="icon" aria-label="Settings">
<!-- Your icon here -->
</.button>
# Loading state
<.button loading phx-click="save">
<.spinner class="mr-2" /> Saving...
</.button>Variants
| Variant | Usage | Appearance |
|---|---|---|
primary | Main actions | Solid background |
secondary | Secondary actions | Muted background |
destructive | Delete, remove | Red/danger color |
outline | Less emphasis | Border only |
ghost | Minimal | Transparent until hover |
link | Inline actions | Underlined text |
Sizes
| Size | Usage | Height |
|---|---|---|
default | Most buttons | 40px |
sm | Compact UI | 32px |
lg | Hero sections | 48px |
icon | Icon-only | 40px square |
Navigation
The button automatically renders as a link when navigation props are provided:
navigate- Full page navigation (new LiveView)patch- Same LiveView navigationhref- Regular link or external URL
Accessibility
- Uses semantic
<button>element (or<a>for links) - Loading state sets
aria-busy="true" - Disabled state uses native
disabledattribute - Icon-only buttons must have
aria-label
Icon Buttons Need Labels
When using size="icon", always provide an aria-label for screen readers:
<.button size="icon" aria-label="Close dialog">
<!-- Your icon here -->
</.button>Related
SutraUI.Spinner- For loading states- Theming Guide - Customize button colors
Summary
Functions
Renders a button component.
Functions
Renders a button component.
Attributes
variant- Visual style. One ofprimary,secondary,destructive,outline,ghost,link. Defaults toprimary.size- Size variant. One ofdefault,sm,lg,icon. Defaults todefault.type- Button type. One ofbutton,submit,reset. Defaults tobutton.loading- Shows loading state and disables button. Defaults tofalse.disabled- Disables the button. Defaults tofalse.navigate- LiveView navigate path.patch- LiveView patch path.href- Regular link URL.class- Additional CSS classes.
Slots
inner_block- Required. The button content.
Attributes
variant(:string) - Visual style variant. Defaults to"primary". Must be one of"primary","secondary","destructive","outline","ghost", or"link".size(:string) - Size variant. Defaults to"default". Must be one of"default","sm","lg", or"icon".type(:string) - Button type attribute. Defaults to"button". Must be one of"button","submit", or"reset".loading(:boolean) - Loading state - disables button and sets aria-busy. Defaults tofalse.disabled(:boolean) - Disabled state. Defaults tofalse.navigate(:string) - LiveView navigate path. Defaults tonil.patch(:string) - LiveView patch path. Defaults tonil.href(:string) - Regular link URL. Defaults tonil.class(:any) - Additional CSS classes. Defaults tonil.- Global attributes are accepted. Additional HTML attributes. Supports all globals plus:
["id", "name", "value", "form", "aria-label", "aria-pressed", "aria-describedby", "aria-expanded", "aria-controls", "aria-haspopup", "phx-click", "phx-target", "phx-value-id", "phx-disable-with", "download"].
Slots
inner_block(required) - Button content.