Skip to content

Commit eab9d31

Browse files
committed
fix: onImagePress when no explicit width and height attrs present
1 parent 1c76620 commit eab9d31

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

src/web/usePressInteractions.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function usePressInteractions(
3131
if (image && container.contains(image)) {
3232
e.preventDefault();
3333

34-
const imageAttributes = parseImageAttributes(image);
34+
const imageAttributes = getImageAttributes(image);
3535

3636
if (imageAttributes) {
3737
onImagePressRef.current?.({
@@ -46,23 +46,41 @@ export function usePressInteractions(
4646
}, [containerRef, onImagePressRef]);
4747
}
4848

49-
function parseImageAttributes(image: HTMLElement): ImageAttributes | undefined {
49+
function getImageAttributes(
50+
image: HTMLImageElement
51+
): ImageAttributes | undefined {
5052
const uri = image.getAttribute('src');
53+
54+
if (!uri) {
55+
return undefined;
56+
}
57+
58+
const { width, height } = getImageDimensions(image);
59+
60+
return {
61+
uri,
62+
width,
63+
height,
64+
};
65+
}
66+
67+
function getImageDimensions(image: HTMLImageElement): {
68+
width: number;
69+
height: number;
70+
} {
5171
const rawWidth = image.getAttribute('width');
5272
const rawHeight = image.getAttribute('height');
5373

54-
if (uri && rawWidth && rawHeight) {
74+
if (rawWidth && rawHeight) {
5575
const width = parseInt(rawWidth, 10);
5676
const height = parseInt(rawHeight, 10);
5777

5878
if (!isNaN(width) && !isNaN(height)) {
59-
return {
60-
uri,
61-
width,
62-
height,
63-
};
79+
return { width, height };
6480
}
6581
}
6682

67-
return undefined;
83+
const rect = image.getBoundingClientRect();
84+
85+
return { width: rect.width, height: rect.height };
6886
}

0 commit comments

Comments
 (0)