How to use Avatar with NextJS Images #3147
Answered
by
mulfyx
quangnmwork
asked this question in
Q&A
-
Hi ,I am currently using Ark UI component. When I try to configure Image with Ark.UI's Avatar.Image it doesn't work. It can't display the image of Image Nextjs. Reproduced code:
Is my usage correct or not. Or can anyone show me how to use it with Nextjs Image. |
Beta Was this translation helpful? Give feedback.
Answered by
mulfyx
Dec 24, 2024
Replies: 2 comments
-
Here's an example of how to make it work: 'use client';
import { Avatar, useAvatarContext } from '@ark-ui/react/avatar';
import Image, { ImageProps } from 'next/image';
const AvatarNextImage = (props: ImageProps) => {
const avatar = useAvatarContext();
const { hidden, ...imageProps } = avatar.getImageProps() as ImageProps;
return (
<Image
{...imageProps}
{...props}
style={{
...props.style,
visibility: hidden ? 'hidden' : 'visible',
objectFit: 'cover',
}}
/>
);
};
export default function Page() {
return (
<div>
<Avatar.Root
style={{
position: 'relative',
['--size' as string]: '80px',
}}
>
<Avatar.Fallback
style={{
position: 'absolute',
inset: '0',
borderRadius: '9999px',
width: 'var(--size)',
height: 'var(--size)',
background: 'gray',
display: 'grid',
placeItems: 'center',
}}
>
PA
</Avatar.Fallback>
<AvatarNextImage
src="https://picsum.photos/id/237/200/300"
alt="hi"
width={80}
height={80}
style={{
position: 'absolute',
inset: '0',
borderRadius: '9999px',
width: 'var(--size)',
height: 'var(--size)',
}}
/>
</Avatar.Root>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Use import { Avatar as ArkAvatar } from "@ark-ui/react";
import { clsx } from "clsx";
import {
getImageProps as getNextImageProps,
type ImageProps,
type StaticImageData,
} from "next/image";
import { type AvatarProps } from "./avatar.types";
import * as styles from "./avatar.css";
function getName(firstName: string, lastName?: string) {
return lastName != null ? `${firstName} ${lastName}` : firstName;
}
function getInitials(firstName: string, lastName?: string) {
if (lastName == null) {
return firstName.slice(0, 2).toUpperCase();
}
return `${firstName[0]}${lastName[0]}`.toUpperCase();
}
function getImageProps(image: StaticImageData, name: string) {
const props: ImageProps = {
src: image,
alt: name,
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { loading, decoding, style, ...imageProps } =
getNextImageProps(props).props;
return imageProps;
}
export function Avatar({ className, firstName, lastName, image }: AvatarProps) {
const name = getName(firstName, lastName);
const initials = getInitials(firstName, lastName);
const imageProps = image != null ? getImageProps(image, name) : null;
return (
<ArkAvatar.Root className={clsx(styles.root, className)} title={name}>
<ArkAvatar.Fallback className={styles.initials}>
{initials}
</ArkAvatar.Fallback>
{imageProps != null && (
<ArkAvatar.Image
{...imageProps}
className={clsx(imageProps.className, styles.image)}
/>
)}
</ArkAvatar.Root>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
segunadebayo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
getImageProps
: https://nextjs.org/docs/app/api-reference/components/image#getimageprops