@@ -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,24 +24,61 @@ def styles
2224 # rubocop:enable Layout/LineLength
2325 end
2426
25- def call
26- card_options = { }
27- card_options [ :class ] = self . class . classes + @class
28-
29- content_tag ( :div , card_options . merge ( @options ) ) do
30- concat ( content_tag ( :div , content , class : "font-normal text-gray-700 dark:text-gray-400" ) )
31- end
32- end
33-
3427 # @param class [Array<String>] Additional CSS classes for the card
3528 # container.
3629 #
3730 # @param options [Hash] Additional HTML options for the card container
3831 # (e.g., custom classes, data attributes). These options are merged into
3932 # the card's root element.
40- def initialize ( class : [ ] , options : { } )
33+ #
34+ # @param title [Hash] An optional title for the card. If provided,
35+ # it will be rendered at the top of the card in a h5 tag using the
36+ # Card::Title component. The hash can contain:
37+ # - `content`: The text content of the title
38+ # - `options`: Additional HTML options to pass to the title element
39+ # Alternatively, you can use the `title` slot to provide the entire
40+ # title element yourself.
41+ def initialize ( class : [ ] , options : { } , title : { } )
4142 @class = Array ( binding . local_variable_get ( :class ) ) || [ ]
4243 @options = options || { }
44+ @title = title
45+ end
46+
47+ protected
48+
49+ def card_options
50+ card_options = { }
51+ card_options [ :class ] = self . class . classes + @class
52+ card_options . merge ( @options )
53+ end
54+
55+ # Returns the HTML to use for the title element if any
56+ def default_title
57+ component = Flowbite ::Card ::Title . new ( **default_title_options )
58+
59+ if default_title_content
60+ component . with_content ( default_title_content )
61+ else
62+ component
63+ end
64+
65+ render ( component )
66+ end
67+
68+ def default_title_content
69+ return nil unless @title
70+
71+ @title [ :content ]
72+ end
73+
74+ # @return [Hash] The options to pass to the default title component
75+ def default_title_options
76+ title_options = @title . dup
77+ title_options [ :options ] || { }
78+ end
79+
80+ def title?
81+ @title . present?
4382 end
4483 end
4584end
0 commit comments