Button
Displays a button, or an element that looks like one.
Installation
Shadwire follows shadcn/ui's open code model: the component's source is copied into your application and becomes yours. Install it with the CLI:
The button registry item installs these files:
-
app/components/ui_component.rb -
app/components/ui/button_component.rb -
app/helpers/ui/button_helper.rb -
vendor/shadwire/shadwire.css
Haven't installed the CLI yet? gem install shadwire, then shadwire init.
Usage
Use the ui_button helper
in any view. It takes the same arguments as the component.
Or render the component
Ui::ButtonComponent
directly:
Examples
Default
The default variant, used for a screen's primary action.
Secondary
A lower-emphasis alternative for secondary actions.
Destructive
For destructive actions, such as deleting a record.
Outline
A border and no fill, for neutral actions.
Ghost
No background and no border until hover.
Link
Looks like a text link while keeping button semantics.
Icon
A square icon-only button (size: :icon). Pass label: on the icon for screen readers.
With icon
Put a ui_icon inside the block and the button sizes the SVG for you.
Sizes
The :sm, :default and :lg sizes side by side.
Loading
A loading state: a spinning icon, with the button disabled.
Disabled
Pass disabled: true as an HTML attribute.
As a link
Render as an <a> with tag: :a, keeping the button's look.
API reference
ui_button(**options, &block)
wraps
Ui::ButtonComponent.new(**options).
| Argument | Type | Default | Description |
|---|---|---|---|
variant |
Symbol | :default |
:default, :secondary, :destructive, :outline, :ghost, :link |
size |
Symbol | :default |
:default, :sm, :lg, :icon |
tag |
Symbol | :button |
The HTML tag to render (e.g. :button, :a). |
disabled |
Boolean | false |
Renders data-disabled. With tag: :button it also renders disabled by default. |
focusable_when_disabled |
Boolean | false |
With disabled: true, keeps the native button focusable through aria-disabled="true" and tabindex="0" instead of disabled. |
class / class_name |
String | nil |
Extra classes, merged after the variant and size classes. |
**attrs |
Hash | {} |
Free HTML attributes (href, data:, aria:, …). |
&block |
Block | — | The button's content: text, a ui_icon, or both. |