Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for post process classes #16

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions lib/generators/class_variants/install/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Description:
Generates initializer file for configure ClassVariants in your application.
11 changes: 11 additions & 0 deletions lib/generators/class_variants/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ClassVariants
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("templates", __dir__)

def copy_initializer
template "class_variants.rb", "config/initializers/class_variants.rb"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# ClassVariants.configure do |config|
# # allow to post process classes with an external utility like TailwindMerger
# config.process_classes_with do |classes|
# TailwindMerge::Merger.new.merge(classes)
# end
# end
Comment on lines +3 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about generating the initializer uncommented and leave only the process_classes_with option commented out, but I think I like this approach better.
We are not configuring anything with it, we might as well not load it.
Good call!

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|
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