view_component-scoped_styles 0.2.0 → 0.3.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: 65712096bb4c02068da2dca36b66bad2d4dffc773234d42910dcadc5ef354a21
4
- data.tar.gz: 12d6dc8afdc1b530a749810a3fb32b13545c6a3421dd8e21c7c8f85c76b1539d
3
+ metadata.gz: 4518c4426e35b342ba212fb90ab4d5e4692d7427fdf5fdbe5ced923a71cc1e42
4
+ data.tar.gz: 601433c85c4aad6602831f8c53e662f73080d8bb40f9104f28fc8842411e0569
5
5
  SHA512:
6
- metadata.gz: 6c7b9ede8f02445070c51dbe34e935ff43e9cbc60c3594d270915454cb6e583b02748b21b1531209fdef17b99d5654815333997fb6d3bd9fe5f46336957899ba
7
- data.tar.gz: 239fe37c5445b1aac40e589da187643a37c66d9b8cdf0f9ffbaae51049d8778bea1a86cab5fa38307a3022070140df2e39d393b76f266a97434cb2ecfbe6b106
6
+ metadata.gz: 8e9a1ab80bf24d000347be17e22d19e684a1ec1f70a08cf95952618c20891b84b8c50d34ab3f08e595c622659d77f5aa423eb5f2f11ebe0213aefa9af75cd9dc
7
+ data.tar.gz: 3846bc856b99e9be1f416e6616e33e4f0fa9388ddd7a253c714bc56198218543a68e17d5872a3aa1dbe92993691adf4257068f4f6e512aede4015d6c6296bdf1
data/README.md CHANGED
@@ -161,6 +161,22 @@ end
161
161
  </div>
162
162
  ```
163
163
 
164
+ Scoped class names are prefixed by default (e.g. `c-a1b2c3d4`). Set a global prefix in configuration, or override per component with `css_class_prefix`:
165
+
166
+ ```ruby
167
+ class ExampleComponent < ViewComponent::Base
168
+ include ViewComponent::ScopedStyles
169
+
170
+ css_class_prefix "vc-"
171
+
172
+ styles do
173
+ <<~CSS
174
+ .component { ... } # becomes .vc-a1b2c3d4 in components.scoped.css
175
+ CSS
176
+ end
177
+ end
178
+ ```
179
+
164
180
  ### Ignoring classes
165
181
 
166
182
  Ignored classes are left unchanged in generated CSS:
@@ -219,6 +235,9 @@ ViewComponent::ScopedStyles.configure do |config|
219
235
 
220
236
  # Optional @layer name for components.scoped.css (e.g. "components"). Default: nil.
221
237
  config.components_layer = nil
238
+
239
+ # Prefix for scoped class names (e.g. "c-" produces "c-a1b2c3d4"). Default: "c-"
240
+ config.css_class_prefix = "c-"
222
241
  end
223
242
  ```
224
243
 
@@ -226,6 +245,7 @@ end
226
245
  | --- | --- | --- |
227
246
  | `components_path` | `"app/components"` | Where ViewComponent classes live, relative to `Rails.root`. |
228
247
  | `components_layer` | `nil` | When set, wraps generated CSS in `@layer <name> { ... }` for cascade control. |
248
+ | `css_class_prefix` | `"c-"` | Prefix prepended to scoped class names (e.g. `"vc-"` → `"vc-a1b2c3d4"`). |
229
249
 
230
250
  ## Related projects
231
251
 
@@ -34,6 +34,10 @@ module ViewComponent
34
34
  def components_layer_value
35
35
  configuration_defaults.components_layer.inspect
36
36
  end
37
+
38
+ def css_class_prefix_value
39
+ configuration_defaults.css_class_prefix.inspect
40
+ end
37
41
  end
38
42
  end
39
43
  end
@@ -6,4 +6,7 @@ ViewComponent::ScopedStyles.configure do |config|
6
6
 
7
7
  # Optional @layer name for components.scoped.css (e.g. "components"). Default: nil.
8
8
  config.components_layer = <%= components_layer_value %>
9
+
10
+ # Prefix for scoped class names (e.g. "c-" produces "c-a1b2c3d4"). Default: "c-"
11
+ config.css_class_prefix = <%= css_class_prefix_value %>
9
12
  end
@@ -52,6 +52,21 @@ module ViewComponent
52
52
  register_styles_if_rails_loaded
53
53
  end
54
54
 
55
+ # Sets the prefix for scoped class names on this component (e.g. +"vc-"+ → +"vc-a1b2c3d4"+).
56
+ #
57
+ # Overrides {ViewComponent::ScopedStyles.configuration}.css_class_prefix for this component.
58
+ #
59
+ # Clears cached generated styles when +prefix+ changes.
60
+ #
61
+ # @param prefix [String, nil] prefix without a hash suffix (e.g. +"c-"+, +"vc-"+)
62
+ def css_class_prefix(prefix = nil)
63
+ if prefix
64
+ const_set(:CSS_CLASS_PREFIX, prefix.to_s)
65
+ clear_component_style_cache
66
+ end
67
+ register_styles_if_rails_loaded
68
+ end
69
+
55
70
  # Returns processed CSS with scoped class selectors, or +nil+ if none.
56
71
  def component_styles
57
72
  return @component_styles if defined?(@component_styles)
@@ -160,7 +175,15 @@ module ViewComponent
160
175
  input = is_primary ? styles_content : "#{styles_content}:#{css_class}"
161
176
  hash = ::Digest::MD5.hexdigest(input)[0..7]
162
177
 
163
- "c-#{hash}"
178
+ "#{scoped_css_class_prefix}#{hash}"
179
+ end
180
+
181
+ def scoped_css_class_prefix
182
+ if const_defined?(:CSS_CLASS_PREFIX, false)
183
+ self::CSS_CLASS_PREFIX
184
+ else
185
+ ViewComponent::ScopedStyles.configuration.css_class_prefix
186
+ end
164
187
  end
165
188
 
166
189
  def clear_component_style_cache
@@ -25,9 +25,15 @@ module ViewComponent
25
25
  # @return [String, nil] default: +nil+ (no layer wrapper)
26
26
  attr_accessor :components_layer
27
27
 
28
+ # Prefix prepended to scoped class names (e.g. +"c-"+ → +"c-a1b2c3d4"+).
29
+ #
30
+ # @return [String] default: +"c-"+
31
+ attr_accessor :css_class_prefix
32
+
28
33
  def initialize
29
34
  @components_path = File.join("app", "components")
30
35
  @components_layer = nil
36
+ @css_class_prefix = "c-"
31
37
  end
32
38
  end
33
39
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ViewComponent
4
4
  module ScopedStyles
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component-scoped_styles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Edwards