# frozen_string_literal: true module Ariadne # A component to render a subheader with optional description below it and optional action # components on the right side class SubheaderComponent < Ariadne::Component DEFAULT_TAGS = { wrapper: :div, actions: :div, header: :h2, description: :span, } DEFAULT_CLASSES = { wrapper: "ariadne-border-b-2 ariadne-border-solid ariadne-border-black ariadne-border-opacity-20", header: "", actions: "", description: "ariadne-text-black/50", } DEFAULT_ATTRIBUTES = { wrapper: {}, header: {}, description: {}, actions: {}, } renders_one :header, lambda { |tag: DEFAULT_TAGS[:header], classes: "", attributes: {}| Ariadne::HeadingComponent.new( tag: check_incoming_tag(DEFAULT_TAGS[:header], tag), classes: merge_class_names(DEFAULT_CLASSES[:header], classes), attributes: DEFAULT_ATTRIBUTES[:header].merge({ id: @header_id }).merge(attributes), ) } renders_one :description, lambda { |tag: DEFAULT_TAGS[:description], classes: "", attributes: {}| Ariadne::BaseComponent.new( tag: check_incoming_tag(DEFAULT_TAGS[:description], tag), classes: merge_class_names(DEFAULT_CLASSES[:description], classes), attributes: DEFAULT_ATTRIBUTES[:description] .merge({ "aria-describedby": @header_id }) .merge(attributes), ) } renders_many :actions # @example Default # # <%= render(Ariadne::SubheaderComponent.new) { "Example" } %> # # @param tag [Symbol, String] The rendered tag name. # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize(tag: DEFAULT_TAGS[:wrapper], classes: "", id: "subheader-#{SecureRandom.uuid}", attributes: {}) @tag = check_incoming_tag(DEFAULT_TAGS[:wrapper], tag) @header_id = id @attributes = DEFAULT_ATTRIBUTES[:wrapper].merge(attributes) @classes = merge_class_names( DEFAULT_CLASSES[:wrapper], classes, ) end end end