/
create-icon.tsx
51 lines (46 loc) · 1.22 KB
/
create-icon.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { forwardRef } from "@chakra-ui/system"
import { __DEV__ } from "@chakra-ui/utils"
import * as React from "react"
import { Icon, IconProps } from "./icon"
interface CreateIconOptions {
/**
* The icon `svg` viewBox
* @default "0 0 24 24"
*/
viewBox?: string
/**
* The `svg` path or group element
* @type React.ReactElement | React.ReactElement[]
*/
path?: React.ReactElement | React.ReactElement[]
/**
* If the `svg` has a single path, simply copy the path's `d` attribute
*/
d?: string
/**
* The display name useful in the dev tools
*/
displayName?: string
/**
* Default props automatically passed to the component; overwriteable
*/
defaultProps?: IconProps
}
export function createIcon(options: CreateIconOptions) {
const {
viewBox = "0 0 24 24",
d: pathDefinition,
displayName,
defaultProps = {},
} = options
const path = React.Children.toArray(options.path)
const Comp = forwardRef<IconProps, "svg">((props, ref) => (
<Icon ref={ref} viewBox={viewBox} {...defaultProps} {...props}>
{path.length ? path : <path fill="currentColor" d={pathDefinition} />}
</Icon>
))
if (__DEV__) {
Comp.displayName = displayName
}
return Comp
}