ui icon indicating copy to clipboard operation
ui copied to clipboard

Avatar component makes 2 calls to the image src

Open 0xdhrv opened this issue 2 years ago • 4 comments

The Avatar component makes 2 calls in every render to the image src

image

0xdhrv avatar Feb 09 '23 04:02 0xdhrv

Sorry, isn't this related to React Strictmode?

dBianchii avatar Apr 09 '23 21:04 dBianchii

The screenshot depicts the live version of the ui.shadcn.com also, trying pure radix avatar doesn't seem to have this effect of repeated calls

0xdhrv avatar Apr 10 '23 03:04 0xdhrv

Oh I see. Thought you were using it in your own app @0xdhrv

dBianchii avatar Apr 10 '23 11:04 dBianchii

When trying the pure radix avatar in next.js i still get the double fetch. But if you change the radix <AvatarPrimitive.Image /> to the <Image /> component from next.js then the issue resolves... This also seems to have a dramatic effect on performance due to the image optimization.

But anyway this is probably an issue with radix..

FYI. i have also tried to run the <AvatarPrimitive.Image asChild /> with the image component. I get the same issue..

Here is my Avatar component, not perfect.

"use client";

import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";

import { cn } from "@/lib/utils";
import Image from "next/image";

const Avatar = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Root
    ref={ref}
    className={cn(
      "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full",
      className
    )}
    {...props}
  />
));
Avatar.displayName = AvatarPrimitive.Root.displayName;

const AvatarImage = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Image>,
  React.ComponentPropsWithoutRef<typeof Image>
>(({ className, src, alt, ...props }, ref) => (
  <Image
    src={src}
    alt={alt}
    {...props}
    className={cn("aspect-square h-full w-full", className)}
  />
));
AvatarImage.displayName = AvatarPrimitive.Image.displayName;

const AvatarFallback = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Fallback>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Fallback
    ref={ref}
    className={cn(
      "flex h-full w-full items-center justify-center rounded-full bg-neutral-100 dark:bg-neutral-800",
      className
    )}
    {...props}
  />
));
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName;

export { Avatar, AvatarImage, AvatarFallback };

marcusarnfast avatar Aug 05 '23 00:08 marcusarnfast

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jul 10 '24 23:07 shadcn