-
-
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 a687ee3
Showing
6 changed files
with
35 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,25 @@ | ||
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; | ||
|
||
// always force-set width property to naturalWidth, with fallback if zero | ||
// prevents SVG's w/o intrinsic size from rendering with size 0x0 | ||
img.setAttribute("width", (img.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