@cyberalien/svg-utils 0.1.6 → 0.1.7

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.
@@ -3,6 +3,7 @@ type Types = string | Record<string, string>;
3
3
  interface Options {
4
4
  ext?: string;
5
5
  data?: Record<string, Types>;
6
+ defaultProp?: string;
6
7
  }
7
8
  /**
8
9
  * Add exports for main files to object
@@ -4,9 +4,10 @@
4
4
  function createExportsForMainFiles(data, options = {}) {
5
5
  const result = options?.data || Object.create(null);
6
6
  const ext = options.ext || "";
7
+ const defaultProp = options.defaultProp || "default";
7
8
  for (const { icon, filename, types } of data) result[`./${icon}${ext}`] = types ? {
8
9
  types: `./${types}`,
9
- default: `./${filename}`
10
+ [defaultProp]: `./${filename}`
10
11
  } : `./${filename}`;
11
12
  return result;
12
13
  }
@@ -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 Svelte component types
7
+ */
8
+ declare const addSvelteComponentTypes: (data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps) => string;
9
+ export { addSvelteComponentTypes };
@@ -0,0 +1,19 @@
1
+ import { addComponentTypes } from "./wrapper.js";
2
+
3
+ /**
4
+ * Add Svelte component types
5
+ */
6
+ const addSvelteComponentTypes = addComponentTypes.bind(null, `import { SvelteComponent } from "svelte";
7
+ import { SvelteHTMLElements } from "svelte/elements";
8
+
9
+ interface IconProps {
10
+ /* PROPS */
11
+ }
12
+
13
+ declare class Component extends SvelteComponent<Omit<SvelteHTMLElements['svg'], 'viewBox' | 'width' | 'height' | 'xmlns'> & IconProps & Record<\`data-\${string}\`, string>> {}
14
+
15
+ export { type IconProps };
16
+ export default Component;
17
+ `);
18
+
19
+ export { addSvelteComponentTypes };
@@ -5,5 +5,5 @@ import { FactoryComponentProps } from "../props/types.js";
5
5
  /**
6
6
  * Add Vue component types
7
7
  */
8
- declare function addVueComponentTypes(data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps): string;
8
+ declare const addVueComponentTypes: (data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps) => string;
9
9
  export { addVueComponentTypes };
@@ -0,0 +1,18 @@
1
+ import { addComponentTypes } from "./wrapper.js";
2
+
3
+ /**
4
+ * Add Vue component types
5
+ */
6
+ const addVueComponentTypes = addComponentTypes.bind(null, `import { DefineSetupFnComponent, PublicProps } from 'vue';
7
+
8
+ interface IconProps {
9
+ /* PROPS */
10
+ }
11
+
12
+ declare const Component: DefineSetupFnComponent<IconProps, {}, {}, IconProps & {}, PublicProps>;
13
+
14
+ export { type IconProps };
15
+ export default Component;
16
+ `);
17
+
18
+ export { addVueComponentTypes };
@@ -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 component types
7
+ */
8
+ declare function addComponentTypes(template: string, data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps): string;
9
+ export { addComponentTypes };
@@ -0,0 +1,18 @@
1
+ import { getGeneratedComponentTypesFilename } from "../filenames/types.js";
2
+ import { stringifyFactoryPropTypes } from "../props/ts.js";
3
+
4
+ /**
5
+ * Add component types
6
+ */
7
+ function addComponentTypes(template, data, options, assets, props) {
8
+ const propTypes = stringifyFactoryPropTypes(props);
9
+ const content = template.replace("/* PROPS */", propTypes);
10
+ const filename = getGeneratedComponentTypesFilename(data, content, options);
11
+ assets.push({
12
+ ...filename,
13
+ content
14
+ });
15
+ return filename.filename;
16
+ }
17
+
18
+ export { addComponentTypes };
@@ -0,0 +1,12 @@
1
+ import { FactoryGeneratedComponent } from "./types/component.js";
2
+ import { FactoryIconData } from "./types/data.js";
3
+ import { ComponentFactoryOptions } from "./types/options.js";
4
+ interface SvelteOptions extends ComponentFactoryOptions {
5
+ ts?: boolean;
6
+ styles?: boolean;
7
+ }
8
+ /**
9
+ * Create Svelte component code
10
+ */
11
+ declare function createSvelteComponent(data: FactoryIconData, options: SvelteOptions): FactoryGeneratedComponent;
12
+ export { createSvelteComponent };
@@ -0,0 +1,97 @@
1
+ import { getComponentSizeValues } from "./helpers/content/size.js";
2
+ import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
+ import { getIconViewBox } from "../svg/viewbox/value.js";
4
+ import { createFactoryImports } from "./helpers/imports/create.js";
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
+ import { addSizeFunctionAsset } from "./helpers/functions/size.js";
9
+ import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
+ import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
+ import { addSvelteComponentTypes } from "./helpers/ts/svelte.js";
12
+
13
+ /**
14
+ * Create Svelte component code
15
+ */
16
+ function createSvelteComponent(data, options) {
17
+ const useTS = options.ts ?? false;
18
+ const styleInComponent = options.styles ?? false;
19
+ const assets = [];
20
+ const imports = createFactoryImports();
21
+ const hasFallback = !!data.fallback;
22
+ if (hasFallback) imports.default["@iconify/css-svelte"] = "Icon";
23
+ const styleContent = generateCSSFilesForComponent(data.icon, imports, assets, {
24
+ ...options,
25
+ styleInComponent
26
+ });
27
+ const componentCode = [];
28
+ const sizeProps = getComponentSizeValues(options, data.viewBox);
29
+ const props = {};
30
+ if (!hasFallback) props.xmlns = "http://www.w3.org/2000/svg";
31
+ const viewBox = data.viewBox;
32
+ const isDynanicViewBox = !sizeProps && options.square && viewBox.width !== viewBox.height;
33
+ const viewBoxPropValue = `viewBox${isDynanicViewBox ? "Computed" : ""}`;
34
+ if (sizeProps) {
35
+ props.width = sizeProps.width;
36
+ props.height = sizeProps.height;
37
+ } else if (hasFallback) {
38
+ props.width = {
39
+ type: "string",
40
+ value: "width",
41
+ template: "width={width}"
42
+ };
43
+ props.height = {
44
+ type: "string",
45
+ value: "height",
46
+ template: "height={height}"
47
+ };
48
+ } else {
49
+ const getSizeProps = addSizeFunctionAsset(imports, assets, options);
50
+ componentCode.push(`let size = $derived(${getSizeProps}(width, height, ${viewBoxPropValue}));`);
51
+ props.width = {
52
+ type: "string",
53
+ value: "width",
54
+ template: "{...size}"
55
+ };
56
+ props.height = {
57
+ type: "string",
58
+ value: "height",
59
+ template: ""
60
+ };
61
+ }
62
+ if (options.square) props.square = { type: "boolean" };
63
+ const getViewBox = (viewBox$1) => hasFallback ? JSON.stringify(viewBox$1) : `'${getIconViewBox(viewBox$1)}'`;
64
+ const viewBoxValue = getViewBox(viewBox);
65
+ if (isDynanicViewBox) componentCode.unshift(`const baseViewBox = ${viewBoxValue};`, `const squareViewBox = ${getViewBox(makeSquareViewBox(viewBox))};`, `let ${viewBoxPropValue} = $derived(square ? squareViewBox : baseViewBox);`);
66
+ else componentCode.unshift(`const viewBox = ${viewBoxValue};`);
67
+ props.viewBox = {
68
+ value: "viewBox",
69
+ template: `viewBox={${viewBoxPropValue}}`
70
+ };
71
+ componentCode.push(`const content = ${stringifyFactoryIconContent(data.icon, options)};`);
72
+ const innerHTML = hasFallback ? "" : "{@html content}";
73
+ props.content = {
74
+ value: "content",
75
+ template: hasFallback ? `content={content} fallback="${data.fallback}"` : ""
76
+ };
77
+ const usedProps = getUsedFactoryProps(props);
78
+ componentCode.unshift(`let {${[...usedProps, "...props"].join(", ")}}${useTS ? ": Props" : ""} = $props();\n`);
79
+ if (useTS) componentCode.unshift(`interface Props {\n${stringifyFactoryPropTypes(props)}\n};\n`);
80
+ const tag = hasFallback ? "Icon" : "svg";
81
+ const template = `<${tag} ${stringifyFactoryProps(props, "{prop}={{value}}")} {...props}>${innerHTML}</${tag}>`;
82
+ let content = `<script${useTS ? " lang=\"ts\"" : ""}>
83
+ ${stringifyFactoryImports(imports)}
84
+ ${componentCode.join("\n")}
85
+ <\/script>
86
+ ${template}
87
+ `;
88
+ if (styleContent) content += `<style>\n:global {\n${styleContent}\n}\n</style>\n`;
89
+ const types = addSvelteComponentTypes(data, options, assets, props);
90
+ return {
91
+ assets,
92
+ content,
93
+ types
94
+ };
95
+ }
96
+
97
+ export { createSvelteComponent };
@@ -4,11 +4,11 @@ import { getIconViewBox } from "../svg/viewbox/value.js";
4
4
  import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
6
  import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
- import { getUsedFactoryProps } from "./helpers/props/ts.js";
8
- import { addVueComponentTypes } from "./helpers/vue/types.js";
9
7
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
10
- import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
11
8
  import { makeSquareViewBox } from "../svg/viewbox/square.js";
9
+ import { getUsedFactoryProps } from "./helpers/props/ts.js";
10
+ import { addVueComponentTypes } from "./helpers/ts/vue.js";
11
+ import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
12
12
 
13
13
  /**
14
14
  * Create functional Vue component code
@@ -5,10 +5,10 @@ import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
6
  import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
7
  import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
- import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
9
- import { addVueComponentTypes } from "./helpers/vue/types.js";
10
8
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
11
9
  import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
+ import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
+ import { addVueComponentTypes } from "./helpers/ts/vue.js";
12
12
 
13
13
  /**
14
14
  * Create Vue component code
@@ -77,9 +77,7 @@ function createVueComponent(data, options) {
77
77
  };
78
78
  const usedProps = getUsedFactoryProps(props);
79
79
  if (usedProps.length) {
80
- const tsCode = useTS ? `<{
81
- ${stringifyFactoryPropTypes(props)}
82
- }>` : "";
80
+ const tsCode = useTS ? `<{\n${stringifyFactoryPropTypes(props)}\n}>` : "";
83
81
  componentCode.unshift(`const props = defineProps${tsCode}(${JSON.stringify(usedProps)});\n`);
84
82
  }
85
83
  const template = `<template><${hasFallback ? "Icon" : "svg"} ${stringifyFactoryProps(props, ":{prop}=\"{value}\"")} /></template>`;
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": "0.1.6",
6
+ "version": "0.1.7",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",
@@ -1,27 +0,0 @@
1
- import { getGeneratedComponentTypesFilename } from "../filenames/types.js";
2
- import { stringifyFactoryPropTypes } from "../props/ts.js";
3
-
4
- /**
5
- * Add Vue component types
6
- */
7
- function addVueComponentTypes(data, options, assets, props) {
8
- const content = `import { DefineSetupFnComponent, PublicProps } from 'vue';
9
-
10
- interface IconProps {
11
- ${stringifyFactoryPropTypes(props)}
12
- }
13
-
14
- declare const Component: DefineSetupFnComponent<IconProps, {}, {}, IconProps & {}, PublicProps>;
15
-
16
- export { type IconProps };
17
- export default Component;
18
- `;
19
- const filename = getGeneratedComponentTypesFilename(data, content, options);
20
- assets.push({
21
- ...filename,
22
- content
23
- });
24
- return filename.filename;
25
- }
26
-
27
- export { addVueComponentTypes };