Module: StyleCapsule::PhlexHelper
- Defined in:
- lib/style_capsule/phlex_helper.rb,
sig/style_capsule.rbs
Overview
Phlex helper module for StyleCapsule stylesheet registry
Include this in your base Phlex component class (e.g., ApplicationComponent):
class ApplicationComponent < Phlex::HTML
include StyleCapsule::PhlexHelper
end
Usage in Phlex layouts:
head do
end
Usage in Phlex components:
class MyComponent < ApplicationComponent
include StyleCapsule::PhlexHelper
style_capsule namespace: :user # Set default namespace for register_stylesheet
def view_template
register_stylesheet("stylesheets/user/my_component") # Uses :user namespace automatically
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) ⇒ void
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 Phlex components:
def view_template
register_stylesheet("stylesheets/user/my_component", "data-turbo-track": "reload")
register_stylesheet("stylesheets/admin/dashboard", namespace: :admin)
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.
43 44 45 46 47 48 49 |
# File 'lib/style_capsule/phlex_helper.rb', line 43 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) ⇒ void
This method returns an undefined value.
Render StyleCapsule registered stylesheets
Usage in Phlex layouts:
head do
stylesheet_registry_tags
stylesheet_registry_tags(namespace: :admin)
end
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/style_capsule/phlex_helper.rb', line 61 def (namespace: nil) output = StyleCapsule::StylesheetRegistry.render_head_stylesheets(view_context, namespace: namespace) output_string = output.to_s if respond_to?(:safe) raw(safe(output_string)) end # Phlex `raw` may return nil when writing to the buffer; callers/tests expect the HTML string. output_string end |