Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avatar doesn't scale when display is none #26522

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions components/avatar/__tests__/__snapshots__/Avatar.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,22 @@ exports[`Avatar Render should show image on success after a failure state 1`] =
className="ant-avatar ant-avatar-circle"
style={Object {}}
>
<span
className="ant-avatar-string"
style={
Object {
"WebkitTransform": "scale(0.72) translateX(-50%)",
"msTransform": "scale(0.72) translateX(-50%)",
"transform": "scale(0.72) translateX(-50%)",
}
}
<ResizeObserver
onResize={[Function]}
>
Fallback
</span>
<span
className="ant-avatar-string"
style={
Object {
"WebkitTransform": "scale(1) translateX(-50%)",
"msTransform": "scale(1) translateX(-50%)",
"transform": "scale(1) translateX(-50%)",
}
}
>
Fallback
</span>
</ResizeObserver>
</span>
</Avatar>
`;
Expand Down
49 changes: 18 additions & 31 deletions components/avatar/avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import classNames from 'classnames';
import ResizeObserver from 'rc-resize-observer';

import { ConfigContext } from '../config-provider';
import devWarning from '../_util/devWarning';
Expand Down Expand Up @@ -41,9 +42,6 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr

const avatarNodeMergeRef = composeRef(ref, avatarNodeRef);

let lastChildrenWidth: number;
let lastNodeWidth: number;

const { getPrefixCls } = React.useContext(ConfigContext);

const setScaleParam = () => {
Expand All @@ -52,19 +50,12 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr
}
const childrenWidth = avatarChildrenRef.current.offsetWidth; // offsetWidth avoid affecting be transform scale
const nodeWidth = avatarNodeRef.current.offsetWidth;
const { gap = 4 } = props;
// denominator is 0 is no meaning
if (
childrenWidth !== 0 &&
nodeWidth !== 0 &&
(lastChildrenWidth !== childrenWidth || lastNodeWidth !== nodeWidth)
) {
lastChildrenWidth = childrenWidth;
lastNodeWidth = nodeWidth;
}

if (gap * 2 < nodeWidth) {
setScale(nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1);
if (childrenWidth !== 0 && nodeWidth !== 0) {
const { gap = 4 } = props;
if (gap * 2 < nodeWidth) {
setScale(nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1);
}
}
};

Expand All @@ -79,13 +70,7 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr

React.useEffect(() => {
setScaleParam();
}, [props.children, props.gap, props.size]);

React.useEffect(() => {
if (props.children) {
setScaleParam();
}
}, [isImgExist]);
Copy link
Member

@zombieJ zombieJ Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

每次 effect 都算一次太耗费了,包一个 ResizeObserver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大佬,这里改好了

}, [props.gap]);

const handleImgLoadError = () => {
const { onError } = props;
Expand Down Expand Up @@ -161,15 +146,17 @@ const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (pr
: {};

childrenToRender = (
<span
className={`${prefixCls}-string`}
ref={(node: HTMLElement) => {
avatarChildrenRef.current = node;
}}
style={{ ...sizeChildrenStyle, ...childrenStyle }}
>
{children}
</span>
<ResizeObserver onResize={setScaleParam}>
<span
className={`${prefixCls}-string`}
ref={(node: HTMLElement) => {
avatarChildrenRef.current = node;
}}
style={{ ...sizeChildrenStyle, ...childrenStyle }}
>
{children}
</span>
</ResizeObserver>
);
} else {
childrenToRender = (
Expand Down