@cypress-design/icon-registry 0.25.0 → 0.26.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/package.json +4 -2
- package/src/compileIcon.ts +103 -0
- package/src/index.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cypress-design/icon-registry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "All svg files to be exposed to the Icon component",
|
|
5
5
|
"main": "dist/index.umd.js",
|
|
6
6
|
"module": "dist/index.es.mjs",
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
"build": "./build.sh"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"src/compileIcon.ts",
|
|
22
|
+
"src/index.ts"
|
|
21
23
|
],
|
|
22
24
|
"dependencies": {
|
|
23
25
|
"@cypress-design/css": "*"
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import _ from 'lodash'
|
|
2
|
+
import { COLOR_PREFIXES } from '@cypress-design/css/dist/color-constants'
|
|
3
|
+
import type { OpenIconProps, ColorIconProps, IconProps } from './icons'
|
|
4
|
+
import { iconsMetadata, ICON_COLOR_PROP_NAMES } from './icons'
|
|
5
|
+
import { iconSet } from './iconsList'
|
|
6
|
+
|
|
7
|
+
const camelCase = _.camelCase
|
|
8
|
+
|
|
9
|
+
export const compileIcon = (
|
|
10
|
+
props: Omit<OpenIconProps, 'name'> & Pick<IconProps, 'name'>
|
|
11
|
+
) => {
|
|
12
|
+
const { interactiveColorsOnGroup, name, ...cleanProps } = props
|
|
13
|
+
const { availableSizes } = iconsMetadata[name]
|
|
14
|
+
|
|
15
|
+
const { sizeWithDefault, compiledClasses } = getComponentAttributes({
|
|
16
|
+
...cleanProps,
|
|
17
|
+
availableSizes,
|
|
18
|
+
interactiveColorsOnGroup,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const nameWithSize = camelCase(`${name}_x${sizeWithDefault}`)
|
|
22
|
+
const iconData = iconSet.find((i) => i.name === nameWithSize)
|
|
23
|
+
if (!iconData) {
|
|
24
|
+
throw new Error(`icon '${name}' at size ${sizeWithDefault} not found`)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// SVGO always brings defs to the end of the file
|
|
28
|
+
// so we can split the file in two parts
|
|
29
|
+
// and use the first part as the body
|
|
30
|
+
const defsStart = iconData.data.indexOf('<defs>')
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
...cleanProps,
|
|
34
|
+
name,
|
|
35
|
+
size: sizeWithDefault,
|
|
36
|
+
compiledClasses,
|
|
37
|
+
body: defsStart >= 0 ? iconData.data.slice(0, defsStart) : iconData.data,
|
|
38
|
+
defs: defsStart >= 0 ? iconData.data.slice(defsStart) : undefined,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const getComponentAttributes = (
|
|
43
|
+
props: {
|
|
44
|
+
availableSizes: readonly string[]
|
|
45
|
+
} & Omit<OpenIconProps, 'name'>
|
|
46
|
+
) => {
|
|
47
|
+
const { size, availableSizes, interactiveColorsOnGroup, ...otherProps } =
|
|
48
|
+
props
|
|
49
|
+
const sizeWithDefault =
|
|
50
|
+
size ??
|
|
51
|
+
(availableSizes.length >= 1
|
|
52
|
+
? availableSizes.indexOf('16') > -1
|
|
53
|
+
? '16'
|
|
54
|
+
: availableSizes[0]
|
|
55
|
+
: '')
|
|
56
|
+
|
|
57
|
+
const protectedInteractiveColorsOnGroup =
|
|
58
|
+
interactiveColorsOnGroup === undefined
|
|
59
|
+
? otherProps['interactive-colors-on-group']
|
|
60
|
+
: interactiveColorsOnGroup
|
|
61
|
+
|
|
62
|
+
delete otherProps['interactive-colors-on-group']
|
|
63
|
+
|
|
64
|
+
// TODO: when all icons are converted to using the design system,
|
|
65
|
+
// replace dark by stroke and light by fill,
|
|
66
|
+
// both here and in the tailwind plugins configs.
|
|
67
|
+
const compiledClasses = Object.keys(otherProps)
|
|
68
|
+
.filter(
|
|
69
|
+
(attrName) =>
|
|
70
|
+
otherProps[attrName as keyof typeof otherProps] &&
|
|
71
|
+
ICON_COLOR_PROP_NAMES.includes(
|
|
72
|
+
attrName as (typeof ICON_COLOR_PROP_NAMES)[number]
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
.map((colorAttrName: string) => {
|
|
76
|
+
const color = otherProps[colorAttrName as keyof ColorIconProps]
|
|
77
|
+
const lowerCaseColor = colorAttrName.toLowerCase().replace(/-/g, '')
|
|
78
|
+
const colorClass = lowerCaseColor.includes('strokecolor')
|
|
79
|
+
? 'dark'
|
|
80
|
+
: 'light'
|
|
81
|
+
const secondaryClass = lowerCaseColor.includes('secondary')
|
|
82
|
+
? '-secondary'
|
|
83
|
+
: ''
|
|
84
|
+
|
|
85
|
+
const pseudoClass = COLOR_PREFIXES.reduce((state, sta) => {
|
|
86
|
+
if (state.length) return state
|
|
87
|
+
return lowerCaseColor.includes(sta.replace(/-/g, '')) ? sta : ''
|
|
88
|
+
}, '')
|
|
89
|
+
|
|
90
|
+
const prefix = pseudoClass.length
|
|
91
|
+
? protectedInteractiveColorsOnGroup !== undefined &&
|
|
92
|
+
protectedInteractiveColorsOnGroup !== false
|
|
93
|
+
? `group-${pseudoClass}:`
|
|
94
|
+
: `icon-${pseudoClass}:`
|
|
95
|
+
: ''
|
|
96
|
+
|
|
97
|
+
const finalClass = `${prefix}icon-${colorClass}${secondaryClass}-${color}`
|
|
98
|
+
|
|
99
|
+
return finalClass
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
return { compiledClasses, sizeWithDefault }
|
|
103
|
+
}
|
package/src/index.ts
ADDED