@cyberalien/svg-utils 1.0.8 → 1.0.10

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/lib/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { ClassProp, classProps, defaultClassProp } from "./classname/const.js";
2
2
  import { splitClassName, toggleClassName } from "./classname/toggle.js";
3
3
  import { UniqueHashOptions } from "./helpers/hash/types.js";
4
4
  import { ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions } from "./xml/types.js";
5
- import { ConvertSVGContentOptions, ConvertedSVGContent } from "./svg-css/types.js";
5
+ import { BaseConvertSVGContentOptions, ConvertSVGContentOptions, ConvertedSVGContent } from "./svg-css/types.js";
6
6
  import { createCSSClassName } from "./css/hash.js";
7
7
  import { stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector } from "./css/stringify.js";
8
8
  import { hashString } from "./helpers/hash/hash.js";
@@ -24,4 +24,4 @@ import { changeSVGIDs } from "./svg/ids/change.js";
24
24
  import { createUniqueIDs } from "./svg/ids/unique.js";
25
25
  import { convertSVGRootToCSS } from "./svg-css/root.js";
26
26
  import { convertSVGContentToCSSRules } from "./svg-css/content.js";
27
- export { ChangeIDResult, ClassProp, ComparisonKey, ConvertSVGContentOptions, ConvertedSVGContent, ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions, UniqueHashOptions, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyXMLContent, toggleClassName, uniquePromise };
27
+ export { BaseConvertSVGContentOptions, ChangeIDResult, ClassProp, ComparisonKey, ConvertSVGContentOptions, ConvertedSVGContent, ParsedXMLNode, ParsedXMLTagElement, ParsedXMLTextElement, StringifyXMLOptions, UniqueHashOptions, changeIDInString, changeSVGIDs, classProps, cloneObject, compareKeys, compareSets, compareValues, convertSVGContentToCSSRules, convertSVGRootToCSS, createCSSClassName, createUniqueIDs, defaultClassProp, getUniqueHash, hashString, hashToString, iterateXMLContent, parseXMLContent, removeDuplicateIDs, removeUnusedIDs, sortObject, splitClassName, stringifyCSSKeyframes, stringifyCSSRules, stringifyCSSSelector, stringifyXMLContent, toggleClassName, uniquePromise };
@@ -1,3 +1,15 @@
1
+ /**
2
+ * Tag used to set attribute value without animation duration
3
+ */
4
+ declare const svgSetTag = "set";
5
+ /**
6
+ * Tag to discard animation
7
+ */
8
+ declare const svgDiscardTag = "discard";
9
+ /**
10
+ * Attribute animation tag
11
+ */
12
+ declare const svgAnimationTag = "animate";
1
13
  /**
2
14
  * Tags for animating one attribute in SVG
3
15
  */
@@ -14,4 +26,4 @@ declare const svgAnimateMotionTag = "animateMotion";
14
26
  * All tags for animating SVG
15
27
  */
16
28
  declare const svgAnimationTags: string[];
17
- export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTags, svgSimpleAnimationTags };
29
+ export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTag, svgAnimationTags, svgDiscardTag, svgSetTag, svgSimpleAnimationTags };
@@ -1,10 +1,22 @@
1
1
  /**
2
+ * Tag used to set attribute value without animation duration
3
+ */
4
+ const svgSetTag = "set";
5
+ /**
6
+ * Tag to discard animation
7
+ */
8
+ const svgDiscardTag = "discard";
9
+ /**
10
+ * Attribute animation tag
11
+ */
12
+ const svgAnimationTag = "animate";
13
+ /**
2
14
  * Tags for animating one attribute in SVG
3
15
  */
4
16
  const svgSimpleAnimationTags = [
5
- "animate",
6
- "set",
7
- "discard"
17
+ svgSetTag,
18
+ svgDiscardTag,
19
+ svgAnimationTag
8
20
  ];
9
21
  /**
10
22
  * Tag for animating transformations in SVG
@@ -23,4 +35,4 @@ const svgAnimationTags = [
23
35
  svgAnimateMotionTag
24
36
  ];
25
37
 
26
- export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTags, svgSimpleAnimationTags };
38
+ export { svgAnimateMotionTag, svgAnimateTransformTag, svgAnimationTag, svgAnimationTags, svgDiscardTag, svgSetTag, svgSimpleAnimationTags };
@@ -8,7 +8,7 @@ import { convertSVGRootToCSS } from "./root.js";
8
8
  function convertSVGContentToCSSRules(content, options) {
9
9
  const root = parseXMLContent(content);
10
10
  if (!root) return { content };
11
- const classes = convertSVGRootToCSS(root, options?.classNamePrefix, options?.hashOptions);
11
+ const classes = convertSVGRootToCSS(root, options);
12
12
  if (classes) {
13
13
  const newContent = stringifyXMLContent(root, options);
14
14
  if (newContent) return {
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * Convert property to CSS
3
3
  */
4
- declare function convertSVGPropertyToCSS(tag: string, prop: string, value: string | number): [string, string] | undefined;
4
+ declare function convertSVGPropertyToCSS(tag: string, prop: string, value: string | number, supportLegacyBrowsers?: boolean): [string, string] | undefined;
5
5
  export { convertSVGPropertyToCSS };
@@ -57,10 +57,10 @@ function getSVGPropertyType(tag, prop) {
57
57
  /**
58
58
  * Convert property to CSS
59
59
  */
60
- function convertSVGPropertyToCSS(tag, prop, value) {
60
+ function convertSVGPropertyToCSS(tag, prop, value, supportLegacyBrowsers = false) {
61
61
  switch (getSVGPropertyType(tag, prop)) {
62
62
  case "path":
63
- if (typeof value !== "string") return;
63
+ if (supportLegacyBrowsers || typeof value !== "string") return;
64
64
  return [prop, `path("${value.replace(/\s+/g, " ")}")`];
65
65
  case "px":
66
66
  if (typeof value === "string" && !value.match(/^[0-9.-]+$/)) return [prop, value];
@@ -5,5 +5,5 @@ import { SVGConvertedToCSSProperties } from "./types.js";
5
5
  *
6
6
  * Returns CSS rules, does not add a class to tag, but does remove properties from tag
7
7
  */
8
- declare function extractSVGTagPropertiesForCSS(tag: ParsedXMLTagElement): SVGConvertedToCSSProperties | undefined;
8
+ declare function extractSVGTagPropertiesForCSS(tag: ParsedXMLTagElement, supportLegacyBrowsers?: boolean): SVGConvertedToCSSProperties | undefined;
9
9
  export { extractSVGTagPropertiesForCSS };
@@ -7,7 +7,7 @@ import { svgAnimateTransformTag, svgSimpleAnimationTags } from "../animations/ta
7
7
  *
8
8
  * Returns CSS rules, does not add a class to tag, but does remove properties from tag
9
9
  */
10
- function extractSVGTagPropertiesForCSS(tag) {
10
+ function extractSVGTagPropertiesForCSS(tag, supportLegacyBrowsers = false) {
11
11
  const result = {
12
12
  props: [],
13
13
  rules: Object.create(null)
@@ -24,7 +24,7 @@ function extractSVGTagPropertiesForCSS(tag) {
24
24
  });
25
25
  for (const prop in tag.attribs) if (!animatedProps.has(prop)) {
26
26
  const value = tag.attribs[prop];
27
- const converted = convertSVGPropertyToCSS(tag.tag, prop, value);
27
+ const converted = convertSVGPropertyToCSS(tag.tag, prop, value, supportLegacyBrowsers);
28
28
  if (converted) {
29
29
  const [propName, propValue] = converted;
30
30
  result.props.push(propName);
@@ -1,9 +1,10 @@
1
- import { CSSHashOptions, CSSRules } from "../css/types.js";
1
+ import { CSSRules } from "../css/types.js";
2
2
  import { ParsedXMLTagElement } from "../xml/types.js";
3
+ import { BaseConvertSVGContentOptions } from "./types.js";
3
4
  /**
4
5
  * Convert SVG tags tree to SVG+CSS
5
6
  *
6
7
  * Returns used CSS class names with rules
7
8
  */
8
- declare function convertSVGRootToCSS(root: ParsedXMLTagElement[], classNamePrefix?: string, hashOptions?: CSSHashOptions): Record<string, CSSRules>;
9
+ declare function convertSVGRootToCSS(root: ParsedXMLTagElement[], options?: BaseConvertSVGContentOptions): Record<string, CSSRules>;
9
10
  export { convertSVGRootToCSS };
@@ -8,11 +8,13 @@ import { extractSVGTagPropertiesForCSS } from "./props/props.js";
8
8
  *
9
9
  * Returns used CSS class names with rules
10
10
  */
11
- function convertSVGRootToCSS(root, classNamePrefix = "", hashOptions) {
11
+ function convertSVGRootToCSS(root, options = {}) {
12
12
  const rules = Object.create(null);
13
+ const hashOptions = options.hashOptions || {};
14
+ const classNamePrefix = options.classNamePrefix || "";
13
15
  iterateXMLContent(root, (node) => {
14
16
  if (node.type === "tag") {
15
- const props = extractSVGTagPropertiesForCSS(node);
17
+ const props = extractSVGTagPropertiesForCSS(node, options.legacy);
16
18
  if (props) {
17
19
  const className = createCSSClassName(props.rules, classNamePrefix, hashOptions);
18
20
  toggleClassName(node.attribs, className, true);
@@ -1,12 +1,17 @@
1
1
  import { CSSHashOptions, CSSKeyframes, CSSRules } from "../css/types.js";
2
2
  import { StringifyXMLOptions } from "../xml/types.js";
3
3
  /**
4
- * Options for converting SVG content to SVG+CSS
4
+ * Options for converting SVG tags to SVG+CSS
5
5
  */
6
- interface ConvertSVGContentOptions extends StringifyXMLOptions {
6
+ interface BaseConvertSVGContentOptions {
7
7
  classNamePrefix?: string;
8
8
  hashOptions?: CSSHashOptions;
9
+ legacy?: boolean;
9
10
  }
11
+ /**
12
+ * Options for converting SVG content to SVG+CSS
13
+ */
14
+ interface ConvertSVGContentOptions extends StringifyXMLOptions, BaseConvertSVGContentOptions {}
10
15
  /**
11
16
  * Result of converting SVG content to SVG+CSS
12
17
  */
@@ -15,4 +20,4 @@ interface ConvertedSVGContent {
15
20
  classes?: Record<string, CSSRules>;
16
21
  keyframes?: Record<string, CSSKeyframes>;
17
22
  }
18
- export { ConvertSVGContentOptions, ConvertedSVGContent };
23
+ export { BaseConvertSVGContentOptions, ConvertSVGContentOptions, ConvertedSVGContent };
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.10",
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 };