From 8accf97b6f5fb76942b03279a3af6fa6210f18db Mon Sep 17 00:00:00 2001 From: Javier Aranda Date: Thu, 17 Oct 2024 14:48:26 +0200 Subject: [PATCH] Add support for TailwindMerge --- CHANGELOG.md | 1 + Gemfile | 1 + Gemfile.lock | 4 ++++ lib/class_variants.rb | 21 +++++++++++++++++++++ lib/class_variants/instance.rb | 10 +++++++++- test/configuration_test.rb | 25 +++++++++++++++++++++++++ test/tailwind_merger_test.rb | 21 +++++++++++++++++++++ 7 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 test/configuration_test.rb create mode 100644 test/tailwind_merger_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e39c1a2..7eaca5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Unreleased +- Add support for TailwindMerge ## 0.0.7 (2023-12-07) - Add support for compound variants ([#8](https://github.com/avo-hq/class_variants/pull/8)) diff --git a/Gemfile b/Gemfile index c5687eb..b3ca7c2 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,4 @@ gem "rubocop-minitest", require: false gem "rubocop-rake", require: false gem "standard", ">= 1.35.1", require: false gem "standard-performance", require: false +gem "tailwind_merge" diff --git a/Gemfile.lock b/Gemfile.lock index 2194e32..1833b40 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 6b0a21d..2d62dc2 100644 --- a/lib/class_variants.rb +++ b/lib/class_variants.rb @@ -4,9 +4,30 @@ require "class_variants/railtie" if defined?(Rails) module ClassVariants + Configuration = Struct.new(:tw_merge, :tw_merge_config) + + @mutex = Mutex.new + class << self + def configuration + @configuration ||= Configuration.new(tw_merge: false, tw_merge_config: {}) + end + + def configure(&block) + yield(configuration) + end + def build(classes, **args) Instance.new classes, **args end + + def tailwind_merge + @mutex.synchronize do + @tailwind_merge ||= begin + require "tailwind_merge" + TailwindMerge::Merger.new(config: configuration.tw_merge_config) + end + end + end end end diff --git a/lib/class_variants/instance.rb b/lib/class_variants/instance.rb index 2ee5cdc..2549ce2 100644 --- a/lib/class_variants/instance.rb +++ b/lib/class_variants/instance.rb @@ -33,7 +33,7 @@ def render(**overrides) result.compact! # Return the final token list - result.join " " + with_tailwind_merge(result.join(" ")) end private @@ -53,5 +53,13 @@ def expand_boolean_variants(variants) output.merge!(next_variant) { |_key, v1, v2| v1.merge!(v2) } end end + + def with_tailwind_merge(classes) + if ClassVariants.configuration.tw_merge + ClassVariants.tailwind_merge.merge(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..4798906 --- /dev/null +++ b/test/configuration_test.rb @@ -0,0 +1,25 @@ +require "test_helper" + +class ConfigurationTest < Minitest::Test + def teardown + ClassVariants.configure do |config| + config.tw_merge = false + end + end + + def test_configuration_tw_merge_default + refute ClassVariants.configuration.tw_merge + end + + def test_configuration_tw_merge_config_default + assert_empty ClassVariants.configuration.tw_merge_config + end + + def test_configure + ClassVariants.configure do |config| + config.tw_merge = true + end + + assert ClassVariants.configuration.tw_merge + end +end diff --git a/test/tailwind_merger_test.rb b/test/tailwind_merger_test.rb new file mode 100644 index 0000000..272e7a5 --- /dev/null +++ b/test/tailwind_merger_test.rb @@ -0,0 +1,21 @@ +require "test_helper" + +class TailwindMergerTest < Minitest::Test + def teardown + ClassVariants.configure do |config| + config.tw_merge = false + end + end + + def test_without_tw_merge + assert_equal "border rounded px-2 py-1 p-5", ClassVariants.build("border rounded px-2 py-1 p-5").render + end + + def test_with_tw_merge + ClassVariants.configure do |config| + config.tw_merge = true + end + + assert_equal "border rounded p-5", ClassVariants.build("border rounded px-2 py-1 p-5").render + end +end