Skip to content

Commit 3c678d1

Browse files
committed
chore: refactor
1 parent 4d73def commit 3c678d1

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/components/UploadcareImage.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@ import React from 'react';
33
import { getInt } from '../utils/helpers';
44
import { uploadcareLoader } from '../utils/loader';
55

6+
const shouldOverrideBlurDataUrl = (props: ImageProps): boolean => {
7+
return props.placeholder === 'blur' && !props.blurDataURL;
8+
};
9+
10+
const generateBlurDataUrl = (
11+
src: string,
12+
width: ImageProps['width']
13+
): string => {
14+
const imageWidth = getInt(width);
15+
const blurImageWidth = imageWidth ? Math.ceil(imageWidth * 0.01) : 10;
16+
return uploadcareLoader({
17+
src: src,
18+
width: blurImageWidth,
19+
quality: 0
20+
});
21+
};
22+
623
export function UploadcareImage({
724
blurDataURL,
825
...props
926
}: ImageProps): JSX.Element {
10-
if (
11-
props.placeholder === 'blur' &&
12-
!blurDataURL &&
13-
typeof props.src === 'string'
14-
) {
15-
const imageWidth = getInt(props.width);
16-
const blurImageWidth = imageWidth ? Math.ceil(imageWidth * 0.01) : 10;
17-
blurDataURL = uploadcareLoader({
18-
src: props.src,
19-
width: blurImageWidth,
20-
quality: 0
21-
});
27+
if (typeof props.src === 'string' && shouldOverrideBlurDataUrl(props)) {
28+
blurDataURL = generateBlurDataUrl(props.src, props.width);
2229
}
2330
return (
2431
<Image loader={uploadcareLoader} blurDataURL={blurDataURL} {...props} />

0 commit comments

Comments
 (0)