# frozen_string_literal: true module Ariadne # Use `AvatarStack` to stack multiple avatars together. class AvatarStackComponent < Ariadne::Component ALIGN_DEFAULT = :left ALIGN_OPTIONS = [ALIGN_DEFAULT, :right].freeze DEFAULT_TAG = :div TAG_OPTIONS = [DEFAULT_TAG, :span].freeze DEFAULT_BODY_TAG = :div BODY_TAG_OPTIONS = [DEFAULT_BODY_TAG, :span].freeze # `Tooltip` that appears on mouse hover or keyboard focus over the stack. # # @param tag [Symbol, String] The rendered tag name # @param text [String] The text content of the tooltip. This should be brief and no longer than a sentence. # @param direction [Symbol] <%= one_of(Ariadne::TooltipComponent::VALID_PLACEMENTS) %> # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] Same arguments as <%= link_to_component(Ariadne::TooltipComponent) %>. renders_one :tooltip, lambda { |tag: Ariadne::TooltipComponent::DEFAULT_TAG, text:, direction: Ariadne::TooltipComponent::DEFAULT_PLACEMENT, classes: "", attributes: {}| raise ArgumentError, "Avatar stacks with a tooltip must have a unique `id` set." if @id.blank? Ariadne::TooltipComponent.new(for_id: @id, tag: tag, text: text, direction: direction, type: :label, classes: classes, attributes: attributes) } # Required list of stacked avatars. # # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> renders_many :avatars, "Ariadne::AvatarComponent" DEFAULT_CLASSES = "ariadne-flex ariadne--space-x-2 ariadne-overflow-hidden" # @example Default # <%= render(Ariadne::AvatarStackComponent.new) do |c| %> # <% c.with_avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %> # <% c.with_avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %> # <% c.with_avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %> # <% end %> # # @example Align right # <%= render(Ariadne::AvatarStackComponent.new(align: :right)) do |c| %> # <% c.with_avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %> # <% c.with_avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %> # <% c.with_avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %> # <% end %> # # @param tag [Symbol] <%= one_of(Ariadne::AvatarStackComponent::TAG_OPTIONS) %> # @param align [Symbol] <%= one_of(Ariadne::AvatarStackComponent::ALIGN_OPTIONS) %> # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize(tag: DEFAULT_TAG, align: ALIGN_DEFAULT, classes: "", attributes: {}) @tag = check_incoming_tag(DEFAULT_TAG, tag) @align = fetch_or_raise(ALIGN_OPTIONS, align) @classes = merge_class_names( DEFAULT_CLASSES, classes, ) @attributes = attributes @attributes[:id] ||= "avatar-stack-#{SecureRandom.hex(4)}" @id = @attributes[:id] end def render? avatars.any? end def tooltipped? tooltip.present? end end end