@rxdrag/website-lib-core 0.0.74 → 0.0.75

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxdrag/website-lib-core",
3
- "version": "0.0.74",
3
+ "version": "0.0.75",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.ts"
@@ -29,6 +29,7 @@
29
29
  "@rxdrag/slate-preview": "1.2.60"
30
30
  },
31
31
  "dependencies": {
32
+ "@iconify/utils": "^3.0.2",
32
33
  "clsx": "^2.1.0",
33
34
  "gsap": "^3.12.7",
34
35
  "hls.js": "^1.6.13",
@@ -3,12 +3,24 @@ import {
3
3
  IconProps as IconifyIconProps,
4
4
  } from "@iconify/react";
5
5
  import { forwardRef } from "react";
6
+ import { svgToIconify } from "./svgToIconify";
7
+
8
+ export { svgToIconify };
6
9
 
7
10
  export type IconProps = Omit<IconifyIconProps, "icon"> & {
8
11
  icon?: string;
12
+ svg?: string;
9
13
  };
10
14
 
11
15
  export const Icon = forwardRef<SVGSVGElement, IconProps>((props, ref) => {
12
- const { icon, ...rest } = props;
13
- return icon ? <IconifyIcon ref={ref} {...rest} icon={icon} /> : null;
16
+ const { icon, svg, ...rest } = props;
17
+
18
+ const iconObj = svg ? svgToIconify(svg) : undefined;
19
+ const finalIcon = icon || iconObj;
20
+
21
+ if (!finalIcon) {
22
+ return null;
23
+ }
24
+
25
+ return <IconifyIcon ref={ref} {...rest} icon={finalIcon} />;
14
26
  });
@@ -0,0 +1,39 @@
1
+ import { IconifyIcon } from "@iconify/react";
2
+
3
+ export function svgToIconify(svgString: string): IconifyIcon | null {
4
+ if (typeof window === 'undefined') return null;
5
+
6
+ try {
7
+ const parser = new DOMParser();
8
+ const doc = parser.parseFromString(svgString, "image/svg+xml");
9
+ const svg = doc.querySelector("svg");
10
+
11
+ if (!svg) {
12
+ console.error("Invalid SVG string");
13
+ return null;
14
+ }
15
+
16
+ const viewBox = svg.getAttribute("viewBox");
17
+ let width = svg.getAttribute("width");
18
+ let height = svg.getAttribute("height");
19
+ const body = svg.innerHTML;
20
+
21
+ // 如果没有显式宽高,尝试从 viewBox 解析
22
+ if (viewBox && (!width || !height)) {
23
+ const parts = viewBox.split(/\s+/).map(parseFloat);
24
+ if (parts.length === 4) {
25
+ if (!width) width = parts[2].toString();
26
+ if (!height) height = parts[3].toString();
27
+ }
28
+ }
29
+
30
+ return {
31
+ body,
32
+ width: width ? parseFloat(width) : undefined,
33
+ height: height ? parseFloat(height) : undefined,
34
+ };
35
+ } catch (e) {
36
+ console.error("Error parsing SVG:", e);
37
+ return null;
38
+ }
39
+ }