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

feat(ImageBase): add prop objectPosition #8016

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/vkui/src/components/ImageBase/ImageBase.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
object-fit: scale-down;
}

.withObjectPosition {
--vkui_internal--ImageBase_object_position_default: 50% 50%;

object-position: var(
--vkui_internal--ImageBase_object_position,
var(--vkui_internal--ImageBase_object_position_default)
);
}

.loaded .img {
visibility: visible;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/vkui/src/components/ImageBase/ImageBase.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ describe(ImageBase, () => {
render(<ImageBaseTest fallbackIcon={<Icon20Add />} size={28} />);
expect(logStub).toHaveBeenCalledTimes(1);
});

it('should apply custom objectPosition style', () => {
render(<ImageBaseTest src="#" objectPosition="center bottom" />);

expect(getImageBaseImgEl()).toHaveClass(styles.withObjectPosition);
expect(getImageBaseImgEl()).toHaveStyle({
'--vkui_internal--ImageBase_object_position': 'center bottom',
});
});
});

describe(getOverlayIconSizeByImageBaseSize, () => {
Expand Down
38 changes: 29 additions & 9 deletions packages/vkui/src/components/ImageBase/ImageBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import { useRef } from 'react';
import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { mergeStyle } from '../../helpers/mergeStyle';
import { useExternRef } from '../../hooks/useExternRef';
import { minOr } from '../../lib/comparing';
import { getFetchPriorityProp } from '../../lib/utils';
import type { AnchorHTMLAttributesOnly, HasRef, HasRootRef, LiteralUnion } from '../../types';
import type {
AnchorHTMLAttributesOnly,
CSSCustomProperties,
HasRef,
HasRootRef,
LiteralUnion,
} from '../../types';
import { Clickable } from '../Clickable/Clickable';
import { ImageBaseBadge, type ImageBaseBadgeProps } from './ImageBaseBadge/ImageBaseBadge';
import {
Expand Down Expand Up @@ -94,6 +101,11 @@ export interface ImageBaseProps
* Подробнее можно почитать в [документации](https://developer.mozilla.org/ru/docs/Web/CSS/object-fit)
*/
objectFit?: React.CSSProperties['objectFit'];
/**
* Пользовательское значения стиля object-position
* Подробнее можно почитать в [документации](https://developer.mozilla.org/ru/docs/Web/CSS/object-position)
*/
objectPosition?: React.CSSProperties['objectPosition'];
/**
* Флаг для сохранения пропорций картинки.
* Для корректной работы необходимо задать размеры хотя бы одной стороны картинки
Expand Down Expand Up @@ -160,6 +172,7 @@ export const ImageBase: React.FC<ImageBaseProps> & {
onError,
withTransparentBackground,
objectFit = 'cover',
objectPosition,
keepAspectRatio = false,
getRootRef,
...restProps
Expand Down Expand Up @@ -238,6 +251,19 @@ export const ImageBase: React.FC<ImageBaseProps> & {
[size],
);

const imgStyles: CSSCustomProperties<string | number> | undefined = objectPosition
? {
'--vkui_internal--ImageBase_object_position': objectPosition,
}
: undefined;

const keepAspectRationStyles = keepAspectRatio
? {
width: widthImg || width,
height: heightImg || height,
}
: undefined;

return (
<ImageBaseContext.Provider value={contextValue}>
<Clickable
Expand All @@ -259,20 +285,14 @@ export const ImageBase: React.FC<ImageBaseProps> & {
className={classNames(
styles.img,
getObjectFitClassName(objectFit),
objectPosition && styles.withObjectPosition,
keepAspectRatio && styles.imgKeepRatio,
)}
crossOrigin={crossOrigin}
decoding={decoding}
loading={loading}
referrerPolicy={referrerPolicy}
style={
keepAspectRatio
? {
width: widthImg || width,
height: heightImg || height,
}
: undefined
}
style={mergeStyle(keepAspectRationStyles, imgStyles)}
sizes={sizes}
src={src}
srcSet={srcSet}
Expand Down
Loading