Skip to content

Commit 23a5832

Browse files
authored
Merge pull request #49 from substancelab/classes_are_additive
Make class arguments additive
2 parents 195ae67 + b60017d commit 23a5832

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+477
-37
lines changed

CHANGELOG.md

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

1313
### Changed
1414

15-
*
15+
* Extra CSS classes passed to components in the `class` argument are now added to the default classes from the component. This optimizes for minor tweaks and additions, which is likely to be the most common use case. If you need to replace all classes on the root element of the component, pass them in `options[:class]` instead.
1616

1717
### Removed
1818

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ renders
224224
- **Link**: `Flowbite::Link` (default link styling)
225225

226226

227+
## Principles
228+
229+
### CSS classes are additive
230+
231+
Passing classes via the `class` argument to a component adds the classes to the
232+
default ones instead of replacing them.
233+
234+
```ruby
235+
render(Component.new(class: "these are added"))
236+
```
237+
238+
This makes for easier customization of components, where you don't have to
239+
recreate the entire classlist, ie in order to increase sizes or add margins or
240+
whatever.
241+
242+
If you want to replace the entire class attribute for a component, pass it as part of the `options` hash, ie
243+
244+
```ruby
245+
render(Component.new(options: {class: "these replace the classes"}))
246+
```
247+
227248
## Development
228249

229250
After checking out the repo, run `bin/setup` to install dependencies.

app/components/flowbite/button.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def styles
6363

6464
attr_reader :button_attributes, :size, :style
6565

66-
def initialize(size: :default, style: :default, **button_attributes)
66+
def initialize(class: nil, size: :default, style: :default, **button_attributes)
67+
@class = Array.wrap(binding.local_variable_get(:class))
6768
@size = size
6869
@style = style
6970
@button_attributes = button_attributes
@@ -80,7 +81,7 @@ def call
8081
private
8182

8283
def classes
83-
self.class.classes(size: size, state: :default, style: style)
84+
self.class.classes(size: size, state: :default, style: style) + @class
8485
end
8586

8687
def options

app/components/flowbite/input/checkbox.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def call
4343
)
4444
end
4545

46-
def initialize(attribute:, form:, disabled: false, options: {}, size: :default, unchecked_value: DEFAULT_UNCHECKED_VALUE, value: DEFAULT_CHECKED_VALUE)
47-
super(attribute: attribute, form: form, disabled: disabled, options: options, size: size)
46+
def initialize(attribute:, form:, class: nil, disabled: false, options: {}, size: :default, unchecked_value: DEFAULT_UNCHECKED_VALUE, value: DEFAULT_CHECKED_VALUE)
47+
super(attribute: attribute, class: binding.local_variable_get(:class), form: form, disabled: disabled, options: options, size: size)
4848
@unchecked_value = unchecked_value
4949
@value = value
5050
end

app/components/flowbite/input/field.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def styles
5757
# rubocop:enable Layout/LineLength
5858
end
5959

60-
def initialize(attribute:, form:, disabled: false, options: {}, size: :default)
60+
def initialize(attribute:, form:, class: nil, disabled: false, options: {}, size: :default)
6161
@attribute = attribute
62+
@class = Array.wrap(binding.local_variable_get(:class))
6263
@disabled = disabled
6364
@form = form
6465
@options = options || {}
@@ -77,7 +78,7 @@ def call
7778

7879
# Returns the CSS classes to apply to the input field
7980
def classes
80-
self.class.classes(size: size, state: state)
81+
self.class.classes(size: size, state: state) + @class
8182
end
8283

8384
# Returns the name of the method used to generate HTML for the input field

app/components/flowbite/input/hint.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ def call
3030
)
3131
end
3232

33-
def initialize(attribute:, form:, options: {})
33+
def initialize(attribute:, form:, class: nil, options: {})
3434
@attribute = attribute
35+
@class = Array.wrap(binding.local_variable_get(:class))
3536
@form = form
3637
@options = options
3738
@object = form.object
3839
end
3940

4041
# Returns an array with the CSS classes to apply to the label element
4142
def classes
42-
self.class.classes(state: state)
43+
self.class.classes(state: state) + @class
4344
end
4445

4546
protected

app/components/flowbite/input/label.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ def errors?
3939
@object.errors.include?(@attribute.intern)
4040
end
4141

42-
def initialize(attribute:, form:, disabled: false, options: {})
42+
def initialize(attribute:, form:, class: nil, disabled: false, options: {})
4343
@attribute = attribute
44+
@class = Array.wrap(binding.local_variable_get(:class))
4445
@disabled = disabled
4546
@form = form
4647
@object = form.object
@@ -49,7 +50,7 @@ def initialize(attribute:, form:, disabled: false, options: {})
4950

5051
# Returns an array with the CSS classes to apply to the label element
5152
def classes
52-
self.class.classes(state: state)
53+
self.class.classes(state: state) + @class
5354
end
5455

5556
protected

app/components/flowbite/input/radio_button.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def call
3737
)
3838
end
3939

40-
def initialize(attribute:, form:, value:, disabled: false, options: {})
41-
super(attribute: attribute, disabled: disabled, form: form, options: options)
40+
def initialize(attribute:, form:, value:, class: nil, disabled: false, options: {})
41+
super(attribute: attribute, class: binding.local_variable_get(:class), disabled: disabled, form: form, options: options)
4242
@value = value
4343
end
4444

app/components/flowbite/input/select.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Select < Field
1414
lg: ["px-4", "py-3", "text-base"]
1515
}.freeze
1616

17-
def initialize(form:, attribute:, collection: [], disabled: false, include_blank: false, multiple: false, options: {}, size: :default)
18-
super(form: form, attribute: attribute, disabled: disabled, options: options, size: size)
17+
def initialize(form:, attribute:, class: nil, collection: [], disabled: false, include_blank: false, multiple: false, options: {}, size: :default)
18+
super(form: form, attribute: attribute, class: binding.local_variable_get(:class), disabled: disabled, options: options, size: size)
1919
@collection = collection
2020
@include_blank = include_blank
2121
@multiple = multiple

app/components/flowbite/input/textarea.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def call
3030

3131
# Returns the CSS classes to apply to the input field
3232
def classes
33-
self.class.classes(size: size, state: state)
33+
self.class.classes(size: size, state: state) + @class
3434
end
3535

3636
# Returns the name of the method used to generate HTML for the input field

0 commit comments

Comments
 (0)