nextui/apps/docs/content/components/avatar/custom-impl.raw.tsx
WK 0d217e466f
refactor: optimization (#5362)
* chore(deps): bump RA versions

* chore(deps): bump RA versions

* chore(deps): bump RA versions

* chore: changeset

* chore(deps): remove unnecessary dependencies

* fix(calendar): typing issue

* refactor(system): remove unused SupportedCalendars

* refactor(system): move I18nProviderProps to type

* refactor: use `spectrumCalendarProps<DateValue>["createCalendar"]`

* feat: add consistent-type-imports

* fix: eslint

* chore: add changeset

* refactor: remove unused deps
2025-06-09 14:17:44 +08:00

65 lines
1.5 KiB
TypeScript

import type {AvatarProps as BaseAvatarProps} from "@heroui/react";
import {forwardRef, useMemo} from "react";
import {AvatarIcon, useAvatar} from "@heroui/react";
export interface AvatarProps extends BaseAvatarProps {}
const MyAvatar = forwardRef<HTMLSpanElement, AvatarProps>((props, ref) => {
const {
src,
icon = <AvatarIcon />,
alt,
classNames,
slots,
name,
showFallback,
fallback: fallbackComponent,
getInitials,
getAvatarProps,
getImageProps,
} = useAvatar({
ref,
...props,
});
const fallback = useMemo(() => {
if (!showFallback && src) return null;
const ariaLabel = alt || name || "avatar";
if (fallbackComponent) {
return (
<div
aria-label={ariaLabel}
className={slots.fallback({class: classNames?.fallback})}
role="img"
>
{fallbackComponent}
</div>
);
}
return name ? (
<span aria-label={ariaLabel} className={slots.name({class: classNames?.name})} role="img">
{getInitials(name)}
</span>
) : (
<span aria-label={ariaLabel} className={slots.icon({class: classNames?.icon})} role="img">
{icon}
</span>
);
}, [showFallback, src, fallbackComponent, name, classNames]);
return (
<div {...getAvatarProps()}>
{src && <img {...getImageProps()} alt={alt} />}
{fallback}
</div>
);
});
MyAvatar.displayName = "MyAvatar";
export default MyAvatar;