Module: StyleCapsule::ViewComponentHelper
- Defined in:
- lib/style_capsule/view_component_helper.rb,
sig/style_capsule.rbs
Overview
ViewComponent helper module for StyleCapsule stylesheet registry
Include this in your base ViewComponent class (e.g., ApplicationComponent):
class ApplicationComponent < ViewComponent::Base
include StyleCapsule::ViewComponentHelper
end
Usage in ViewComponent layouts:
def call
helpers.
end
Usage in ViewComponent components:
class MyComponent < ApplicationComponent
style_capsule namespace: :user # Set default namespace for register_stylesheet
def call
register_stylesheet("stylesheets/user/my_component") # Uses :user namespace automatically
content_tag(:div, "Content")
end
end
Instance Method Summary collapse
-
#register_stylesheet(file_path, namespace: nil, **options) ⇒ void
Register a stylesheet file for head rendering.
-
#stylesheet_registry_tags(namespace: nil) ⇒ String
Render StyleCapsule registered stylesheets.
Instance Method Details
#register_stylesheet(file_path, namespace: nil, **options) ⇒ void
This method returns an undefined value.
Register a stylesheet file for head rendering
Usage in ViewComponent components:
def call
register_stylesheet("stylesheets/user/my_component", "data-turbo-track": "reload")
register_stylesheet("stylesheets/admin/dashboard", namespace: :admin)
content_tag(:div, "Content")
end
If the component has a default namespace set via style_capsule or stylesheet_registry, it will be used automatically when namespace is not explicitly provided.
42 43 44 45 46 47 48 |
# File 'lib/style_capsule/view_component_helper.rb', line 42 def register_stylesheet(file_path, namespace: nil, **) # Use component's default namespace if not explicitly provided if namespace.nil? && respond_to?(:class) && self.class.respond_to?(:stylesheet_namespace) namespace = self.class.stylesheet_namespace end StyleCapsule::StylesheetRegistry.register(file_path, namespace: namespace, **) end |
#stylesheet_registry_tags(namespace: nil) ⇒ String
Render StyleCapsule registered stylesheets
Usage in ViewComponent layouts:
def call
helpers.stylesheet_registry_tags
helpers.stylesheet_registry_tags(namespace: :admin)
end
60 61 62 |
# File 'lib/style_capsule/view_component_helper.rb', line 60 def (namespace: nil) StyleCapsule::StylesheetRegistry.render_head_stylesheets(helpers, namespace: namespace) end |