Module: Barnardos::RubyDesignSystem::ComponentHelper

Included in:
BarnardosDesignSystemHelper
Defined in:
lib/barnardos/ruby_design_system/component_helper.rb

Overview

Provides helper methods available in the host app’s server.

Usage: Add the following to /app/helpers/application_helper.rb within the module ApplicationHelper:

include Barnardos::RubyDesignSystem::ComponentHelper

Instance Method Summary collapse

Instance Method Details

#fieldset(legend: nil, hint: nil) ⇒ String

Creates a FieldSet Component around passed in block. Also adds Fieldset-children inner wrapper around block

Parameters:

  • legend (String) (defaults to: nil)

    outputs into a Fieldset-legend component within the Fieldset if set

  • hint (String) (defaults to: nil)

    outputs into a Hint component within the Fieldset if set

Returns:

  • (String)

    HTML containing the passed in block wrapped by a Fieldset Component and sub components



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/barnardos/ruby_design_system/component_helper.rb', line 55

def fieldset(legend: nil, hint: nil)
   :fieldset, class: 'Fieldset' do
    concat((:legend, legend, class: 'Fieldset-legend')) if legend
    concat(
      (:div, class: 'Fieldset-children') do
        concat((:p, hint, class: 'Hint')) if hint
        concat(yield)
      end
    )
  end
end

Creates a Link Component

All parameters are as link_to see Rails API

Returns:

  • (String)

    HTML anchor with class Link



37
38
39
40
# File 'lib/barnardos/ruby_design_system/component_helper.rb', line 37

def link(name = nil, options = nil, html_options = {}, &block)
  html_options[:class] = [html_options[:class], 'Link'].flatten.compact
  link_to(name, options, html_options, &block)
end

Overrides the Rails helper method link_to Adds the html_options option new_window which changes the link so that it will link to a new window

Returns:

  • (String)

    HTML anchor with target set as _blank if html_options include ‘new_window: true`



45
46
47
48
# File 'lib/barnardos/ruby_design_system/component_helper.rb', line 45

def link_to(name = nil, options = nil, html_options = {}, &block)
  html_options[:target] = '_blank' if html_options.delete(:new_window)
  super
end

Creates a ProminentLink Component.

Parameters:

  • name (String) (defaults to: nil)

    Link text that appears on page

  • options (defaults to: nil)

    Same as link_to see Rails API

  • html_options (Hash) (defaults to: {})

    Also same as link_to with additional option colour which sets the ProminentLink colour

Returns:

  • (String)

    HTML anchor with class ProminentLink and optionally ‘ProminentLink–<colour>` if colour entered



17
18
19
20
21
22
# File 'lib/barnardos/ruby_design_system/component_helper.rb', line 17

def prominent_link(name = nil, options = nil, html_options = {}, &block)
  colour = html_options.delete(:colour)
  colour_class = colour && "ProminentLink--#{colour}"
  html_options[:class] = [html_options[:class], 'ProminentLink', colour_class].flatten.compact
  link_to(name, options, html_options, &block)
end

Creates a SkipLink Component

Parameters:

  • text (String)

    Link text that appears on page

  • anchor (String) (defaults to: '#main-anchor')

    the anchor name that is the target for the skip link

Returns:

  • (String)

    HTML anchor with class SkipLink



29
30
31
# File 'lib/barnardos/ruby_design_system/component_helper.rb', line 29

def skip_link(text, anchor: '#main-anchor')
  link_to text, anchor, class: 'SkipLink'
end