-
Notifications
You must be signed in to change notification settings - Fork 776
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] Allow Avatar.Image
to be supplied a component
to render the image
#2230
Comments
Did you try this? <Avatar.Image asChild>
<NextImage />
</Avatar.Image> |
This will throw type errors because none of the required props are being supplied to |
For me this does not throw type errors, it just outright does not work. Instead it renders the |
I faced a similar issue. My workaround was to also supply <Avatar.Image asChild src='image.svg'>
<NextImage src='image.svg' alt='Some alt text' {...otherProps} />
</Avatar.Image> It's a little bit redundant, but it could be wrapped inside a wrapper component. I think it would be nice to make |
Another issue with having to supply the src on Avatar.Image, is that asset imports, which enables further build-time optimisation, is not possible as Avatar.Image would not accept it for the src value. |
This is not only redundant but highly inefficient. Just checked devtools and both the avatar and next-image components load the asset. So we end up loading the optimized image by next-image and then the avatar-image also loads the image. So, in the end there are 2 requests for the same image. |
The solution i am using currently is to not use the <Avatar.Root>
{imageSrc && <NextImage src={imageSrc} alt='Some alt text' {...otherProps} />}
{!imageSrc && <Avatar.Fallback>John Doe</Avatar.Fallback>}
</Avatar.Root> If |
I am facing the same issue. Fixing this would make radix way more viable for the people |
agreed, would be a really nice feature |
Any update on this? |
Any update? |
How to use custom components?import NextImage from 'next/image';
import * as Avatar from '@radix-ui/react-avatar';
const App = () => (
<Avatar.Root>
<Avatar.Image src="./img/url" alt="" width={20} height={20} asChild>
<Image />
</Avatar.Image>
<Avatar.Fallback delayMs={600}>Fallback</Avatar.Fallback>
</Avatar.Root>
);
Why does the image load twice?I couldn't resolve this bug, but it seems to be related to Next.js rather than Radix Primitive. I ran the following demo in both pure React and Next.js environments, and found that the pure React application loaded the image only once, while the Next.js one loaded it twice. "use client"; // just for next.js
const CustomImg = React.forwardRef((props, forwardRef) => <img {...props} ref={forwardedRef} />);
const Page = () => (
<Avatar.Root>
<Avatar.Image src="./img/url" alt="" asChild>
<CustomImg />
</Avatar.Image>
<Avatar.Fallback delayMs={600}>Fallback</Avatar.Fallback>
</Avatar.Root>
); Additionally, from the source code, it appears that the Avatar component is not the issue, because the part that handles const Primitive = NODES.reduce((primitive, node) => {
const Node = React.forwardRef(
(props: PrimitivePropsWithRef<typeof node>, forwardedRef: any) => {
const { asChild, ...primitiveProps } = props;
+ const Comp: any = asChild ? Slot : node;
React.useEffect(() => {
(window as any)[Symbol.for('radix-ui')] = true;
}, []);
return <Comp {...primitiveProps} ref={forwardedRef} />;
},
);
Node.displayName = `Primitive.${node}`;
return { ...primitive, [node]: Node };
}, {} as Primitives); |
Unfortunately, this approach will cause the Fallback feature to fail 😢. |
According to changes made in #2999, this code is no longer executed via Avatar.Image. What I mean is, Avatar.Image |
Feature request
Overview
The
Avatar.Image
primitive assumes I want to use animg
without the ability to override that. I'd like to use the Next.js Image component instead for its optimizations. If I try to use the Next Image component instead of the primitive, the Fallback fails to work properly.Examples in other libraries
Other libraries, such as Material UI, allow you to pass an optional
component
as a prop.Who does this impact? Who is this for?
This would affect anyone utilizing the Next.js framework who wants to use it's image optimizations.
Other Notes
If there are any other primitives that make use of images, allowing overrides on them would be greatly appreciated.
The text was updated successfully, but these errors were encountered: