Class: Polaris::AvatarComponent

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

Constant Summary collapse

SIZE_DEFAULT =
:medium
SIZE_MAPPINGS =
{
  extra_small: "Polaris-Avatar--sizeExtraSmall",
  small: "Polaris-Avatar--sizeSmall",
  medium: "Polaris-Avatar--sizeMedium",
  large: "Polaris-Avatar--sizeLarge"
}
SIZE_OPTIONS =
SIZE_MAPPINGS.keys
SHAPE_DEFAULT =
:round
SHAPE_MAPPINGS =
{
  square: "Polaris-Avatar--shapeSquare",
  round: "Polaris-Avatar--shapeRound"
}
SHAPE_OPTIONS =
SHAPE_MAPPINGS.keys
STYLE_CLASSES =
%w[styleOne styleTwo styleThree styleFour styleFive]

Constants included from ViewHelper

ViewHelper::POLARIS_HELPERS, ViewHelper::POLARIS_TEXT_STYLES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods included from ViewHelper

#polaris_body_styles, #polaris_html_classes, #polaris_html_styles, #polaris_icon_source

Methods included from StylesListHelper

#styles_list

Methods included from OptionHelper

#append_option, #prepend_option

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #fetch_or_fallback_nested

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(customer: false, initials: nil, name: nil, size: SIZE_DEFAULT, shape: SHAPE_DEFAULT, source: nil, icon: nil, **system_arguments) ⇒ AvatarComponent

Returns a new instance of AvatarComponent.



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

def initialize(
  customer: false,
  initials: nil,
  name: nil,
  size: SIZE_DEFAULT,
  shape: SHAPE_DEFAULT,
  source: nil,
  icon: nil,
  **system_arguments
)
  @customer = customer
  @initials = initials
  @name = name
  @size = size
  @shape = shape
  @source = source
  @icon = icon
  @system_arguments = system_arguments
end

Instance Method Details

#style_classObject



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

def style_class
  if @name.present?
    STYLE_CLASSES[xor_hash(@name) % STYLE_CLASSES.length]
  else
    STYLE_CLASSES.first
  end
end

#system_argumentsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/components/polaris/avatar_component.rb', line 51

def system_arguments
  @system_arguments.tap do |opts|
    opts[:tag] = "span"
    opts[:role] = "img"
    if @name
      opts[:aria] ||= {}
      opts[:aria][:label] ||= @name
    end
    opts[:classes] = class_names(
      opts[:classes],
      "Polaris-Avatar",
      SIZE_MAPPINGS[fetch_or_fallback(SIZE_OPTIONS, @size, SIZE_DEFAULT)],
      SHAPE_MAPPINGS[fetch_or_fallback(SHAPE_OPTIONS, @shape, SHAPE_DEFAULT)],
      "Polaris-Avatar--#{style_class}" => !@customer && !@source
    )
  end
end

#xor_hash(string) ⇒ Object



69
70
71
# File 'app/components/polaris/avatar_component.rb', line 69

def xor_hash(string)
  string.bytes.reduce(0) { |hash, byte| hash ^ byte }
end