-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.rb
More file actions
100 lines (87 loc) · 2.85 KB
/
card.rb
File metadata and controls
100 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true
module Flowbite
# Renders a card element.
#
# To render a title in the card, use the title argument or the title slot.
#
# @example Using the title slot
# <%= render(Flowbite::Card.new) do |card| %>
# <% card.with_title do %>
# <div><%= parent_category.name %></div>
# <%= render(Flowbite::Card::Title.new) { category.name } %>
# <% end %>
# <% end %>
#
# @viewcomponent_slot [Flowbite::Card::Title] title The title of the card,
# rendered at the top. Use +with_title+ to set custom content.
#
# @see https://flowbite.com/docs/components/cards/
# @lookbook_embed CardPreview
class Card < ViewComponent::Base
renders_one :title
class << self
def classes(state: :default, style: :default)
style = styles.fetch(style)
style.fetch(state)
end
# rubocop:disable Layout/LineLength
def styles
Flowbite::Styles.from_hash(
{
default: {
default: ["p-6", "bg-neutral-primary-soft", "border", "border-default", "rounded-base", "shadow-xs"]
}
}.freeze
)
end
# rubocop:enable Layout/LineLength
end
# @param class [Array<String>] Additional CSS classes for the card
# container.
#
# @param options [Hash] Additional HTML options for the card container
# (e.g., custom classes, data attributes). These options are merged into
# the card's root element.
#
# @param title [Hash] An optional title for the card. If provided,
# it will be rendered at the top of the card in a h5 tag using the
# Card::Title component. The hash can contain:
# - `content`: The text content of the title
# - `options`: Additional HTML options to pass to the title element
# Alternatively, you can use the `title` slot to provide the entire
# title element yourself.
def initialize(class: [], options: {}, title: {})
@class = Array(binding.local_variable_get(:class)) || []
@options = options || {}
@title = title
end
protected
def card_options
card_options = {}
card_options[:class] = self.class.classes + @class
card_options.merge(@options)
end
# Returns the HTML to use for the title element if any
def default_title
component = Flowbite::Card::Title.new(**default_title_options)
if default_title_content
component.with_content(default_title_content)
else
component
end
render(component)
end
def default_title_content
return nil unless @title
@title[:content]
end
# @return [Hash] The options to pass to the default title component
def default_title_options
title_options = @title.dup
title_options[:options] || {}
end
def title?
@title.present?
end
end
end