Module: CSSModules::ViewHelper

Defined in:
lib/css_modules/view_helper.rb

Overview

Provides helpers to the view layer. Add it to a controller with Rails’ ‘helper` method.

Examples:

including ViewHelper in ApplicationController (and therefore all its descendants)

class ApplicationController < ActionController::Base
  helper CSSModules::ViewHelper
end

Defined Under Namespace

Classes: StyleModule

Instance Method Summary collapse

Instance Method Details

#css_module(module_name) ⇒ StyleModule #css_module(module_name, selector_names, bare_selector_names) ⇒ String #css_module(module_name) {|a| ... } ⇒ StyleModule

Overloads:

  • #css_module(module_name) ⇒ StyleModule

    Apply the styles from ‘module_name` for `selector_name`

    Examples:

    Passing a module to a partial

    style_module = css_module("events_index")
    render(partial: "header", locals: { style_module: style_module })

    Parameters:

    • module_name (String)

    Returns:

    • (StyleModule)

      helper for modulizing selectors within ‘module_name`

  • #css_module(module_name, selector_names, bare_selector_names) ⇒ String

    Apply the styles from ‘module_name` for `selector_names`

    Examples:

    Getting a selector within a module

    css_module("events_index", "header")
    # => "..." (opaque string which matches the stylesheet)

    Parameters:

    • module_name (String)
    • selector_names (String)

      Space-separated DOM ids or class names

    • bare_selector_names (String)

      Space-separated selectors to be appended without the module

    Returns:

    • (String)

      modulized selector name for ‘class=` or `id=` in a view

  • #css_module(module_name) {|a| ... } ⇒ StyleModule

    Modulize selectors within a block using the yielded helper.

    Examples:

    modulizing a few selectors

    <% css_module("events_index") do |events_module| %>
      <h1 class="<%= events_module.selector("heading") %>">All events</h1>
      <p id="<%= events_module.selector("description") %>"> ... </p>
    <% end %>

    Parameters:

    • module_name (String)

    Yield Parameters:

    • a (StyleModule)

      helper for modulizing selectors within ‘module_name`

    Returns:

    • (StyleModule)

      a helper for modulizing selectors within ‘module_name`



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/css_modules/view_helper.rb', line 44

def css_module(module_name, selector_names = nil, bare_selector_names = nil, &block)
  lookup = StyleModule.new(module_name)

  if selector_names.nil? && block_given?
    yield(lookup)
    lookup
  elsif selector_names.present?
    lookup.selector(selector_names.to_s, bare_selector_names.to_s)
  else
    lookup
  end
end