Skip to content

[avatar] Fix image status edge cases - #4835

Merged
atomiks merged 8 commits into
mui:masterfrom
atomiks:codex/avatar-behavior-coverage
Jun 17, 2026
Merged

atomiks merged 8 commits into
mui:masterfrom
atomiks:codex/avatar-behavior-coverage

Conversation

@atomiks

@atomiks atomiks commented May 18, 2026

Copy link
Copy Markdown
Contributor

This is part of the Codex component behavior and test coverage sweep. It fixes Avatar behavior/API issues found during review and adds focused regression coverage for the affected paths.

Root cause

Avatar had edge cases where its implementation and tests did not fully cover the expected image loading, fallback, and native image prop contract.

Changes

  • Fix Avatar image prop forwarding for crossOrigin, referrerPolicy, srcSet, and sizes.
  • Use the same responsive image inputs for the loading probe that the rendered image receives.
  • Reset image loading status when Avatar.Image unmounts so fallback can render again.
  • Reconcile Avatar.Fallback delay changes.
  • Add regression and type tests for the affected Avatar paths.

Original findings addressed

  • [P2] Avatar.Image drops crossOrigin and referrerPolicy from the rendered <img>.
  • [P2] srcSet/sizes are part of the native image API but loading status only looks at src.
  • [P2] Removing a loaded Avatar.Image can leave fallback hidden forever.
  • [P3] Fallback.delay is one-shot state and does not reconcile prop changes.

@atomiks atomiks added type: bug It doesn't behave as expected. component: avatar Changes related to the avatar component. labels May 18, 2026
@pkg-pr-new

pkg-pr-new Bot commented May 18, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4835
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4835
    

commit: 7a544ea

@code-infra-dashboard

code-infra-dashboard Bot commented May 18, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+45B(+0.01%) 🔺+31B(+0.02%)

Details of bundle changes

Performance

Total duration: 1,188.71 ms -91.66 ms(-7.2%) | Renders: 78 (+0)

Test Duration Renders
Menu open (500 items) 59.71 ms ▼-16.54 ms(-21.7%) 12 (+0)

13 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@atomiks
atomiks force-pushed the codex/avatar-behavior-coverage branch from 6259b46 to 28a5958 Compare May 18, 2026 04:10
@netlify

netlify Bot commented May 18, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 6259b46
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a0a90668bb6360008672fe7
😎 Deploy Preview https://deploy-preview-4835--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented May 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 7a544ea
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a3259b4ecf1df00083ea265
😎 Deploy Preview https://deploy-preview-4835--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks marked this pull request as ready for review June 10, 2026 07:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses Avatar image-loading edge cases by improving native <img> prop forwarding and aligning the internal loading probe logic with responsive image inputs, while adding targeted regression + type coverage for the affected behaviors.

Changes:

  • Forward additional native image props (crossOrigin, referrerPolicy, srcSet, sizes) and use them consistently for both the rendered <img> and the loading-status probe.
  • Reset imageLoadingStatus when Avatar.Image unmounts so Avatar.Fallback can re-render after an image is removed.
  • Add regression tests and type tests covering the updated Avatar image/fallback behavior and public typing contract.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/react/src/avatar/image/useImageLoadingStatus.ts Extends the loading probe to support responsive image inputs (srcSet, sizes) and adjusts status initialization when src is missing.
packages/react/src/avatar/image/AvatarImage.tsx Uses broader element props for loading status, forwards status into root context, and resets status on unmount.
packages/react/src/avatar/image/AvatarImage.test.tsx Adds JSDOM regression tests for native prop forwarding, srcSet-only rendering, probe prop usage, and onLoadingStatusChange.
packages/react/src/avatar/fallback/AvatarFallback.tsx Updates delay handling to allow showing immediately when delay becomes undefined.
packages/react/src/avatar/fallback/AvatarFallback.test.tsx Adds regression coverage for image unmount fallback visibility and delay prop reconciliation.
packages/react/src/avatar/Avatar.spec.tsx Adds type-level assertions for ImageLoadingStatus and forwarded native <img> props.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@michaldudak michaldudak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR review

No merge-blocking issues found. The focused Avatar jsdom suites pass, but there is one small regression in dynamic delay behavior.

Bugs (1)

1. 🟡 Preserve visible fallback when delay is added after mount

Location: packages/react/src/avatar/fallback/AvatarFallback.tsx:23

const [delayPassed, setDelayPassed] = React.useState(false);

// ...

enabled: imageLoadingStatus !== 'loaded' && (delay === undefined || delayPassed),

When Avatar.Fallback starts without a delay, the fallback is visible immediately because delay === undefined. If the prop later changes to a number, delayPassed is still false, so the fallback disappears until the timer elapses. On master, delayPassed initialized to delay === undefined, so this transition kept the already-visible fallback mounted.

Failure scenario: A controlled avatar renders fallback immediately, then a settings/state update changes delay from undefined to 600; the initials disappear for 600ms even though the image is still missing or errored.

Fix: Initialize delayPassed with delay === undefined, and add the inverse regression test for undefined -> number.

Tests (0)

No findings.

Simplifications (0)

No findings.

Docs (0)

No findings.

Verdict

Approve after nits — one non-blocking dynamic prop regression should be fixed.

@atomiks
atomiks force-pushed the codex/avatar-behavior-coverage branch from dc40e44 to bbca359 Compare June 17, 2026 02:56
@atomiks
atomiks force-pushed the codex/avatar-behavior-coverage branch from bbca359 to 13295a4 Compare June 17, 2026 02:59

@michaldudak michaldudak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one non-blocking delay reconciliation bug. CI is green, and the rest of the Avatar image/status changes looked okay in this pass.

@michaldudak michaldudak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last issue Codex found looks like a very unlikely edge case. No need to fix it IMO if it increases the bundle size.

@atomiks
atomiks requested a review from jjenzz as a code owner June 17, 2026 08:04
@atomiks
atomiks merged commit 2d8b8f6 into mui:master Jun 17, 2026
6 checks passed
@atomiks
atomiks deleted the codex/avatar-behavior-coverage branch June 17, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: avatar Changes related to the avatar component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants