Skip to content

Commit

Permalink
refactor(avatar): use filterDOMProps approach
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed May 29, 2024
1 parent b4e5d85 commit d482b61
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions packages/components/avatar/src/use-avatar.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type {AvatarSlots, AvatarVariantProps, SlotsToClasses} from "@nextui-org/theme";

import {avatar} from "@nextui-org/theme";
import {HTMLNextUIProps, PropGetter, useProviderContext} from "@nextui-org/system";
import {
DOMAttributes,
DOMElement,
HTMLNextUIProps,
PropGetter,
useProviderContext,
} from "@nextui-org/system";
import {mergeProps} from "@react-aria/utils";
import {useDOMRef} from "@nextui-org/react-utils";
import {ReactRef, useDOMRef, filterDOMProps} from "@nextui-org/react-utils";
import {clsx, safeText, dataAttr} from "@nextui-org/shared-utils";
import {ReactRef} from "@nextui-org/react-utils";
import {useFocusRing} from "@react-aria/focus";
import {useMemo, useCallback} from "react";
import {useImage} from "@nextui-org/use-image";
Expand Down Expand Up @@ -141,6 +146,8 @@ export function useAvatar(originalProps: UseAvatarProps = {}) {

const isImgLoaded = imageStatus === "loaded";

const shouldFilterDOMProps = typeof ImgComponent === "string";

/**
* Fallback avatar applies under 2 conditions:
* - If `src` was passed and the image has not loaded or failed to load
Expand Down Expand Up @@ -196,26 +203,20 @@ export function useAvatar(originalProps: UseAvatarProps = {}) {
);

const getImageProps = useCallback<PropGetter>(
(props = {}) => {
let allowDisableAnimation = false;

if (
typeof ImgComponent !== "string" &&
(ImgComponent as any)?.render?.displayName === "NextUI.Image"
) {
allowDisableAnimation = true;
}

return {
ref: imgRef,
src: src,
...(allowDisableAnimation && {disableAnimation}),
"data-loaded": dataAttr(isImgLoaded),
className: slots.img({class: classNames?.img}),
...mergeProps(imgProps, props),
};
},
[slots, isImgLoaded, imgProps, disableAnimation, src, imgRef],
(props = {}) => ({
ref: imgRef,
src: src,
"data-loaded": dataAttr(isImgLoaded),
className: slots.img({class: classNames?.img}),
...mergeProps(
imgProps,
props,
filterDOMProps({disableAnimation} as DOMAttributes<DOMElement>, {
enabled: shouldFilterDOMProps,
}),
),
}),
[slots, isImgLoaded, imgProps, disableAnimation, src, imgRef, filterDOMProps],
);

return {
Expand Down

0 comments on commit d482b61

Please sign in to comment.