Skip to content

Commit 27cd51f

Browse files
committed
set up repo
0 parents  commit 27cd51f

File tree

7 files changed

+2109
-0
lines changed

7 files changed

+2109
-0
lines changed

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel
35+
36+
# build output
37+
/dist/

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# next-image-from-file
2+
3+
A wrapper around [next/image](https://nextjs.org/docs/api-reference/next/image) that automatically sets `width` and `height`.
4+
5+
This is useful in testing and development-environments where `next-image-loader` is not available (e.G. [storybook](https://storybook.js.org/)).
6+
7+
## Usage
8+
9+
### Storybook
10+
```js
11+
// .storybook/preview.js
12+
import * as NextImage from 'next/image'
13+
import NextImageFromFile from 'next-image-from-file'
14+
15+
const OriginalNextImage = NextImage.default
16+
17+
Object.defineProperty(NextImage, 'default', {
18+
configurable: true,
19+
value: NextImageFromFile
20+
})
21+
```

next-env.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/types/global" />
3+
/// <reference types="next/image-types/global" />
4+
5+
// NOTE: This file should not be edited
6+
// see https://nextjs.org/docs/basic-features/typescript for more information.

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "next-image-from-file",
3+
"version": "0.1.0",
4+
"description": "A wrapper around next/image that automatically sets width and height.",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/farbenmeer/next-image-from-file.git"
13+
},
14+
"author": "farbenmeer GmbH",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/farbenmeer/next-image-from-file/issues"
18+
},
19+
"homepage": "https://github.com/farbenmeer/next-image-from-file#readme",
20+
"peerDependencies": {
21+
"next": "^11.1.2",
22+
"react": "^17.0.2",
23+
"react-dom": "^17.0.2"
24+
},
25+
"devDependencies": {
26+
"@types/react": "^17.0.22",
27+
"@types/react-dom": "^17.0.9",
28+
"next": "^11.1.2",
29+
"react": "^17.0.2",
30+
"react-dom": "^17.0.2",
31+
"typescript": "^4.4.3"
32+
}
33+
}

src/index.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Image, { ImageProps } from "next/image";
2+
import React from "react";
3+
4+
export default function NextImageFromFile(props: ImageProps) {
5+
const [image, setImage] = React.useState<null | {
6+
width: number;
7+
height: number;
8+
}>(null);
9+
10+
if (typeof props.src !== "string" || (props.width && props.height)) {
11+
return <Image {...props} unoptimized />;
12+
}
13+
14+
if (!image)
15+
return (
16+
<img
17+
src={props.src}
18+
onLoad={(event) => {
19+
const target = event.target as HTMLImageElement;
20+
setImage({ width: target.width, height: target.height });
21+
}}
22+
/>
23+
);
24+
25+
return (
26+
<Image
27+
{...props}
28+
src={props.src}
29+
width={image.width}
30+
height={image.height}
31+
unoptimized
32+
/>
33+
);
34+
}

tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist/",
4+
"strictNullChecks": true,
5+
"moduleResolution": "node",
6+
"module": "commonjs",
7+
"jsx": "react",
8+
"target": "ES6",
9+
"allowJs": true,
10+
"declaration": true,
11+
"allowSyntheticDefaultImports": true,
12+
"esModuleInterop": true,
13+
"noImplicitAny": true
14+
},
15+
"include": ["next-env.d.ts", "src/**/*"],
16+
"exclude": ["dist", "node_modules"]
17+
}

0 commit comments

Comments
 (0)