@cyberalien/svg-utils 1.0.7 → 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.
- package/lib/components/helpers/filenames/types.js +1 -1
- package/lib/components/helpers/ts/jsx.d.ts +13 -0
- package/lib/components/helpers/ts/jsx.js +39 -0
- package/lib/components/helpers/ts/wrapper.js +1 -1
- package/lib/components/jsx.d.ts +14 -0
- package/lib/components/jsx.js +124 -0
- package/lib/components/raw.js +2 -2
- package/lib/components/svelte.js +5 -4
- package/lib/components/types/jsx.d.ts +2 -0
- package/lib/components/types/jsx.js +1 -0
- package/lib/components/vue-func.js +2 -2
- package/lib/components/vue.js +2 -2
- package/package.json +1 -1
|
@@ -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,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 };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FactoryGeneratedComponent } from "./types/component.js";
|
|
2
|
+
import { FactoryIconData } from "./types/data.js";
|
|
3
|
+
import { ComponentFactoryOptions } from "./types/options.js";
|
|
4
|
+
import { JSXMode } from "./types/jsx.js";
|
|
5
|
+
interface Options extends ComponentFactoryOptions {
|
|
6
|
+
jsx: JSXMode;
|
|
7
|
+
fallbackPackage?: string;
|
|
8
|
+
ts?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create functional Vue component code
|
|
12
|
+
*/
|
|
13
|
+
declare function createJSXComponent(data: FactoryIconData, options: Options): FactoryGeneratedComponent;
|
|
14
|
+
export { createJSXComponent };
|
|
@@ -0,0 +1,124 @@
|
|
|
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 { addJSXComponentTypes } from "./helpers/ts/jsx.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
|
+
let importPackage = "react";
|
|
24
|
+
let createElement = "createElement";
|
|
25
|
+
switch (options.jsx) {
|
|
26
|
+
case "preact":
|
|
27
|
+
importPackage = "preact";
|
|
28
|
+
createElement = "h";
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
const fallbackPackage = options.fallbackPackage || null;
|
|
32
|
+
const hasFallback = !!(fallbackPackage && data.fallback);
|
|
33
|
+
if (hasFallback) {
|
|
34
|
+
imports.named[fallbackPackage] = new Set(["Icon"]);
|
|
35
|
+
dependencies.add(fallbackPackage);
|
|
36
|
+
}
|
|
37
|
+
const reactNamedImports = new Set([createElement]);
|
|
38
|
+
imports.named[importPackage] = reactNamedImports;
|
|
39
|
+
const style = generateCSSFilesForComponent(data.icon, imports, assets, options);
|
|
40
|
+
const isEmbeddedCSS = options.cssMode === "embed";
|
|
41
|
+
let hasFixedSize = !!options.width && !!options.height;
|
|
42
|
+
const viewBox = data.viewBox;
|
|
43
|
+
const hasComputedViewbox = options.square && !hasFixedSize && viewBox.width !== viewBox.height;
|
|
44
|
+
const isStringViewBox = !hasFallback;
|
|
45
|
+
const hasComputedRatio = hasComputedViewbox && isStringViewBox;
|
|
46
|
+
if (!hasComputedViewbox && (options.width || options.height)) hasFixedSize = true;
|
|
47
|
+
const componentExternalCode = [];
|
|
48
|
+
const componentInternalCode = [];
|
|
49
|
+
const props = {};
|
|
50
|
+
if (!hasFallback) props.xmlns = "http://www.w3.org/2000/svg";
|
|
51
|
+
props.props = {
|
|
52
|
+
value: "props",
|
|
53
|
+
template: "...props,"
|
|
54
|
+
};
|
|
55
|
+
const getViewBox = (viewBox$1) => isStringViewBox ? `'${stringifyIconViewBox(viewBox$1)}'` : JSON.stringify(minifyViewBox(viewBox$1));
|
|
56
|
+
if (hasComputedViewbox) {
|
|
57
|
+
componentExternalCode.push(`const baseViewBox = ${getViewBox(viewBox)};`, `const squareViewBox = ${getViewBox(makeSquareViewBox(viewBox))};`);
|
|
58
|
+
componentInternalCode.push(`const viewBox = useMemo(() => square ? squareViewBox : baseViewBox, [square]);`);
|
|
59
|
+
} else componentExternalCode.push(`const viewBox = ${getViewBox(viewBox)};`);
|
|
60
|
+
const ratioValue = getViewBoxRatio(viewBox);
|
|
61
|
+
if (hasComputedRatio) componentInternalCode.push(`const ratio = useMemo(() => square ? 1 : ${ratioValue}, [square]);`);
|
|
62
|
+
if (hasFixedSize) {
|
|
63
|
+
const sizeProps = getComponentSizeValues(options, data.viewBox);
|
|
64
|
+
if (!sizeProps) throw new Error("Fixed size expected, but could not be determined");
|
|
65
|
+
props.width = sizeProps.width;
|
|
66
|
+
props.height = sizeProps.height;
|
|
67
|
+
} else if (hasFallback) {
|
|
68
|
+
props.width = {
|
|
69
|
+
type: "string",
|
|
70
|
+
value: "width",
|
|
71
|
+
template: "width,"
|
|
72
|
+
};
|
|
73
|
+
props.height = {
|
|
74
|
+
type: "string",
|
|
75
|
+
value: "height",
|
|
76
|
+
template: "height,"
|
|
77
|
+
};
|
|
78
|
+
} else {
|
|
79
|
+
const getSizeProps = addSizeFunctionAsset(imports, assets, options);
|
|
80
|
+
componentInternalCode.push(`const size = useMemo(() => ${getSizeProps}(width, height, ${hasComputedRatio ? "ratio" : ratioValue}), [width, height${hasComputedRatio ? ", ratio" : ""}]);`);
|
|
81
|
+
reactNamedImports.add("useMemo");
|
|
82
|
+
props.width = {
|
|
83
|
+
type: "string",
|
|
84
|
+
value: "width",
|
|
85
|
+
template: "...size,"
|
|
86
|
+
};
|
|
87
|
+
props.height = {
|
|
88
|
+
type: "string",
|
|
89
|
+
value: "height",
|
|
90
|
+
template: ""
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (options.square) props.square = { type: "boolean" };
|
|
94
|
+
props.viewBox = {
|
|
95
|
+
value: "viewBox",
|
|
96
|
+
template: "viewBox,"
|
|
97
|
+
};
|
|
98
|
+
props.content = {
|
|
99
|
+
value: stringifyFactoryIconContent(data.icon, options, isEmbeddedCSS ? style : void 0),
|
|
100
|
+
template: hasFallback ? void 0 : "dangerouslySetInnerHTML: {__html: {value}},"
|
|
101
|
+
};
|
|
102
|
+
if (hasFallback && data.fallback) props.fallback = data.fallback;
|
|
103
|
+
componentInternalCode.push(`return ${createElement}(${hasFallback ? "Icon" : "'svg'"}, {
|
|
104
|
+
\t\t${stringifyFactoryPropsAsJSON(props, "\n ")}
|
|
105
|
+
\t});`);
|
|
106
|
+
const beforeFunction = componentExternalCode.length ? componentExternalCode.join("\n") + "\n\n" : "";
|
|
107
|
+
const usedProps = getUsedFactoryProps(props);
|
|
108
|
+
const propsDestricturing = usedProps.length ? `{${[...usedProps, "...props"].join(", ")}}` : "props";
|
|
109
|
+
const componentFunction = `function Component${useTS ? `<{\n${stringifyFactoryPropTypes(props)}\n}>` : ""}(${propsDestricturing}) {
|
|
110
|
+
\t${componentInternalCode.join("\n ")}
|
|
111
|
+
}
|
|
112
|
+
`;
|
|
113
|
+
const content = `${stringifyFactoryImports(imports)}\n${beforeFunction}${componentFunction}\nexport default Component;\n`;
|
|
114
|
+
const types = addJSXComponentTypes(data, options, assets, props);
|
|
115
|
+
return {
|
|
116
|
+
assets,
|
|
117
|
+
content,
|
|
118
|
+
style: isEmbeddedCSS ? void 0 : style,
|
|
119
|
+
types,
|
|
120
|
+
dependencies: dependencies.size ? dependencies : void 0
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { createJSXComponent };
|
package/lib/components/raw.js
CHANGED
|
@@ -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
|
package/lib/components/svelte.js
CHANGED
|
@@ -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
|
-
|
|
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}>`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -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
|
package/lib/components/vue.js
CHANGED
|
@@ -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.
|
|
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/",
|