Skip to content

Commit

Permalink
Add support for post process classes
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Oct 24, 2024
1 parent 69468f6 commit 1f95f93
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
9 changes: 9 additions & 0 deletions lib/class_variants.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/class_variants/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion lib/class_variants/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
19 changes: 19 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions test/process_classes_with_test.rb
Original file line number Diff line number Diff line change
@@ -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|
tailwind_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 tailwind_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

0 comments on commit 1f95f93

Please sign in to comment.