Add avatar component
This commit is contained in:
parent
62c26771b9
commit
e19786cbaa
|
@ -0,0 +1,4 @@
|
|||
%img{ class: avatar_classes,
|
||||
alt: alt_text,
|
||||
src: avatar_image,
|
||||
loading: :lazy }
|
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AvatarComponent < ViewComponent::Base
|
||||
ALLOWED_SIZES = %w[xs sm md lg xl xxl].freeze
|
||||
|
||||
def initialize(user:, size:, classes: [])
|
||||
@user = user
|
||||
@size = size if ALLOWED_SIZES.include? size
|
||||
@classes = classes
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def size_to_version(size)
|
||||
case size
|
||||
when "xs", "sm"
|
||||
:small
|
||||
when "md", "lg"
|
||||
:medium
|
||||
when "xl", "xxl"
|
||||
:large
|
||||
end
|
||||
end
|
||||
|
||||
def alt_text = "@#{@user.screen_name}"
|
||||
|
||||
def avatar_classes = @classes.unshift("avatar-#{@size}")
|
||||
|
||||
def avatar_image = @user.profile_picture.url(size_to_version(@size))
|
||||
end
|
Loading…
Reference in New Issue