# frozen_string_literal: true
module Ariadne
module Dropdown
# This component is part of `DropdownComponent` and should not be
# used as a standalone component.
class MenuComponent < Ariadne::Component
DEFAULT_AS_OPTION = :default
VALID_AS_OPTIONS = [DEFAULT_AS_OPTION, :list].freeze
DEFAULT_DIRECTION = :se
VALID_DIRECTIONS = [DEFAULT_DIRECTION, :sw, :w, :e, :ne, :s].freeze
DEFAULT_HEADER_CLASSES = "ariadne-text-sm ariadne-font-medium ariadne-text-gray-900"
# @param classes [String] <%= link_to_classes_docs %>
# @param attributes [Hash] <%= link_to_attributes_docs %>
renders_one :header, lambda { |classes: "", attributes: {}|
actual_classes = class_names(DEFAULT_HEADER_CLASSES, classes)
Ariadne::BaseComponent.new(tag: :span, classes: actual_classes, attributes: attributes)
}
# @param tag [Boolean] <%= one_of(Ariadne::Dropdown::MenuComponent::Item::VALID_TAGS) %>.
# @param divider [Boolean] Whether the item is a divider without any function.
# @param classes [String] <%= link_to_classes_docs %>
# @param attributes [Hash] <%= link_to_attributes_docs %>
renders_many :items, lambda { |tag: Ariadne::Dropdown::MenuComponent::Item::DEFAULT_TAG, divider: false, classes: "", attributes: {}|
Ariadne::Dropdown::MenuComponent::Item.new(tag: tag, as: @as, divider: divider, classes: classes, attributes: attributes)
}
DEFAULT_TAG = :"details-menu"
TAG_OPTIONS = [DEFAULT_TAG].freeze
DEFAULT_CLASSES = ""
# @param as [Symbol] When `as` is `:list`, wraps the menu in a `
` with a `- ` for each item.
# @param direction [Symbol] <%= one_of(Ariadne::Dropdown::MenuComponent::VALID_DIRECTIONS) %>.
# @param classes [String] <%= link_to_classes_docs %>
# @param attributes [Hash] <%= link_to_attributes_docs %>
def initialize(as: DEFAULT_AS_OPTION, direction: VALID_DIRECTIONS, classes: "", attributes: {})
@tag = DEFAULT_TAG
@classes = class_names(
DEFAULT_CLASSES,
classes,
)
@attributes = attributes
@direction = direction
@as = fetch_or_raise(VALID_AS_OPTIONS, as)
@attributes[:role] = "menu"
end
private def list?
@as == :list
end
# Items to be rendered in the `Dropdown` menu.
class Item < Ariadne::Component
DEFAULT_TAG = :a
BUTTON_TAGS = [:button, :summary].freeze
VALID_TAGS = [DEFAULT_TAG, *BUTTON_TAGS].freeze
DEFAULT_ITEM_CLASSES = "ariadne-block ariadne-px-4 ariadne-py-2 ariadne-text-sm ariadne-text-gray-700"
def initialize(as:, tag: DEFAULT_TAG, divider: false, classes: "", attributes: {})
@divider = divider
@as = as
@classes = class_names(DEFAULT_ITEM_CLASSES, classes)
@attributes = attributes
@tag = fetch_or_raise(VALID_TAGS, tag)
@tag = :li if list? && divider?
@attributes[:role] ||= :menuitem
@attributes[:role] = :separator if @divider
end
def call
component = if BUTTON_TAGS.include?(@tag)
Ariadne::ButtonComponent.new(scheme: Ariadne::ButtonComponent::LINK_SCHEME, classes: @classes, attributes: @attributes)
else
Ariadne::BaseComponent.new(tag: @tag, classes: @classes, attributes: @attributes)
end
# divider has no content
render(component) if divider?
render(component) { content }
end
def divider?
@divider
end
def list?
@as == :list
end
end
end
end
end