From e15dbacf3d3a03ff48ad14e176d46ec0f41f05eb Mon Sep 17 00:00:00 2001 From: Javier Aranda Date: Thu, 24 Oct 2024 12:02:03 +0200 Subject: [PATCH] Add support for post process classes --- CHANGELOG.md | 1 + Gemfile | 4 ++++ Gemfile.lock | 4 ++++ lib/class_variants.rb | 9 ++++++++ lib/class_variants/configuration.rb | 11 ++++++++++ lib/class_variants/instance.rb | 10 ++++++++- test/configuration_test.rb | 19 ++++++++++++++++ test/process_classes_with_test.rb | 34 +++++++++++++++++++++++++++++ 8 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/class_variants/configuration.rb create mode 100644 test/configuration_test.rb create mode 100644 test/process_classes_with_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index b1ae942..02dde64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Add support for slots ([#15](https://github.com/avo-hq/class_variants/pull/15)) - Allow passing additional classes when render ([#17](https://github.com/avo-hq/class_variants/pull/17)) - Add helper module for defining variants ([#18](https://github.com/avo-hq/class_variants/pull/18)) +- Add support for post process classes ([#16](https://github.com/avo-hq/class_variants/pull/16)) ## 0.0.8 (2024-10-24) - Deprecate usage of positional arguments ([#12](https://github.com/avo-hq/class_variants/pull/12)) diff --git a/Gemfile b/Gemfile index c5687eb..a9445bb 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,7 @@ gem "rubocop-minitest", require: false gem "rubocop-rake", require: false gem "standard", ">= 1.35.1", require: false gem "standard-performance", require: false + +install_if -> { !RUBY_VERSION.start_with?("3.0") } do + gem "tailwind_merge" +end diff --git a/Gemfile.lock b/Gemfile.lock index b33918a..e2a63e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -15,6 +15,7 @@ GEM json (2.7.2) language_server-protocol (3.17.0.3) lint_roller (1.1.0) + lru_redux (1.1.0) minitest (5.25.1) parallel (1.26.3) parser (3.3.5.0) @@ -64,6 +65,8 @@ GEM lint_roller (~> 1.1) rubocop-performance (~> 1.22.0) stringio (3.1.1) + tailwind_merge (0.13.1) + lru_redux (~> 1.1) unicode-display_width (2.6.0) PLATFORMS @@ -81,6 +84,7 @@ DEPENDENCIES rubocop-rake standard (>= 1.35.1) standard-performance + tailwind_merge BUNDLED WITH 2.5.20 diff --git a/lib/class_variants.rb b/lib/class_variants.rb index 037268b..1b25b74 100644 --- a/lib/class_variants.rb +++ b/lib/class_variants.rb @@ -1,11 +1,20 @@ require "class_variants/version" require "class_variants/action_view/helpers" +require "class_variants/configuration" require "class_variants/instance" require "class_variants/helper" require "class_variants/railtie" if defined?(Rails) module ClassVariants class << self + def configuration + @configuration ||= Configuration.new + end + + def configure(&block) + yield(configuration) + end + def build(...) Instance.new(...) end diff --git a/lib/class_variants/configuration.rb b/lib/class_variants/configuration.rb new file mode 100644 index 0000000..6716879 --- /dev/null +++ b/lib/class_variants/configuration.rb @@ -0,0 +1,11 @@ +module ClassVariants + class Configuration + def process_classes_with(&block) + if block_given? + @process_classes_with = block + else + @process_classes_with + end + end + end +end diff --git a/lib/class_variants/instance.rb b/lib/class_variants/instance.rb index ab8d698..55c170e 100644 --- a/lib/class_variants/instance.rb +++ b/lib/class_variants/instance.rb @@ -34,7 +34,7 @@ def render(slot = :default, **overrides) result.compact! # Return the final token list - result.join " " + with_classess_processor(result.join(" ")) end private @@ -97,5 +97,13 @@ def expand_compound_variants(compound_variants) compound_variant.merge(slot: :default) end end + + def with_classess_processor(classes) + if ClassVariants.configuration.process_classes_with.respond_to?(:call) + ClassVariants.configuration.process_classes_with.call(classes) + else + classes + end + end end end diff --git a/test/configuration_test.rb b/test/configuration_test.rb new file mode 100644 index 0000000..0a2d525 --- /dev/null +++ b/test/configuration_test.rb @@ -0,0 +1,19 @@ +require "test_helper" + +class ConfigurationTest < Minitest::Test + def teardown + ClassVariants.configuration.instance_variable_set(:@process_classes_with, nil) + end + + def test_configuration_process_classes_with_default + refute ClassVariants.configuration.process_classes_with + end + + def test_configure + ClassVariants.configure do |config| + config.process_classes_with { |classes| classes } + end + + assert_respond_to ClassVariants.configuration.process_classes_with, :call + end +end diff --git a/test/process_classes_with_test.rb b/test/process_classes_with_test.rb new file mode 100644 index 0000000..7b8309c --- /dev/null +++ b/test/process_classes_with_test.rb @@ -0,0 +1,34 @@ +require "test_helper" + +class ProcessClassesWithTest < Minitest::Test + def teardown + ClassVariants.configuration.instance_variable_set(:@process_classes_with, nil) + end + + def test_without_classes_processor + assert_equal "border rounded px-2 py-1 p-5", ClassVariants.build(base: "border rounded px-2 py-1 p-5").render + end + + def test_with_classes_processor + ClassVariants.configure do |config| + config.process_classes_with do |classes| + merger.merge(classes) + end + end + + assert_equal "border rounded p-5", ClassVariants.build(base: "border rounded px-2 py-1 p-5").render + end + + private + + def merger + require "tailwind_merge" + TailwindMerge::Merger.new + rescue LoadError + Class.new do + def merge(classes) + classes.gsub("px-2 py-1 ", "") + end + end.new + end +end