Class: Primer::AvatarComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/avatar_component.rb

Overview

Avatars are images used to represent users and organizations on GitHub. Use the default round avatar for users, and the ‘square` argument for organizations or any other non-human avatars.

Constant Summary collapse

SMALL_THRESHOLD =
24

Constants inherited from Component

Component::STATUSES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#primer

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(src:, alt:, size: 20, square: false, href: nil, **system_arguments) ⇒ AvatarComponent

Returns a new instance of AvatarComponent.

Examples:

Default

<%= render(Primer::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser")) %>

Square

<%= render(Primer::AvatarComponent.new(src: "http://placekitten.com/200/200", alt: "@kittenuser", square: true)) %>

Link

<%= render(Primer::AvatarComponent.new(href: "#", src: "http://placekitten.com/200/200", alt: "@kittenuser")) %>

Parameters:

  • src (String)

    The source url of the avatar image.

  • alt (String)

    Passed through to alt on img tag.

  • size (Integer) (defaults to: 20)

    Adds the avatar-small class if less than 24.

  • square (Boolean) (defaults to: false)

    Used to create a square avatar.

  • 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 %>



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/primer/avatar_component.rb', line 25

def initialize(src:, alt:, size: 20, square: false, href: nil, **system_arguments)
  @href = href
  @system_arguments = system_arguments
  @system_arguments[:tag] = :img
  @system_arguments[:src] = src
  @system_arguments[:alt] = alt
  @system_arguments[:size] = size
  @system_arguments[:height] = size
  @system_arguments[:width] = size

  @system_arguments[:classes] = class_names(
    system_arguments[:classes],
    "avatar" => !href,
    "avatar--small" => size < SMALL_THRESHOLD,
    "circle" => !square
  )
end

Class Method Details

.statusObject



53
54
55
# File 'app/components/primer/avatar_component.rb', line 53

def self.status
  Primer::Component::STATUSES[:beta]
end

Instance Method Details

#callObject



43
44
45
46
47
48
49
50
51
# File 'app/components/primer/avatar_component.rb', line 43

def call
  if @href
    render(Primer::LinkComponent.new(href: @href, classes: "avatar")) do
      render(Primer::BaseComponent.new(**@system_arguments)) { content }
    end
  else
    render(Primer::BaseComponent.new(**@system_arguments)) { content }
  end
end