Class: Primer::Alpha::ActionMenu::Menu

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

Overview

This component is part of <%= link_to_component(Primer::Alpha::ActionMenu) %> and should not be used as a standalone component.

Direct Known Subclasses

PrimaryMenu, SubMenu

Constant Summary collapse

DEFAULT_PRELOAD =
false
DEFAULT_SELECT_VARIANT =
:none
SELECT_VARIANT_OPTIONS =
[
  :single,
  :multiple,
  DEFAULT_SELECT_VARIANT
].freeze

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 Primer::AttributesHelper

Primer::AttributesHelper::PLURAL_ARIA_ATTRIBUTES, Primer::AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Attribute Summary collapse

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 Primer::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(anchor_align:, anchor_side:, menu_id: self.class.generate_id, size: Primer::Alpha::Overlay::DEFAULT_SIZE, src: nil, preload: DEFAULT_PRELOAD, select_variant: DEFAULT_SELECT_VARIANT, form_arguments: {}, overlay_arguments: {}, list_arguments: {}, **system_arguments) ⇒ Menu

Returns a new instance of Menu.

Parameters:

  • anchor_align (Symbol)

    <%= one_of(Primer::Alpha::Overlay::ANCHOR_ALIGN_OPTIONS) %>

  • anchor_side (Symbol)

    <%= one_of(Primer::Alpha::Overlay::ANCHOR_SIDE_OPTIONS) %>

  • menu_id (String) (defaults to: self.class.generate_id)

    Id of the menu.

  • size (Symbol) (defaults to: Primer::Alpha::Overlay::DEFAULT_SIZE)

    <%= one_of(Primer::Alpha::Overlay::SIZE_OPTIONS) %>

  • src (String) (defaults to: nil)

    Used with an ‘include-fragment` element to load menu content from the given source URL.

  • preload (Boolean) (defaults to: DEFAULT_PRELOAD)

    When true, and src is present, loads the ‘include-fragment` on trigger hover.

  • select_variant (Symbol) (defaults to: DEFAULT_SELECT_VARIANT)

    <%= one_of(Primer::Alpha::ActionMenu::Menu::SELECT_VARIANT_OPTIONS) %>

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

    Allows an ‘ActionMenu` to act as a select list in multi- and single-select modes. Pass the `builder:` and `name:` options to this hash. `builder:` should be an instance of `ActionView::Helpers::FormBuilder`, which are created by the standard Rails `#form_with` and `#form_for` helpers. The `name:` option is the desired name of the field that will be included in the params sent to the server on form submission.

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

    Arguments to pass to the underlying <%= link_to_component(Primer::Alpha::Overlay) %>

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

    Arguments to pass to the underlying <%= link_to_component(Primer::Alpha::ActionMenu::List) %>

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/components/primer/alpha/action_menu/menu.rb', line 67

def initialize(
  anchor_align:,
  anchor_side:,
  menu_id: self.class.generate_id,
  size: Primer::Alpha::Overlay::DEFAULT_SIZE,
  src: nil,
  preload: DEFAULT_PRELOAD,
  select_variant: DEFAULT_SELECT_VARIANT,
  form_arguments: {},
  overlay_arguments: {},
  list_arguments: {},
  **system_arguments
)
  @menu_id = menu_id
  @src = src
  @preload = fetch_or_fallback_boolean(preload, DEFAULT_PRELOAD)
  @anchor_side = anchor_side
  @anchor_align = anchor_align

  @select_variant = fetch_or_fallback(SELECT_VARIANT_OPTIONS, select_variant, DEFAULT_SELECT_VARIANT)
  @form_arguments = form_arguments

  overlay_arguments[:data] = merge_data(
    overlay_arguments, data: {
      target: "action-menu.overlay"
    }
  )

  @overlay = Primer::Alpha::Overlay.new(
    id: "#{@menu_id}-overlay",
    title: "Menu",
    visually_hide_title: true,
    anchor_align: anchor_align,
    anchor_side: anchor_side,
    size: size,
    **overlay_arguments
  )

  @list = Primer::Alpha::ActionMenu::List.new(
    menu_id: @menu_id,
    select_variant: select_variant,
    form_arguments: form_arguments,
    **list_arguments
  )

  system_arguments # rubocop:disable Lint/Void
end

Instance Attribute Details

#anchor_alignObject (readonly)

Returns the value of attribute anchor_align.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def anchor_align
  @anchor_align
end

#anchor_sideObject (readonly)

Returns the value of attribute anchor_side.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def anchor_side
  @anchor_side
end

#form_argumentsObject (readonly)

Returns the value of attribute form_arguments.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def form_arguments
  @form_arguments
end

#listObject (readonly)

Returns the value of attribute list.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def list
  @list
end

Returns the value of attribute menu_id.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def menu_id
  @menu_id
end

#preloadObject (readonly) Also known as: preload?

Returns the value of attribute preload.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def preload
  @preload
end

#select_variantObject (readonly)

Returns the value of attribute select_variant.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def select_variant
  @select_variant
end

#srcObject (readonly)

Returns the value of attribute src.



19
20
21
# File 'app/components/primer/alpha/action_menu/menu.rb', line 19

def src
  @src
end

Instance Method Details

#itemsArray<ViewComponent::Slot>

Gets the list of configured menu items, which includes regular items, avatar items, groups, and dividers.

Returns:

  • (Array<ViewComponent::Slot>)


28
29
# File 'app/components/primer/alpha/action_menu/menu.rb', line 28

def items
end

#with_avatar_item(**system_arguments) ⇒ Object

Adds an avatar item to the menu.

Parameters:

  • system_arguments (Hash)

    The arguments accepted by <%= link_to_component(Primer::Alpha::ActionList) %>‘s `item` slot.



10
11
# File 'app/components/primer/alpha/action_menu/menu.rb', line 10

def with_avatar_item(**system_arguments)
end

#with_divider(**system_arguments) ⇒ Object

Adds a divider to the list. Dividers visually separate items.

Parameters:

  • system_arguments (Hash)

    The arguments accepted by <%= link_to_component(Primer::Alpha::ActionList::Divider) %>.



16
17
# File 'app/components/primer/alpha/action_menu/menu.rb', line 16

def with_divider(**system_arguments)
end

#with_group(**system_arguments) ⇒ Object

Adds a group to the menu. Groups are a logical set of items with a header.

Parameters:

  • system_arguments (Hash)

    The arguments accepted by <%= link_to_component(Primer::Alpha::ActionMenu::Group) %>.



22
23
# File 'app/components/primer/alpha/action_menu/menu.rb', line 22

def with_group(**system_arguments)
end

#with_item(**system_arguments) ⇒ Object

Adds an item to the menu.

Parameters:

  • system_arguments (Hash)

    The arguments accepted by <%= link_to_component(Primer::Alpha::ActionList) %>‘s `item` slot.



4
5
# File 'app/components/primer/alpha/action_menu/menu.rb', line 4

def with_item(**system_arguments)
end

#with_sub_menu_item(**system_arguments, &block) ⇒ Object

Adds a sub-menu to the menu.

Parameters:

  • system_arguments (Hash)

    The arguments accepted by <%= link_to_component(Primer::Alpha::ActionMenu::SubMenuItem) %>.



118
119
120
121
122
123
124
125
126
# File 'app/components/primer/alpha/action_menu/menu.rb', line 118

def with_sub_menu_item(**system_arguments, &block)
  @list.with_item(
    component_klass: SubMenuItem,
    select_variant: select_variant,
    form_arguments: form_arguments,
    **system_arguments,
    &block
  )
end