# frozen_string_literal: true
module Ariadne
# `Avatar` can be used to represent users.
#
# - Use the default circle avatar for users, and the square shape
# for or any other non-human avatars.
# - By default, `Avatar` will render a static ``. To have `Avatar` function as a link, set the `href` which will wrap the `` in a ``.
# - Set `size` to update the height and width of the `Avatar` in pixels.
# - To stack multiple avatars together, use <%= link_to_component(Ariadne::AvatarStackComponent) %>.
#
# @accessibility
# Images should have text alternatives that describe the information or function represented.
# If the avatar functions as a link, provide alt text that helps convey the function. For instance,
# if `Avatar` is a link to a user profile, the alt attribute should be `@kittenuser profile`
# rather than `@kittenuser`.
# [Learn more about best image practices (WAI Images)](https://www.w3.org/WAI/tutorials/images/)
class AvatarComponent < Ariadne::Component
DEFAULT_SIZE = 20
SMALL_THRESHOLD = 24
DEFAULT_SHAPE = :circle
SHAPE_OPTIONS = [DEFAULT_SHAPE, :square].freeze
SIZE_OPTIONS = [16, DEFAULT_SIZE, SMALL_THRESHOLD, 32, 40, 48, 80].freeze
DEFAULT_CLASSES = "ariadne-inline-block ariadne-rounded-full ariadne-ring-2 ariadne-ring-white"
# @example Default
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser")) %>
#
# @example Square
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", shape: :square)) %>
#
# @example Link
# <%= render(Ariadne::AvatarComponent.new(href: "#", src: "http://placekitten.com/200/200", alt: "@kittenuser profile")) %>
#
# @example With size
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 16)) %>
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 20)) %>
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 24)) %>
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 32)) %>
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 40)) %>
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 48)) %>
# <%= render(Ariadne::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", size: 80)) %>
#
# @param src [String] The source url of the avatar image.
# @param alt [String] Passed through to alt on img tag.
# @param size [Integer] <%= one_of(Ariadne::AvatarComponent::SIZE_OPTIONS) %>
# @param shape [Symbol] Shape of the avatar. <%= one_of(Ariadne::AvatarComponent::SHAPE_OPTIONS) %>
# @param href [String] The URL to link to. If used, component will be wrapped by an `` tag.
# @param classes [String] <%= link_to_classes_docs %>
# @param attributes [Hash] <%= link_to_attributes_docs %>
def initialize(src:, alt:, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, classes: "", attributes: {})
@tag = :img
@href = href
@classes = merge_class_names(
DEFAULT_CLASSES,
classes,
)
@attributes = attributes
@attributes[:src] = src
@attributes[:alt] = alt
@attributes[:size] = fetch_or_raise(SIZE_OPTIONS, size)
@attributes[:height] = @attributes[:size]
@attributes[:width] = @attributes[:size]
end
def call
if @href
render(Ariadne::LinkComponent.new(href: @href, classes: @classes, attributes: @attributes)) do
render(Ariadne::BaseComponent.new(tag: @tag)) { content }
end
else
render(Ariadne::BaseComponent.new(tag: @tag, classes: @classes, attributes: @attributes)) { content }
end
end
end
end