|
| 1 | +import { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react' |
| 2 | +import { createPortal } from 'react-dom' |
| 3 | +import { ChromePicker, ColorResult, RGBColor } from 'react-color' |
| 4 | +import { offset, shift, useFloating } from '@floating-ui/react' |
| 5 | +import { useTheme } from '@emotion/react' |
| 6 | +import isFunction from 'lodash/isFunction' |
| 7 | + |
| 8 | +import { Props, defaultProps } from './ColorPicker.types' |
| 9 | +import * as styles from './ColorPicker.styles' |
| 10 | +import { get } from '../_theme' |
| 11 | +import { rgbToHex, hexToRgbA } from '../_utils/commonStyles' |
| 12 | +import { colors } from '../_utils/colors' |
| 13 | + |
| 14 | +const ColorPicker: FC<Props> = (props) => { |
| 15 | + const { defaultColor: defaultColorProps, className, id, menuProps, portalTarget, onToggle, onColorChange } = { ...defaultProps, ...props } |
| 16 | + const defaultColor: RGBColor = useMemo(() => { |
| 17 | + if (typeof defaultColorProps === 'string') { |
| 18 | + if (defaultColorProps.length === 7) { |
| 19 | + // hex color |
| 20 | + return hexToRgbA(defaultColorProps, 1, true) as RGBColor |
| 21 | + } else { |
| 22 | + // rgba color |
| 23 | + const parsed = defaultColorProps.split('(')[1].split(')')[0].split(',') |
| 24 | + return { |
| 25 | + r: parseInt(parsed[0]), |
| 26 | + g: parseInt(parsed[1]), |
| 27 | + b: parseInt(parsed[2]), |
| 28 | + a: parseFloat(parsed[3]), |
| 29 | + } |
| 30 | + } |
| 31 | + } else if (defaultColorProps) { |
| 32 | + return defaultColorProps |
| 33 | + } |
| 34 | + |
| 35 | + return hexToRgbA(colors.primary, 1, true) as RGBColor |
| 36 | + }, [defaultColorProps]) |
| 37 | + |
| 38 | + const [color, setColor] = useState<RGBColor>(defaultColor) |
| 39 | + const [open, setOpen] = useState(false) |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + setColor(defaultColor) |
| 43 | + }, [defaultColor]) |
| 44 | + |
| 45 | + const { x, y, refs, strategy } = useFloating({ |
| 46 | + placement: menuProps?.placement || 'bottom-start', |
| 47 | + strategy: 'fixed', |
| 48 | + middleware: [shift(), offset(4)], |
| 49 | + }) |
| 50 | + |
| 51 | + const ref = useRef(null) |
| 52 | + const theme = useTheme() |
| 53 | + |
| 54 | + const getValue = useCallback((c: RGBColor) => (c.a !== 1 ? `rgba(${c.r},${c.g},${c.b},${c.a})` : rgbToHex(c.r, c.g, c.b)), []) |
| 55 | + |
| 56 | + const handleChange = useCallback( |
| 57 | + (color: ColorResult) => { |
| 58 | + setColor(color.rgb) |
| 59 | + isFunction(onColorChange) && onColorChange(getValue(color.rgb)) |
| 60 | + }, |
| 61 | + [getValue, onColorChange] |
| 62 | + ) |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + function handleClickOutside(event: any) { |
| 66 | + // @ts-ignore |
| 67 | + if (ref?.current && !ref?.current?.contains(event.target)) { |
| 68 | + setOpen(false) |
| 69 | + isFunction(onToggle) && onToggle(false) |
| 70 | + } |
| 71 | + } |
| 72 | + if (open) { |
| 73 | + document.addEventListener('mousedown', handleClickOutside) |
| 74 | + return () => { |
| 75 | + document.removeEventListener('mousedown', handleClickOutside) |
| 76 | + } |
| 77 | + } |
| 78 | + }, [ref, open, onToggle]) |
| 79 | + |
| 80 | + const FloatingPanel = ( |
| 81 | + <div |
| 82 | + css={styles.floatingMenu} |
| 83 | + ref={refs.setFloating} |
| 84 | + style={{ |
| 85 | + position: strategy, |
| 86 | + top: y ?? 0, |
| 87 | + left: x ?? 0, |
| 88 | + width: 'max-content', |
| 89 | + }} |
| 90 | + > |
| 91 | + <ChromePicker |
| 92 | + color={color} |
| 93 | + onChange={handleChange} |
| 94 | + styles={{ |
| 95 | + default: { |
| 96 | + body: { |
| 97 | + background: get(theme, `ColorPicker.background`), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }} |
| 101 | + /> |
| 102 | + </div> |
| 103 | + ) |
| 104 | + |
| 105 | + return ( |
| 106 | + <div className={className} id={id} ref={ref}> |
| 107 | + <div |
| 108 | + css={[styles.colorPicker, getValue(color).startsWith('#') ? styles.hex : styles.rgba]} |
| 109 | + onClick={() => setOpen((prev) => !prev)} |
| 110 | + ref={refs.setReference} |
| 111 | + > |
| 112 | + <div |
| 113 | + css={styles.color} |
| 114 | + style={{ |
| 115 | + background: getValue(color), |
| 116 | + }} |
| 117 | + /> |
| 118 | + {color ? <span css={styles.label}>{getValue(color)}</span> : null} |
| 119 | + </div> |
| 120 | + {open && portalTarget && createPortal(FloatingPanel, portalTarget as Element)} |
| 121 | + {open && !portalTarget && FloatingPanel} |
| 122 | + </div> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +ColorPicker.displayName = 'ColorPicker' |
| 127 | +ColorPicker.defaultProps = defaultProps |
| 128 | + |
| 129 | +export default ColorPicker |
0 commit comments