@cypress-design/vue-icon 0.23.1 → 0.24.0

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/generate-icons.js CHANGED
@@ -16,42 +16,107 @@ const iconsComponents = Object.keys(iconsMetadata).map((name) => {
16
16
  const iconBodies = iconSet.reduce((acc, icon) => {
17
17
  const sizeIndex = availableIds.indexOf(icon.name)
18
18
  if (sizeIndex > -1) {
19
- acc[iconMetadata.availableSizes[sizeIndex]] = icon.data
19
+ const indexOfDefs = icon.data.indexOf('<defs>')
20
+ acc[iconMetadata.availableSizes[sizeIndex]] = {
21
+ body: indexOfDefs >= 0 ? icon.data.slice(0, indexOfDefs) : icon.data,
22
+ // avoid defs: undefined in final exported code
23
+ ...(indexOfDefs >= 0 ? { defs: icon.data.slice(indexOfDefs) } : {}),
24
+ }
20
25
  }
21
26
  return acc
22
27
  }, {})
28
+
23
29
  return dedent`
24
- export const Icon${pascalCaseName} = (props: SVGAttributes & Omit<iconsRegistry.Icon${pascalCaseName}Props, 'name'>) => {
25
- const { interactiveColorsOnGroup, name, ...cleanProps } = props
26
- const { sizeWithDefault: size, compiledClasses } = iconsRegistry.getComponentAttributes({
27
- ...cleanProps,
28
- availableSizes: ${JSON.stringify(iconMetadata.availableSizes)},
29
- interactiveColorsOnGroup
30
- })
31
- const iconBodies: Record<string, string> = ${JSON.stringify(
30
+ export const Icon${pascalCaseName} = defineComponent((props: Omit<iconsRegistry.Icon${pascalCaseName}Props, 'name'> & {
31
+ class?: string
32
+ }, { attrs }: { attrs: Omit<SVGAttributes, 'name' | 'class'> }) => {
33
+ const iconPropsStep = useIconProps(props, ${JSON.stringify(
32
34
  iconBodies,
33
35
  null,
34
36
  2
35
- )}
36
- const body = iconBodies[size]
37
+ )}, ${JSON.stringify(iconMetadata.availableSizes)}, ${JSON.stringify(name)})
38
+
39
+ const { componentProps, defs } = compileVueIconProperties(iconPropsStep)
40
+
41
+ const { shouldRenderDefs } = useShouldRenderDefs(
42
+ ${JSON.stringify(name)},
43
+ defs)
44
+
45
+ return () => hyperSVG(componentProps, defs, shouldRenderDefs, attrs, props.class)
46
+ },
47
+ // @ts-expect-error - vue types need an update
48
+ __iconComponentOpts__)
49
+ `
50
+ })
51
+
52
+ writeFile(`
53
+ import { h, defineComponent, computed } from 'vue'
54
+ import type { ComputedRef, SVGAttributes, Ref } from 'vue'
55
+ import * as iconsRegistry from '@cypress-design/icon-registry'
56
+ import { compileVueIconProperties, useShouldRenderDefs } from './compileProperties'
57
+
58
+ const __iconComponentOpts__ = {
59
+ props: [...iconsRegistry.ICON_COLOR_PROP_NAMES, 'interactiveColorsOnGroup', 'size', 'class'],
60
+ } as const
61
+
62
+ function useIconProps(props: SVGAttributes & Omit<iconsRegistry.IconProps, 'name'>, iconBodiesAndDefs: Record<string, {body: string, defs?: string}>, availableSizes: string[], name: string) {
63
+ return computed(() => {
64
+ const { interactiveColorsOnGroup, ...cleanProps } = props
65
+
66
+ const { sizeWithDefault: size, compiledClasses } = iconsRegistry.getComponentAttributes({
67
+ ...cleanProps,
68
+ availableSizes,
69
+ interactiveColorsOnGroup,
70
+ })
71
+
72
+ const { body, defs } = iconBodiesAndDefs[size] || {}
37
73
  if(!body){
38
- throw Error(\`Icon "${name}" is not available in size ${'$'}{size}\`)
74
+ throw Error(\`Icon "${'$'}{name}" is not available in size ${'$'}{size}\`)
39
75
  }
40
- return h('svg', compileVueIconProperties({
76
+
77
+ return {
41
78
  ...cleanProps,
79
+ name,
42
80
  size,
43
81
  body,
82
+ defs,
44
83
  compiledClasses
45
- }))
46
- }
47
- `
48
- })
84
+ }
85
+ })
86
+ }
49
87
 
50
- writeFile(`
51
- import * as iconsRegistry from '@cypress-design/icon-registry'
52
- import { compileVueIconProperties } from './compileProperties'
53
- import type { SVGAttributes } from 'vue'
54
- import { h } from 'vue'
88
+ function hyperSVG(
89
+ componentProps: ComputedRef<SVGAttributes>,
90
+ defs: ComputedRef<string | undefined>,
91
+ shouldRenderDefs: Ref<boolean>,
92
+ attrs: SVGAttributes,
93
+ className?: string,
94
+ ) {
95
+
96
+ return shouldRenderDefs.value && defs.value
97
+ ? [
98
+ h('svg', {
99
+ innerHTML: defs.value,
100
+ height: 0,
101
+ width: 0,
102
+ style: 'position:absolute',
103
+ class: [
104
+ 'pointer-events-none',
105
+ 'opacity-0',
106
+ ],
107
+ }),
108
+ h('svg', {
109
+ ...attrs,
110
+ ...componentProps.value,
111
+ class: [className, componentProps.value.class],
112
+ }),
113
+ ]
114
+ : h('svg', {
115
+ ...attrs,
116
+ ...componentProps.value,
117
+ class: [className, componentProps.value.class],
118
+ })
119
+ }
55
120
 
56
121
  ${iconsComponents.join('\n\n\n')}
57
122
  `)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cypress-design/vue-icon",
3
- "version": "0.23.1",
3
+ "version": "0.24.0",
4
4
  "files": [
5
5
  "*"
6
6
  ],
@@ -21,13 +21,13 @@
21
21
  "build:types": "yarn tsc --project ./tsconfig.build.json"
22
22
  },
23
23
  "dependencies": {
24
- "@cypress-design/icon-registry": "^0.24.0"
24
+ "@cypress-design/icon-registry": "^0.25.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@cypress-design/rollup-plugin-tailwind-keep": "*",
28
28
  "@types/lodash": "^4.14.195",
29
29
  "lodash": "^4.17.21",
30
- "dedent": "^0.7.0"
30
+ "dedent": "^1.2.0"
31
31
  },
32
32
  "license": "MIT"
33
33
  }