Skip to content

Commit

Permalink
Allow option to opt out of base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonksthebear committed Mar 28, 2024
1 parent 0742ba8 commit 691a557
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
classy-yaml (1.1)
classy-yaml (1.2)
actionpack (>= 6)
activesupport (>= 6)
railties (>= 6)
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ btn:

Now, calling `yass(btn: :blue)` or `yass(btn: :yellow)` will ALSO pull in the classes from `btn: :base`.

### Optionally Skipping Base

You can optionally skip including the base on a `yass` call by including the key/value `skip_base: true`. So, using the example above,
we can perform:
```
btn:
base: "px-3 py-2"
blue: "text-blue-200 bg-blue-500"
yellow: "text-yellow-200 bg-blue-500"
```

Now, calling `yass(btn: :blue, skip_base: true)` and this will skip pulling in the classes from `btn: :base`. This is helpful
when defining animation classes and you only want to include the different classes, such as `active` and `inactive` for instance.

### ViewComponent
There is a special helper built for ViewComponent and sidecar assets. In your `example_component.rb`, add the line `include Classy::Yaml::ComponentHelpers`. This helper will tell `yass` to check if there is a `example_component.yml` file, and first use that for definitions. If the definitions aren't found in the `example_component.yml`, then it will fallback to `config/utility_classes.yml`.
Expand Down
Binary file removed classy-yaml-0.8.5.gem
Binary file not shown.
Binary file added classy-yaml-1.2.gem
Binary file not shown.
27 changes: 15 additions & 12 deletions lib/classy/yaml/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def yass(*args)

return if classy_yamls.blank?

skip_base_hash = args.find { |arg| arg.is_a?(Hash) && arg.keys.include?(:skip_base) } || {}
keys, classes = flatten_args(values: args)
classes += fetch_classes(keys, classy_yamls: classy_yamls)
classes += fetch_classes(keys, classy_yamls: classy_yamls, skip_base: skip_base_hash[:skip_base])

return classes.flatten.join(" ")
end
Expand Down Expand Up @@ -48,22 +49,24 @@ def flatten_args(root: [], values: [], keys: [], added_classes: [])
return keys, added_classes
end

def fetch_classes(keys, classy_yamls: [])
def fetch_classes(keys, classy_yamls: [], skip_base: false)
classes = []

keys.map do |key|
base_classes = nil
fetched_classes = nil

classy_yamls.reverse_each do |classy_yaml|
begin
base_classes ||= if classy_yaml.send(:dig, *key).is_a?(Hash)
classy_yaml.send(:dig, *(key + ['base'])).try(:split, " ")
else
classy_yaml.send(:dig, *(key[0...-1] + ['base'])).try(:split, " ")
end
rescue
Rails.logger.warn(Classy::Yaml::InvalidKeyError.new(data: key))
unless skip_base == true
begin
base_classes ||= if classy_yaml.send(:dig, *key).is_a?(Hash)
classy_yaml.send(:dig, *(key + ['base'])).try(:split, " ")
else
classy_yaml.send(:dig, *(key[0...-1] + ['base'])).try(:split, " ")
end
rescue
Rails.logger.warn(Classy::Yaml::InvalidKeyError.new(data: key))
end
end

begin
Expand All @@ -78,8 +81,8 @@ def fetch_classes(keys, classy_yamls: [])
end
end

classes << base_classes
classes << fetched_classes
classes << base_classes unless base_classes.blank?
classes << fetched_classes unless fetched_classes.blank?
end

classes.reject!(&:blank?)
Expand Down
2 changes: 1 addition & 1 deletion lib/classy/yaml/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Classy
module Yaml
VERSION = '1.1'
VERSION = '1.2'
end
end
5 changes: 5 additions & 0 deletions test/classy/yaml_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ class Classy::YamlComponentTest < ViewComponent::TestCase
render_inline(TestComponent::NestedComponent.new classy: :nested_inherited, inherited: true)
assert_text "nested-inherited"
end

test "can skip base" do
render_inline(TestComponent.new classy: {nested_base: :nested, skip_base: true})
assert_text "nested-class"
end
end
4 changes: 4 additions & 0 deletions test/classy/yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ class Classy::YamlTest < ActiveSupport::TestCase
assert_equal "single-class", yass(:single)
assert_equal 'extra-single-class', yass(:extra_single)
end

test "allow skipping of base" do
assert_equal "nested-class", yass(nested_base: :nested, skip_base: true)
end
end

0 comments on commit 691a557

Please sign in to comment.