Class: Yattho::Beta::Avatar

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/beta/avatar.rb

Overview

‘Avatar` can be used to represent users and organizations on GitHub.

  • Use the default circle avatar for users, and the square shape

for organizations or any other non-human avatars.

  • By default, ‘Avatar` will render a static `<img>`. To have `Avatar` function as a link, set the `href` which will wrap the `<img>` in a `<a>`.

  • Set ‘size` to update the height and width of the `Avatar` in pixels.

  • To stack multiple avatars together, use <%= link_to_component(Yattho::Beta::AvatarStack) %>.

Constant Summary collapse

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

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(src:, alt:, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, **system_arguments) ⇒ Avatar

Returns a new instance of Avatar.

Examples:

Default

<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser")) %>

Square

<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", shape: :square)) %>

Link

<%= render(Yattho::Beta::Avatar.new(href: "#", src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser profile")) %>

With size

<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 16)) %>
<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 20)) %>
<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 24)) %>
<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 32)) %>
<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 40)) %>
<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 48)) %>
<%= render(Yattho::Beta::Avatar.new(src: Yattho::ExampleImage::BASE64_SRC, alt: "@kittenuser", size: 80)) %>

Parameters:

  • src (String)

    The source url of the avatar image.

  • alt (String)

    Passed through to alt on img tag.

  • size (Integer) (defaults to: DEFAULT_SIZE)

    <%= one_of(Yattho::Beta::Avatar::SIZE_OPTIONS) %>

  • shape (Symbol) (defaults to: DEFAULT_SHAPE)

    Shape of the avatar. <%= one_of(Yattho::Beta::Avatar::SHAPE_OPTIONS) %>

  • href (String) (defaults to: nil)

    The URL to link to. If used, component will be wrapped by an ‘<a>` tag.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/components/yattho/beta/avatar.rb', line 54

def initialize(src:, alt:, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, **system_arguments)
  @href = href
  @system_arguments = deny_tag_argument(**system_arguments)
  @system_arguments[:tag] = :img
  @system_arguments[:src] = src
  @system_arguments[:alt] = alt
  @system_arguments[:size] = fetch_or_fallback(SIZE_OPTIONS, size, DEFAULT_SIZE)
  @system_arguments[:height] = @system_arguments[:size]
  @system_arguments[:width] = @system_arguments[:size]

  @system_arguments[:classes] = class_names(
    system_arguments[:classes],
    "avatar",
    "avatar-small" => size < SMALL_THRESHOLD,
    "circle" => shape == DEFAULT_SHAPE,
    "lh-0" => href # Addresses an overflow issue with linked avatars
  )
end

Instance Method Details

#callObject



73
74
75
76
77
78
79
80
81
# File 'app/components/yattho/beta/avatar.rb', line 73

def call
  if @href
    render(Yattho::Beta::Link.new(href: @href, classes: @system_arguments[:classes])) do
      render(Yattho::BaseComponent.new(**@system_arguments.except(:classes))) { content }
    end
  else
    render(Yattho::BaseComponent.new(**@system_arguments)) { content }
  end
end