Class: Bulmacomp::BreadcrumbComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/bulmacomp/breadcrumb_component.rb

Overview

Make an html structure for a bulma breadcrumb

Examples:

Empty breadcrumb:

render Bulmacomp::BreadcrumbComponent.new()

<nav class="breadcrumb" aria-label="breadcrumbs">
  <ul>
  </ul>
</nav>

Breadcrumb with elements:

elements = [link_to('one','#'), link_to('two','#')]
render Bulmacomp::BreadcrumbComponent.new(elements: elements)
<nav class="breadcrumb" aria-label="breadcrumbs">
  <ul>
    <li><a href="#">one</a></li>
    <li><a href="#">two</a></li>
  </ul>
</nav>

Breadcrump with yield:

render Bulmacomp::BreadcrumbComponent.new() do
   <li><a href="#">Bulma</a></li>
end

<nav class="breadcrumb" aria-label="breadcrumbs">
  <ul>
    <li><a href="#">Bulma</a></li>
 </ul>
</nav>

Instance Method Summary collapse

Constructor Details

#initialize(elements: [], **opts) {|optional| ... } ⇒ BreadcrumbComponent

Returns a new instance of BreadcrumbComponent.

Parameters:

  • opts (Hash)

    options to generate content

  • elements (Array<String>) (defaults to: [])

    array of elements to push into this breadcrumbs collection

Options Hash (**opts):

  • :* (String)

    each other key going as tag option, default is class: ‘breadcrumb’, aria_label: ‘breadcrumbs’

Yields:

  • (optional)

    breadcrumb content



42
43
44
45
46
# File 'app/components/bulmacomp/breadcrumb_component.rb', line 42

def initialize(elements: [], **opts)
  super
  @elements = elements
  @opts = { class: 'breadcrumb', aria: { label: 'breadcrumbs' } }.merge(opts)
end

Instance Method Details

#callString

Returns html_safe breadcrumb.

Returns:

  • (String)

    html_safe breadcrumb



49
50
51
# File 'app/components/bulmacomp/breadcrumb_component.rb', line 49

def call
  tag.nav tag.ul(ul_content), **@opts
end

#ul_contentText

Returns , safe join of elements arguments and proc content.

Returns:

  • (Text)

    , safe join of elements arguments and proc content



54
55
56
# File 'app/components/bulmacomp/breadcrumb_component.rb', line 54

def ul_content
  safe_join(@elements.map { |e| tag.li(e) }.<<(content))
end