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

Commit 50e4f57

Browse files
Merge pull request #496 from dhazelett/dh/feature/fontawesome-duotone
fix: support multiple paths in icons
2 parents 473cb94 + 812297f commit 50e4f57

File tree

1 file changed

+41
-18
lines changed
  • packages/chakra-ui-core/src/utils

1 file changed

+41
-18
lines changed
Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,72 @@
11
import { merge } from 'lodash-es'
22

3+
/**
4+
* @param {String} prefix - prefix for the icon pack
5+
* @param {Array} icon - icon definition
6+
* @returns {{path: string, viewBox: string, attrs: *}}
7+
*/
8+
const createIcon = (prefix, 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+
})
19+
)
20+
21+
return `<g fill="currentColor" class="${prefix}-group">${paths.join('')}</g>`
22+
}
23+
24+
if (prefix.startsWith('fa')) {
25+
path = Array.isArray(data)
26+
? createGroupedPath(data, prefix.substr(0, 2))
27+
: createPath(data)
28+
} else {
29+
path = prefix.startsWith('fe') ? content : svg
30+
}
31+
32+
return {
33+
path,
34+
viewBox: `0 0 ${w} ${h}`,
35+
attrs,
36+
}
37+
}
38+
339
/**
440
* @description Custom parse all Icons provided by user
541
* @param {Object} iconSet - Registered Icons object
642
* @returns {Object}
743
*/
844
const parseIcons = (iconSet = {}) => {
945
const parseIcon = (iconObject) => {
10-
const { icon } = iconObject
46+
const { icon, prefix, iconName } = iconObject
1147
// Is library icon
1248
if (icon) {
13-
const [w, h, content, svg, path, , attrs] = icon
1449
return {
15-
[`${iconObject.iconName}`]: {
16-
path: iconObject.prefix.startsWith('fa')
17-
? `<path d="${path}" fill="currentColor" />`
18-
: iconObject.prefix.startsWith('fe')
19-
? content
20-
: svg,
21-
viewBox: `0 0 ${w} ${h}`,
22-
attrs
23-
}
50+
[`${iconName}`]: createIcon(prefix, icon)
2451
}
2552
} else {
2653
return {}
2754
}
2855
}
2956

30-
const result = Object.values(iconSet)
57+
return Object.values(iconSet)
3158
.map(value => parseIcon(value))
3259
.reduce((target, source) => merge(target, source), {})
33-
34-
return result
3560
}
3661

3762
/**
3863
* @description Parse Icon packs
39-
* @param {String} pack Icon pack name
4064
* @param {Object} iconSet Registered Icon set
4165
* @returns {Object} Parsed pack icons object
4266
*/
4367
export const parsePackIcons = (iconSet) => {
4468
// TODO: Add support for other icon libraries
45-
// - Material Icons
69+
// - Material Icons: these are string constants, and need lots of work
4670
// - Tailwind Icons (Hero icons)
47-
const packIcons = parseIcons(iconSet)
48-
return packIcons
71+
return parseIcons(iconSet)
4972
}

0 commit comments

Comments
 (0)