Card
Displays a card with header, content, and footer.
Usage
Card with image
You might like "RubyUI"
@joeldrapper

Card(class: 'w-96') do
CardHeader do
CardTitle { 'You might like "RubyUI"' }
CardDescription { "@joeldrapper" }
end
CardContent do
AspectRatio(ratio: "16/9", class: "rounded-md overflow-hidden border") do
img(
alt: "Placeholder",
class: "absolute inset-0 size-full object-cover",
loading: "lazy",
src: image_url('pattern.jpg')
)
end
end
CardFooter(class: 'justify-end gap-2') do
Button(variant: :outline) { "See more" }
Button(variant: :primary) { "Buy now" }
end
end
Card with full-width image

Introducing RubyUI
Kickstart your project today!
Card(class: 'w-96') do
img(
alt: "Placeholder",
class: "aspect-video w-full object-cover",
loading: "lazy",
src: image_url('pattern.jpg')
)
CardHeader do
CardTitle { 'Introducing RubyUI' }
CardDescription { "Kickstart your project today!" }
end
CardFooter(class: 'justify-end') do
Button(variant: :outline) { "Get started" }
end
end
Account balance
Current Balance
$2,602
**** 4620
Card(class: 'w-96') do
CardHeader do
div(class: 'w-10 h-10 rounded-xl flex items-center justify-center bg-violet-100 text-violet-700 -rotate-6') do
cash_icon
end
end
CardContent(class: 'space-y-1') do
CardDescription(class: 'font-medium') { "Current Balance" }
h5(class: 'font-semibold text-4xl') { '$2,602' }
end
CardFooter do
Text(size: "2", class: "text-muted-foreground") { "**** 4620" }
end
end
With action
Login to your account
Enter your email below to login to your account
Card(class: 'w-96') do
CardHeader do
CardTitle { "Login to your account" }
CardDescription { "Enter your email below to login to your account" }
CardAction do
Button(variant: :link) { "Sign up" }
end
end
CardContent do
div(class: "grid gap-2") do
FormFieldLabel(for: "card-email") { "Email" }
Input(id: "card-email", type: "email", placeholder: "[email protected]")
end
end
CardFooter do
Button(class: "w-full") { "Login" }
end
end
Small
Small card
size: :sm tightens the spacing and the title.
Use it in dense layouts such as sidebars and grids.
Card(size: :sm, class: 'w-80') do
CardHeader do
CardTitle { "Small card" }
CardDescription { "size: :sm tightens the spacing and the title." }
end
CardContent do
plain "Use it in dense layouts such as sidebars and grids."
end
CardFooter do
Button(variant: :outline, size: :sm, class: "w-full") { "Action" }
end
end
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Card
Manual installation
1
Add RubyUI::Card to app/components/ruby_ui/card/card.rb
# frozen_string_literal: true
module RubyUI
# A container for a header, content and footer, like shadcn's Card. The card
# owns the spacing: it stacks its parts with --card-spacing as gap and
# vertical padding, and each part pads only its sides. size: :sm tightens
# the spacing and the title; it is exposed as data-size for the parts.
class Card < Base
SIZES = %i[default sm].freeze
def initialize(size: :default, **attrs)
@size = size.to_s.to_sym
@size = :default unless SIZES.include?(@size) # nil or unknown: default size
super(**attrs)
end
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card", size: @size},
class: [
# No overflow-hidden (vega has it): RubyUI popups such as Select and
# Popover render inside the card, and clipping would hide them.
"group/card flex flex-col gap-(--card-spacing) rounded-xl bg-card py-(--card-spacing) text-sm text-card-foreground shadow-xs ring-1 ring-foreground/10",
"[--card-spacing:--spacing(6)] data-[size=sm]:[--card-spacing:--spacing(4)]",
# An image as the first or last child runs to the card's edge.
"has-[>img:first-child]:pt-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl"
]
}
end
end
end
2
Add RubyUI::CardAction to app/components/ruby_ui/card/card_action.rb
# frozen_string_literal: true
module RubyUI
# Top-right slot of a CardHeader for a control (a button, a link). It spans
# the title and description rows.
class CardAction < Base
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card-action"},
class: "col-start-2 row-span-2 row-start-1 self-start justify-self-end"
}
end
end
end
3
Add RubyUI::CardContent to app/components/ruby_ui/card/card_content.rb
# frozen_string_literal: true
module RubyUI
class CardContent < Base
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card-content"},
class: "px-(--card-spacing)"
}
end
end
end
4
Add RubyUI::CardDescription to app/components/ruby_ui/card/card_description.rb
# frozen_string_literal: true
module RubyUI
class CardDescription < Base
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card-description"},
class: "text-sm text-muted-foreground"
}
end
end
end
5
Add RubyUI::CardDocs to app/components/ruby_ui/card/card_docs.rb
# frozen_string_literal: true
class Views::Docs::Card < Views::Base
def view_template
component = "Card"
div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do
render Docs::Header.new(title: "Card", description: "Displays a card with header, content, and footer.")
Heading(level: 2) { "Usage" }
render Docs::VisualCodeExample.new(title: "Card with image", context: self) do
<<~RUBY
Card(class: 'w-96') do
CardHeader do
CardTitle { 'You might like "RubyUI"' }
CardDescription { "@joeldrapper" }
end
CardContent do
AspectRatio(ratio: "16/9", class: "rounded-md overflow-hidden border") do
img(
alt: "Placeholder",
class: "absolute inset-0 size-full object-cover",
loading: "lazy",
src: image_url('pattern.jpg')
)
end
end
CardFooter(class: 'justify-end gap-2') do
Button(variant: :outline) { "See more" }
Button(variant: :primary) { "Buy now" }
end
end
RUBY
end
render Docs::VisualCodeExample.new(title: "Card with full-width image", context: self) do
<<~RUBY
Card(class: 'w-96') do
img(
alt: "Placeholder",
class: "aspect-video w-full object-cover",
loading: "lazy",
src: image_url('pattern.jpg')
)
CardHeader do
CardTitle { 'Introducing RubyUI' }
CardDescription { "Kickstart your project today!" }
end
CardFooter(class: 'justify-end') do
Button(variant: :outline) { "Get started" }
end
end
RUBY
end
render Docs::VisualCodeExample.new(title: "Account balance", context: self) do
<<~RUBY
Card(class: 'w-96') do
CardHeader do
div(class: 'w-10 h-10 rounded-xl flex items-center justify-center bg-violet-100 text-violet-700 -rotate-6') do
cash_icon
end
end
CardContent(class: 'space-y-1') do
CardDescription(class: 'font-medium') { "Current Balance" }
h5(class: 'font-semibold text-4xl') { '$2,602' }
end
CardFooter do
Text(size: "2", class: "text-muted-foreground") { "**** 4620" }
end
end
RUBY
end
render Docs::VisualCodeExample.new(title: "With action", context: self) do
<<~RUBY
Card(class: 'w-96') do
CardHeader do
CardTitle { "Login to your account" }
CardDescription { "Enter your email below to login to your account" }
CardAction do
Button(variant: :link) { "Sign up" }
end
end
CardContent do
div(class: "grid gap-2") do
FormFieldLabel(for: "card-email") { "Email" }
Input(id: "card-email", type: "email", placeholder: "[email protected]")
end
end
CardFooter do
Button(class: "w-full") { "Login" }
end
end
RUBY
end
render Docs::VisualCodeExample.new(title: "Small", context: self) do
<<~RUBY
Card(size: :sm, class: 'w-80') do
CardHeader do
CardTitle { "Small card" }
CardDescription { "size: :sm tightens the spacing and the title." }
end
CardContent do
plain "Use it in dense layouts such as sidebars and grids."
end
CardFooter do
Button(variant: :outline, size: :sm, class: "w-full") { "Action" }
end
end
RUBY
end
render Components::ComponentSetup::Tabs.new(component_name: component)
render Docs::ComponentsTable.new(component_files(component))
end
end
def arrow_icon(classes: nil)
svg(
xmlns: "http://www.w3.org/2000/svg",
viewbox: "0 0 20 20",
fill: "currentColor",
class: ["w-4 h-4", classes]
) do |s|
s.path(
fill_rule: "evenodd",
d:
"M3 10a.75.75 0 01.75-.75h10.638L10.23 5.29a.75.75 0 111.04-1.08l5.5 5.25a.75.75 0 010 1.08l-5.5 5.25a.75.75 0 11-1.04-1.08l4.158-3.96H3.75A.75.75 0 013 10z",
clip_rule: "evenodd"
)
end
end
def cash_icon(classes: nil)
svg(
xmlns: "http://www.w3.org/2000/svg",
fill: "none",
viewbox: "0 0 24 24",
stroke_width: "1.5",
stroke: "currentColor",
class: ["w-6 h-6", classes]
) do |s|
s.path(
stroke_linecap: "round",
stroke_linejoin: "round",
d:
"M2.25 18.75a60.07 60.07 0 0115.797 2.101c.727.198 1.453-.342 1.453-1.096V18.75M3.75 4.5v.75A.75.75 0 013 6h-.75m0 0v-.375c0-.621.504-1.125 1.125-1.125H20.25M2.25 6v9m18-10.5v.75c0 .414.336.75.75.75h.75m-1.5-1.5h.375c.621 0 1.125.504 1.125 1.125v9.75c0 .621-.504 1.125-1.125 1.125h-.375m1.5-1.5H21a.75.75 0 00-.75.75v.75m0 0H3.75m0 0h-.375a1.125 1.125 0 01-1.125-1.125V15m1.5 1.5v-.75A.75.75 0 003 15h-.75M15 10.5a3 3 0 11-6 0 3 3 0 016 0zm3 0h.008v.008H18V10.5zm-12 0h.008v.008H6V10.5z"
)
end
end
end
6
Add RubyUI::CardFooter to app/components/ruby_ui/card/card_footer.rb
# frozen_string_literal: true
module RubyUI
class CardFooter < Base
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card-footer"},
class: "flex items-center rounded-b-xl px-(--card-spacing) [.border-t]:pt-(--card-spacing)"
}
end
end
end
7
Add RubyUI::CardHeader to app/components/ruby_ui/card/card_header.rb
# frozen_string_literal: true
module RubyUI
# Title and description in a grid; a CardAction takes a second column on
# the right, spanning both rows.
class CardHeader < Base
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card-header"},
class: [
"group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing)",
"has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto]",
"[.border-b]:pb-(--card-spacing)"
]
}
end
end
end
8
Add RubyUI::CardTitle to app/components/ruby_ui/card/card_title.rb
# frozen_string_literal: true
module RubyUI
# A div, as in shadcn: the card can't know its heading level. Wrap the text
# in an h2/h3 when the title should be a heading.
class CardTitle < Base
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {slot: "card-title"},
class: "font-heading text-base leading-normal font-medium group-data-[size=sm]/card:text-sm"
}
end
end
end
Components
| Component | Built using | Source |
|---|---|---|
Card | Phlex | |
CardAction | Phlex | |
CardContent | Phlex | |
CardDescription | Phlex | |
CardDocs | Phlex | |
CardFooter | Phlex | |
CardHeader | Phlex | |
CardTitle | Phlex |