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 +4 -4
- data/README.md +20 -0
- data/lib/generators/view_component/scoped_styles/install_generator.rb +4 -0
- data/lib/generators/view_component/scoped_styles/templates/view_component_scoped_styles.rb.tt +3 -0
- data/lib/view_component/scoped_styles/concern.rb +24 -1
- data/lib/view_component/scoped_styles/configuration.rb +6 -0
- data/lib/view_component/scoped_styles/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4518c4426e35b342ba212fb90ab4d5e4692d7427fdf5fdbe5ced923a71cc1e42
|
|
4
|
+
data.tar.gz: 601433c85c4aad6602831f8c53e662f73080d8bb40f9104f28fc8842411e0569
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
data/lib/generators/view_component/scoped_styles/templates/view_component_scoped_styles.rb.tt
CHANGED
|
@@ -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
|
-
"
|
|
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
|
|