diff --git a/Gemfile.lock b/Gemfile.lock index d59df8d..fbf368c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - classy-yaml (1.1) + classy-yaml (1.2) actionpack (>= 6) activesupport (>= 6) railties (>= 6) diff --git a/README.md b/README.md index 1baecbe..b9cb281 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/classy-yaml-0.8.5.gem b/classy-yaml-0.8.5.gem deleted file mode 100644 index 0b57ed8..0000000 Binary files a/classy-yaml-0.8.5.gem and /dev/null differ diff --git a/classy-yaml-1.2.gem b/classy-yaml-1.2.gem new file mode 100644 index 0000000..8e0c578 Binary files /dev/null and b/classy-yaml-1.2.gem differ diff --git a/lib/classy/yaml/helpers.rb b/lib/classy/yaml/helpers.rb index 0421b6e..ed247a4 100644 --- a/lib/classy/yaml/helpers.rb +++ b/lib/classy/yaml/helpers.rb @@ -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 @@ -48,7 +49,7 @@ 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| @@ -56,14 +57,16 @@ def fetch_classes(keys, classy_yamls: []) 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 @@ -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?) diff --git a/lib/classy/yaml/version.rb b/lib/classy/yaml/version.rb index 837f511..1108a00 100644 --- a/lib/classy/yaml/version.rb +++ b/lib/classy/yaml/version.rb @@ -1,5 +1,5 @@ module Classy module Yaml - VERSION = '1.1' + VERSION = '1.2' end end diff --git a/test/classy/yaml_component_test.rb b/test/classy/yaml_component_test.rb index 6ac32b1..c417a13 100644 --- a/test/classy/yaml_component_test.rb +++ b/test/classy/yaml_component_test.rb @@ -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 diff --git a/test/classy/yaml_test.rb b/test/classy/yaml_test.rb index b840612..5e42019 100644 --- a/test/classy/yaml_test.rb +++ b/test/classy/yaml_test.rb @@ -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