classy-yaml 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b91dad78932d510d2299faae67a08584bad33501c3a048435603a6e673049090
4
- data.tar.gz: c3d9d4e2c543e3f3b70b94260f2293d5d3f6a24474e228690aa5c97f5a80666f
3
+ metadata.gz: 352e03027c4cfd4154d22f4d26aa5cc0faa040a9815476505a3167487adcd0ab
4
+ data.tar.gz: 5f80b9e47137978a7d252f6ff8a83a6d43cbaa658d424af5c8bab962b8c1fc56
5
5
  SHA512:
6
- metadata.gz: 1bc0b46c0d2f9fb2a49eedada6e497c7aebf14bc6f95f75609461620f44d5df12066dd8437fe8be90240053670dad19bf6cf1a80a1d8a3f08f0a54ea8e1ce6ad
7
- data.tar.gz: bcf8f2d58c819e91a935076eb920094e27b2855e30d13038fd26ad3942447d1c5c14f646427e18d3787a6c39774da89bc63443995cf0373935eceb28db0186b4
6
+ metadata.gz: 243dca26cd467edbbc0d1df13fbdd297491015779cdab12f8c339bec63186cb234957a88d9b00e341fd83b6f96994a4f9761716d9b9aaf25992020bc561a1aef
7
+ data.tar.gz: b2e9b69ae589341503b21ce6f57b999029ebebaf77ef5b794c9759a93a37efdf5d7a98fd416d76dd6a3b4e41511c37d453994134d01e3794d9c40b2edc7dd270
data/README.md CHANGED
@@ -1,72 +1,119 @@
1
1
  # Classy::Yaml
2
- This was created to provide convenient utility class grouping for environments without a bundler (or any situation where you might not want to add custom css classes). YAML files are a perfect structure for these situations.
2
+ This was created to provide convenient utility class grouping for environments without a bundler (or any situation where you might not want to add custom css classes).
3
3
 
4
4
  ## Usage
5
5
  After installing the gem, the helper method `yass` will be available from anywhere. It looks for the definitions in the YAML file `config/utility_classes.yml` (note: you must add this yourself).
6
6
 
7
- The YAML structure should follow this format:
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
8
10
 
11
+ ```ruby
12
+ gem "classy-yaml"
9
13
  ```
10
- btn:
11
- blue: "text-blue-200 bg-blue-500"
12
- yellow: "text-yellow-200 bg-blue-500"
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle install
13
18
  ```
14
19
 
15
- Then, you can add these classes to any element by calling `yass(btn: :blue)` or `yass(btn: :yellow)`.
20
+ Or install it yourself as:
21
+ ```bash
22
+ $ gem install classy-yaml
23
+ ```
16
24
 
17
- You may wonder though, what about shared classes you want all nested definitions to inherit? There is a special syntax for this type of inheritance:
25
+ ## Usage
18
26
 
27
+ ### Default Settings
28
+
29
+ You can override any of the following settings in an initializer such as `config/initializers/classy_yaml.rb`:
30
+
31
+ ```ruby
32
+ Classy::Yaml.setup do |config|
33
+ config.default_file = "config/utility_classes.yml"
34
+ config.extra_files = []
35
+ config.override_tag_helpers = false # Process all ERB tag helper class: attribute through `yass` when it is a non-string
36
+ end
19
37
  ```
20
- btn:
21
- base: "px-3 py-2"
22
- blue: "text-blue-200 bg-blue-500"
23
- yellow: "text-yellow-200 bg-blue-500"
24
- ```
25
38
 
26
- Now, calling `yass(btn: :blue)` or `yass(btn: :yellow)` will ALSO pull in the classes from `btn: :base`.
39
+ ### YAML Configuration
40
+
41
+ Create your utility classes YAML file (`config/utility_classes.yml`):
27
42
 
28
- ### Optionally Skipping Base
43
+ ```yaml
44
+ btn:
45
+ base: "px-4 py-2 rounded font-medium" # Base class is automatically applied when referencing a sibling of base
46
+ primary: "bg-blue-500 text-white hover:bg-blue-600"
47
+ secondary: "bg-gray-200 text-gray-800 hover:bg-gray-300"
48
+ danger: "bg-red-500 text-white hover:bg-red-600"
29
49
 
30
- You can optionally skip including the base on a `yass` call by including the key/value `skip_base: true`. So, using the example above,
31
- we can perform:
50
+ card:
51
+ compact: "m-4"
32
52
  ```
53
+
54
+ You can also define classes as YAML arrays
55
+
56
+ ```yaml
33
57
  btn:
34
- base: "px-3 py-2"
35
- blue: "text-blue-200 bg-blue-500"
36
- yellow: "text-yellow-200 bg-blue-500"
58
+ base: "px-4 py-2 rounded font-medium" # Base class is automatically applied when referencing a sibling of base
59
+ primary: "bg-blue-500 text-white hover:bg-blue-600"
60
+ secondary: "bg-gray-200 text-gray-800 hover:bg-gray-300"
61
+ danger: "bg-red-500 text-white hover:bg-red-600"
62
+ warning: # Hideous example
63
+ - "bg-yellow-200"
64
+ - "text-gray-100"
37
65
  ```
38
66
 
39
- Now, calling `yass(btn: :blue, skip_base: true)` and this will skip pulling in the classes from `btn: :base`. This is helpful
40
- when defining animation classes and you only want to include the different classes, such as `active` and `inactive` for instance.
67
+ > [!WARNING]
68
+ > If using tailwind, be sure to point the tailwind configuration to your yaml files for parsing
41
69
 
42
- ### ViewComponent
43
- 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`.
70
+ ### Using the Helper
44
71
 
45
- So, it may look like this
72
+ In your views or components:
46
73
 
47
- ```
48
- # config/utility_classes.yml
49
- btn:
50
- base: "px-3 py-2"
51
- blue: "text-blue-200 bg-blue-500"
52
- yellow: "text-yellow-200 bg-blue-500"
53
-
54
-
55
- # app/components/example_component/example_component.yml
56
- btn:
57
- purple: "text-purple-200 bg-blue-500"
58
-
74
+ ```erb
75
+ <!-- Basic usage -->
76
+ <button class="<%= yass(btn: :primary) %>">
77
+ Click me
78
+ </button>
59
79
 
60
- # app/components/example_component/example_component.html.erb
80
+ <!-- <button class="px-4 py-2 rounded font-medium bg-blue-500 text-white hover:bg-blue-600">Click me</button> -->
81
+
82
+ <!-- Multiple classes -->
83
+ <button class="<%= yass(btn: :primary, card: :compact) %>">
84
+ Button in card
85
+ </button>
86
+
87
+ <!-- <button class="px-4 py-2 rounded font-medium bg-blue-500 text-white hover:bg-blue-600 m-4">Click me</button> -->
88
+
89
+ <!-- Skip base classes -->
90
+ <button class="<%= yass(btn: :primary, skip_base: true) %>">
91
+ Button without base
92
+ </button>
61
93
 
62
- <button class="<%=yass(btn: :purple)%>">Classy Button</button>
94
+ <!-- <button class="bg-blue-500 text-white hover:bg-blue-600">Click me</button> -->
63
95
 
64
- # will add the classes "px-3 py-2 text-purple-200 bg-blue-500"
96
+ <!-- With config.override_tag_helpers = true -->
97
+ <%= tag.div class: { btn: :primary, card: :compact } do %>
98
+ Automatic class processing
99
+ <% end %>
100
+
101
+ <div class="px-4 py-2 rounded font-medium bg-blue-500 text-white hover:bg-blue-600 m-4"></div>
65
102
  ```
66
103
 
67
- As you can see, this will merge definitions found in the `ExampleComponent`'s sidecar asset with the main application's definitions. Not only will it merge, but it will overwrite them as well, allowing for true customization on a per-component basis:
104
+ ### ViewComponent Integration
105
+
106
+ Include the component helpers in your ViewComponent:
68
107
 
108
+ ```ruby
109
+ class ExampleComponent < ViewComponent::Base
110
+ include Classy::Yaml::ComponentHelpers
111
+ end
69
112
  ```
113
+
114
+ This will automatically look for a sidecar file `example_component.yml` file alongside your component. For example:
115
+
116
+ ```yaml
70
117
  # config/utility_classes.yml
71
118
  btn:
72
119
  base: "px-3 py-2"
@@ -76,73 +123,38 @@ btn:
76
123
 
77
124
  # app/components/example_component/example_component.yml
78
125
  btn:
79
- blue: "text-blue-50"
80
-
126
+ purple: "text-purple-200 bg-blue-500"
127
+
81
128
 
82
129
  # app/components/example_component/example_component.html.erb
83
130
 
84
- <button class="<%=yass(btn: :purple)%>">Classy Button</button>
131
+ <button class="<%= yass(btn: :purple) %>">Classy Button</button>
85
132
 
86
- # will add the classes "px-3 py-2 text-blue-50"
133
+ # <button class="px-3 py-2 text-purple-200 bg-blue-500">Click me</button>
87
134
  ```
88
135
 
89
- yass(:button, :primary) # => "btn btn-primary"
90
- yass(:button, :secondary) # => "btn btn-secondary"
91
- yass(:button, :large) # => "btn btn-lg"
92
- yass(:button, :primary, :large) # => "btn btn-primary btn-lg"
93
-
94
- ### Array style definition
136
+ ## Tailwind Merge Integration
95
137
 
96
- ```
97
- array:
98
- - "px-3 py-2"
99
- - "bg-gray"
100
- - "text-purple"
101
- ```
138
+ Classy YAML detects if [tailwind_merge](https://github.com/gjtorikian/tailwind_merge) is installed and will leverage it when fetching classes with `yass`. Please read their documentation for installation and benefits.
102
139
 
103
- yass(:array) # => "px-3 py-2 bg-gray text-purple"
104
140
 
105
- ## Configuration
106
- You can configure the gem by creating an initializer in your Rails app. The following options are available:
141
+ ## Configuration Options
107
142
 
108
- ```ruby
109
- Classy::Yaml.setup do |config|
110
- # The default path for the utility classes YAML file
111
- config.default_path = "config/utility_classes.yml"
112
-
113
- # Any extra files you want to load in addition to the default file. The last file loaded will overwrite any previous definitions
114
- config.extra_files = [
115
- "app/yamls/extra_styles.yml"
116
- ]
117
- end
118
- ```
143
+ | Option | Default | Description |
144
+ |--------|---------|-------------|
145
+ | `default_file` | `"config/utility_classes.yml"` | Path to the main YAML file |
146
+ | `extra_files` | `[]` | Array of additional YAML files (highest priority) |
147
+ | `engine_files` | `[]` | Array of engine YAML files (lowest priority) |
148
+ | `override_tag_helpers` | `false` | Automatically process class symbols/hashes in Rails tag helpers |
119
149
 
120
- ## Installation
121
- Add this line to your application's Gemfile:
150
+ ## Contributing
122
151
 
123
- ```ruby
124
- gem 'classy-yaml'
125
- ```
152
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Tonksthebear/classy-yaml. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
126
153
 
127
- And then execute:
128
- ```bash
129
- $ bundle
130
- ```
154
+ ## License
131
155
 
132
- Or install it yourself as:
133
- ```bash
134
- $ gem install classy-yaml
135
- ```
156
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
136
157
 
137
- If using purging for styles, be sure to add the YAML files to the purge directories. For example, below is a tailwind.config.js:
138
- ```
139
- purge: [
140
- "./config/utility_classes.yml",
141
- "./app/components/**/*.yml" // If using view component sidecar assets
142
- ]
143
- ```
144
- ## Contributing
145
- This is my first attempt at an actual gem usable for all. Any and all suggestions are absolutely welcome. I'm not the cleanest programmer, so code quality suggestions are welcome too 👍 I extracted this logic from my private work and I felt it was useful enough to be worth publishing.
158
+ ## Code of Conduct
146
159
 
147
- ## License
148
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
160
+ Everyone interacting in the Classy::Yaml project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
@@ -34,11 +34,39 @@ module Classy
34
34
  keys, classes = flatten_args(values: args)
35
35
  classes += fetch_classes(keys, classy_yamls: classy_yamls, skip_base: skip_base_hash[:skip_base])
36
36
 
37
- classes.flatten.uniq.join(" ")
37
+ # Use tailwind_merge if available, otherwise fall back to simple join
38
+ merge_classes(classes.flatten.uniq)
38
39
  end
39
40
 
40
41
  private
41
42
 
43
+ # Merges CSS classes using tailwind_merge if available, otherwise uses simple join
44
+ # @param classes [Array] Array of CSS class strings
45
+ # @return [String] Merged CSS classes
46
+ def merge_classes(classes)
47
+ return "" if classes.blank?
48
+
49
+ if tailwind_merge_available?
50
+ # Use tailwind_merge for intelligent class merging
51
+ TailwindMerge::Merger.new.merge(classes)
52
+ else
53
+ # Fall back to simple space-joined classes
54
+ classes.join(" ")
55
+ end
56
+ end
57
+
58
+ # Checks if tailwind_merge gem is available
59
+ # @return [Boolean] True if tailwind_merge is available
60
+ def tailwind_merge_available?
61
+ return @tailwind_merge_available if defined?(@tailwind_merge_available)
62
+ @tailwind_merge_available = begin
63
+ require "tailwind_merge"
64
+ true
65
+ rescue LoadError
66
+ false
67
+ end
68
+ end
69
+
42
70
  # Loads a YAML file and adds its contents to the classy_yamls array
43
71
  # @param file_path [String, Pathname] Path to the YAML file
44
72
  # @param classy_yamls [Array] Array to add the parsed YAML to
@@ -1,5 +1,5 @@
1
1
  module Classy
2
2
  module Yaml
3
- VERSION = "1.5.0"
3
+ VERSION = "1.6.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classy-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tonksthebear
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tailwind_merge
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: view_component
57
71
  requirement: !ruby/object:Gem::Requirement