Skip to content

Commit

Permalink
feat(Image): add prop filter (#8171)
Browse files Browse the repository at this point in the history
* feat(Image): add prop filter

* fix(Image): fix prettier
  • Loading branch information
EldarMuhamethanov authored Jan 23, 2025
1 parent 9205131 commit bff743b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
13 changes: 13 additions & 0 deletions packages/vkui/src/components/Image/Image.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ const story: Meta<ImageProps> = {
title: 'Blocks/Image',
component: Image,
parameters: { ...CanvasFullLayout, ...DisableCartesianParam },
argTypes: {
filter: {
control: { type: 'select' },
options: ['blur', 'contrast', 'grayscale', 'hue-rotate', 'drop-shadow'],
mapping: {
'blur': 'blur(5px)',
'contrast': 'contrast(200%)',
'grayscale': 'grayscale(80%)',
'hue-rotate': 'hue-rotate(90deg)',
'drop-shadow': 'drop-shadow(16px 16px 20px red) invert(75%)',
},
},
},
};

export default story;
Expand Down
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 @@ -80,6 +80,15 @@
);
}

.withFilter {
--vkui_internal--ImageBase_object_filter_default: none;

filter: var(
--vkui_internal--ImageBase_object_filter,
var(--vkui_internal--ImageBase_object_filter_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 @@ -223,6 +223,15 @@ describe(ImageBase, () => {
});
});

it('should apply custom filter style', () => {
render(<ImageBaseTest src="#" filter="blur(5px)" />);

expect(getImageBaseImgEl()).toHaveClass(styles.withFilter);
expect(getImageBaseImgEl()).toHaveStyle({
'--vkui_internal--ImageBase_object_filter': 'blur(5px)',
});
});

describe('DEV errros', () => {
beforeEach(() => setNodeEnv('development'));
afterEach(() => setNodeEnv('test'));
Expand Down
21 changes: 16 additions & 5 deletions packages/vkui/src/components/ImageBase/ImageBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export interface ImageBaseProps
* см. https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/elementtiming
*/
elementTiming?: string;
/**
* Пользовательское значения стиля filter
* Подробнее можно почитать в [документации](https://developer.mozilla.org/ru/docs/Web/CSS/filter)
*/
filter?: React.CSSProperties['filter'];
}

const getObjectFitClassName = (objectFit: React.CSSProperties['objectFit']) => {
Expand Down Expand Up @@ -177,6 +182,7 @@ export const ImageBase: React.FC<ImageBaseProps> & {
withTransparentBackground,
objectFit = 'cover',
objectPosition,
filter,
keepAspectRatio = false,
getRootRef,
elementTiming,
Expand Down Expand Up @@ -256,11 +262,15 @@ export const ImageBase: React.FC<ImageBaseProps> & {
[size],
);

const imgStyles: CSSCustomProperties<string | number> | undefined = objectPosition
? {
'--vkui_internal--ImageBase_object_position': objectPosition,
}
: undefined;
const imgStyles:
| CSSCustomProperties<React.CSSProperties['objectPosition'] | React.CSSProperties['filter']>
| undefined =
objectPosition || filter
? {
'--vkui_internal--ImageBase_object_position': objectPosition,
'--vkui_internal--ImageBase_object_filter': filter,
}
: undefined;

const keepAspectRationStyles = keepAspectRatio
? {
Expand Down Expand Up @@ -291,6 +301,7 @@ export const ImageBase: React.FC<ImageBaseProps> & {
styles.img,
getObjectFitClassName(objectFit),
objectPosition && styles.withObjectPosition,
filter && styles.withFilter,
keepAspectRatio && styles.imgKeepRatio,
)}
crossOrigin={crossOrigin}
Expand Down

0 comments on commit bff743b

Please sign in to comment.