# frozen_string_literal: true module Ariadne # Use `DetailsComponent` to reveal content after clicking a button. class DetailsComponent < Ariadne::Component DEFAULT_TAG = :details DEFAULT_BODY_TAG = :div VALID_BODY_TAGS = [:ul, :"details-menu", :"details-dialog", DEFAULT_BODY_TAG].freeze NO_OVERLAY = :none OVERLAY_MAPPINGS = { NO_OVERLAY => "", :default => "", }.freeze # Use the Summary as a trigger to reveal the content. # # @param button [Boolean] (true) Whether to render the Summary as a button or not. # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> renders_one :summary, lambda { |button: true, classes: "", attributes: {}| tag = :summary attributes[:role] = "button" if button Ariadne::ButtonComponent.new(tag: tag, classes: classes, attributes: attributes) else Ariadne::BaseComponent.new(tag: tag, classes: classes, attributes: attributes) end } DEFAULT_BODY_CLASSES = "ariadne-absolute ariadne-mt-2 ariadne-w-56 ariadne-divide-y ariadne-divide-gray-100 ariadne-rounded-md ariadne-shadow-lg ariadne-ring-1 ariadne-ring-black ariadne-ring-opacity-5 focus:ariadne-outline-none" # Use the Body slot as the main content to be shown when triggered by the Summary. # # @param tag [Symbol] The tag to use for the body/ <%= one_of(Ariadne::DetailsComponent::VALID_BODY_TAGS) %> # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> renders_one :body, lambda { |tag: DEFAULT_BODY_TAG, classes: "", attributes: {}| tag = fetch_or_raise(VALID_BODY_TAGS, tag) actual_classes = merge_class_names(DEFAULT_BODY_CLASSES, classes) Ariadne::BaseComponent.new(tag: tag, classes: actual_classes, attributes: attributes) } DEFAULT_CLASSES = "" # @example Default # # <%= render Ariadne::DetailsComponent.new do |c| %> # <% c.with_summary do %> # Summary # <% end %> # <% c.with_body do %> # Body # <% end %> # <% end %> # # @param overlay [Symbol] Dictates the type of overlay to render with. <%= one_of(Ariadne::DetailsComponent::OVERLAY_MAPPINGS.keys) %> # @param reset [Boolean] If set to true, it will remove the default caret and remove style from the summary element # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize(overlay: NO_OVERLAY, reset: true, classes: "", attributes: {}) @tag = DEFAULT_TAG @reset = reset @classes = merge_class_names( DEFAULT_CLASSES, classes, @reset ? "ariadne__details-reset" : "", ) @attributes = attributes @overlay = fetch_or_raise(OVERLAY_MAPPINGS.keys, overlay) end def render? summary? && body? end end end