-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from "react"; | ||
|
||
import { IconContext, DefaultContext } from "./iconContext"; | ||
|
||
export interface IconTree { | ||
tag: string; | ||
attr: { [key: string]: string }; | ||
child: IconTree[]; | ||
} | ||
|
||
function Tree2Element(tree: IconTree[]): React.ReactElement[] { | ||
return ( | ||
tree && | ||
tree.map((node, i) => | ||
React.createElement( | ||
node.tag, | ||
{ key: i, ...node.attr }, | ||
Tree2Element(node.child) | ||
) | ||
) | ||
); | ||
} | ||
export function GenIcon(data: IconTree) { | ||
// eslint-disable-next-line react/display-name | ||
return (props: IconBaseProps) => ( | ||
<IconBase attr={{ ...data.attr }} {...props}> | ||
{Tree2Element(data.child)} | ||
</IconBase> | ||
); | ||
} | ||
|
||
export interface IconBaseProps extends React.SVGAttributes<SVGElement> { | ||
children?: React.ReactNode; | ||
size?: string | number; | ||
color?: string; | ||
title?: string; | ||
} | ||
|
||
export type IconType = (props: IconBaseProps) => JSX.Element; | ||
export function IconBase( | ||
props: IconBaseProps & { attr?: Record<string, string> } | ||
): JSX.Element { | ||
const elem = (conf: IconContext) => { | ||
const { attr, title, ...svgProps } = props; | ||
|
||
let className; | ||
if (conf.className) className = conf.className; | ||
if (props.className) | ||
className = (className ? className + " " : "") + props.className; | ||
|
||
return ( | ||
<svg | ||
stroke="currentColor" | ||
fill="currentColor" | ||
strokeWidth="0" | ||
{...conf.attr} | ||
{...attr} | ||
{...svgProps} | ||
className={className} | ||
style={{ | ||
color: props.color || conf.color, | ||
...conf.style, | ||
...props.style, | ||
}} | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
{title && <title>{title}</title>} | ||
{props.children} | ||
</svg> | ||
); | ||
}; | ||
|
||
return IconContext !== undefined ? ( | ||
<IconContext.Consumer> | ||
{(conf: IconContext) => elem(conf)} | ||
</IconContext.Consumer> | ||
) : ( | ||
elem(DefaultContext) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters