-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract AttributeSet and ClassList from Base
- Loading branch information
1 parent
7f38e3b
commit b6e322e
Showing
10 changed files
with
130 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module PhlexyUI | ||
class AttributeSet | ||
def initialize(base_modifiers, attributes_map) | ||
@base_modifiers = base_modifiers | ||
@attributes_map = attributes_map | ||
end | ||
|
||
def to_h | ||
attributes_modifiers.each_with_object({}) do |modifier, final_attributes_hash| | ||
value = attributes_map[modifier] | ||
|
||
if value.is_a?(Hash) | ||
final_attributes_hash.merge!(value) | ||
elsif value | ||
final_attributes_hash[modifier] = value | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :base_modifiers, :attributes_map | ||
|
||
def attributes_modifiers | ||
base_modifiers.select { |modifier| attributes_map.key?(modifier) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module PhlexyUI | ||
class ClassList | ||
RESPONSIVE_PREFIXES = %i[sm md lg].freeze | ||
|
||
def initialize(component_html_class: nil, base_modifiers: [], options: {}, modifiers_map: {}) | ||
@component_html_class = component_html_class | ||
@base_modifiers = base_modifiers | ||
@options = options | ||
@modifiers_map = modifiers_map | ||
end | ||
|
||
def to_a | ||
classes = [] | ||
add_component_class(classes) | ||
add_base_classes(classes) | ||
add_responsive_classes(classes) | ||
add_additional_class(classes) | ||
classes | ||
end | ||
|
||
private | ||
|
||
attr_reader :component_html_class, :base_modifiers, :options, :modifiers_map | ||
|
||
def classes_modifiers | ||
base_modifiers.select { |modifier| modifiers_map.key?(modifier) } | ||
end | ||
|
||
def add_component_class(classes) | ||
return unless component_html_class | ||
|
||
classes << with_config_prefix(component_html_class) | ||
end | ||
|
||
def add_base_classes(classes) | ||
classes.concat(html_classes_for_modifiers(classes_modifiers)) | ||
end | ||
|
||
def html_classes_for_modifiers(modifiers, responsive_prefix: nil) | ||
modifiers.map do |modifier| | ||
with_responsive_prefix( | ||
with_config_prefix( | ||
modifiers_map.fetch(modifier) | ||
), | ||
responsive_prefix | ||
) | ||
end | ||
end | ||
|
||
def add_responsive_classes(classes) | ||
RESPONSIVE_PREFIXES.each do |responsive_prefix| | ||
if (values = options.delete(responsive_prefix)) | ||
classes.concat( | ||
html_classes_for_modifiers( | ||
Array(values), | ||
responsive_prefix: | ||
) | ||
) | ||
end | ||
end | ||
end | ||
|
||
def add_additional_class(classes) | ||
classes << options.delete(:class) if options[:class] | ||
end | ||
|
||
def with_config_prefix(string) | ||
"#{PhlexyUI.configuration.prefix}#{string}" | ||
end | ||
|
||
def with_responsive_prefix(string, responsive_prefix = nil) | ||
responsive_prefix ? "#{responsive_prefix}:#{string}" : string | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters