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.stylesheet_registry_tags
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
  (:div, "Content")
end
end

Instance Method Summary collapse

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.

Parameters:

  • file_path (String)

    Path to stylesheet (relative to app/assets/stylesheets)

  • namespace (Symbol, String, nil) (defaults to: nil)

    Optional namespace for separation (nil/blank uses component's default or global default)

  • options (Hash)

    Options for stylesheet_link_tag

  • namespace: (Symbol, String, nil) (defaults to: nil)


42
43
44
45
46
47
48
# File 'lib/style_capsule/view_component_helper.rb', line 42

def register_stylesheet(file_path, namespace: nil, **options)
  # 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, **options)
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

Parameters:

  • namespace (Symbol, String, nil) (defaults to: nil)

    Optional namespace to render (nil/blank renders all)

  • namespace: (Symbol, String, nil) (defaults to: nil)

Returns:

  • (String)

    HTML-safe string with stylesheet tags



60
61
62
# File 'lib/style_capsule/view_component_helper.rb', line 60

def stylesheet_registry_tags(namespace: nil)
  StyleCapsule::StylesheetRegistry.render_head_stylesheets(helpers, namespace: namespace)
end