import React, { useMemo } from 'react' import withDefaults from '../utils/with-defaults' import useTheme from '../use-theme' import { SnippetTypes } from '../utils/prop-types' import { HuiThemesPalette } from '../themes' interface Props { type?: SnippetTypes invert?: boolean className?: string } const defaultProps = { type: 'default' as SnippetTypes, invert: false, className: '' } type NativeAttrs = Omit, keyof Props> export type TagProps = Props & typeof defaultProps & NativeAttrs export type TagColors = { color: string bgColor: string borderColor: string } const getColors = (type: SnippetTypes, palette: HuiThemesPalette, invert: boolean) => { const colors: { [key in SnippetTypes]: Pick & Partial } = { default: { color: palette.foreground }, success: { color: palette.success }, warning: { color: palette.warning }, error: { color: palette.error }, secondary: { color: palette.secondary }, dark: { color: palette.foreground, bgColor: palette.background }, lite: { color: palette.foreground, bgColor: palette.accents_2 } } const hideBorder = invert || type === 'lite' const cardStyle = { ...colors[type], bgColor: colors[type].bgColor || palette.background, borderColor: hideBorder ? 'transparent' : colors[type].color } return !invert ? cardStyle : { ...cardStyle, color: cardStyle.bgColor, bgColor: cardStyle.color } } const Tag: React.FC> = ({ type, children, className, invert, ...props }) => { const theme = useTheme() const { color, bgColor, borderColor } = useMemo( () => getColors(type, theme.palette, invert), [type, theme.palette, invert] ) return ( {children} ) } const MemoTag = React.memo(Tag) export default withDefaults(MemoTag, defaultProps)