Skip to content

Commit 24cdf00

Browse files
committed
Render the title as a component slot
1 parent 1f7fc29 commit 24cdf00

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Added
99

10-
*
10+
* Flowbite::Card now displays a title via the title argument/slot.
1111

1212
### Changed
1313

app/components/flowbite/card.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Flowbite
55
#
66
# See https://flowbite.com/docs/components/cards/
77
class Card < ViewComponent::Base
8+
renders_one :title
9+
810
class << self
911
def classes(state: :default, style: :default)
1012
style = styles.fetch(style)
@@ -22,12 +24,6 @@ def styles
2224
# rubocop:enable Layout/LineLength
2325
end
2426

25-
def card_options
26-
card_options = {}
27-
card_options[:class] = self.class.classes + @class
28-
card_options.merge(@options)
29-
end
30-
3127
# @param class [Array<String>] Additional CSS classes for the card
3228
# container.
3329
#
@@ -37,15 +33,36 @@ def card_options
3733
#
3834
# @param title [String, nil] An optional title for the card. If provided,
3935
# it will be rendered at the top of the card in a h5 tag using the
40-
# Card::Title component.
36+
# Card::Title component. Alternatively, you can use the `title` slot to
37+
# provide the entire title element yourself.
4138
def initialize(class: [], options: {}, title: nil)
4239
@class = Array(binding.local_variable_get(:class)) || []
4340
@options = options || {}
4441
@title = title
4542
end
4643

47-
def title
44+
protected
45+
46+
def card_options
47+
card_options = {}
48+
card_options[:class] = self.class.classes + @class
49+
card_options.merge(@options)
50+
end
51+
52+
# Returns the HTML to use for the title element if any
53+
def default_title
54+
return nil unless title?
55+
56+
component = Flowbite::Card::Title.new.with_content(default_title_content)
57+
render(component)
58+
end
59+
60+
def default_title_content
4861
@title
4962
end
63+
64+
def title?
65+
@title.present?
66+
end
5067
end
5168
end

app/components/flowbite/card/card.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<%= content_tag(:div, card_options) do %>
2-
<% if title.present? %>
3-
<%= render(Flowbite::Card::Title.new) { title } %>
4-
<% end %>
2+
<%= title %>
53
<% if content.present? %>
64
<div class="font-normal text-gray-700 dark:text-gray-400"><%= content %></div>
75
<% end %>

demo/test/components/previews/card_preview.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@ def with_title_argument
1414
end
1515
end
1616

17+
def with_title_slot
18+
render(Flowbite::Card.new) do |component|
19+
component.with_title { "<h1 class=\"text-3xl\">This title replace the entire title element</h1>".html_safe }
20+
"Use the title slot to control all aspects of the title element"
21+
end
22+
end
23+
1724
# @!endgroup
1825
end

test/components/flowbite/card_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ def test_with_title_argument
3333

3434
assert_selector("h5.mb-2.text-2xl.font-bold.tracking-tight.text-gray-900.dark\\:text-white", text: "Card Title")
3535
end
36+
37+
def test_with_title_slot_when_using_with_title
38+
render_inline(Flowbite::Card.new) do |component|
39+
component.with_title { "<h1>This is the full title</h1>".html_safe }
40+
end
41+
42+
assert_no_selector("h5.mb-2.text-2xl.font-bold.tracking-tight.text-gray-900.dark\\:text-white")
43+
assert_selector("h1", text: "This is the full title")
44+
end
3645
end

0 commit comments

Comments
 (0)