1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-23 03:54:21 +00:00
bluelib/src/components/Color/index.js
2021-03-23 18:15:30 +01:00

51 lines
1.4 KiB
JavaScript

import React, {Fragment} from "react"
import useBluelibClassNames from "../../hooks/useBluelibClassNames"
import PropTypes from "prop-types"
import classNames from "classnames"
import color from "color"
export default function Color({children, builtin, custom}) {
const potentialClassName = useBluelibClassNames(`color-${builtin}`)
if (builtin && custom) {
throw new Error("<Color> tags may only have one prop between `builtin` and `custom`.")
}
let extraClassName = ""
let extraStyle = {}
if (builtin !== undefined) {
extraClassName = potentialClassName
}
if (custom !== undefined) {
let c = color(custom)
extraStyle["--bluelib-color-r"] = c.red()
extraStyle["--bluelib-color-g"] = c.green()
extraStyle["--bluelib-color-b"] = c.blue()
}
children = React.Children.map(children, child => {
if (!child.props) {
return child
}
return React.cloneElement(child, {
className: classNames(child.props.className, extraClassName),
style: {...child.props.style, ...extraStyle},
})
})
return (
<Fragment>
{children}
</Fragment>
)
}
Color.propTypes = {
children: PropTypes.node,
builtin: PropTypes.oneOf(["red", "orange", "yellow", "lime", "cyan", "blue", "magenta", "gray"]),
custom: PropTypes.string,
}