Class: Primer::OpenProject::SubHeader::Menu

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/open_project/sub_header/menu.rb

Overview

A Helper class to create an ActionMenu with a required icon on the trigger button. It is meant to be used inside the SubHeader Do not use standalone

Constant Summary

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Methods included from AttributesHelper

#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Methods included from ExperimentalSlotHelpers

included

Methods included from ExperimentalRenderHelpers

included

Constructor Details

#initialize(icon_only: false, leading_icon:, label:, trailing_icon: nil, button_arguments: {}, **system_arguments) ⇒ Menu

Returns a new instance of Menu.

Parameters:

  • icon_only (Boolean) (defaults to: false)

    Whether the trigger button is an IconButton

  • leading_icon (Symbol)

    Name of Octicon icon to use as either leading icon or IconButton.

  • label (String)

    The button label

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

    Additional arguments for the button

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/components/primer/open_project/sub_header/menu.rb', line 34

def initialize(icon_only: false, leading_icon:, label:, trailing_icon: nil, button_arguments: {}, **system_arguments)
  @icon_only = icon_only
  @leading_icon = leading_icon
  @trailing_icon = trailing_icon
  @label = label

  if @label.nil? || @label.empty?
    raise ArgumentError, "You need to provide a valid label."
  end

  @button_arguments = button_arguments

  @menu = Primer::Alpha::ActionMenu.new(**system_arguments)
end

Instance Method Details

#before_renderObject



52
53
54
55
56
57
# File 'app/components/primer/open_project/sub_header/menu.rb', line 52

def before_render
  if show_button
    raise ArgumentError,
          "Do not use the show_button slot within the SubHeader, as it is reserved. Instead provide a leading_icon within the subHeader button slot"
  end
end

#callObject



59
60
61
62
63
64
# File 'app/components/primer/open_project/sub_header/menu.rb', line 59

def call
  render(@menu) do
    set_show_button(**@button_arguments)
    content
  end
end

#set_show_button(**system_arguments) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/primer/open_project/sub_header/menu.rb', line 15

def set_show_button(**system_arguments)
  aria_label = aria("label", system_arguments) || @label

  if @icon_only
    @menu.with_show_button(icon: @leading_icon, "aria-label": aria_label, **system_arguments)
  else
    @menu.with_show_button("aria-label": aria_label, **system_arguments) do |button|
      button.with_leading_visual_icon(icon: @leading_icon)
      button.with_trailing_action_icon(icon: @trailing_icon) unless @trailing_icon.nil?
      @label
    end
  end
end