Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Avatar] Support cross origin in useImageLoadingStatus #1433

Merged
merged 5 commits into from
Feb 13, 2025

Conversation

ISnackable
Copy link
Contributor

Description

Add crossOrigin Support to useImageLoadingStatus Hook.

This PR adds crossOrigin support to the useImageLoadingStatus hook to handle images hosted on external domains that require specific crossOrigin settings (anonymous or use-credentials).

By including this option, we ensure broader compatibility with external image sources and resolve potential loading issues.

Note: radix-ui/primitives(https://github.com/radix-ui/primitives) has this exact issue and already has a PR radix-ui/primitives#3261. So here's a corresponding PR for Base UI 😊

Copy link

netlify bot commented Feb 7, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit bda28ef
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/67ad5b4c9d943600089a7320
😎 Deploy Preview https://deploy-preview-1433--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@zannager zannager added the component: avatar This is the name of the generic UI component, not the React module! label Feb 9, 2025
Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!
I added a few comments. Could you also run pnpm proptypes and commit the changed files after you resolve them so proptypes definitions are generated?

packages/react/src/avatar/image/useImageLoadingStatus.ts Outdated Show resolved Hide resolved
packages/react/src/avatar/image/useImageLoadingStatus.ts Outdated Show resolved Hide resolved
@ISnackable
Copy link
Contributor Author

My bad, I didn't even notice the proptypes. 😅 I think that should be all, do let me know if there's anything else. Thanks!

}
if (options?.crossOrigin) {
image.crossOrigin = options.crossOrigin;
if (typeof crossOrigin === 'string') {
Copy link
Member

Choose a reason for hiding this comment

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

Is this check necessary? If crossOrigin is undefined, it'll be passed as such to image and it should still work, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, according to MDM CrossOrigin Docs:

The crossorigin content attribute on media elements is a CORS settings attribute.

These attributes are enumerated, and have the following possible values:

anonymous
Request uses CORS headers and credentials flag is set to 'same-origin'. There is no exchange of user credentials via cookies, client-side TLS certificates or HTTP authentication, unless destination is the same origin.

use-credentials
Request uses CORS headers, credentials flag is set to 'include' and user credentials are always included.

""
Setting the attribute name to an empty value, like crossorigin or crossorigin="", is the same as anonymous.

An invalid keyword and an empty string will be handled as the anonymous keyword.

Assuming that an image is loading from a cors server, without the image.crossOrigin set, the image.src will hit a cors error, then image.onload will never be called. Which mean the image won't render. So by checking if it's a string type (empty string/invalid strings), this way we can just treat it as how MDM defined crossOrigin.

Hopefully that make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In short, we would want to make sure "" is assigned to image.crossOrigin.

Copy link
Member

Choose a reason for hiding this comment

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

I meant if an invalid or undefined crossOrigin is passed to AvatarImage, it should be forwarded as-is to useImageLoadingStatus. By adding if (typeof crossOrigin === 'string') {, we're actually changing the behavior described in MDN, because if someone set <AvatarImage crossOrigin={42}>, this implementation would not set crossOrigin on an image at all, whereas, according to MDN, it should treat it as "anonymous".

Copy link
Contributor Author

@ISnackable ISnackable Feb 13, 2025

Choose a reason for hiding this comment

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

Yes, you're right. Fixed it. Had to do an nullish check since image.crossOrigin doesn't accept an undefined type.

Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

Thanks, it looks good now!

@michaldudak michaldudak merged commit 36da5a5 into mui:master Feb 13, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: avatar This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants