2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-04 06:10:24 +01:00
|
|
|
class Dropdown::MenuComponent < ApplicationComponent
|
|
|
|
renders_one :button_inner_html
|
2023-06-08 11:25:39 +02:00
|
|
|
renders_one :menu_header_html
|
2023-01-04 06:10:24 +01:00
|
|
|
# beware, items elements like button_to/link_to must include role: 'menuitem' for aria reason
|
|
|
|
renders_many :items, -> (options = {}, &block) do
|
2023-01-30 14:58:13 +01:00
|
|
|
tag.li(**options.merge(role: 'none'), &block)
|
2023-01-04 06:10:24 +01:00
|
|
|
end
|
|
|
|
renders_many :forms
|
|
|
|
|
|
|
|
def initialize(wrapper:,
|
|
|
|
wrapper_options: {},
|
|
|
|
button_options: {},
|
2023-02-27 17:05:39 +01:00
|
|
|
menu_options: {},
|
|
|
|
role: nil)
|
2023-01-04 06:10:24 +01:00
|
|
|
@wrapper = wrapper
|
|
|
|
@wrapper_options = wrapper_options
|
|
|
|
@button_options = button_options
|
|
|
|
@menu_options = menu_options
|
2023-02-27 17:05:39 +01:00
|
|
|
@role = role
|
2023-01-04 06:10:24 +01:00
|
|
|
end
|
|
|
|
|
2023-01-06 14:11:00 +01:00
|
|
|
def wrapper_options
|
|
|
|
@wrapper_options.deep_merge({
|
|
|
|
class: wrapper_class_names,
|
|
|
|
data: { controller: 'menu-button' }
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-01-04 06:10:24 +01:00
|
|
|
def wrapper_class_names
|
|
|
|
['dropdown'] + Array(@wrapper_options[:class])
|
|
|
|
end
|
|
|
|
|
|
|
|
def button_id
|
|
|
|
"#{menu_id}_button"
|
|
|
|
end
|
|
|
|
|
|
|
|
def menu_id
|
|
|
|
@menu_options[:id] ||= SecureRandom.uuid
|
|
|
|
@menu_options[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def menu_role
|
2023-02-27 17:05:39 +01:00
|
|
|
return @role if @role
|
2023-01-04 06:10:24 +01:00
|
|
|
forms? ? :region : :menu
|
|
|
|
end
|
|
|
|
|
|
|
|
def menu_class_names
|
|
|
|
['dropdown-content'] + Array(@menu_options[:class])
|
|
|
|
end
|
|
|
|
|
|
|
|
def button_class_names
|
|
|
|
['fr-btn', 'dropdown-button'] + Array(@button_options[:class])
|
|
|
|
end
|
2023-07-04 10:15:58 +02:00
|
|
|
|
|
|
|
def disabled?
|
|
|
|
@button_options[:disabled] == true
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
{ menu_button_target: 'button' }.deep_merge(@button_options[:data].to_h)
|
|
|
|
end
|
2023-01-04 06:10:24 +01:00
|
|
|
end
|