Skip to content

Commit b23e493

Browse files
committed
Add support for TailwindMerge
1 parent fe9ddbd commit b23e493

File tree

7 files changed

+82
-1
lines changed

7 files changed

+82
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Unreleased
2+
- Add support for TailwindMerge ([#16](https://github.com/avo-hq/class_variants/pull/16))
23

34
## 0.0.7 (2023-12-07)
45
- Add support for compound variants ([#8](https://github.com/avo-hq/class_variants/pull/8))

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ gem "rubocop-minitest", require: false
1111
gem "rubocop-rake", require: false
1212
gem "standard", ">= 1.35.1", require: false
1313
gem "standard-performance", require: false
14+
gem "tailwind_merge"

Gemfile.lock

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ GEM
1515
json (2.7.2)
1616
language_server-protocol (3.17.0.3)
1717
lint_roller (1.1.0)
18+
lru_redux (1.1.0)
1819
minitest (5.25.1)
1920
parallel (1.26.3)
2021
parser (3.3.5.0)
@@ -64,6 +65,8 @@ GEM
6465
lint_roller (~> 1.1)
6566
rubocop-performance (~> 1.22.0)
6667
stringio (3.1.1)
68+
tailwind_merge (0.13.1)
69+
lru_redux (~> 1.1)
6770
unicode-display_width (2.6.0)
6871

6972
PLATFORMS
@@ -81,6 +84,7 @@ DEPENDENCIES
8184
rubocop-rake
8285
standard (>= 1.35.1)
8386
standard-performance
87+
tailwind_merge
8488

8589
BUNDLED WITH
8690
2.5.20

lib/class_variants.rb

+21
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,30 @@
44
require "class_variants/railtie" if defined?(Rails)
55

66
module ClassVariants
7+
Configuration = Struct.new(:tw_merge, :tw_merge_config)
8+
9+
@mutex = Mutex.new
10+
711
class << self
12+
def configuration
13+
@configuration ||= Configuration.new(tw_merge: false, tw_merge_config: {})
14+
end
15+
16+
def configure(&block)
17+
yield(configuration)
18+
end
19+
820
def build(classes, **args)
921
Instance.new classes, **args
1022
end
23+
24+
def tailwind_merge
25+
@mutex.synchronize do
26+
@tailwind_merge ||= begin
27+
require "tailwind_merge"
28+
TailwindMerge::Merger.new(config: configuration.tw_merge_config)
29+
end
30+
end
31+
end
1132
end
1233
end

lib/class_variants/instance.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def render(**overrides)
3333
result.compact!
3434

3535
# Return the final token list
36-
result.join " "
36+
with_tailwind_merge(result.join(" "))
3737
end
3838

3939
private
@@ -53,5 +53,13 @@ def expand_boolean_variants(variants)
5353
output.merge!(next_variant) { |_key, v1, v2| v1.merge!(v2) }
5454
end
5555
end
56+
57+
def with_tailwind_merge(classes)
58+
if ClassVariants.configuration.tw_merge
59+
ClassVariants.tailwind_merge.merge(classes)
60+
else
61+
classes
62+
end
63+
end
5664
end
5765
end

test/configuration_test.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "test_helper"
2+
3+
class ConfigurationTest < Minitest::Test
4+
def teardown
5+
ClassVariants.configure do |config|
6+
config.tw_merge = false
7+
end
8+
end
9+
10+
def test_configuration_tw_merge_default
11+
refute ClassVariants.configuration.tw_merge
12+
end
13+
14+
def test_configuration_tw_merge_config_default
15+
assert_empty ClassVariants.configuration.tw_merge_config
16+
end
17+
18+
def test_configure
19+
ClassVariants.configure do |config|
20+
config.tw_merge = true
21+
end
22+
23+
assert ClassVariants.configuration.tw_merge
24+
end
25+
end

test/tailwind_merger_test.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "test_helper"
2+
3+
class TailwindMergerTest < Minitest::Test
4+
def teardown
5+
ClassVariants.configure do |config|
6+
config.tw_merge = false
7+
end
8+
end
9+
10+
def test_without_tw_merge
11+
assert_equal "border rounded px-2 py-1 p-5", ClassVariants.build("border rounded px-2 py-1 p-5").render
12+
end
13+
14+
def test_with_tw_merge
15+
ClassVariants.configure do |config|
16+
config.tw_merge = true
17+
end
18+
19+
assert_equal "border rounded p-5", ClassVariants.build("border rounded px-2 py-1 p-5").render
20+
end
21+
end

0 commit comments

Comments
 (0)