Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 316d649

Browse files
authored
chore: make parsePackIcons more generic
1 parent d7dcdae commit 316d649

File tree

1 file changed

+37
-30
lines changed
  • packages/chakra-ui-core/src/utils

1 file changed

+37
-30
lines changed
Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,74 @@
11
import { merge } from 'lodash-es'
22

33
/**
4-
* @description Parse FontAwesome icon path
5-
* @param {String|Array} path a single svg path, or array of paths
6-
* @returns {String}
4+
* @param {String} pack
5+
* @param {Array} icon
6+
* @returns {{path: string, viewBox: string, attrs: *}}
77
*/
8-
const parseFontAwesomeIcon = (path) => {
9-
// duotone icon
10-
if (Array.isArray(path) && path.length === 2) {
11-
const paths = path.map((d, idx) =>
12-
`<path d="${d}" fill="currentColor" class="${idx ? 'fa-primary' : 'fa-secondary'}" />`
8+
const createIcon = (pack, icon) => {
9+
const [w, h, content, svg, data, , attrs] = icon
10+
let path
11+
12+
const createPath = (d, attrs = {}) => `<path d="${d}" ${attrs.className ? `class="${attrs.className}"` : ''} fill="currentColor" />`
13+
14+
const createGroupedPath = (groups, prefix) => {
15+
const paths = groups.map((d, idx) =>
16+
createPath(d, {
17+
className: idx ? `${prefix}-primary` : `${prefix}-secondary`,
18+
})
1319
)
1420

15-
return `<defs><style>.fa-secondary{opacity:.4}</style></defs><g class="fa-group">${paths.join('')}</g>`
21+
return `<g fill="currentColor" class="${prefix}-group">${paths.join('')}</g>`
22+
}
23+
24+
if (pack === 'fa') {
25+
path = Array.isArray(data)
26+
? createGroupedPath(data, pack)
27+
: createPath(data)
28+
} else {
29+
path = pack.startsWith('fe') ? content : svg
1630
}
1731

18-
return `<path d="${path}" fill="currentColor" />`
32+
return {
33+
path,
34+
viewBox: `0 0 ${w} ${h}`,
35+
attrs,
36+
}
1937
}
2038

2139
/**
2240
* @description Custom parse all Icons provided by user
41+
* @param {String} pack - the name of the icon pack being used (fe, fa, mdi, etc)
2342
* @param {Object} iconSet - Registered Icons object
2443
* @returns {Object}
2544
*/
26-
const parseIcons = (iconSet = {}) => {
45+
const parseIcons = (pack, iconSet = {}) => {
2746
const parseIcon = (iconObject) => {
28-
const { icon } = iconObject
47+
const { icon, iconName } = iconObject
2948
// Is library icon
3049
if (icon) {
31-
const [w, h, content, svg, path, , attrs] = icon
3250
return {
33-
[`${iconObject.iconName}`]: {
34-
path: iconObject.prefix.startsWith('fa')
35-
? parseFontAwesomeIcon(path)
36-
: iconObject.prefix.startsWith('fe')
37-
? content
38-
: svg,
39-
viewBox: `0 0 ${w} ${h}`,
40-
attrs
41-
}
51+
[`${iconName}`]: createIcon(pack, icon)
4252
}
4353
} else {
4454
return {}
4555
}
4656
}
4757

48-
const result = Object.values(iconSet)
58+
return Object.values(iconSet)
4959
.map(value => parseIcon(value))
5060
.reduce((target, source) => merge(target, source), {})
51-
52-
return result
5361
}
5462

5563
/**
5664
* @description Parse Icon packs
57-
* @param {String} pack Icon pack name
65+
* @param {String} pack - the name of the icon pack being used (fe, fa, mdi, etc)
5866
* @param {Object} iconSet Registered Icon set
5967
* @returns {Object} Parsed pack icons object
6068
*/
61-
export const parsePackIcons = (iconSet) => {
69+
export const parsePackIcons = (pack, iconSet) => {
6270
// TODO: Add support for other icon libraries
63-
// - Material Icons
71+
// - Material Icons: these are string constants, and need lots of work
6472
// - Tailwind Icons (Hero icons)
65-
const packIcons = parseIcons(iconSet)
66-
return packIcons
73+
return parseIcons(pack, iconSet)
6774
}

0 commit comments

Comments
 (0)