view_component-scoped_styles 0.2.0 → 0.4.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 +30 -2
- data/lib/generators/view_component/scoped_styles/install_generator.rb +18 -2
- data/lib/generators/view_component/scoped_styles/templates/view_component_scoped_styles.rb.tt +10 -1
- data/lib/view_component/scoped_styles/concern.rb +24 -1
- data/lib/view_component/scoped_styles/configuration.rb +21 -2
- data/lib/view_component/scoped_styles/stylist/writer.rb +9 -3
- 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: c2f015fce0d3e52a95641a3a394e7d348c90db92e29b0d023f7a26990806a544
|
|
4
|
+
data.tar.gz: a856272767f5b7d5a882d6e8576bb0fe5195d94cacc3b11220f718cbdfa9123c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b2bb1e1106aa4fdb8c0d44df137c60d21680c050f9a9a9eef0f9b64bb6c05509f6a4f40bc40f9708806833d7a7d757317299417b64c7fb4834bbd8b98d725a32
|
|
7
|
+
data.tar.gz: 9f78813f81df45c463411cb5f3b1079b84c8f3745fbc870fd84e8512e1692eb71f98523a526bff2ad55964c26f2d190eccb57cbfc38d6ffea2da7a337d0a3620
|
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:
|
|
@@ -190,7 +206,7 @@ or via the `component_class` helper:
|
|
|
190
206
|
|
|
191
207
|
### Using the scoped CSS
|
|
192
208
|
|
|
193
|
-
All scoped CSS will be compiled into `app/assets/stylesheets/components.scoped.css
|
|
209
|
+
All scoped CSS will be compiled into a single bundled stylesheet. By default that is `app/assets/stylesheets/components.scoped.css`; both the directory and filename are configurable (see [Configuration](#configuration)).
|
|
194
210
|
|
|
195
211
|
You should import this stylesheet within your app:
|
|
196
212
|
|
|
@@ -217,15 +233,27 @@ ViewComponent::ScopedStyles.configure do |config|
|
|
|
217
233
|
# Where ViewComponent classes live (relative to Rails.root). Default: "app/components"
|
|
218
234
|
config.components_path = File.join("app", "components")
|
|
219
235
|
|
|
220
|
-
#
|
|
236
|
+
# Where the bundled scoped stylesheet is written (relative to Rails.root). Default: "app/assets/stylesheets"
|
|
237
|
+
config.assets_path = File.join("app", "assets", "stylesheets")
|
|
238
|
+
|
|
239
|
+
# Filename of the bundled scoped stylesheet. Default: "components.scoped.css"
|
|
240
|
+
config.stylesheet_name = "components.scoped.css"
|
|
241
|
+
|
|
242
|
+
# Optional @layer name for the bundled scoped stylesheet (e.g. "components"). Default: nil.
|
|
221
243
|
config.components_layer = nil
|
|
244
|
+
|
|
245
|
+
# Prefix for scoped class names (e.g. "c-" produces "c-a1b2c3d4"). Default: "c-"
|
|
246
|
+
config.css_class_prefix = "c-"
|
|
222
247
|
end
|
|
223
248
|
```
|
|
224
249
|
|
|
225
250
|
| Option | Default | Description |
|
|
226
251
|
| --- | --- | --- |
|
|
227
252
|
| `components_path` | `"app/components"` | Where ViewComponent classes live, relative to `Rails.root`. |
|
|
253
|
+
| `assets_path` | `"app/assets/stylesheets"` | Directory where the bundled scoped stylesheet is written, relative to `Rails.root`. |
|
|
254
|
+
| `stylesheet_name` | `"components.scoped.css"` | Filename of the bundled scoped stylesheet within `assets_path`. |
|
|
228
255
|
| `components_layer` | `nil` | When set, wraps generated CSS in `@layer <name> { ... }` for cascade control. |
|
|
256
|
+
| `css_class_prefix` | `"c-"` | Prefix prepended to scoped class names (e.g. `"vc-"` → `"vc-a1b2c3d4"`). |
|
|
229
257
|
|
|
230
258
|
## Related projects
|
|
231
259
|
|
|
@@ -27,13 +27,29 @@ module ViewComponent
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def components_path_expression
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
path_expression_for(configuration_defaults.components_path)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def assets_path_expression
|
|
34
|
+
path_expression_for(configuration_defaults.assets_path)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def stylesheet_name_value
|
|
38
|
+
configuration_defaults.stylesheet_name.inspect
|
|
32
39
|
end
|
|
33
40
|
|
|
34
41
|
def components_layer_value
|
|
35
42
|
configuration_defaults.components_layer.inspect
|
|
36
43
|
end
|
|
44
|
+
|
|
45
|
+
def css_class_prefix_value
|
|
46
|
+
configuration_defaults.css_class_prefix.inspect
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def path_expression_for(path)
|
|
50
|
+
segments = Pathname(path).each_filename.to_a
|
|
51
|
+
"File.join(#{segments.map { |segment| %("#{segment}") }.join(", ")})"
|
|
52
|
+
end
|
|
37
53
|
end
|
|
38
54
|
end
|
|
39
55
|
end
|
data/lib/generators/view_component/scoped_styles/templates/view_component_scoped_styles.rb.tt
CHANGED
|
@@ -4,6 +4,15 @@ ViewComponent::ScopedStyles.configure do |config|
|
|
|
4
4
|
# Where ViewComponent classes live (relative to Rails.root). Default: "app/components"
|
|
5
5
|
config.components_path = <%= components_path_expression %>
|
|
6
6
|
|
|
7
|
-
#
|
|
7
|
+
# Where the bundled scoped stylesheet is written (relative to Rails.root). Default: "app/assets/stylesheets"
|
|
8
|
+
config.assets_path = <%= assets_path_expression %>
|
|
9
|
+
|
|
10
|
+
# Filename of the bundled scoped stylesheet. Default: "components.scoped.css"
|
|
11
|
+
config.stylesheet_name = <%= stylesheet_name_value %>
|
|
12
|
+
|
|
13
|
+
# Optional @layer name for the bundled scoped stylesheet (e.g. "components"). Default: nil.
|
|
8
14
|
config.components_layer = <%= components_layer_value %>
|
|
15
|
+
|
|
16
|
+
# Prefix for scoped class names (e.g. "c-" produces "c-a1b2c3d4"). Default: "c-"
|
|
17
|
+
config.css_class_prefix = <%= css_class_prefix_value %>
|
|
9
18
|
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
|
|
@@ -8,6 +8,8 @@ module ViewComponent
|
|
|
8
8
|
#
|
|
9
9
|
# ViewComponent::ScopedStyles.configure do |config|
|
|
10
10
|
# config.components_path = File.join("app", "view_components")
|
|
11
|
+
# config.assets_path = File.join("app", "assets", "stylesheets")
|
|
12
|
+
# config.stylesheet_name = "components.scoped.css"
|
|
11
13
|
# config.components_layer = "components"
|
|
12
14
|
# end
|
|
13
15
|
class Configuration
|
|
@@ -16,8 +18,17 @@ module ViewComponent
|
|
|
16
18
|
# @return [String] default: +"app/components"+
|
|
17
19
|
attr_accessor :components_path
|
|
18
20
|
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
+
# Directory where the bundled scoped stylesheet is written, relative to {Rails.root}.
|
|
22
|
+
#
|
|
23
|
+
# @return [String] default: +"app/assets/stylesheets"+
|
|
24
|
+
attr_accessor :assets_path
|
|
25
|
+
|
|
26
|
+
# Filename of the bundled scoped stylesheet within {assets_path}.
|
|
27
|
+
#
|
|
28
|
+
# @return [String] default: +"components.scoped.css"+
|
|
29
|
+
attr_accessor :stylesheet_name
|
|
30
|
+
|
|
31
|
+
# Optional CSS cascade layer name for the bundled scoped stylesheet.
|
|
21
32
|
#
|
|
22
33
|
# When set, the bundled stylesheet is wrapped in +@layer <name> { ... }+ so
|
|
23
34
|
# you can control specificity relative to other layers in your app.
|
|
@@ -25,9 +36,17 @@ module ViewComponent
|
|
|
25
36
|
# @return [String, nil] default: +nil+ (no layer wrapper)
|
|
26
37
|
attr_accessor :components_layer
|
|
27
38
|
|
|
39
|
+
# Prefix prepended to scoped class names (e.g. +"c-"+ → +"c-a1b2c3d4"+).
|
|
40
|
+
#
|
|
41
|
+
# @return [String] default: +"c-"+
|
|
42
|
+
attr_accessor :css_class_prefix
|
|
43
|
+
|
|
28
44
|
def initialize
|
|
29
45
|
@components_path = File.join("app", "components")
|
|
46
|
+
@assets_path = File.join("app", "assets", "stylesheets")
|
|
47
|
+
@stylesheet_name = "components.scoped.css"
|
|
30
48
|
@components_layer = nil
|
|
49
|
+
@css_class_prefix = "c-"
|
|
31
50
|
end
|
|
32
51
|
end
|
|
33
52
|
|
|
@@ -48,7 +48,7 @@ module ViewComponent
|
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
def write_atomically(content)
|
|
51
|
-
if
|
|
51
|
+
if config.components_layer
|
|
52
52
|
content = content.rstrip + "\n}\n"
|
|
53
53
|
end
|
|
54
54
|
|
|
@@ -84,7 +84,7 @@ module ViewComponent
|
|
|
84
84
|
def initial_content
|
|
85
85
|
content = "/* Generated by ViewComponent::ScopedStyles - Do not edit manually */\n"
|
|
86
86
|
|
|
87
|
-
if (layer =
|
|
87
|
+
if (layer = config.components_layer)
|
|
88
88
|
content += "@layer #{layer} {\n"
|
|
89
89
|
end
|
|
90
90
|
|
|
@@ -92,7 +92,13 @@ module ViewComponent
|
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
def stylesheet_path
|
|
95
|
-
@stylesheet_path ||=
|
|
95
|
+
@stylesheet_path ||= begin
|
|
96
|
+
Rails.root.join(config.assets_path, config.stylesheet_name)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def config
|
|
101
|
+
@config ||= ViewComponent::ScopedStyles.configuration
|
|
96
102
|
end
|
|
97
103
|
end
|
|
98
104
|
end
|