@cyberalien/svg-utils 1.0.8 → 1.0.9

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,13 @@
1
+ import { GeneratedAssetFile } from "../../types/component.js";
2
+ import { FactoryIconData } from "../../types/data.js";
3
+ import { ComponentFactoryOptions } from "../../types/options.js";
4
+ import { FactoryComponentProps } from "../props/types.js";
5
+ import { JSXMode } from "../../types/jsx.js";
6
+ interface Options extends ComponentFactoryOptions {
7
+ jsx: JSXMode;
8
+ }
9
+ /**
10
+ * Add JSX component types
11
+ */
12
+ declare function addJSXComponentTypes(data: FactoryIconData, options: Options, assets: GeneratedAssetFile[], props: FactoryComponentProps): string;
13
+ export { addJSXComponentTypes };
@@ -0,0 +1,39 @@
1
+ import { addComponentTypes } from "./wrapper.js";
2
+
3
+ const iconPropsTemplate = `interface IconProps {
4
+ /* PROPS */
5
+ }`;
6
+ const exportTemplate = `export { type IconProps };
7
+ export default Component;`;
8
+ const omitProps = `'viewBox' | 'width' | 'height' | 'xmlns'`;
9
+ /**
10
+ * Add JSX component types
11
+ */
12
+ function addJSXComponentTypes(data, options, assets, props) {
13
+ let template;
14
+ switch (options.jsx) {
15
+ case "react":
16
+ template = `import type { ForwardRefExoticComponent, SVGProps } from 'react';
17
+
18
+ ${iconPropsTemplate}
19
+
20
+ const Component: ForwardRefExoticComponent<
21
+ Omit<SVGProps<SVGSVGElement>, ${omitProps}> & IconProps
22
+ >;
23
+
24
+ ${exportTemplate}
25
+ `;
26
+ break;
27
+ case "preact": template = `import type { JSX } from 'preact';
28
+
29
+ ${iconPropsTemplate}
30
+
31
+ const Component: (props: Omit<JSX.SVGAttributes<SVGSVGElement>, ${omitProps}> & IconProps) => JSX.Element;
32
+
33
+ ${exportTemplate}
34
+ `;
35
+ }
36
+ return addComponentTypes(template, data, options, assets, props);
37
+ }
38
+
39
+ export { addJSXComponentTypes };
@@ -1,8 +1,9 @@
1
1
  import { FactoryGeneratedComponent } from "./types/component.js";
2
2
  import { FactoryIconData } from "./types/data.js";
3
3
  import { ComponentFactoryOptions } from "./types/options.js";
4
+ import { JSXMode } from "./types/jsx.js";
4
5
  interface Options extends ComponentFactoryOptions {
5
- jsx: 'react';
6
+ jsx: JSXMode;
6
7
  fallbackPackage?: string;
7
8
  ts?: boolean;
8
9
  }
@@ -10,7 +10,7 @@ import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
10
  import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
11
  import { minifyViewBox } from "../svg/viewbox/minify.js";
12
12
  import { getViewBoxRatio } from "./helpers/content/ratio.js";
13
- import { addReactComponentTypes } from "./helpers/ts/react.js";
13
+ import { addJSXComponentTypes } from "./helpers/ts/jsx.js";
14
14
 
15
15
  /**
16
16
  * Create functional Vue component code
@@ -20,8 +20,14 @@ function createJSXComponent(data, options) {
20
20
  const assets = [];
21
21
  const imports = createFactoryImports();
22
22
  const dependencies = /* @__PURE__ */ new Set();
23
- const importPackage = "react";
24
- const createElement = "createElement";
23
+ let importPackage = "react";
24
+ let createElement = "createElement";
25
+ switch (options.jsx) {
26
+ case "preact":
27
+ importPackage = "preact";
28
+ createElement = "h";
29
+ break;
30
+ }
25
31
  const fallbackPackage = options.fallbackPackage || null;
26
32
  const hasFallback = !!(fallbackPackage && data.fallback);
27
33
  if (hasFallback) {
@@ -105,7 +111,7 @@ function createJSXComponent(data, options) {
105
111
  }
106
112
  `;
107
113
  const content = `${stringifyFactoryImports(imports)}\n${beforeFunction}${componentFunction}\nexport default Component;\n`;
108
- const types = addReactComponentTypes(data, options, assets, props);
114
+ const types = addJSXComponentTypes(data, options, assets, props);
109
115
  return {
110
116
  assets,
111
117
  content,
@@ -0,0 +1,2 @@
1
+ type JSXMode = 'react' | 'preact';
2
+ export { JSXMode };
@@ -0,0 +1 @@
1
+ export { };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Common functions for working with SVG used by various packages.",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "1.0.8",
6
+ "version": "1.0.9",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",
@@ -1,9 +0,0 @@
1
- import { GeneratedAssetFile } from "../../types/component.js";
2
- import { FactoryIconData } from "../../types/data.js";
3
- import { ComponentFactoryOptions } from "../../types/options.js";
4
- import { FactoryComponentProps } from "../props/types.js";
5
- /**
6
- * Add React component types
7
- */
8
- declare const addReactComponentTypes: (data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps) => string;
9
- export { addReactComponentTypes };
@@ -1,20 +0,0 @@
1
- import { addComponentTypes } from "./wrapper.js";
2
-
3
- /**
4
- * Add React component types
5
- */
6
- const addReactComponentTypes = addComponentTypes.bind(null, `import type { ForwardRefExoticComponent, SVGProps } from 'react';
7
-
8
- interface IconProps {
9
- /* PROPS */
10
- }
11
-
12
- const Component: ForwardRefExoticComponent<
13
- Omit<SVGProps<SVGSVGElement>, 'viewBox' | 'width' | 'height' | 'xmlns'> & IconProps
14
- >;
15
-
16
- export { type IconProps };
17
- export default Component;
18
- `);
19
-
20
- export { addReactComponentTypes };