view_component-scoped_styles 0.7.0 → 0.8.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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cbe5f5accd78d4f725e75db0267dad949175cbe53ed68bb003dccc4745c294f4
|
|
4
|
+
data.tar.gz: 2f90b7dd291d0b5b3910a8c4e5e8be1e9c5000d8a69b07315582afac30abd2df
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 89c318654efd8e5dbba628f5aade499754d32588b35fee364c7e91a45bc3fb0ae2001c1e1b3fdd390ede8890f081d060ed221551e513ee021c5639aafc708b4b
|
|
7
|
+
data.tar.gz: 0d6adc27535b6ab7219935e78964b305cc13fd5b8590a82487b9dbc2d418deaddc15d0d870cd87bf8d1a4d62e96a993437909f4fe807d0a413dda3851a5eb05d
|
data/README.md
CHANGED
|
@@ -17,6 +17,7 @@ E.g. `.button` becomes `.button_a1b2c3d4`
|
|
|
17
17
|
- [Referencing classes](#referencing-classes)
|
|
18
18
|
- [Global classes](#global-classes)
|
|
19
19
|
- [Using the scoped CSS](#using-the-scoped-css)
|
|
20
|
+
- [Multiple stylesheets](#multiple-stylesheets)
|
|
20
21
|
- [Configuration](#configuration)
|
|
21
22
|
- [Related projects](#related-projects)
|
|
22
23
|
- [Development](#development)
|
|
@@ -317,7 +318,7 @@ Regenerate the bundled stylesheet after migrating. Scoped names are content-deri
|
|
|
317
318
|
|
|
318
319
|
### Using the scoped CSS
|
|
319
320
|
|
|
320
|
-
|
|
321
|
+
Scoped CSS is compiled into `app/assets/stylesheets/components.scoped.css` by default. Components can select a different output bundle with `stylesheet`. The default stylesheet is `app/assets/stylesheets/components.scoped.css`; both the directory and filename are configurable (see [Configuration](#configuration)).
|
|
321
322
|
|
|
322
323
|
You should import this stylesheet within your app:
|
|
323
324
|
|
|
@@ -327,6 +328,38 @@ You should import this stylesheet within your app:
|
|
|
327
328
|
@import url("./components.scoped.css");
|
|
328
329
|
```
|
|
329
330
|
|
|
331
|
+
### Multiple stylesheets
|
|
332
|
+
|
|
333
|
+
Set `stylesheet` before declaring a `styles` block to choose the component's output bundle. The gem appends `.scoped.css` to the name. This also works with sidecar CSS:
|
|
334
|
+
|
|
335
|
+
```ruby
|
|
336
|
+
class Admin::ButtonComponent < ViewComponent::Base
|
|
337
|
+
include ViewComponent::ScopedStyles
|
|
338
|
+
|
|
339
|
+
stylesheet "admin"
|
|
340
|
+
|
|
341
|
+
styles do
|
|
342
|
+
".component { color: blue; }"
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
class Admin::CardComponent < ViewComponent::Base
|
|
347
|
+
include ViewComponent::ScopedStyles
|
|
348
|
+
|
|
349
|
+
stylesheet "admin"
|
|
350
|
+
end
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
Both components compile into `app/assets/stylesheets/admin.scoped.css`. Any number of components can use the same bundle name. Components without a selection use `config.stylesheet_name`. Subclasses inherit their parent's selection and can override it.
|
|
354
|
+
|
|
355
|
+
All output files use `config.assets_path` and, when configured, `config.components_layer`. Include each bundle where you need it:
|
|
356
|
+
|
|
357
|
+
```erb
|
|
358
|
+
<%= stylesheet_link_tag "admin.scoped", "data-turbo-track": "reload" %>
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Cross-component class references still work across bundles. Load both bundles when both components' styles are needed. When moving an existing component between bundles, remove its marked CSS block from the old generated file, since registration updates the selected file only.
|
|
362
|
+
|
|
330
363
|
## Configuration
|
|
331
364
|
|
|
332
365
|
Run the install generator in your Rails app:
|
|
@@ -23,6 +23,21 @@ module ViewComponent
|
|
|
23
23
|
IGNORED_CSS_CLASSES = [].freeze
|
|
24
24
|
|
|
25
25
|
class_methods do
|
|
26
|
+
# Selects an output bundle, written as <name>.scoped.css in assets_path.
|
|
27
|
+
# Subclasses inherit this choice unless they specify their own bundle.
|
|
28
|
+
def stylesheet(name = nil)
|
|
29
|
+
if name
|
|
30
|
+
@scoped_stylesheet = name.to_s
|
|
31
|
+
register_styles_if_rails_loaded
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
if defined?(@scoped_stylesheet)
|
|
35
|
+
@scoped_stylesheet
|
|
36
|
+
elsif superclass.respond_to?(:stylesheet)
|
|
37
|
+
superclass.stylesheet
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
26
41
|
# Sets which CSS class is the root for +component_class+ (no argument).
|
|
27
42
|
# Also triggers style registration in development when Rails is loaded
|
|
28
43
|
# (registration also runs via the Railtie for all styled components).
|
|
@@ -9,6 +9,9 @@ module ViewComponent
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def print!(component_class)
|
|
12
|
+
bundle = component_class.stylesheet
|
|
13
|
+
filename = bundle ? "#{bundle}.scoped.css" : config.stylesheet_name
|
|
14
|
+
@stylesheet_path = Rails.root.join(config.assets_path, filename)
|
|
12
15
|
component_name = component_class.name
|
|
13
16
|
component_class.instance_variable_get(:@component_id)
|
|
14
17
|
styles = component_class.component_styles
|
|
@@ -29,6 +32,7 @@ module ViewComponent
|
|
|
29
32
|
|
|
30
33
|
def update_stylesheet_with(component_name, styles)
|
|
31
34
|
content = File.read(stylesheet_path)
|
|
35
|
+
content = content.sub(/\n\}\s*\z/, "\n") if config.components_layer
|
|
32
36
|
updated_content = merge_styles_into(content, component_name, styles)
|
|
33
37
|
|
|
34
38
|
write_atomically(updated_content)
|
|
@@ -91,11 +95,7 @@ module ViewComponent
|
|
|
91
95
|
content
|
|
92
96
|
end
|
|
93
97
|
|
|
94
|
-
|
|
95
|
-
@stylesheet_path ||= begin
|
|
96
|
-
Rails.root.join(config.assets_path, config.stylesheet_name)
|
|
97
|
-
end
|
|
98
|
-
end
|
|
98
|
+
attr_reader :stylesheet_path
|
|
99
99
|
|
|
100
100
|
def config
|
|
101
101
|
@config ||= ViewComponent::ScopedStyles.configuration
|