You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(ui5-avatar): add dynamic image error handling with fallback logic (#11642)
This commit fixes image error handling in the Avatar component to ensure
fallback content is displayed when images fail to load.
**Problem Fixed:**
- Avatar would show broken/empty content when image sources were invalid
- No automatic fallback when images failed to load dynamically
- Missing error detection for already-broken images at initialization
**Solution:**
- **Dynamic Image Error Detection**: Added real-time monitoring of image
load states with automatic fallback switching
- **Reactive Error Handling**: Image failures now trigger immediate UI
updates to show appropriate fallback content
- **Comprehensive Error Detection**: Handles both runtime failures and
already-broken images via `complete` and `naturalWidth` checks
**Technical Implementation:**
- Added `_imageLoadError` reactive property to track image state changes
- Implemented event listeners for `load` and `error` events on image
elements
- Enhanced `hasImage` getter to consider both image presence and load
state
- Added proper event listener lifecycle management to prevent memory
leaks
- Optimized re-rendering with cached `_hasImage` property in template
**Fallback Hierarchy (unchanged):**
1. Image (if provided and loads successfully)
2. Icon (if `icon` property is set)
3. Initials (if valid and fit)
4. Fallback Icon (final fallback)
**Edge Cases Handled:**
- Images that fail after initial load (dynamic URL changes)
- Images that are already broken when component initializes
- Seamless switching between broken → fixed → broken states
**Files Modified:**
- Avatar.ts: Core image error handling and reactive state management
- AvatarTemplate.tsx: Updated to use cached `_hasImage` property
- Avatar.cy.tsx: Added comprehensive test coverage for error scenarios
Fixes: #11140
0 commit comments