From d475d85c50b58481ceeb1470768380fbc8b6934a Mon Sep 17 00:00:00 2001 From: David Alejandro <15317732+davidalejandroaguilar@users.noreply.github.com> Date: Thu, 5 Sep 2024 05:24:11 +0900 Subject: [PATCH] TEST: Use a small DSL for registering components modifiers So that we can automatically include custom modifiers --- lib/phlexy_ui/base.rb | 19 +++++++++++++++++++ lib/phlexy_ui/card.rb | 8 +++----- lib/phlexy_ui/class_list.rb | 13 ------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/phlexy_ui/base.rb b/lib/phlexy_ui/base.rb index acf238e..58f5991 100644 --- a/lib/phlexy_ui/base.rb +++ b/lib/phlexy_ui/base.rb @@ -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: [], diff --git a/lib/phlexy_ui/card.rb b/lib/phlexy_ui/card.rb index 48b7f3b..8269193 100644 --- a/lib/phlexy_ui/card.rb +++ b/lib/phlexy_ui/card.rb @@ -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| @@ -45,9 +45,7 @@ def actions(**options, &) end end - private - - CARD_MODIFIERS_MAP = { + register_modifiers( # "sm:image-full" # "md:image-full" # "lg:image-full" @@ -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 diff --git a/lib/phlexy_ui/class_list.rb b/lib/phlexy_ui/class_list.rb index 4af0b1e..0b3df26 100644 --- a/lib/phlexy_ui/class_list.rb +++ b/lib/phlexy_ui/class_list.rb @@ -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 @@ -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)