import { CopyToClipboard } from '@fluentui/docs-components'; import { ComponentSlotStylesInput, ComponentSlotStyle, createComponent, ICSSInJSStyle, } from '@fluentui/react-northstar'; import { AcceptIcon, ClipboardCopiedToIcon } from '@fluentui/react-icons-northstar'; import * as Color from 'color'; import * as _ from 'lodash'; import * as React from 'react'; type ColorBoxProps = { children?: React.ReactNode; name?: string; copyToClipboardIcon?: boolean; rounded?: boolean; size?: 'small' | 'normal' | 'big'; value: string; showColorValue?: boolean; styles?: ComponentSlotStyle; }; type ColorBoxVariables = { colorBlack: string; colorWhite: string; fontSize: { big: string; normal: string; small: string; }; padding: { big: string; normal: string; small: string; }; }; export const colorBoxVariables = (siteVariables): ColorBoxVariables => ({ colorBlack: siteVariables.colors.black, colorWhite: siteVariables.colors.white, fontSize: { big: '1.25em', small: '.85em', normal: '1.25em', }, padding: { big: '4rem .75rem .75rem .75rem', small: '.75rem', normal: '2.5rem .75rem .75rem .75rem', }, }); export const colorBoxStyles: ComponentSlotStylesInput = { root: ({ props: p, variables: v }): ICSSInJSStyle => ({ ...(p.showColorValue && !_.isNil(p.value) && { backgroundImage: 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAKUlEQVQoU2NkYGAwZkAD////RxdiYBwKCv///4/hGUZGkNNRAeMQUAgAtxof+nLDzyUAAAAASUVORK5CYII=")', backgroundRepeat: 'repeat', }), ...(p.showColorValue && _.isNil(p.value) && { backgroundColor: 'transparent', }), borderRadius: p.rounded && '.25rem', color: p.value !== undefined && Color(p.value).isDark() ? v.colorWhite : v.colorBlack, }), inner: ({ props: p, variables: v }) => ({ backgroundColor: p.value, display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', fontSize: v.padding[p.size], padding: v.padding[p.size], }), name: { fontWeight: 'bold', }, value: { fontFamily: 'Monospace', textAlign: 'right', userSelect: 'all', '> span': { cursor: 'pointer', }, }, }; const ColorBox = createComponent({ displayName: 'ColorBox', render: ({ children, name, value, showColorValue, copyToClipboardIcon, config: { classes } }) => (
{children || _.startCase(name)}
{copyToClipboardIcon && ( {(active, onClick) => (
{value && active ? : } {value || 'Not defined'}
)}
)} {!copyToClipboardIcon && showColorValue && {value || 'Not defined'}}
), }); ColorBox.defaultProps = { size: 'normal', copyToClipboardIcon: true, showColorValue: true, }; export default ColorBox;