@cypress-design/vue-icon 0.23.1 → 0.23.2

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,47 +1,131 @@
1
1
  import type { OpenIconProps } from '@cypress-design/icon-registry'
2
2
  import { ICON_COLOR_PROP_NAMES } from '@cypress-design/icon-registry'
3
+ import {
4
+ ComputedRef,
5
+ Ref,
6
+ computed,
7
+ onBeforeMount,
8
+ onUnmounted,
9
+ ref,
10
+ } from 'vue'
3
11
  import type { SVGAttributes } from 'vue'
4
12
 
5
- export const compileVueIconProperties = ({
6
- body,
7
- compiledClasses,
8
- size,
9
- ...attributes
10
- }: Omit<OpenIconProps, 'name'> &
11
- SVGAttributes & {
12
- body: string
13
- compiledClasses: string[]
14
- size: string
15
- interactiveColorsOnGroup?: boolean
16
- }) => {
17
- const filteredAttributes = Object.keys(attributes).reduce(
18
- (newAttributes, attrName) => {
19
- if (
20
- !ICON_COLOR_PROP_NAMES.includes(
21
- attrName as (typeof ICON_COLOR_PROP_NAMES)[number]
22
- ) &&
23
- attrName !== 'name'
24
- ) {
25
- // @ts-expect-error - TS doesn't know that attrName is a key of SVGAttributes
26
- newAttributes[attrName] =
27
- attributes[attrName as keyof typeof attributes]
13
+ /**
14
+ * Icon names of the SVGs whose defs already appear in the DOM
15
+ */
16
+ const defsAlreadyLoaded = new Set<string>()
17
+
18
+ /**
19
+ * There is a bug in the chrome renderer
20
+ * If an svg containing defs is twice on the page
21
+ * AND
22
+ * the first instance is hidden (display:none)
23
+ *
24
+ * Then the defs are not rendered for the second instance
25
+ * @param iconName
26
+ * @param defs
27
+ */
28
+ export const useShouldRenderDefs = (
29
+ iconName: string,
30
+ defs: ComputedRef<string | undefined>
31
+ ) => {
32
+ const shouldRenderDefs = ref(false)
33
+
34
+ onBeforeMount(() => {
35
+ const hasDocMarker =
36
+ // on SSR, we always want the first instance to come with the defs
37
+ typeof document === 'undefined' ||
38
+ // in interactive mode, the defs can be loaded, then removed, then loaded again
39
+ // so we check that they are here before adding them again
40
+ !document.querySelector(`[data-cy-icon-unified-defs="${iconName}"]`)
41
+ shouldRenderDefs.value = !defsAlreadyLoaded.has(iconName) && hasDocMarker
42
+ if (hasDocMarker) {
43
+ defsAlreadyLoaded.add(iconName)
44
+ }
45
+ })
46
+
47
+ onUnmounted(() => {
48
+ if (shouldRenderDefs.value && defs.value) {
49
+ // to avoid ssr mismatch errors and jank
50
+ // add the defs inside an extra SVG and append it to the end of the body only when unmounting
51
+ const newDefs = document.createElementNS(
52
+ 'http://www.w3.org/2000/svg',
53
+ 'svg'
54
+ )
55
+ newDefs.setAttribute('width', '0')
56
+ newDefs.setAttribute('height', '0')
57
+ newDefs.setAttribute('data-cy-icon-unified-defs', iconName)
58
+ newDefs.style.position = 'absolute'
59
+ newDefs.innerHTML = defs.value
60
+ document.body.appendChild(newDefs)
61
+ }
62
+ })
63
+ return { shouldRenderDefs }
64
+ }
65
+
66
+ export const compileVueIconProperties = (
67
+ opts: Ref<
68
+ OpenIconProps &
69
+ SVGAttributes & {
70
+ body: string
71
+ defs?: string
72
+ compiledClasses: string[]
73
+ size: string
74
+ interactiveColorsOnGroup?: boolean
28
75
  }
29
- return newAttributes
30
- },
31
- {} as Omit<SVGAttributes, 'name'>
32
- )
33
-
34
- const componentProps = {
35
- width: size,
36
- height: size,
37
- viewBox: `0 0 ${size} ${size}`,
38
- fill: 'none',
39
- innerHTML: body,
40
- class: undefined as string | undefined,
41
- ...filteredAttributes, // add all standard attributes back to the svg tag
42
- }
43
- if (compiledClasses.length) {
44
- componentProps.class = compiledClasses.join(' ')
76
+ >
77
+ ) => {
78
+ const ret = computed(() => {
79
+ const {
80
+ name: iconName,
81
+ body,
82
+ defs,
83
+ compiledClasses,
84
+ size,
85
+ ...attributes
86
+ } = opts.value
87
+ const filteredAttributes = Object.keys(attributes).reduce(
88
+ (newAttributes, attrName) => {
89
+ if (
90
+ !ICON_COLOR_PROP_NAMES.includes(
91
+ attrName as (typeof ICON_COLOR_PROP_NAMES)[number]
92
+ ) &&
93
+ attrName !== 'name'
94
+ ) {
95
+ // @ts-expect-error - TS doesn't know that attrName is a key of SVGAttributes
96
+ newAttributes[attrName] =
97
+ attributes[attrName as keyof typeof attributes]
98
+ }
99
+ return newAttributes
100
+ },
101
+ {} as Omit<SVGAttributes, 'name'>
102
+ )
103
+
104
+ const iconFileId = `${iconName}_${size}`
105
+
106
+ const componentProps = {
107
+ width: size,
108
+ height: size,
109
+ viewBox: `0 0 ${size} ${size}`,
110
+ fill: 'none',
111
+ innerHTML: body,
112
+ class: undefined as string | undefined,
113
+ ...filteredAttributes, // add all standard attributes back to the svg tag
114
+ }
115
+
116
+ if (compiledClasses.length) {
117
+ componentProps.class = compiledClasses.join(' ')
118
+ }
119
+
120
+ return {
121
+ componentProps,
122
+ iconFileId,
123
+ defs,
124
+ }
125
+ })
126
+
127
+ return {
128
+ componentProps: computed(() => ret.value.componentProps),
129
+ defs: computed(() => ret.value.defs),
45
130
  }
46
- return componentProps
47
131
  }
package/dist/Icon.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { OpenIconProps, IconProps } from '@cypress-design/icon-registry';
2
- import type { SVGAttributes } from 'vue';
3
- declare const _default: (props: OpenIconProps & Pick<IconProps, 'name'> & Omit<SVGAttributes, 'name'>) => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
4
- [key: string]: any;
5
- }>;
2
+ declare const _default: (props: OpenIconProps & Pick<IconProps, "name"> & {
3
+ class?: string | undefined;
4
+ } & {}) => any;
6
5
  export default _default;
7
6
  //# sourceMappingURL=Icon.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Icon.d.ts","sourceRoot":"","sources":["../Icon.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAA;AAE7E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,KAAK,CAAA;gCAM/B,aAAa,GAAG,KAAK,SAAS,EAAE,MAAM,CAAC,GAAG,KAAK,aAAa,EAAE,MAAM,CAAC;;;AAH9E,wBAWC"}
1
+ {"version":3,"file":"Icon.d.ts","sourceRoot":"","sources":["../Icon.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAA;;;;AAQ7E,wBAwDC"}