@vx-oss/heroui-v2-use-image 2.1.14-alpha.0

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 ADDED
@@ -0,0 +1,32 @@
1
+ SPDX-License-Identifier: MIT
2
+
3
+ The MIT License (MIT)
4
+
5
+ Parameters
6
+
7
+ Creator / Maintainer : Vezham Technologies Private Limited
8
+ Original Author : NextUI Inc
9
+ Licensor : Vezham Technologies Private Limited
10
+
11
+ Copyright © 2025, Vezham Technologies Private Limited. All rights reserved.
12
+ Copyright (c) 2020, NextUI Inc.
13
+
14
+ ---
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in all
24
+ copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @vx-oss/heroui-v2-use-image
2
+
3
+ A Quick description of the component
4
+
5
+ > This is an internal utility, not intended for public usage.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ yarn add @vx-oss/heroui-v2-use-image
11
+ # or
12
+ npm i @vx-oss/heroui-v2-use-image
13
+ ```
14
+
15
+ ## Contribution
16
+
17
+ Yes please! See the
18
+ [contributing guidelines](https://github.com/heroui-inc/heroui/blob/master/CONTRIBUTING.md)
19
+ for details.
20
+
21
+ ## License
22
+
23
+ This project is licensed under the terms of the
24
+ [MIT license](https://github.com/heroui-inc/heroui/blob/master/LICENSE).
@@ -0,0 +1,69 @@
1
+ import { ImgHTMLAttributes } from 'react';
2
+
3
+ /**
4
+ * Part of this code is taken from @chakra-ui/react package ❤️
5
+ */
6
+
7
+ type NativeImageProps = ImgHTMLAttributes<HTMLImageElement>;
8
+ interface UseImageProps {
9
+ /**
10
+ * The image `src` attribute
11
+ */
12
+ src?: string;
13
+ /**
14
+ * The image `srcset` attribute
15
+ */
16
+ srcSet?: string;
17
+ /**
18
+ * The image `sizes` attribute
19
+ */
20
+ sizes?: string;
21
+ /**
22
+ * A callback for when the image `src` has been loaded
23
+ */
24
+ onLoad?: NativeImageProps["onLoad"];
25
+ /**
26
+ * A callback for when there was an error loading the image `src`
27
+ */
28
+ onError?: NativeImageProps["onError"];
29
+ /**
30
+ * If `true`, opt out of the `fallbackSrc` logic and use as `img`
31
+ */
32
+ ignoreFallback?: boolean;
33
+ /**
34
+ * The key used to set the crossOrigin on the HTMLImageElement into which the image will be loaded.
35
+ * This tells the browser to request cross-origin access when trying to download the image data.
36
+ */
37
+ crossOrigin?: NativeImageProps["crossOrigin"];
38
+ /**
39
+ * Defines the `loading` attribute for the image
40
+ */
41
+ loading?: NativeImageProps["loading"];
42
+ /**
43
+ * If `true`, image load will be bypassed and the load will be handled by `as` component.
44
+ */
45
+ shouldBypassImageLoad?: boolean;
46
+ }
47
+ type Status = "loading" | "failed" | "pending" | "loaded";
48
+ type FallbackStrategy = "onError" | "beforeLoadOrError";
49
+ /**
50
+ * React hook that loads an image in the browser,
51
+ * and lets us know the `status` so we can show image
52
+ * fallback if it is still `pending`
53
+ *
54
+ * @returns the status of the image loading progress
55
+ *
56
+ * @example
57
+ *
58
+ * ```jsx
59
+ * function App(){
60
+ * const status = useImage({ src: "image.png" })
61
+ * return status === "loaded" ? <img src="image.png" /> : <Placeholder />
62
+ * }
63
+ * ```
64
+ */
65
+ declare function useImage(props?: UseImageProps): Status;
66
+ declare const shouldShowFallbackImage: (status: Status, fallbackStrategy: FallbackStrategy) => boolean;
67
+ type UseImageReturn = ReturnType<typeof useImage>;
68
+
69
+ export { type FallbackStrategy, type UseImageProps, type UseImageReturn, shouldShowFallbackImage, useImage };
@@ -0,0 +1,69 @@
1
+ import { ImgHTMLAttributes } from 'react';
2
+
3
+ /**
4
+ * Part of this code is taken from @chakra-ui/react package ❤️
5
+ */
6
+
7
+ type NativeImageProps = ImgHTMLAttributes<HTMLImageElement>;
8
+ interface UseImageProps {
9
+ /**
10
+ * The image `src` attribute
11
+ */
12
+ src?: string;
13
+ /**
14
+ * The image `srcset` attribute
15
+ */
16
+ srcSet?: string;
17
+ /**
18
+ * The image `sizes` attribute
19
+ */
20
+ sizes?: string;
21
+ /**
22
+ * A callback for when the image `src` has been loaded
23
+ */
24
+ onLoad?: NativeImageProps["onLoad"];
25
+ /**
26
+ * A callback for when there was an error loading the image `src`
27
+ */
28
+ onError?: NativeImageProps["onError"];
29
+ /**
30
+ * If `true`, opt out of the `fallbackSrc` logic and use as `img`
31
+ */
32
+ ignoreFallback?: boolean;
33
+ /**
34
+ * The key used to set the crossOrigin on the HTMLImageElement into which the image will be loaded.
35
+ * This tells the browser to request cross-origin access when trying to download the image data.
36
+ */
37
+ crossOrigin?: NativeImageProps["crossOrigin"];
38
+ /**
39
+ * Defines the `loading` attribute for the image
40
+ */
41
+ loading?: NativeImageProps["loading"];
42
+ /**
43
+ * If `true`, image load will be bypassed and the load will be handled by `as` component.
44
+ */
45
+ shouldBypassImageLoad?: boolean;
46
+ }
47
+ type Status = "loading" | "failed" | "pending" | "loaded";
48
+ type FallbackStrategy = "onError" | "beforeLoadOrError";
49
+ /**
50
+ * React hook that loads an image in the browser,
51
+ * and lets us know the `status` so we can show image
52
+ * fallback if it is still `pending`
53
+ *
54
+ * @returns the status of the image loading progress
55
+ *
56
+ * @example
57
+ *
58
+ * ```jsx
59
+ * function App(){
60
+ * const status = useImage({ src: "image.png" })
61
+ * return status === "loaded" ? <img src="image.png" /> : <Placeholder />
62
+ * }
63
+ * ```
64
+ */
65
+ declare function useImage(props?: UseImageProps): Status;
66
+ declare const shouldShowFallbackImage: (status: Status, fallbackStrategy: FallbackStrategy) => boolean;
67
+ type UseImageReturn = ReturnType<typeof useImage>;
68
+
69
+ export { type FallbackStrategy, type UseImageProps, type UseImageReturn, shouldShowFallbackImage, useImage };
package/dist/index.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ shouldShowFallbackImage: () => shouldShowFallbackImage,
24
+ useImage: () => useImage
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_react = require("react");
28
+ var import_heroui_v2_react_utils = require("@vx-oss/heroui-v2-react-utils");
29
+ var import_heroui_v2_use_safe_layout_effect = require("@vx-oss/heroui-v2-use-safe-layout-effect");
30
+ function useImage(props = {}) {
31
+ const {
32
+ onLoad,
33
+ onError,
34
+ ignoreFallback,
35
+ src,
36
+ crossOrigin,
37
+ srcSet,
38
+ sizes,
39
+ loading,
40
+ shouldBypassImageLoad = false
41
+ } = props;
42
+ const isHydrated = (0, import_heroui_v2_react_utils.useIsHydrated)();
43
+ const imageRef = (0, import_react.useRef)(isHydrated ? new Image() : null);
44
+ const [status, setStatus] = (0, import_react.useState)("pending");
45
+ (0, import_react.useEffect)(() => {
46
+ if (!imageRef.current) return;
47
+ imageRef.current.onload = (event) => {
48
+ flush();
49
+ setStatus("loaded");
50
+ onLoad == null ? void 0 : onLoad(event);
51
+ };
52
+ imageRef.current.onerror = (error) => {
53
+ flush();
54
+ setStatus("failed");
55
+ onError == null ? void 0 : onError(error);
56
+ };
57
+ }, [imageRef.current]);
58
+ const flush = () => {
59
+ if (imageRef.current) {
60
+ imageRef.current.onload = null;
61
+ imageRef.current.onerror = null;
62
+ imageRef.current = null;
63
+ }
64
+ };
65
+ const load = (0, import_react.useCallback)(() => {
66
+ if (!src) return "pending";
67
+ if (ignoreFallback || shouldBypassImageLoad) return "loaded";
68
+ const img = new Image();
69
+ img.src = src;
70
+ if (crossOrigin) img.crossOrigin = crossOrigin;
71
+ if (srcSet) img.srcset = srcSet;
72
+ if (sizes) img.sizes = sizes;
73
+ if (loading) img.loading = loading;
74
+ imageRef.current = img;
75
+ if (img.complete && img.naturalWidth) {
76
+ return "loaded";
77
+ }
78
+ return "loading";
79
+ }, [src, crossOrigin, srcSet, sizes, onLoad, onError, loading, shouldBypassImageLoad]);
80
+ (0, import_heroui_v2_use_safe_layout_effect.useSafeLayoutEffect)(() => {
81
+ if (isHydrated) {
82
+ setStatus(load());
83
+ }
84
+ }, [isHydrated, load]);
85
+ return ignoreFallback ? "loaded" : status;
86
+ }
87
+ var shouldShowFallbackImage = (status, fallbackStrategy) => status !== "loaded" && fallbackStrategy === "beforeLoadOrError" || status === "failed" && fallbackStrategy === "onError";
88
+ // Annotate the CommonJS export names for ESM import in node:
89
+ 0 && (module.exports = {
90
+ shouldShowFallbackImage,
91
+ useImage
92
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,66 @@
1
+ // src/index.ts
2
+ import { useRef, useState, useEffect, useCallback } from "react";
3
+ import { useIsHydrated } from "@vx-oss/heroui-v2-react-utils";
4
+ import { useSafeLayoutEffect } from "@vx-oss/heroui-v2-use-safe-layout-effect";
5
+ function useImage(props = {}) {
6
+ const {
7
+ onLoad,
8
+ onError,
9
+ ignoreFallback,
10
+ src,
11
+ crossOrigin,
12
+ srcSet,
13
+ sizes,
14
+ loading,
15
+ shouldBypassImageLoad = false
16
+ } = props;
17
+ const isHydrated = useIsHydrated();
18
+ const imageRef = useRef(isHydrated ? new Image() : null);
19
+ const [status, setStatus] = useState("pending");
20
+ useEffect(() => {
21
+ if (!imageRef.current) return;
22
+ imageRef.current.onload = (event) => {
23
+ flush();
24
+ setStatus("loaded");
25
+ onLoad == null ? void 0 : onLoad(event);
26
+ };
27
+ imageRef.current.onerror = (error) => {
28
+ flush();
29
+ setStatus("failed");
30
+ onError == null ? void 0 : onError(error);
31
+ };
32
+ }, [imageRef.current]);
33
+ const flush = () => {
34
+ if (imageRef.current) {
35
+ imageRef.current.onload = null;
36
+ imageRef.current.onerror = null;
37
+ imageRef.current = null;
38
+ }
39
+ };
40
+ const load = useCallback(() => {
41
+ if (!src) return "pending";
42
+ if (ignoreFallback || shouldBypassImageLoad) return "loaded";
43
+ const img = new Image();
44
+ img.src = src;
45
+ if (crossOrigin) img.crossOrigin = crossOrigin;
46
+ if (srcSet) img.srcset = srcSet;
47
+ if (sizes) img.sizes = sizes;
48
+ if (loading) img.loading = loading;
49
+ imageRef.current = img;
50
+ if (img.complete && img.naturalWidth) {
51
+ return "loaded";
52
+ }
53
+ return "loading";
54
+ }, [src, crossOrigin, srcSet, sizes, onLoad, onError, loading, shouldBypassImageLoad]);
55
+ useSafeLayoutEffect(() => {
56
+ if (isHydrated) {
57
+ setStatus(load());
58
+ }
59
+ }, [isHydrated, load]);
60
+ return ignoreFallback ? "loaded" : status;
61
+ }
62
+ var shouldShowFallbackImage = (status, fallbackStrategy) => status !== "loaded" && fallbackStrategy === "beforeLoadOrError" || status === "failed" && fallbackStrategy === "onError";
63
+ export {
64
+ shouldShowFallbackImage,
65
+ useImage
66
+ };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@vx-oss/heroui-v2-use-image",
3
+ "version": "2.1.14-alpha.0",
4
+ "description": "React hook for progressing image loading",
5
+ "keywords": [
6
+ "use-image"
7
+ ],
8
+ "author": "Vx OSS Devs <oss-developers@vezham.com>",
9
+ "homepage": "https://vezham.com",
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "sideEffects": false,
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/vezham/heroui-v2.git",
22
+ "directory": "packages/hooks/use-image"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/vezham/heroui-v2/issues"
26
+ },
27
+ "dependencies": {
28
+ "@vx-oss/heroui-v2-react-utils": "2.1.15-alpha.0",
29
+ "@vx-oss/heroui-v2-use-safe-layout-effect": "2.1.9-alpha.0"
30
+ },
31
+ "peerDependencies": {
32
+ "react": ">=18 || >=19.0.0-rc.0"
33
+ },
34
+ "clean-package": "../../../clean-package.config.json",
35
+ "tsup": {
36
+ "clean": true,
37
+ "target": "es2019",
38
+ "format": [
39
+ "cjs",
40
+ "esm"
41
+ ]
42
+ },
43
+ "module": "dist/index.mjs",
44
+ "types": "dist/index.d.ts",
45
+ "exports": {
46
+ ".": {
47
+ "types": "./dist/index.d.ts",
48
+ "import": "./dist/index.mjs",
49
+ "require": "./dist/index.js"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "scripts": {
54
+ "build": "tsup src --dts",
55
+ "build:fast": "tsup src",
56
+ "dev": "pnpm build:fast --watch",
57
+ "clean": "rimraf dist .turbo",
58
+ "typecheck": "tsc --noEmit"
59
+ }
60
+ }