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.
Uses a “fallback first” pattern for flicker-free loading:
-
Always renders fallback SVG as initial <img> src (visible immediately)
-
Client-side JS test-loads the real URL in background
-
On success, swaps to real image; on failure, fallback stays visible
This approach is inspired by OpenProject’s Angular PrincipalRendererService.
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.
29 30 31 32 33 34 35 36 37 38 |
# File 'app/components/primer/open_project/avatar_with_fallback.rb', line 29 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 @avatar_src = src.presence @fallback_svg = generate_fallback_svg(alt, size) # Always render fallback first - JS will swap to real image on successful load super(src: @fallback_svg, alt: alt, size: size, shape: shape, href: href, **system_arguments) end |
Instance Method Details
#call ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/components/primer/open_project/avatar_with_fallback.rb', line 40 def call render( Primer::BaseComponent.new( tag: :"avatar-fallback", data: { unique_id: @unique_id, alt_text: @system_arguments[:alt], avatar_src: @avatar_src } ) ) { super } end |