Skip to content

Commit 8486ac8

Browse files
committed
Make it possible to add custom classes to cards
1 parent b6310ec commit 8486ac8

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ Add Flowbite to your Tailwind CSS configuration. In your `app/assets/tailwind/ap
147147
) %>
148148
```
149149

150+
## How to customize components
151+
152+
### Add specific CSS classes
153+
154+
A common use case for customizing a component is to add more CSS classes when
155+
rendering it, fx to change the size or spacing. flowbite-components is optimized
156+
for this case and all you need to do is specify the extra classes:
157+
158+
```erb
159+
<%= render(Flowbite::Card.new(class: "w-full my-8")) { "Content" } %>
160+
```
161+
renders
162+
```html
163+
<div class="... all the usual classes... w-full my-8">
164+
```
165+
166+
If you want to fully replace the existing classes, you can pass an entirely new
167+
`class` attribute via options:
168+
169+
```erb
170+
<%= render(Flowbite::Card.new(options: {class: "w-full my-8"})) { "Content" } %>
171+
```
172+
renders
173+
```html
174+
<div class="w-full my-8">
175+
```
176+
150177
## Available Components
151178

152179
### Form Components

app/components/flowbite/card.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ def styles
2121
end
2222

2323
def call
24-
content_tag(:div, {class: self.class.classes}.merge(@options)) do
24+
card_options = {}
25+
card_options[:class] = self.class.classes + @class
26+
27+
content_tag(:div, card_options.merge(@options)) do
2528
concat(content_tag(:div, content, class: "font-normal text-gray-700 dark:text-gray-400"))
2629
end
2730
end
2831

32+
# @param class [Array<String>] Additional CSS classes for the card
33+
# container.
34+
#
2935
# @param options [Hash] Additional HTML options for the card container
3036
# (e.g., custom classes, data attributes). These options are merged into
3137
# the card's root element.
32-
def initialize(options: {})
38+
def initialize(class: [], options: {})
39+
@class = Array(binding.local_variable_get(:class)) || []
3340
@options = options || {}
3441
end
3542
end

test/components/flowbite/card_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ def test_passes_options_to_the_card_as_attributes
1414

1515
assert_selector("div#card-1")
1616
end
17+
18+
def test_adds_the_classes_to_the_default_classes
19+
render_inline(Flowbite::Card.new(class: "custom-class another")) { "Card Content" }
20+
21+
assert_selector("div.p-6.bg-white.border.border-gray-200.rounded-lg.shadow-sm.custom-class.another")
22+
end
23+
24+
def test_overrides_the_default_classes
25+
render_inline(Flowbite::Card.new(options: {class: "custom-class another"})) { "Card Content" }
26+
27+
assert_no_selector("div.p-6.bg-white.border.border-gray-200.rounded-lg.shadow-sm")
28+
assert_selector("div.custom-class.another")
29+
end
1730
end

0 commit comments

Comments
 (0)