Module: ViewComponent::ScopedStyles

Extended by:
ActiveSupport::Concern
Defined in:
lib/view_component/scoped_styles.rb,
lib/view_component/scoped_styles/concern.rb,
lib/view_component/scoped_styles/railtie.rb,
lib/view_component/scoped_styles/stylist.rb,
lib/view_component/scoped_styles/version.rb,
lib/view_component/scoped_styles/configuration.rb,
lib/view_component/scoped_styles/stylist/writer.rb,
lib/generators/view_component/scoped_styles/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Configuration, Railtie, Stylist

Constant Summary collapse

CACHED_VARIABLES =
%i[@component_styles @component_id @component_class_map].freeze
CLASS_SELECTOR_PATTERN =
/\.([a-zA-Z_][\w-]*)\b/
COMPONENT_CSS_CLASS =

Default root class for component_class when it matches a selector in the CSS.

"component".freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationConfiguration

Returns the global configuration object, creating it on first access.

Returns:



38
39
40
# File 'lib/view_component/scoped_styles/configuration.rb', line 38

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ void

This method returns an undefined value.

Yields the global configuration for block-style setup.

Yield Parameters:



46
# File 'lib/view_component/scoped_styles/configuration.rb', line 46

def configure = yield(configuration)

Instance Method Details

#component_class(name = nil) ⇒ Object

Scoped CSS class for a selector (e.g. “c-99d08d5a”).

With no argument, returns the scoped root class (COMPONENT_CSS_CLASS when it appears in the CSS, otherwise the first class in the stylesheet).

Parameters:

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

    CSS class without a leading dot (e.g. “input-box”)



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/view_component/scoped_styles/concern.rb', line 139

def component_class(name = nil)
  return nil unless component_has_styles? || component_has_stylesheet?

  self.class.component_styles

  if name
    class_map = self.class.instance_variable_get(:@component_class_map)
    class_map[name.to_s.delete_prefix(".")]
  else
    self.class.instance_variable_get(:@component_id)
  end
end