-
-
Notifications
You must be signed in to change notification settings - Fork 803
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6208f6c
commit 08e002f
Showing
6 changed files
with
48 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useLayoutEffect, useRef } from "react"; | ||
|
||
const DEFAULT_WIDTH = "200"; | ||
|
||
// Props used by the <img> element | ||
type IDetailImageProps = JSX.IntrinsicElements["img"]; | ||
|
||
export const DetailImage = (props: IDetailImageProps) => { | ||
const imgRef = useRef<HTMLImageElement>(null); | ||
|
||
function fixWidth() { | ||
const img = imgRef.current; | ||
if (!img) return; | ||
|
||
// prevent SVG's w/o intrinsic size from rendering as 0x0 | ||
if (img.naturalWidth === 0) { | ||
// If the naturalWidth is zero, it means the image either hasn't loaded yet | ||
// or we're on Firefox and it is an SVG w/o an intrinsic size. | ||
// So set the width to our fallback width. | ||
img.setAttribute("width", DEFAULT_WIDTH.toString()); | ||
} else { | ||
// If we have a `naturalWidth`, this could either be the actual intrinsic width | ||
// of the image, or the image is an SVG w/o an intrinsic size and we're on Chrome or Safari, | ||
// which seem to return a size calculated in some browser-specific way. | ||
// Worse yet, once rendered, Safari will then return the value of `img.width` as `img.naturalWidth`, | ||
// so we need to clone the image to disconnect it from the DOM, and then get the `naturalWidth` of the clone, | ||
// in order to always return the same `naturalWidth` for a given src. | ||
const i = img.cloneNode() as HTMLImageElement; | ||
img.setAttribute("width", (i.naturalWidth || DEFAULT_WIDTH).toString()); | ||
} | ||
} | ||
|
||
useLayoutEffect(() => { | ||
fixWidth(); | ||
}, [props.src]); | ||
|
||
return <img ref={imgRef} onLoad={() => fixWidth()} {...props} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters