From 1a0a8df61540b0a3e985b43630e0f8a2a8731c4c 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] Use a small DSL for registering components modifiers So that we can automatically include custom modifiers --- lib/phlexy_ui/base.rb | 17 +++++++++++++++++ lib/phlexy_ui/card.rb | 8 +++----- lib/phlexy_ui/class_list.rb | 13 ------------- spec/lib/phlexy_ui/card_spec.rb | 4 ++-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/phlexy_ui/base.rb b/lib/phlexy_ui/base.rb index acf238e..5c5e44b 100644 --- a/lib/phlexy_ui/base.rb +++ b/lib/phlexy_ui/base.rb @@ -7,10 +7,27 @@ def initialize(*base_modifiers, **options) @options = options end + class << self + 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: self.class), + **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) diff --git a/spec/lib/phlexy_ui/card_spec.rb b/spec/lib/phlexy_ui/card_spec.rb index 8ed4dcf..b7d4329 100644 --- a/spec/lib/phlexy_ui/card_spec.rb +++ b/spec/lib/phlexy_ui/card_spec.rb @@ -293,8 +293,8 @@ card w-96 shadow-xl - #{viewport}:rounded-box - #{viewport}:p-4> + #{viewport}:p-4 + #{viewport}:rounded-box"> HTML