@vx-oss/heroui-v2-use-image 2.1.14-alpha.2 → 2.1.15-beta.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/dist/index.js CHANGED
@@ -40,43 +40,55 @@ function useImage(props = {}) {
40
40
  shouldBypassImageLoad = false
41
41
  } = props;
42
42
  const isHydrated = (0, import_heroui_v2_react_utils.useIsHydrated)();
43
- const imageRef = (0, import_react.useRef)(isHydrated ? new Image() : null);
43
+ const imageRef = (0, import_react.useRef)(null);
44
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 = () => {
45
+ const flush = (0, import_react.useCallback)(() => {
59
46
  if (imageRef.current) {
60
47
  imageRef.current.onload = null;
61
48
  imageRef.current.onerror = null;
62
49
  imageRef.current = null;
63
50
  }
64
- };
51
+ }, []);
65
52
  const load = (0, import_react.useCallback)(() => {
66
53
  if (!src) return "pending";
67
54
  if (ignoreFallback || shouldBypassImageLoad) return "loaded";
55
+ flush();
68
56
  const img = new Image();
69
- img.src = src;
57
+ img.onload = (event) => {
58
+ flush();
59
+ setStatus("loaded");
60
+ onLoad == null ? void 0 : onLoad(event);
61
+ };
62
+ img.onerror = (error) => {
63
+ flush();
64
+ setStatus("failed");
65
+ onError == null ? void 0 : onError(error);
66
+ };
70
67
  if (crossOrigin) img.crossOrigin = crossOrigin;
71
68
  if (srcSet) img.srcset = srcSet;
72
69
  if (sizes) img.sizes = sizes;
73
70
  if (loading) img.loading = loading;
71
+ img.src = src;
74
72
  imageRef.current = img;
75
- if (img.complete && img.naturalWidth) {
76
- return "loaded";
73
+ if (img.complete) {
74
+ if (img.naturalWidth && img.naturalHeight) {
75
+ return "loaded";
76
+ }
77
+ return "failed";
77
78
  }
78
79
  return "loading";
79
- }, [src, crossOrigin, srcSet, sizes, onLoad, onError, loading, shouldBypassImageLoad]);
80
+ }, [
81
+ src,
82
+ crossOrigin,
83
+ srcSet,
84
+ sizes,
85
+ onLoad,
86
+ onError,
87
+ ignoreFallback,
88
+ loading,
89
+ shouldBypassImageLoad,
90
+ flush
91
+ ]);
80
92
  (0, import_heroui_v2_use_safe_layout_effect.useSafeLayoutEffect)(() => {
81
93
  if (isHydrated) {
82
94
  setStatus(load());
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { useRef, useState, useEffect, useCallback } from "react";
2
+ import { useRef, useState, useCallback } from "react";
3
3
  import { useIsHydrated } from "@vx-oss/heroui-v2-react-utils";
4
4
  import { useSafeLayoutEffect } from "@vx-oss/heroui-v2-use-safe-layout-effect";
5
5
  function useImage(props = {}) {
@@ -15,43 +15,55 @@ function useImage(props = {}) {
15
15
  shouldBypassImageLoad = false
16
16
  } = props;
17
17
  const isHydrated = useIsHydrated();
18
- const imageRef = useRef(isHydrated ? new Image() : null);
18
+ const imageRef = useRef(null);
19
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 = () => {
20
+ const flush = useCallback(() => {
34
21
  if (imageRef.current) {
35
22
  imageRef.current.onload = null;
36
23
  imageRef.current.onerror = null;
37
24
  imageRef.current = null;
38
25
  }
39
- };
26
+ }, []);
40
27
  const load = useCallback(() => {
41
28
  if (!src) return "pending";
42
29
  if (ignoreFallback || shouldBypassImageLoad) return "loaded";
30
+ flush();
43
31
  const img = new Image();
44
- img.src = src;
32
+ img.onload = (event) => {
33
+ flush();
34
+ setStatus("loaded");
35
+ onLoad == null ? void 0 : onLoad(event);
36
+ };
37
+ img.onerror = (error) => {
38
+ flush();
39
+ setStatus("failed");
40
+ onError == null ? void 0 : onError(error);
41
+ };
45
42
  if (crossOrigin) img.crossOrigin = crossOrigin;
46
43
  if (srcSet) img.srcset = srcSet;
47
44
  if (sizes) img.sizes = sizes;
48
45
  if (loading) img.loading = loading;
46
+ img.src = src;
49
47
  imageRef.current = img;
50
- if (img.complete && img.naturalWidth) {
51
- return "loaded";
48
+ if (img.complete) {
49
+ if (img.naturalWidth && img.naturalHeight) {
50
+ return "loaded";
51
+ }
52
+ return "failed";
52
53
  }
53
54
  return "loading";
54
- }, [src, crossOrigin, srcSet, sizes, onLoad, onError, loading, shouldBypassImageLoad]);
55
+ }, [
56
+ src,
57
+ crossOrigin,
58
+ srcSet,
59
+ sizes,
60
+ onLoad,
61
+ onError,
62
+ ignoreFallback,
63
+ loading,
64
+ shouldBypassImageLoad,
65
+ flush
66
+ ]);
55
67
  useSafeLayoutEffect(() => {
56
68
  if (isHydrated) {
57
69
  setStatus(load());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vx-oss/heroui-v2-use-image",
3
- "version": "2.1.14-alpha.2",
3
+ "version": "2.1.15-beta.0",
4
4
  "description": "React hook for progressing image loading",
5
5
  "keywords": [
6
6
  "use-image"
@@ -25,8 +25,8 @@
25
25
  "url": "https://github.com/vezham/heroui-v2/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@vx-oss/heroui-v2-use-safe-layout-effect": "2.1.9-alpha.2",
29
- "@vx-oss/heroui-v2-react-utils": "2.1.15-alpha.2"
28
+ "@vx-oss/heroui-v2-use-safe-layout-effect": "2.1.9-beta.0",
29
+ "@vx-oss/heroui-v2-react-utils": "2.1.15-beta.0"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "react": ">=18 || >=19.0.0-rc.0"