-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set dimensions on <img> tags automatically
The large images on projects page cause a layout shift when they load, this will fix that.
- Loading branch information
Showing
8 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
import { imageSize } from "image-size"; | ||
import path from "path"; | ||
import { STATIC_DIR } from "../consts.js"; | ||
import React from "react"; | ||
|
||
export function Img({ | ||
src, | ||
...rest | ||
}: { | ||
src?: string | undefined; | ||
className?: string | undefined; | ||
alt?: string | undefined; | ||
width?: string | number | undefined; | ||
height?: string | number | undefined; | ||
}) { | ||
if (src === undefined) { | ||
throw new Error("Image without src not supported"); | ||
} | ||
// For local files, read the file and determine the dimensions automatically. | ||
// Also acts as a check that the file exists. Since we convert to absolute | ||
// URLs for atom feeds, this will not trigger for those. | ||
if (!/http(s)?:\/\//.test(src)) { | ||
const { width, height } = imageSize(path.join(STATIC_DIR, src)); | ||
rest = { width, height, ...rest }; | ||
} | ||
|
||
return <img src={src} loading="lazy" {...rest} />; | ||
} |
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
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 |
---|---|---|
|
@@ -280,6 +280,7 @@ pre > pre { | |
|
||
img { | ||
max-width: 100%; | ||
height: auto; | ||
} | ||
|
||
.photo { | ||
|