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): keepAspectRation=true by default #7838

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vkui/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const Avatar: React.FC<AvatarProps> & {
initials,
fallbackIcon: fallbackIconProp,
children,
keepAspectRatio = true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Мы такое поведение правили 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

прикольно, но я думаю кейс с аватаркой у которой соотношение сторон может быть разным - очень редкий кейс

Copy link
Contributor

Choose a reason for hiding this comment

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

Тогда можно переименовать в disableAspectRatio или что-то вроде того. Я думаю, что Вика это имела ввиду.

Copy link
Contributor

@inomdzhon inomdzhon Oct 29, 2024

Choose a reason for hiding this comment

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

Тогда можно переименовать в disableAspectRatio или что-то вроде того. Я думаю, что Вика это имела ввиду.

Как вариант


@qurle напомни, почему в целом хочется сделать true?

Copy link
Contributor

Choose a reason for hiding this comment

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

@qurle напомни, почему в целом хочется сделать true?

Мне кажется, что я мог неправильно понять назначение пропа, приду в личку.

...restProps
}: AvatarProps) => {
const gradientName =
Expand All @@ -109,6 +110,7 @@ export const Avatar: React.FC<AvatarProps> & {
return (
<ImageBase
{...restProps}
keepAspectRatio={keepAspectRatio}
size={size}
fallbackIcon={fallbackIcon}
className={classNames(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe(ImageBase, () => {
});

it('should have className with keepRatio', () => {
render(<ImageBaseTest src="#" keepAspectRatio={true} />);
render(<ImageBaseTest src="#" keepAspectRatio={true} widthSize="100%" />);

expect(getImageBaseImgEl()).toHaveClass(styles.imgKeepRatio);
});
Expand Down
14 changes: 9 additions & 5 deletions packages/vkui/src/components/ImageBase/ImageBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export interface ImageBaseProps
objectFit?: React.CSSProperties['objectFit'];
/**
* Флаг для сохранения пропорций картинки.
* Для корректной работы необходимо задать размеры хотя бы одной стороны картинки
* Для корректной работы необходимо задать размеры одной стороны картинки
*/
keepAspectRatio?: boolean;
}
Expand Down Expand Up @@ -153,10 +153,14 @@ export const ImageBase: React.FC<ImageBaseProps> & {
keepAspectRatio = false,
...restProps
}: ImageBaseProps) => {
const oneOfTwoDimensionsDefined =
(widthSize !== undefined && heightSize === undefined) ||
(widthSize === undefined && heightSize !== undefined);
const needToKeepAspectRatio = keepAspectRatio && oneOfTwoDimensionsDefined;
const size = sizeProp ?? minOr([sizeToNumber(widthSize), sizeToNumber(heightSize)], defaultSize);

const width = widthSize ?? (keepAspectRatio ? undefined : size);
const height = heightSize ?? (keepAspectRatio ? undefined : size);
const width = widthSize ?? (needToKeepAspectRatio ? undefined : size);
const height = heightSize ?? (needToKeepAspectRatio ? undefined : size);

const [loaded, setLoaded] = React.useState(false);
const [failed, setFailed] = React.useState(false);
Expand Down Expand Up @@ -224,14 +228,14 @@ export const ImageBase: React.FC<ImageBaseProps> & {
className={classNames(
styles.img,
getObjectFitClassName(objectFit),
keepAspectRatio && styles.imgKeepRatio,
needToKeepAspectRatio && styles.imgKeepRatio,
)}
crossOrigin={crossOrigin}
decoding={decoding}
loading={loading}
referrerPolicy={referrerPolicy}
style={
keepAspectRatio
needToKeepAspectRatio
? {
width: widthImg || width,
height: heightImg || height,
Expand Down