Skip to content

Commit

Permalink
TEST: Use a small DSL for registering components modifiers
Browse files Browse the repository at this point in the history
So that we can automatically include custom modifiers
  • Loading branch information
davidalejandroaguilar committed Sep 4, 2024
1 parent 2baf96f commit d475d85
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
19 changes: 19 additions & 0 deletions lib/phlexy_ui/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@ def initialize(*base_modifiers, **options)
@options = options
end

class << self
private

attr_reader :modifiers

def register_modifiers(modifiers)
@modifiers ||= {}
@modifiers.merge(modifiers)
end
end

private

attr_reader :base_modifiers, :options, :as, :id

def modifiers
{
**self.class.modifiers,
**PhlexyUI.configuration.modifiers.for(component:),
**PhlexyUI.configuration.modifiers.for(component: nil)
}
end

def generate_classes!(
component_html_class: nil,
base_modifiers: [],
Expand Down
8 changes: 3 additions & 5 deletions lib/phlexy_ui/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(*, as: :section, **)
def view_template(&)
generate_classes!(
component_html_class: :card,
modifiers_map: CARD_MODIFIERS_MAP,
modifiers_map: modifiers,
base_modifiers:,
options:
).then do |classes|
Expand Down Expand Up @@ -45,9 +45,7 @@ def actions(**options, &)
end
end

private

CARD_MODIFIERS_MAP = {
register_modifiers(
# "sm:image-full"
# "md:image-full"
# "lg:image-full"
Expand Down Expand Up @@ -116,6 +114,6 @@ def actions(**options, &)
# "md:bg-error md:text-error-content"
# "lg:bg-error lg:text-error-content"
error: "bg-error text-error-content"
}.freeze
)
end
end
13 changes: 0 additions & 13 deletions lib/phlexy_ui/class_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ def selected_base_modifiers
base_modifiers.select { |modifier| modifiers_map.key?(modifier) }
end

def selected_custom_modifiers
base_modifiers.select do |modifier|
PhlexyUI.configuration.modifiers.key?(modifier, component:) ||
PhlexyUI.configuration.modifiers.key?(modifier)
end
end

def add_component_class(classes)
return unless component_html_class

Expand All @@ -51,12 +44,6 @@ def add_selected_modifiers_classes(classes)
selected_base_modifiers
)
)

classes.concat(
html_classes_for_modifiers(
selected_custom_modifiers
)
)
end

def html_classes_for_modifiers(modifiers, responsive_prefix: nil)
Expand Down

0 comments on commit d475d85

Please sign in to comment.