sunny-img 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/index.cjs +108 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sunny-img contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# sunny-img
|
|
2
|
+
|
|
3
|
+
A lightweight, framework-agnostic React image component inspired by `next/image`.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add sunny-img
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { Image } from 'sunny-img'
|
|
11
|
+
|
|
12
|
+
export function Hero() {
|
|
13
|
+
return <Image alt="Sunlight crossing a mountain ridge" height={800} src="/hero.jpg" width={1200} />
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`sunny-img` provides intrinsic or fill layouts, native lazy loading, priority
|
|
18
|
+
loading, custom-loader `srcSet` generation, blur placeholders, and ref
|
|
19
|
+
forwarding. It does not run an image optimization server; connect `loader` to
|
|
20
|
+
your image CDN when you want transformed image URLs.
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let react = require("react");
|
|
25
|
+
react = __toESM(react);
|
|
26
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
27
|
+
react_jsx_runtime = __toESM(react_jsx_runtime);
|
|
28
|
+
|
|
29
|
+
//#region src/image.tsx
|
|
30
|
+
const imageWidths = [
|
|
31
|
+
320,
|
|
32
|
+
480,
|
|
33
|
+
640,
|
|
34
|
+
750,
|
|
35
|
+
828,
|
|
36
|
+
1080,
|
|
37
|
+
1200,
|
|
38
|
+
1600,
|
|
39
|
+
1920,
|
|
40
|
+
2560
|
|
41
|
+
];
|
|
42
|
+
function uniqueWidths(width, sizes) {
|
|
43
|
+
if (sizes?.includes("vw") || width === void 0) return [...imageWidths];
|
|
44
|
+
return [width, width * 2];
|
|
45
|
+
}
|
|
46
|
+
const Image = (0, react.forwardRef)(function Image$1({ alt, blurDataURL, fill = false, height, loader, loading, onLoad, onLoadingComplete, placeholder = "empty", priority = false, quality, sizes, src, style, width,...props }, ref) {
|
|
47
|
+
const [loaded, setLoaded] = (0, react.useState)(false);
|
|
48
|
+
if (!fill && (width === void 0 || height === void 0)) throw new Error("sunny-img: width and height are required unless fill is true.");
|
|
49
|
+
if (placeholder === "blur" && !blurDataURL) throw new Error("sunny-img: blurDataURL is required when placeholder is \"blur\".");
|
|
50
|
+
const widths = uniqueWidths(width, sizes);
|
|
51
|
+
const resolvedSrc = loader ? loader({
|
|
52
|
+
src,
|
|
53
|
+
width: widths.at(-1) ?? width ?? imageWidths[0],
|
|
54
|
+
quality
|
|
55
|
+
}) : src;
|
|
56
|
+
const srcSet = loader ? widths.map((candidate) => `${loader({
|
|
57
|
+
src,
|
|
58
|
+
width: candidate,
|
|
59
|
+
quality
|
|
60
|
+
})} ${candidate}w`).join(", ") : void 0;
|
|
61
|
+
const blurStyles = placeholder === "blur" && !loaded ? {
|
|
62
|
+
backgroundImage: `url("${blurDataURL}")`,
|
|
63
|
+
backgroundPosition: "center",
|
|
64
|
+
backgroundRepeat: "no-repeat",
|
|
65
|
+
backgroundSize: "cover",
|
|
66
|
+
filter: "blur(16px)",
|
|
67
|
+
transform: "scale(1.04)"
|
|
68
|
+
} : {};
|
|
69
|
+
const fillStyles = fill ? {
|
|
70
|
+
bottom: 0,
|
|
71
|
+
height: "100%",
|
|
72
|
+
left: 0,
|
|
73
|
+
position: "absolute",
|
|
74
|
+
right: 0,
|
|
75
|
+
top: 0,
|
|
76
|
+
width: "100%"
|
|
77
|
+
} : {};
|
|
78
|
+
function handleLoad(event) {
|
|
79
|
+
setLoaded(true);
|
|
80
|
+
onLoad?.(event);
|
|
81
|
+
onLoadingComplete?.(event.currentTarget);
|
|
82
|
+
}
|
|
83
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
|
|
84
|
+
...props,
|
|
85
|
+
ref,
|
|
86
|
+
alt,
|
|
87
|
+
"data-sunny-img": "",
|
|
88
|
+
decoding: "async",
|
|
89
|
+
fetchPriority: priority ? "high" : props.fetchPriority,
|
|
90
|
+
height: fill ? void 0 : height,
|
|
91
|
+
loading: loading ?? (priority ? "eager" : "lazy"),
|
|
92
|
+
onLoad: handleLoad,
|
|
93
|
+
sizes,
|
|
94
|
+
src: resolvedSrc,
|
|
95
|
+
srcSet,
|
|
96
|
+
style: {
|
|
97
|
+
...fillStyles,
|
|
98
|
+
...blurStyles,
|
|
99
|
+
...style
|
|
100
|
+
},
|
|
101
|
+
width: fill ? void 0 : width
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
exports.Image = Image;
|
|
107
|
+
exports.imageWidths = imageWidths;
|
|
108
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Image","blurStyles: CSSProperties","fillStyles: CSSProperties"],"sources":["../src/image.tsx"],"sourcesContent":["import {\n forwardRef,\n useState,\n type CSSProperties,\n type ImgHTMLAttributes,\n type SyntheticEvent,\n} from 'react'\n\nexport const imageWidths = [320, 480, 640, 750, 828, 1080, 1200, 1600, 1920, 2560] as const\n\nexport interface ImageLoaderParams {\n src: string\n width: number\n quality?: number\n}\n\nexport type ImageLoader = (params: ImageLoaderParams) => string\n\nexport interface SunnyImageProps\n extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'alt' | 'height' | 'loading' | 'src' | 'width'> {\n alt: string\n src: string\n width?: number\n height?: number\n fill?: boolean\n loader?: ImageLoader\n quality?: number\n priority?: boolean\n loading?: 'eager' | 'lazy'\n placeholder?: 'empty' | 'blur'\n blurDataURL?: string\n onLoadingComplete?: (image: HTMLImageElement) => void\n}\n\nfunction uniqueWidths(width?: number, sizes?: string): number[] {\n if (sizes?.includes('vw') || width === undefined) {\n return [...imageWidths]\n }\n\n return [width, width * 2]\n}\n\nexport const Image = forwardRef<HTMLImageElement, SunnyImageProps>(\n function Image(\n {\n alt,\n blurDataURL,\n fill = false,\n height,\n loader,\n loading,\n onLoad,\n onLoadingComplete,\n placeholder = 'empty',\n priority = false,\n quality,\n sizes,\n src,\n style,\n width,\n ...props\n },\n ref,\n ) {\n const [loaded, setLoaded] = useState(false)\n\n if (!fill && (width === undefined || height === undefined)) {\n throw new Error('sunny-img: width and height are required unless fill is true.')\n }\n\n if (placeholder === 'blur' && !blurDataURL) {\n throw new Error('sunny-img: blurDataURL is required when placeholder is \"blur\".')\n }\n\n const widths = uniqueWidths(width, sizes)\n const resolvedSrc = loader\n ? loader({ src, width: widths.at(-1) ?? width ?? imageWidths[0], quality })\n : src\n const srcSet = loader\n ? widths.map((candidate) => `${loader({ src, width: candidate, quality })} ${candidate}w`).join(', ')\n : undefined\n const blurStyles: CSSProperties =\n placeholder === 'blur' && !loaded\n ? {\n backgroundImage: `url(\"${blurDataURL}\")`,\n backgroundPosition: 'center',\n backgroundRepeat: 'no-repeat',\n backgroundSize: 'cover',\n filter: 'blur(16px)',\n transform: 'scale(1.04)',\n }\n : {}\n const fillStyles: CSSProperties = fill\n ? {\n bottom: 0,\n height: '100%',\n left: 0,\n position: 'absolute',\n right: 0,\n top: 0,\n width: '100%',\n }\n : {}\n\n function handleLoad(event: SyntheticEvent<HTMLImageElement>): void {\n setLoaded(true)\n onLoad?.(event)\n onLoadingComplete?.(event.currentTarget)\n }\n\n return (\n <img\n {...props}\n ref={ref}\n alt={alt}\n data-sunny-img=\"\"\n decoding=\"async\"\n fetchPriority={priority ? 'high' : props.fetchPriority}\n height={fill ? undefined : height}\n loading={loading ?? (priority ? 'eager' : 'lazy')}\n onLoad={handleLoad}\n sizes={sizes}\n src={resolvedSrc}\n srcSet={srcSet}\n style={{ ...fillStyles, ...blurStyles, ...style }}\n width={fill ? undefined : width}\n />\n )\n },\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,MAAa,cAAc;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;CAAM;CAAM;CAAM;CAAM;CAAK;AA0BlF,SAAS,aAAa,OAAgB,OAA0B;AAC9D,KAAI,OAAO,SAAS,KAAK,IAAI,UAAU,OACrC,QAAO,CAAC,GAAG,YAAY;AAGzB,QAAO,CAAC,OAAO,QAAQ,EAAE;;AAG3B,MAAa,8BACX,SAASA,QACP,EACE,KACA,aACA,OAAO,OACP,QACA,QACA,SACA,QACA,mBACA,cAAc,SACd,WAAW,OACX,SACA,OACA,KACA,OACA,MACA,GAAG,SAEL,KACA;CACA,MAAM,CAAC,QAAQ,iCAAsB,MAAM;AAE3C,KAAI,CAAC,SAAS,UAAU,UAAa,WAAW,QAC9C,OAAM,IAAI,MAAM,gEAAgE;AAGlF,KAAI,gBAAgB,UAAU,CAAC,YAC7B,OAAM,IAAI,MAAM,mEAAiE;CAGnF,MAAM,SAAS,aAAa,OAAO,MAAM;CACzC,MAAM,cAAc,SAChB,OAAO;EAAE;EAAK,OAAO,OAAO,GAAG,GAAG,IAAI,SAAS,YAAY;EAAI;EAAS,CAAC,GACzE;CACJ,MAAM,SAAS,SACX,OAAO,KAAK,cAAc,GAAG,OAAO;EAAE;EAAK,OAAO;EAAW;EAAS,CAAC,CAAC,GAAG,UAAU,GAAG,CAAC,KAAK,KAAK,GACnG;CACJ,MAAMC,aACJ,gBAAgB,UAAU,CAAC,SACvB;EACE,iBAAiB,QAAQ,YAAY;EACrC,oBAAoB;EACpB,kBAAkB;EAClB,gBAAgB;EAChB,QAAQ;EACR,WAAW;EACZ,GACD,EAAE;CACR,MAAMC,aAA4B,OAC9B;EACE,QAAQ;EACR,QAAQ;EACR,MAAM;EACN,UAAU;EACV,OAAO;EACP,KAAK;EACL,OAAO;EACR,GACD,EAAE;CAEN,SAAS,WAAW,OAA+C;AACjE,YAAU,KAAK;AACf,WAAS,MAAM;AACf,sBAAoB,MAAM,cAAc;;AAG1C,QACE,2CAAC;EACC,GAAI;EACC;EACA;EACL,kBAAe;EACf,UAAS;EACT,eAAe,WAAW,SAAS,MAAM;EACzC,QAAQ,OAAO,SAAY;EAC3B,SAAS,YAAY,WAAW,UAAU;EAC1C,QAAQ;EACD;EACP,KAAK;EACG;EACR,OAAO;GAAE,GAAG;GAAY,GAAG;GAAY,GAAG;GAAO;EACjD,OAAO,OAAO,SAAY;GAC1B;EAGP"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as react0 from "react";
|
|
2
|
+
import { ImgHTMLAttributes } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/image.d.ts
|
|
5
|
+
declare const imageWidths: readonly [320, 480, 640, 750, 828, 1080, 1200, 1600, 1920, 2560];
|
|
6
|
+
interface ImageLoaderParams {
|
|
7
|
+
src: string;
|
|
8
|
+
width: number;
|
|
9
|
+
quality?: number;
|
|
10
|
+
}
|
|
11
|
+
type ImageLoader = (params: ImageLoaderParams) => string;
|
|
12
|
+
interface SunnyImageProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'alt' | 'height' | 'loading' | 'src' | 'width'> {
|
|
13
|
+
alt: string;
|
|
14
|
+
src: string;
|
|
15
|
+
width?: number;
|
|
16
|
+
height?: number;
|
|
17
|
+
fill?: boolean;
|
|
18
|
+
loader?: ImageLoader;
|
|
19
|
+
quality?: number;
|
|
20
|
+
priority?: boolean;
|
|
21
|
+
loading?: 'eager' | 'lazy';
|
|
22
|
+
placeholder?: 'empty' | 'blur';
|
|
23
|
+
blurDataURL?: string;
|
|
24
|
+
onLoadingComplete?: (image: HTMLImageElement) => void;
|
|
25
|
+
}
|
|
26
|
+
declare const Image: react0.ForwardRefExoticComponent<SunnyImageProps & react0.RefAttributes<HTMLImageElement>>;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { Image, type ImageLoader, type ImageLoaderParams, type SunnyImageProps, imageWidths };
|
|
29
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as react0 from "react";
|
|
2
|
+
import { ImgHTMLAttributes } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/image.d.ts
|
|
5
|
+
declare const imageWidths: readonly [320, 480, 640, 750, 828, 1080, 1200, 1600, 1920, 2560];
|
|
6
|
+
interface ImageLoaderParams {
|
|
7
|
+
src: string;
|
|
8
|
+
width: number;
|
|
9
|
+
quality?: number;
|
|
10
|
+
}
|
|
11
|
+
type ImageLoader = (params: ImageLoaderParams) => string;
|
|
12
|
+
interface SunnyImageProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'alt' | 'height' | 'loading' | 'src' | 'width'> {
|
|
13
|
+
alt: string;
|
|
14
|
+
src: string;
|
|
15
|
+
width?: number;
|
|
16
|
+
height?: number;
|
|
17
|
+
fill?: boolean;
|
|
18
|
+
loader?: ImageLoader;
|
|
19
|
+
quality?: number;
|
|
20
|
+
priority?: boolean;
|
|
21
|
+
loading?: 'eager' | 'lazy';
|
|
22
|
+
placeholder?: 'empty' | 'blur';
|
|
23
|
+
blurDataURL?: string;
|
|
24
|
+
onLoadingComplete?: (image: HTMLImageElement) => void;
|
|
25
|
+
}
|
|
26
|
+
declare const Image: react0.ForwardRefExoticComponent<SunnyImageProps & react0.RefAttributes<HTMLImageElement>>;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { Image, type ImageLoader, type ImageLoaderParams, type SunnyImageProps, imageWidths };
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { forwardRef, useState } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/image.tsx
|
|
5
|
+
const imageWidths = [
|
|
6
|
+
320,
|
|
7
|
+
480,
|
|
8
|
+
640,
|
|
9
|
+
750,
|
|
10
|
+
828,
|
|
11
|
+
1080,
|
|
12
|
+
1200,
|
|
13
|
+
1600,
|
|
14
|
+
1920,
|
|
15
|
+
2560
|
|
16
|
+
];
|
|
17
|
+
function uniqueWidths(width, sizes) {
|
|
18
|
+
if (sizes?.includes("vw") || width === void 0) return [...imageWidths];
|
|
19
|
+
return [width, width * 2];
|
|
20
|
+
}
|
|
21
|
+
const Image = forwardRef(function Image$1({ alt, blurDataURL, fill = false, height, loader, loading, onLoad, onLoadingComplete, placeholder = "empty", priority = false, quality, sizes, src, style, width,...props }, ref) {
|
|
22
|
+
const [loaded, setLoaded] = useState(false);
|
|
23
|
+
if (!fill && (width === void 0 || height === void 0)) throw new Error("sunny-img: width and height are required unless fill is true.");
|
|
24
|
+
if (placeholder === "blur" && !blurDataURL) throw new Error("sunny-img: blurDataURL is required when placeholder is \"blur\".");
|
|
25
|
+
const widths = uniqueWidths(width, sizes);
|
|
26
|
+
const resolvedSrc = loader ? loader({
|
|
27
|
+
src,
|
|
28
|
+
width: widths.at(-1) ?? width ?? imageWidths[0],
|
|
29
|
+
quality
|
|
30
|
+
}) : src;
|
|
31
|
+
const srcSet = loader ? widths.map((candidate) => `${loader({
|
|
32
|
+
src,
|
|
33
|
+
width: candidate,
|
|
34
|
+
quality
|
|
35
|
+
})} ${candidate}w`).join(", ") : void 0;
|
|
36
|
+
const blurStyles = placeholder === "blur" && !loaded ? {
|
|
37
|
+
backgroundImage: `url("${blurDataURL}")`,
|
|
38
|
+
backgroundPosition: "center",
|
|
39
|
+
backgroundRepeat: "no-repeat",
|
|
40
|
+
backgroundSize: "cover",
|
|
41
|
+
filter: "blur(16px)",
|
|
42
|
+
transform: "scale(1.04)"
|
|
43
|
+
} : {};
|
|
44
|
+
const fillStyles = fill ? {
|
|
45
|
+
bottom: 0,
|
|
46
|
+
height: "100%",
|
|
47
|
+
left: 0,
|
|
48
|
+
position: "absolute",
|
|
49
|
+
right: 0,
|
|
50
|
+
top: 0,
|
|
51
|
+
width: "100%"
|
|
52
|
+
} : {};
|
|
53
|
+
function handleLoad(event) {
|
|
54
|
+
setLoaded(true);
|
|
55
|
+
onLoad?.(event);
|
|
56
|
+
onLoadingComplete?.(event.currentTarget);
|
|
57
|
+
}
|
|
58
|
+
return /* @__PURE__ */ jsx("img", {
|
|
59
|
+
...props,
|
|
60
|
+
ref,
|
|
61
|
+
alt,
|
|
62
|
+
"data-sunny-img": "",
|
|
63
|
+
decoding: "async",
|
|
64
|
+
fetchPriority: priority ? "high" : props.fetchPriority,
|
|
65
|
+
height: fill ? void 0 : height,
|
|
66
|
+
loading: loading ?? (priority ? "eager" : "lazy"),
|
|
67
|
+
onLoad: handleLoad,
|
|
68
|
+
sizes,
|
|
69
|
+
src: resolvedSrc,
|
|
70
|
+
srcSet,
|
|
71
|
+
style: {
|
|
72
|
+
...fillStyles,
|
|
73
|
+
...blurStyles,
|
|
74
|
+
...style
|
|
75
|
+
},
|
|
76
|
+
width: fill ? void 0 : width
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
export { Image, imageWidths };
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["Image","blurStyles: CSSProperties","fillStyles: CSSProperties"],"sources":["../src/image.tsx"],"sourcesContent":["import {\n forwardRef,\n useState,\n type CSSProperties,\n type ImgHTMLAttributes,\n type SyntheticEvent,\n} from 'react'\n\nexport const imageWidths = [320, 480, 640, 750, 828, 1080, 1200, 1600, 1920, 2560] as const\n\nexport interface ImageLoaderParams {\n src: string\n width: number\n quality?: number\n}\n\nexport type ImageLoader = (params: ImageLoaderParams) => string\n\nexport interface SunnyImageProps\n extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'alt' | 'height' | 'loading' | 'src' | 'width'> {\n alt: string\n src: string\n width?: number\n height?: number\n fill?: boolean\n loader?: ImageLoader\n quality?: number\n priority?: boolean\n loading?: 'eager' | 'lazy'\n placeholder?: 'empty' | 'blur'\n blurDataURL?: string\n onLoadingComplete?: (image: HTMLImageElement) => void\n}\n\nfunction uniqueWidths(width?: number, sizes?: string): number[] {\n if (sizes?.includes('vw') || width === undefined) {\n return [...imageWidths]\n }\n\n return [width, width * 2]\n}\n\nexport const Image = forwardRef<HTMLImageElement, SunnyImageProps>(\n function Image(\n {\n alt,\n blurDataURL,\n fill = false,\n height,\n loader,\n loading,\n onLoad,\n onLoadingComplete,\n placeholder = 'empty',\n priority = false,\n quality,\n sizes,\n src,\n style,\n width,\n ...props\n },\n ref,\n ) {\n const [loaded, setLoaded] = useState(false)\n\n if (!fill && (width === undefined || height === undefined)) {\n throw new Error('sunny-img: width and height are required unless fill is true.')\n }\n\n if (placeholder === 'blur' && !blurDataURL) {\n throw new Error('sunny-img: blurDataURL is required when placeholder is \"blur\".')\n }\n\n const widths = uniqueWidths(width, sizes)\n const resolvedSrc = loader\n ? loader({ src, width: widths.at(-1) ?? width ?? imageWidths[0], quality })\n : src\n const srcSet = loader\n ? widths.map((candidate) => `${loader({ src, width: candidate, quality })} ${candidate}w`).join(', ')\n : undefined\n const blurStyles: CSSProperties =\n placeholder === 'blur' && !loaded\n ? {\n backgroundImage: `url(\"${blurDataURL}\")`,\n backgroundPosition: 'center',\n backgroundRepeat: 'no-repeat',\n backgroundSize: 'cover',\n filter: 'blur(16px)',\n transform: 'scale(1.04)',\n }\n : {}\n const fillStyles: CSSProperties = fill\n ? {\n bottom: 0,\n height: '100%',\n left: 0,\n position: 'absolute',\n right: 0,\n top: 0,\n width: '100%',\n }\n : {}\n\n function handleLoad(event: SyntheticEvent<HTMLImageElement>): void {\n setLoaded(true)\n onLoad?.(event)\n onLoadingComplete?.(event.currentTarget)\n }\n\n return (\n <img\n {...props}\n ref={ref}\n alt={alt}\n data-sunny-img=\"\"\n decoding=\"async\"\n fetchPriority={priority ? 'high' : props.fetchPriority}\n height={fill ? undefined : height}\n loading={loading ?? (priority ? 'eager' : 'lazy')}\n onLoad={handleLoad}\n sizes={sizes}\n src={resolvedSrc}\n srcSet={srcSet}\n style={{ ...fillStyles, ...blurStyles, ...style }}\n width={fill ? undefined : width}\n />\n )\n },\n)\n"],"mappings":";;;;AAQA,MAAa,cAAc;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;CAAM;CAAM;CAAM;CAAM;CAAK;AA0BlF,SAAS,aAAa,OAAgB,OAA0B;AAC9D,KAAI,OAAO,SAAS,KAAK,IAAI,UAAU,OACrC,QAAO,CAAC,GAAG,YAAY;AAGzB,QAAO,CAAC,OAAO,QAAQ,EAAE;;AAG3B,MAAa,QAAQ,WACnB,SAASA,QACP,EACE,KACA,aACA,OAAO,OACP,QACA,QACA,SACA,QACA,mBACA,cAAc,SACd,WAAW,OACX,SACA,OACA,KACA,OACA,MACA,GAAG,SAEL,KACA;CACA,MAAM,CAAC,QAAQ,aAAa,SAAS,MAAM;AAE3C,KAAI,CAAC,SAAS,UAAU,UAAa,WAAW,QAC9C,OAAM,IAAI,MAAM,gEAAgE;AAGlF,KAAI,gBAAgB,UAAU,CAAC,YAC7B,OAAM,IAAI,MAAM,mEAAiE;CAGnF,MAAM,SAAS,aAAa,OAAO,MAAM;CACzC,MAAM,cAAc,SAChB,OAAO;EAAE;EAAK,OAAO,OAAO,GAAG,GAAG,IAAI,SAAS,YAAY;EAAI;EAAS,CAAC,GACzE;CACJ,MAAM,SAAS,SACX,OAAO,KAAK,cAAc,GAAG,OAAO;EAAE;EAAK,OAAO;EAAW;EAAS,CAAC,CAAC,GAAG,UAAU,GAAG,CAAC,KAAK,KAAK,GACnG;CACJ,MAAMC,aACJ,gBAAgB,UAAU,CAAC,SACvB;EACE,iBAAiB,QAAQ,YAAY;EACrC,oBAAoB;EACpB,kBAAkB;EAClB,gBAAgB;EAChB,QAAQ;EACR,WAAW;EACZ,GACD,EAAE;CACR,MAAMC,aAA4B,OAC9B;EACE,QAAQ;EACR,QAAQ;EACR,MAAM;EACN,UAAU;EACV,OAAO;EACP,KAAK;EACL,OAAO;EACR,GACD,EAAE;CAEN,SAAS,WAAW,OAA+C;AACjE,YAAU,KAAK;AACf,WAAS,MAAM;AACf,sBAAoB,MAAM,cAAc;;AAG1C,QACE,oBAAC;EACC,GAAI;EACC;EACA;EACL,kBAAe;EACf,UAAS;EACT,eAAe,WAAW,SAAS,MAAM;EACzC,QAAQ,OAAO,SAAY;EAC3B,SAAS,YAAY,WAAW,UAAU;EAC1C,QAAQ;EACD;EACP,KAAK;EACG;EACR,OAAO;GAAE,GAAG;GAAY,GAAG;GAAY,GAAG;GAAO;EACjD,OAAO,OAAO,SAAY;GAC1B;EAGP"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sunny-img",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A lightweight React image component inspired by next/image",
|
|
5
|
+
"keywords": ["react", "image", "responsive-image", "lazy-loading"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsdown",
|
|
22
|
+
"dev": "tsdown --watch",
|
|
23
|
+
"prepublishOnly": "pnpm test && pnpm typecheck && pnpm build",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"typecheck": "tsc --noEmit"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": ">=18.2.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/react": "^19.2.0",
|
|
32
|
+
"@types/react-dom": "^19.2.0",
|
|
33
|
+
"react": "^19.2.0",
|
|
34
|
+
"react-dom": "^19.2.0",
|
|
35
|
+
"tsdown": "^0.15.0",
|
|
36
|
+
"typescript": "^6.0.2",
|
|
37
|
+
"vitest": "^3.2.4"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": false
|
|
45
|
+
}
|
|
46
|
+
}
|