radix-ui / primitives

Radix Primitives is an open-source UI component library for building high-quality, accessible design systems and web apps. Maintained by @workos.
https://radix-ui.com/primitives
MIT License
15.03k stars 734 forks source link

Avatars flash on mount even if cached #2044

Open lauri865 opened 1 year ago

lauri865 commented 1 year ago

Bug report

Current Behavior

Avatars never render on first mount, even if cached.

Expected behavior

If avatar image is cached by the browser, the image should render straight away on initial mount.

Reproducible example

Refresh this page: https://www.radix-ui.com/docs/primitives/components/avatar

Suggested solution

Instead of loading the image in useEffect, the image loading should be started directly in the useState initializer. If the image is cached, image.complete of new Image will return true synchronously, and then the state value can be returned as "loaded" right away.

frykten commented 11 months ago

This ticket could be closed: https://github.com/radix-ui/primitives/pull/2340

lauri865 commented 11 months ago

@frykten, it does not fix this judging by the code. img.onload is not triggered synchronously / on the same tick, so simply changing to useLayoutEffect does nothing to detect on first render whether the image is loaded or not.

freshgiammi commented 10 months ago

This is also the case when providing a base64 string as src.

I'm expecting the component not to flash and show fallback, but render the image directly since there's no network request involved here. However, I see the state moving from loading to loaded, which in turn makes the fallback render when no delay is set.

Reproduction: https://codesandbox.io/s/radix-avatar-b64issue-rr24j8

pondorasti commented 6 months ago

+1

wouter173 commented 4 months ago

+1

romulovalez commented 3 weeks ago

+1