xxf_react 0.3.8 → 0.4.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.
@@ -0,0 +1,17 @@
1
+ import { ImageProps } from "next/image";
2
+ import React, { ReactNode } from "react";
3
+ export interface XImageProps extends ImageProps {
4
+ /** 图片加载失败时显示的备用图片 URL 或自定义组件 */
5
+ fallback?: string | ReactNode;
6
+ }
7
+ /**
8
+ * 增强的 Image 组件,支持加载失败时显示备用图片或自定义组件
9
+ * @param src - 图片地址
10
+ * @param fallback - 加载失败时显示的备用图片 URL 或 ReactNode
11
+ * @param onError - 加载失败回调
12
+ * @param className - 样式类名
13
+ * @param props - 其他 Next.js Image 支持的属性
14
+ */
15
+ export declare function XImage({ src, fallback, onError, ...props }: XImageProps): React.JSX.Element;
16
+ export default XImage;
17
+ //# sourceMappingURL=XImage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"XImage.d.ts","sourceRoot":"","sources":["../../../src/layout/image/XImage.tsx"],"names":[],"mappings":"AAEA,OAAc,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,EAAsB,SAAS,EAAC,MAAM,OAAO,CAAC;AAE5D,MAAM,WAAW,WAAY,SAAQ,UAAU;IAC3C,gCAAgC;IAChC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACjC;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,EACI,GAAG,EACH,QAAQ,EACR,OAAO,EACP,GAAG,KAAK,EACX,EAAE,WAAW,qBAkDpC;AAED,eAAe,MAAM,CAAC"}
@@ -0,0 +1,45 @@
1
+ "use client";
2
+ import Image from "next/image";
3
+ import React, { useState, useEffect } from "react";
4
+ /**
5
+ * 增强的 Image 组件,支持加载失败时显示备用图片或自定义组件
6
+ * @param src - 图片地址
7
+ * @param fallback - 加载失败时显示的备用图片 URL 或 ReactNode
8
+ * @param onError - 加载失败回调
9
+ * @param className - 样式类名
10
+ * @param props - 其他 Next.js Image 支持的属性
11
+ */
12
+ export function XImage({ src, fallback, onError, ...props }) {
13
+ const [imgSrc, setImgSrc] = useState(src);
14
+ const [hasError, setHasError] = useState(false);
15
+ // src 变化时重置状态
16
+ useEffect(() => {
17
+ setImgSrc(src);
18
+ setHasError(false);
19
+ }, [src]);
20
+ const handleError = (e) => {
21
+ if (fallback && !hasError) {
22
+ setHasError(true);
23
+ // 如果 fallback 是字符串(URL),则切换图片源
24
+ if (typeof fallback === "string") {
25
+ setImgSrc(fallback);
26
+ return; // fallback 切换时不触发 onError,等 fallback 也失败再触发
27
+ }
28
+ }
29
+ onError === null || onError === void 0 ? void 0 : onError(e);
30
+ };
31
+ // fallback 是 ReactNode 且已出错,用容器包裹并应用样式属性
32
+ if (hasError && fallback && typeof fallback !== "string") {
33
+ const width = typeof props.width === "number" ? props.width : props.width ? Number(props.width) : undefined;
34
+ const height = typeof props.height === "number" ? props.height : props.height ? Number(props.height) : undefined;
35
+ return (React.createElement("div", { className: props.className, style: {
36
+ width,
37
+ height,
38
+ position: props.fill ? "absolute" : undefined,
39
+ inset: props.fill ? 0 : undefined,
40
+ ...props.style,
41
+ } }, fallback));
42
+ }
43
+ return (React.createElement(Image, { ...props, src: imgSrc, onError: handleError }));
44
+ }
45
+ export default XImage;
@@ -3,4 +3,5 @@ export * from './resize/core/SizedLayoutContext';
3
3
  export * from './resize/core/ResizeObserverHook';
4
4
  export * from './resize/impl/SizedContainer';
5
5
  export * from './resize/impl/SizedLayout';
6
+ export * from './image/XImage';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/layout/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAA;AAC9C,cAAc,kCAAkC,CAAA;AAChD,cAAc,kCAAkC,CAAA;AAChD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,2BAA2B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/layout/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAA;AAC9C,cAAc,kCAAkC,CAAA;AAChD,cAAc,kCAAkC,CAAA;AAChD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,2BAA2B,CAAA;AACzC,cAAc,gBAAgB,CAAA"}
@@ -3,3 +3,4 @@ export * from './resize/core/SizedLayoutContext';
3
3
  export * from './resize/core/ResizeObserverHook';
4
4
  export * from './resize/impl/SizedContainer';
5
5
  export * from './resize/impl/SizedLayout';
6
+ export * from './image/XImage';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxf_react",
3
- "version": "0.3.8",
3
+ "version": "0.4.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -74,5 +74,8 @@
74
74
  "tailwind-variants": "^3.2.2",
75
75
  "tiny-lru": "^11.4.7",
76
76
  "use-debounce": "^10.0.6"
77
+ },
78
+ "devDependencies": {
79
+ "next": "^16.0.7"
77
80
  }
78
81
  }