Sha256: 97be82d8272277310125a01f1c05924303db06b1ab6e81196538107040830d9e

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Primer
  # Use DetailsComponent to reveal content after clicking a button.
  class DetailsComponent < Primer::Component
    status :beta

    NO_OVERLAY = :none
    OVERLAY_MAPPINGS = {
      NO_OVERLAY => "",
      :default => "details-overlay",
      :dark => "details-overlay details-overlay-dark"
    }.freeze

    # Use the Summary slot as a trigger to reveal the content.
    #
    # @param button [Boolean] Whether to render the Summary as a button or not.
    # @param kwargs [Hash] The same arguments as <%= link_to_system_arguments_docs %>.
    renders_one :summary, lambda { |button: true, **system_arguments|
      system_arguments[:tag] = :summary
      system_arguments[:role] = "button"

      return Primer::BaseComponent.new(**system_arguments) unless button

      Primer::ButtonComponent.new(**system_arguments)
    }

    # Use the Body slot as the main content to be shown when triggered by the Summary.
    #
    # @param kwargs [Hash] The same arguments as <%= link_to_system_arguments_docs %>.
    renders_one :body, lambda { |**system_arguments|
      system_arguments[:tag] ||= :div
      Primer::BaseComponent.new(**system_arguments)
    }

    # @param overlay [Symbol] Dictates the type of overlay to render with. <%= one_of(Primer::DetailsComponent::OVERLAY_MAPPINGS.keys) %>
    # @param reset [Boolean] Defatuls to false. If set to true, it will remove the default caret and remove style from the summary element
    # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
    def initialize(overlay: NO_OVERLAY, reset: false, **system_arguments)
      @system_arguments = system_arguments
      @system_arguments[:tag] = :details
      @system_arguments[:classes] = class_names(
        system_arguments[:classes],
        OVERLAY_MAPPINGS[fetch_or_fallback(OVERLAY_MAPPINGS.keys, overlay, NO_OVERLAY)],
        "details-reset" => reset
      )
    end

    def render?
      summary.present? && body.present?
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
primer_view_components-0.0.38 app/components/primer/details_component.rb
primer_view_components-0.0.37 app/components/primer/details_component.rb
primer_view_components-0.0.36 app/components/primer/details_component.rb
primer_view_components-0.0.35 app/components/primer/details_component.rb
primer_view_components-0.0.34 app/components/primer/details_component.rb
primer_view_components-0.0.33 app/components/primer/details_component.rb
primer_view_components-0.0.32 app/components/primer/details_component.rb