Last active
October 9, 2022 18:35
-
-
Save wmdmark/3f847d37baaf5520fc272d96b7546c23 to your computer and use it in GitHub Desktop.
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
import { Icon } from "@chakra-ui/react" | |
import * as icons from "@pathwright/pathicons" | |
import PropTypes from "prop-types" | |
const Pathicon = ({ name, ...props }) => { | |
const componentName = componentNameFromIconName(name) | |
const IconComponent = icons[componentName] | |
if (!IconComponent) { | |
console.warn(`Icon ${name} (${componentName}) not found`) | |
return <Icon /> | |
} | |
const boxSize = props.size ? `${props.size}px` : props.boxSize || undefined | |
return <Icon boxSize={boxSize} as={IconComponent} alt={name} {...props} /> | |
} | |
export const iconNameFromComponentName = componentName => { | |
let name = componentName.replace("Icon", "") | |
// split by capital letters | |
const split = name.split(/(?=[A-Z])/) | |
// join with hyphen | |
name = split.join("-").toLowerCase() | |
return name | |
} | |
export const componentNameFromIconName = iconName => { | |
// split by hyphen | |
const split = iconName.split("-") | |
// capitalize each element | |
const capitalized = split.map( | |
word => word.charAt(0).toUpperCase() + word.slice(1) | |
) | |
// join with capital letters | |
return `${capitalized.join("")}Icon` | |
} | |
export const iconNames = Object.keys(icons).map(name => | |
iconNameFromComponentName(name) | |
) | |
Pathicon.defaultProps = { | |
size: 24, | |
fill: "currentColor" | |
} | |
Pathicon.propTypes = { | |
size: PropTypes.number, | |
name: PropTypes.oneOf([...iconNames]) | |
} | |
export default Pathicon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment