163 lines
5.5 KiB
JavaScript
163 lines
5.5 KiB
JavaScript
"use client";
|
|
|
|
import * as React from 'react';
|
|
import ResizeObserver from '@rc-component/resize-observer';
|
|
import { composeRef } from "@rc-component/util/es/ref";
|
|
import { clsx } from 'clsx';
|
|
import { isNumber, isPlainObject } from '../_util/is';
|
|
import { responsiveArray } from '../_util/responsiveObserver';
|
|
import { devUseWarning } from '../_util/warning';
|
|
import { useComponentConfig } from '../config-provider/context';
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
|
import useSize from '../config-provider/hooks/useSize';
|
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
|
import AvatarContext from './AvatarContext';
|
|
import useStyle from './style';
|
|
const Avatar = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
const {
|
|
prefixCls: customizePrefixCls,
|
|
shape,
|
|
size: customSize,
|
|
src,
|
|
srcSet,
|
|
icon,
|
|
className,
|
|
rootClassName,
|
|
style,
|
|
alt,
|
|
draggable,
|
|
children,
|
|
crossOrigin,
|
|
gap = 4,
|
|
onError,
|
|
...others
|
|
} = props;
|
|
const [scale, setScale] = React.useState(1);
|
|
const [mounted, setMounted] = React.useState(false);
|
|
const [isImgExist, setIsImgExist] = React.useState(true);
|
|
const avatarNodeRef = React.useRef(null);
|
|
const avatarChildrenRef = React.useRef(null);
|
|
const avatarNodeMergedRef = composeRef(ref, avatarNodeRef);
|
|
const {
|
|
getPrefixCls,
|
|
className: contextClassName,
|
|
style: contextStyle
|
|
} = useComponentConfig('avatar');
|
|
const avatarCtx = React.useContext(AvatarContext);
|
|
const setScaleParam = () => {
|
|
if (!avatarChildrenRef.current || !avatarNodeRef.current) {
|
|
return;
|
|
}
|
|
const childrenWidth = avatarChildrenRef.current.offsetWidth; // offsetWidth avoid affecting be transform scale
|
|
const nodeWidth = avatarNodeRef.current.offsetWidth;
|
|
// denominator is 0 is no meaning
|
|
if (childrenWidth !== 0 && nodeWidth !== 0) {
|
|
if (gap * 2 < nodeWidth) {
|
|
setScale(nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1);
|
|
}
|
|
}
|
|
};
|
|
React.useEffect(() => {
|
|
setMounted(true);
|
|
}, []);
|
|
React.useEffect(() => {
|
|
setIsImgExist(true);
|
|
setScale(1);
|
|
}, [src]);
|
|
React.useEffect(setScaleParam, [gap]);
|
|
const handleImgLoadError = () => {
|
|
const errorFlag = onError?.();
|
|
if (errorFlag !== false) {
|
|
setIsImgExist(false);
|
|
}
|
|
};
|
|
const size = useSize(ctxSize => customSize ?? avatarCtx?.size ?? ctxSize ?? 'medium');
|
|
const needResponsive = Object.keys(isPlainObject(size) ? size || {} : {}).some(key => responsiveArray.includes(key));
|
|
const screens = useBreakpoint(needResponsive);
|
|
const responsiveSizeStyle = React.useMemo(() => {
|
|
if (!isPlainObject(size)) {
|
|
return {};
|
|
}
|
|
const currentBreakpoint = responsiveArray.find(screen => screens[screen]);
|
|
const currentSize = size[currentBreakpoint];
|
|
return currentSize ? {
|
|
width: currentSize,
|
|
height: currentSize,
|
|
fontSize: currentSize && (icon || children) ? currentSize / 2 : 18
|
|
} : {};
|
|
}, [screens, size, icon, children]);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
const warning = devUseWarning('Avatar');
|
|
process.env.NODE_ENV !== "production" ? warning(!(typeof icon === 'string' && icon.length > 2), 'breaking', `\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`) : void 0;
|
|
}
|
|
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
|
|
const rootCls = useCSSVarCls(prefixCls);
|
|
const [hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
|
const sizeCls = clsx({
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
[`${prefixCls}-sm`]: size === 'small'
|
|
});
|
|
const hasImageElement = /*#__PURE__*/React.isValidElement(src);
|
|
const mergedShape = shape || avatarCtx?.shape || 'circle';
|
|
const classString = clsx(prefixCls, sizeCls, contextClassName, `${prefixCls}-${mergedShape}`, {
|
|
[`${prefixCls}-image`]: hasImageElement || src && isImgExist,
|
|
[`${prefixCls}-icon`]: !!icon
|
|
}, cssVarCls, rootCls, className, rootClassName, hashId);
|
|
const sizeStyle = isNumber(size) ? {
|
|
width: size,
|
|
height: size,
|
|
fontSize: icon ? size / 2 : 18
|
|
} : {};
|
|
let childrenToRender;
|
|
if (typeof src === 'string' && isImgExist) {
|
|
childrenToRender = /*#__PURE__*/React.createElement("img", {
|
|
src: src,
|
|
draggable: draggable,
|
|
srcSet: srcSet,
|
|
onError: handleImgLoadError,
|
|
alt: alt,
|
|
crossOrigin: crossOrigin
|
|
});
|
|
} else if (hasImageElement) {
|
|
childrenToRender = src;
|
|
} else if (icon) {
|
|
childrenToRender = icon;
|
|
} else if (mounted || scale !== 1) {
|
|
const transformString = `scale(${scale})`;
|
|
const childrenStyle = {
|
|
msTransform: transformString,
|
|
WebkitTransform: transformString,
|
|
transform: transformString
|
|
};
|
|
childrenToRender = /*#__PURE__*/React.createElement(ResizeObserver, {
|
|
onResize: setScaleParam
|
|
}, /*#__PURE__*/React.createElement("span", {
|
|
className: `${prefixCls}-string`,
|
|
ref: avatarChildrenRef,
|
|
style: childrenStyle
|
|
}, children));
|
|
} else {
|
|
childrenToRender = /*#__PURE__*/React.createElement("span", {
|
|
className: `${prefixCls}-string`,
|
|
style: {
|
|
opacity: 0
|
|
},
|
|
ref: avatarChildrenRef
|
|
}, children);
|
|
}
|
|
return /*#__PURE__*/React.createElement("span", {
|
|
...others,
|
|
style: {
|
|
...sizeStyle,
|
|
...responsiveSizeStyle,
|
|
...contextStyle,
|
|
...style
|
|
},
|
|
className: classString,
|
|
ref: avatarNodeMergedRef
|
|
}, childrenToRender);
|
|
});
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Avatar.displayName = 'Avatar';
|
|
}
|
|
export default Avatar; |