Skip to content

Commit

Permalink
Add support for TailwindMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Oct 17, 2024
1 parent fe9ddbd commit 8accf97
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -81,6 +84,7 @@ DEPENDENCIES
rubocop-rake
standard (>= 1.35.1)
standard-performance
tailwind_merge

BUNDLED WITH
2.5.20
21 changes: 21 additions & 0 deletions lib/class_variants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion lib/class_variants/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def render(**overrides)
result.compact!

# Return the final token list
result.join " "
with_tailwind_merge(result.join(" "))
end

private
Expand All @@ -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
25 changes: 25 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/tailwind_merger_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8accf97

Please sign in to comment.