@cyberalien/svg-utils 1.0.7 → 1.0.8

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.
@@ -1,6 +1,6 @@
1
1
  import { getUniqueHash } from "../../../helpers/hash/unique.js";
2
- import { getGeneratedComponentFilename } from "../../export/filename.js";
3
2
  import { getGeneratedAssetFilename } from "./asset.js";
3
+ import { getGeneratedComponentFilename } from "../../export/filename.js";
4
4
 
5
5
  /**
6
6
  * Generate component types filename based on options
@@ -0,0 +1,9 @@
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 };
@@ -0,0 +1,20 @@
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 };
@@ -1,5 +1,5 @@
1
- import { getGeneratedComponentTypesFilename } from "../filenames/types.js";
2
1
  import { stringifyFactoryPropTypes } from "../props/ts.js";
2
+ import { getGeneratedComponentTypesFilename } from "../filenames/types.js";
3
3
 
4
4
  /**
5
5
  * Add component types
@@ -0,0 +1,13 @@
1
+ import { FactoryGeneratedComponent } from "./types/component.js";
2
+ import { FactoryIconData } from "./types/data.js";
3
+ import { ComponentFactoryOptions } from "./types/options.js";
4
+ interface Options extends ComponentFactoryOptions {
5
+ jsx: 'react';
6
+ fallbackPackage?: string;
7
+ ts?: boolean;
8
+ }
9
+ /**
10
+ * Create functional Vue component code
11
+ */
12
+ declare function createJSXComponent(data: FactoryIconData, options: Options): FactoryGeneratedComponent;
13
+ export { createJSXComponent };
@@ -0,0 +1,118 @@
1
+ import { getComponentSizeValues } from "./helpers/content/size.js";
2
+ import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
+ import { stringifyIconViewBox } from "../svg/viewbox/value.js";
4
+ import { createFactoryImports } from "./helpers/imports/create.js";
5
+ import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
+ import { addSizeFunctionAsset } from "./helpers/functions/size.js";
7
+ import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
8
+ import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
9
+ import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
+ import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
+ import { minifyViewBox } from "../svg/viewbox/minify.js";
12
+ import { getViewBoxRatio } from "./helpers/content/ratio.js";
13
+ import { addReactComponentTypes } from "./helpers/ts/react.js";
14
+
15
+ /**
16
+ * Create functional Vue component code
17
+ */
18
+ function createJSXComponent(data, options) {
19
+ const useTS = options.ts ?? false;
20
+ const assets = [];
21
+ const imports = createFactoryImports();
22
+ const dependencies = /* @__PURE__ */ new Set();
23
+ const importPackage = "react";
24
+ const createElement = "createElement";
25
+ const fallbackPackage = options.fallbackPackage || null;
26
+ const hasFallback = !!(fallbackPackage && data.fallback);
27
+ if (hasFallback) {
28
+ imports.named[fallbackPackage] = new Set(["Icon"]);
29
+ dependencies.add(fallbackPackage);
30
+ }
31
+ const reactNamedImports = new Set([createElement]);
32
+ imports.named[importPackage] = reactNamedImports;
33
+ const style = generateCSSFilesForComponent(data.icon, imports, assets, options);
34
+ const isEmbeddedCSS = options.cssMode === "embed";
35
+ let hasFixedSize = !!options.width && !!options.height;
36
+ const viewBox = data.viewBox;
37
+ const hasComputedViewbox = options.square && !hasFixedSize && viewBox.width !== viewBox.height;
38
+ const isStringViewBox = !hasFallback;
39
+ const hasComputedRatio = hasComputedViewbox && isStringViewBox;
40
+ if (!hasComputedViewbox && (options.width || options.height)) hasFixedSize = true;
41
+ const componentExternalCode = [];
42
+ const componentInternalCode = [];
43
+ const props = {};
44
+ if (!hasFallback) props.xmlns = "http://www.w3.org/2000/svg";
45
+ props.props = {
46
+ value: "props",
47
+ template: "...props,"
48
+ };
49
+ const getViewBox = (viewBox$1) => isStringViewBox ? `'${stringifyIconViewBox(viewBox$1)}'` : JSON.stringify(minifyViewBox(viewBox$1));
50
+ if (hasComputedViewbox) {
51
+ componentExternalCode.push(`const baseViewBox = ${getViewBox(viewBox)};`, `const squareViewBox = ${getViewBox(makeSquareViewBox(viewBox))};`);
52
+ componentInternalCode.push(`const viewBox = useMemo(() => square ? squareViewBox : baseViewBox, [square]);`);
53
+ } else componentExternalCode.push(`const viewBox = ${getViewBox(viewBox)};`);
54
+ const ratioValue = getViewBoxRatio(viewBox);
55
+ if (hasComputedRatio) componentInternalCode.push(`const ratio = useMemo(() => square ? 1 : ${ratioValue}, [square]);`);
56
+ if (hasFixedSize) {
57
+ const sizeProps = getComponentSizeValues(options, data.viewBox);
58
+ if (!sizeProps) throw new Error("Fixed size expected, but could not be determined");
59
+ props.width = sizeProps.width;
60
+ props.height = sizeProps.height;
61
+ } else if (hasFallback) {
62
+ props.width = {
63
+ type: "string",
64
+ value: "width",
65
+ template: "width,"
66
+ };
67
+ props.height = {
68
+ type: "string",
69
+ value: "height",
70
+ template: "height,"
71
+ };
72
+ } else {
73
+ const getSizeProps = addSizeFunctionAsset(imports, assets, options);
74
+ componentInternalCode.push(`const size = useMemo(() => ${getSizeProps}(width, height, ${hasComputedRatio ? "ratio" : ratioValue}), [width, height${hasComputedRatio ? ", ratio" : ""}]);`);
75
+ reactNamedImports.add("useMemo");
76
+ props.width = {
77
+ type: "string",
78
+ value: "width",
79
+ template: "...size,"
80
+ };
81
+ props.height = {
82
+ type: "string",
83
+ value: "height",
84
+ template: ""
85
+ };
86
+ }
87
+ if (options.square) props.square = { type: "boolean" };
88
+ props.viewBox = {
89
+ value: "viewBox",
90
+ template: "viewBox,"
91
+ };
92
+ props.content = {
93
+ value: stringifyFactoryIconContent(data.icon, options, isEmbeddedCSS ? style : void 0),
94
+ template: hasFallback ? void 0 : "dangerouslySetInnerHTML: {__html: {value}},"
95
+ };
96
+ if (hasFallback && data.fallback) props.fallback = data.fallback;
97
+ componentInternalCode.push(`return ${createElement}(${hasFallback ? "Icon" : "'svg'"}, {
98
+ \t\t${stringifyFactoryPropsAsJSON(props, "\n ")}
99
+ \t});`);
100
+ const beforeFunction = componentExternalCode.length ? componentExternalCode.join("\n") + "\n\n" : "";
101
+ const usedProps = getUsedFactoryProps(props);
102
+ const propsDestricturing = usedProps.length ? `{${[...usedProps, "...props"].join(", ")}}` : "props";
103
+ const componentFunction = `function Component${useTS ? `<{\n${stringifyFactoryPropTypes(props)}\n}>` : ""}(${propsDestricturing}) {
104
+ \t${componentInternalCode.join("\n ")}
105
+ }
106
+ `;
107
+ const content = `${stringifyFactoryImports(imports)}\n${beforeFunction}${componentFunction}\nexport default Component;\n`;
108
+ const types = addReactComponentTypes(data, options, assets, props);
109
+ return {
110
+ assets,
111
+ content,
112
+ style: isEmbeddedCSS ? void 0 : style,
113
+ types,
114
+ dependencies: dependencies.size ? dependencies : void 0
115
+ };
116
+ }
117
+
118
+ export { createJSXComponent };
@@ -1,11 +1,11 @@
1
1
  import { getComponentSizeValues } from "./helpers/content/size.js";
2
2
  import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
- import { getGeneratedComponentTypesFilename } from "./helpers/filenames/types.js";
4
3
  import { stringifyIconViewBox } from "../svg/viewbox/value.js";
5
4
  import { createFactoryImports } from "./helpers/imports/create.js";
6
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
7
- import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
8
6
  import { factoryPropTemplate, stringifyFactoryProps } from "./helpers/props/stringify.js";
7
+ import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
8
+ import { getGeneratedComponentTypesFilename } from "./helpers/filenames/types.js";
9
9
 
10
10
  /**
11
11
  * Create raw component code
@@ -3,14 +3,14 @@ import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
3
  import { stringifyIconViewBox } from "../svg/viewbox/value.js";
4
4
  import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
- import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
- import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
6
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
7
+ import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
+ import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
9
9
  import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
10
  import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
- import { addSvelteComponentTypes } from "./helpers/ts/svelte.js";
12
11
  import { minifyViewBox } from "../svg/viewbox/minify.js";
13
12
  import { getViewBoxRatio } from "./helpers/content/ratio.js";
13
+ import { addSvelteComponentTypes } from "./helpers/ts/svelte.js";
14
14
 
15
15
  /**
16
16
  * Create Svelte component code
@@ -86,7 +86,8 @@ function createSvelteComponent(data, options) {
86
86
  template: hasFallback ? `content={content} fallback="${data.fallback}"` : ""
87
87
  };
88
88
  const usedProps = getUsedFactoryProps(props);
89
- componentCode.unshift(`let {${[...usedProps, "...props"].join(", ")}}${useTS ? ": Props" : ""} = $props();\n`);
89
+ const propsDestricturing = usedProps.length ? `{${[...usedProps, "...props"].join(", ")}}` : "props";
90
+ componentCode.unshift(`let ${propsDestricturing}${useTS ? ": Props" : ""} = $props();\n`);
90
91
  if (useTS) componentCode.unshift(`interface Props {\n${stringifyFactoryPropTypes(props)}\n};\n`);
91
92
  const tag = hasFallback ? "Icon" : "svg";
92
93
  const template = `<${tag} ${stringifyFactoryProps(props, "{prop}={{value}}")} {...props}>${innerHTML}</${tag}>`;
@@ -3,14 +3,14 @@ import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
3
  import { stringifyIconViewBox } from "../svg/viewbox/value.js";
4
4
  import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
- import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
6
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
7
+ import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
8
+ import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
8
9
  import { makeSquareViewBox } from "../svg/viewbox/square.js";
9
10
  import { getUsedFactoryProps } from "./helpers/props/ts.js";
10
11
  import { minifyViewBox } from "../svg/viewbox/minify.js";
11
12
  import { getViewBoxRatio } from "./helpers/content/ratio.js";
12
13
  import { addVueComponentTypes } from "./helpers/ts/vue.js";
13
- import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
14
14
 
15
15
  /**
16
16
  * Create functional Vue component code
@@ -3,9 +3,9 @@ import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
3
  import { stringifyIconViewBox } from "../svg/viewbox/value.js";
4
4
  import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
- import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
- import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
6
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
7
+ import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
+ import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
9
9
  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";
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.7",
6
+ "version": "1.0.8",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",