view_component-scoped_styles 0.7.0 → 0.8.1

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: 76cfb539bacc159bd6913d32e02fd65f4bdf694e94956fdb9187b68b951badbe
4
- data.tar.gz: 6cd273fa833779ed196785c3318ffb79c3c55929c9637aa067d6d2e97a1bbb14
3
+ metadata.gz: 7a5b9375fce9459d0ff5176843540baf0af45fc19c75771f9e55a23bd400f0ad
4
+ data.tar.gz: b153dd8c88aabf25dbfdbcb1a74584f35a7c58a57b1394de77fad68765f16b18
5
5
  SHA512:
6
- metadata.gz: b7ebb5bd1bbba3ad26b14119f80d54623a0aae174956c178600c130106d23517053ab232b91ba4f516cebca07fb7c08581a34ad4cab83a6cce00c611957110a3
7
- data.tar.gz: 0caa033226552ccf3ea15d7a6d8af1240449d37a68d1c18f2cb549882c46ee42145cf0f41434e28af4fe68fe242337c3f4b75af27ee93a917412f337e1c32a98
6
+ metadata.gz: b43ff7e5452e2cddfe8e40dcabcffe184dcab1ca0d8a43bc037689b6c3b742440a3eba4805ad17727304b1e5404739638d13c45f45eed4e965bf10861a3ed2c4
7
+ data.tar.gz: c53b57921b99b62b247f080087152056dcefc9529dbe00b5c213fa5ce8511baadcd906739991fba0c72b4d96e23f2733dce637ec3084134c6e0e4112c22a50d4
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
- 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)).
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,13 +9,19 @@ 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
15
18
 
16
- ensure_stylesheet_exists!
17
-
18
- update_stylesheet_with(component_name, styles)
19
+ FileUtils.mkdir_p(File.dirname(stylesheet_path))
20
+ File.open("#{stylesheet_path}.lock", "a") do |lock|
21
+ lock.flock(File::LOCK_EX)
22
+ ensure_stylesheet_exists!
23
+ update_stylesheet_with(component_name, styles)
24
+ end
19
25
  end
20
26
 
21
27
  private
@@ -23,12 +29,12 @@ module ViewComponent
23
29
  def ensure_stylesheet_exists!
24
30
  return if File.exist?(stylesheet_path)
25
31
 
26
- FileUtils.mkdir_p(File.dirname(stylesheet_path))
27
32
  File.write(stylesheet_path, initial_content)
28
33
  end
29
34
 
30
35
  def update_stylesheet_with(component_name, styles)
31
36
  content = File.read(stylesheet_path)
37
+ content = content.sub(/\n\}\s*\z/, "\n") if config.components_layer
32
38
  updated_content = merge_styles_into(content, component_name, styles)
33
39
 
34
40
  write_atomically(updated_content)
@@ -91,11 +97,7 @@ module ViewComponent
91
97
  content
92
98
  end
93
99
 
94
- def stylesheet_path
95
- @stylesheet_path ||= begin
96
- Rails.root.join(config.assets_path, config.stylesheet_name)
97
- end
98
- end
100
+ attr_reader :stylesheet_path
99
101
 
100
102
  def config
101
103
  @config ||= ViewComponent::ScopedStyles.configuration
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ViewComponent
4
4
  module ScopedStyles
5
- VERSION = "0.7.0"
5
+ VERSION = "0.8.1"
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.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Edwards