Class: Primer::OpenProject::AvatarWithFallback
- Inherits:
-
Beta::Avatar
- Object
- ViewComponent::Base
- Component
- Beta::Avatar
- Primer::OpenProject::AvatarWithFallback
- Defined in:
- app/components/primer/open_project/avatar_with_fallback.rb
Overview
OpenProject-specific Avatar component that extends Primer::Beta::Avatar to support fallback rendering with initials when no image source is provided.
When src is nil, this component renders an SVG with initials extracted from the alt text. The AvatarFallbackElement web component then enhances it client-side by applying a consistent background color based on the user’s unique_id (using the same hash function as OP Core for consistency).
This component follows the “extension over mutation” pattern - it extends Primer::Beta::Avatar without modifying its interface, ensuring compatibility with upstream changes.
Constant Summary collapse
- FONT_STACK =
"-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'"
Constants inherited from Beta::Avatar
Beta::Avatar::DEFAULT_SHAPE, Beta::Avatar::DEFAULT_SIZE, Beta::Avatar::SHAPE_OPTIONS, Beta::Avatar::SIZE_OPTIONS, Beta::Avatar::SMALL_THRESHOLD
Constants inherited from Component
Component::INVALID_ARIA_LABEL_TAGS
Constants included from Status::Dsl
Constants included from ViewHelper
Constants included from TestSelectorHelper
TestSelectorHelper::TEST_SELECTOR_TAG
Constants included from FetchOrFallbackHelper
FetchOrFallbackHelper::InvalidValueError
Constants included from AttributesHelper
AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(src: nil, alt: nil, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, unique_id: nil, **system_arguments) ⇒ AvatarWithFallback
constructor
A new instance of AvatarWithFallback.
Methods inherited from Component
Methods included from JoinStyleArgumentsHelper
Methods included from TestSelectorHelper
Methods included from FetchOrFallbackHelper
#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?
Methods included from ClassNameHelper
Methods included from AttributesHelper
#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes
Methods included from ExperimentalSlotHelpers
Methods included from ExperimentalRenderHelpers
Constructor Details
#initialize(src: nil, alt: nil, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, unique_id: nil, **system_arguments) ⇒ AvatarWithFallback
Returns a new instance of AvatarWithFallback.
31 32 33 34 35 36 37 38 39 |
# File 'app/components/primer/open_project/avatar_with_fallback.rb', line 31 def initialize(src: nil, alt: nil, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, unique_id: nil, **system_arguments) require_src_or_alt_arguments(src, alt) @unique_id = unique_id @fallback_svg = generate_fallback_svg(alt, size) final_src = src.blank? ? @fallback_svg : src super(src: final_src, alt: alt, size: size, shape: shape, href: href, **system_arguments) end |
Instance Method Details
#call ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/components/primer/open_project/avatar_with_fallback.rb', line 41 def call render( Primer::BaseComponent.new( tag: :"avatar-fallback", data: { unique_id: @unique_id, alt_text: @system_arguments[:alt], fallback_src: @fallback_svg } ) ) { super } end |