Skip to content

Commit 95f5a08

Browse files
committed
refactor(SLB-354): simplify mock images
read natural width and height instead of fetching the image
1 parent 004931b commit 95f5a08

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed
Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use client';
2-
import { imageDimensionsFromData } from 'image-dimensions';
32
import {
43
createContext,
54
forwardRef,
65
PropsWithChildren,
76
useContext,
87
useEffect,
98
useRef,
9+
useState,
1010
} from 'react';
1111

1212
import {
@@ -31,6 +31,7 @@ export function ImageSettings({
3131
children,
3232
...settings
3333
}: PropsWithChildren<Partial<ImageSettingsType>>) {
34+
console.log('settings', settings.alterSrc);
3435
return (
3536
<ImageSettingsContext.Provider
3637
value={{ ...defaultImageSettings, ...settings }}
@@ -40,69 +41,49 @@ export function ImageSettings({
4041
);
4142
}
4243

43-
async function imageDimensions(src: string) {
44-
const response = await fetch(src);
45-
if (!response.body) {
46-
throw new Error('Failed to fetch image');
47-
}
48-
const buffer = await response.arrayBuffer();
49-
const data = new Uint8Array(buffer);
50-
const result = imageDimensionsFromData(data);
51-
return result;
52-
}
53-
5444
function sizerImage(width: number, height: number) {
5545
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}"></svg>`;
5646
return `data:image/svg+xml;base64,${base64(svg)}`;
5747
}
5848

5949
export const Image = forwardRef(function Image(
60-
{ focalPoint, ...props }: ImageProps,
50+
{ src, focalPoint, ...props }: ImageProps,
6151
ref,
6252
) {
6353
const alterSrc = useContext(ImageSettingsContext).alterSrc;
54+
const alteredSrc = alterSrc && src ? alterSrc(src) : src;
6455
const imageRef = useRef<HTMLImageElement | null>(null);
6556
useEffect(() => {
6657
const r = imageRef || ref;
67-
if (r.current && props.src) {
68-
const url = alterSrc ? alterSrc(props.src) : props.src;
69-
imageDimensions(url)
70-
.then((source) => {
71-
if (!r.current || !source) {
72-
return;
73-
}
74-
const target = inferTargetDimensions(
75-
source,
76-
props.width,
77-
props.height,
78-
);
79-
r.current.style.backgroundImage = `url(${url})`;
80-
r.current.style.backgroundSize = 'cover';
81-
r.current.style.maxWidth = '100%';
58+
if (r.current && alteredSrc) {
59+
const source = {
60+
width: r.current.naturalWidth,
61+
height: r.current.naturalHeight,
62+
};
63+
if (!r.current || !source) {
64+
return;
65+
}
66+
const target = inferTargetDimensions(source, props.width, props.height);
67+
r.current.style.backgroundImage = `url(${alteredSrc})`;
68+
r.current.style.backgroundSize = 'cover';
69+
r.current.style.maxWidth = '100%';
8270

83-
r.current.style.backgroundPosition = calculateFocusPosition(
84-
r.current.naturalWidth,
85-
r.current.naturalHeight,
86-
focalPoint || [
87-
r.current.naturalWidth / 2,
88-
r.current.naturalHeight / 2,
89-
],
90-
);
91-
r.current.src = sizerImage(target.width, target.height);
92-
return;
93-
})
94-
.catch((error) => {
95-
throw error;
96-
});
71+
r.current.style.backgroundPosition = calculateFocusPosition(
72+
r.current.naturalWidth,
73+
r.current.naturalHeight,
74+
focalPoint || [r.current.naturalWidth / 2, r.current.naturalHeight / 2],
75+
);
76+
r.current.src = sizerImage(target.width, target.height);
77+
return;
9778
}
9879
}, [
9980
imageRef,
10081
ref,
10182
focalPoint,
10283
props.width,
10384
props.height,
104-
props.src,
85+
alteredSrc,
10586
alterSrc,
10687
]);
107-
return <img ref={imageRef || ref} {...props} />;
88+
return <img src={alteredSrc} ref={imageRef || ref} {...props} />;
10889
});

0 commit comments

Comments
 (0)