|
| 1 | +/** |
| 2 | + * A Colour picker Custom Component |
| 3 | + * |
| 4 | + * Can handle named colours, Hex, RGB and HSL formats, with an optional alpha |
| 5 | + * channel |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { lazy, Suspense, useRef } from 'react' |
| 9 | +import { HsvColor } from 'react-colorful' |
| 10 | +import { colord, extend, getFormat, HsvaColor } from 'colord' |
| 11 | +import namesPlugin from 'colord/plugins/names' |
| 12 | +import { useDebouncedCallback } from 'use-debounce' |
| 13 | +import { StringEdit, toPathString, type CustomNodeProps } from '@json-edit-react' |
| 14 | +import { Loading } from '../_common/Loading' |
| 15 | + |
| 16 | +extend([namesPlugin]) |
| 17 | + |
| 18 | +const HsvaColorPicker = lazy(() => |
| 19 | + import('react-colorful').then((m) => ({ default: m.HsvaColorPicker })) |
| 20 | +) |
| 21 | + |
| 22 | +const HsvColorPicker = lazy(() => |
| 23 | + import('react-colorful').then((m) => ({ default: m.HsvColorPicker })) |
| 24 | +) |
| 25 | + |
| 26 | +export interface ColorPickerProps { |
| 27 | + loadingText?: string |
| 28 | + swatchStyles?: React.CSSProperties |
| 29 | + invalidColorError?: string |
| 30 | + /** |
| 31 | + * If true, the color picker will include an alpha channel slider |
| 32 | + * @default false |
| 33 | + */ |
| 34 | + alpha?: boolean |
| 35 | + /** |
| 36 | + * If `keepAsColor` is true, if the text input is not a valid color, the |
| 37 | + * component will display an error on submission and not update the value |
| 38 | + * @default true |
| 39 | + */ |
| 40 | + keepAsColor?: boolean |
| 41 | +} |
| 42 | + |
| 43 | +export const ColorPickerComponent: React.FC<CustomNodeProps<ColorPickerProps>> = ({ |
| 44 | + isEditing, |
| 45 | + setIsEditing, |
| 46 | + handleKeyPress, |
| 47 | + value, |
| 48 | + setValue, |
| 49 | + getStyles, |
| 50 | + nodeData, |
| 51 | + originalNode, |
| 52 | + customNodeProps = {}, |
| 53 | + onError, |
| 54 | + handleEdit, |
| 55 | + ...props |
| 56 | +}) => { |
| 57 | + const { |
| 58 | + loadingText, |
| 59 | + swatchStyles, |
| 60 | + alpha = false, |
| 61 | + keepAsColor = true, |
| 62 | + invalidColorError = 'Invalid Color', |
| 63 | + } = customNodeProps |
| 64 | + |
| 65 | + const text = value as string |
| 66 | + |
| 67 | + // The current internal state of the color picker |
| 68 | + const [hsvValue, setHsvValue] = React.useState(colord(text).toHsv()) |
| 69 | + |
| 70 | + const lastValidColor = useRef(text) |
| 71 | + |
| 72 | + // Debounced setValue to avoid excessive updates while dragging the picker |
| 73 | + const debouncedSetValue = useDebouncedCallback((value: string) => { |
| 74 | + setValue(value) |
| 75 | + }, 150) |
| 76 | + |
| 77 | + const stringStyle = getStyles('string', nodeData) |
| 78 | + |
| 79 | + const PickerComponent = alpha ? HsvaColorPicker : HsvColorPicker |
| 80 | + |
| 81 | + if (typeof text !== 'string') return null |
| 82 | + |
| 83 | + return ( |
| 84 | + <Suspense |
| 85 | + fallback={ |
| 86 | + <div style={stringStyle}> |
| 87 | + <Loading text={loadingText} /> |
| 88 | + </div> |
| 89 | + } |
| 90 | + > |
| 91 | + {isEditing ? ( |
| 92 | + <div style={{ position: 'relative' }}> |
| 93 | + <StringEdit |
| 94 | + styles={getStyles('input', nodeData)} |
| 95 | + pathString={toPathString(nodeData.path)} |
| 96 | + {...props} |
| 97 | + value={text} |
| 98 | + setValue={ |
| 99 | + ((value: string) => { |
| 100 | + // Only update the color picker display if the input text is a |
| 101 | + // valid color |
| 102 | + const parsed = colord(value) |
| 103 | + if (parsed.isValid()) { |
| 104 | + setHsvValue(parsed.toHsv()) |
| 105 | + } |
| 106 | + setValue(value) |
| 107 | + }) as React.Dispatch<React.SetStateAction<string>> |
| 108 | + } |
| 109 | + handleEdit={() => { |
| 110 | + if (keepAsColor && !colord(text).isValid()) { |
| 111 | + handleEdit(lastValidColor.current) |
| 112 | + onError({ code: 'UPDATE_ERROR', message: invalidColorError }, text) |
| 113 | + return |
| 114 | + } |
| 115 | + lastValidColor.current = text |
| 116 | + setHsvValue(colord(text).toHsv()) |
| 117 | + handleEdit(text) |
| 118 | + }} |
| 119 | + /> |
| 120 | + <PickerComponent |
| 121 | + style={{ |
| 122 | + position: 'absolute', |
| 123 | + bottom: '1.5em', |
| 124 | + zIndex: 1000, |
| 125 | + boxShadow: 'rgba(0, 0, 0, 0.35) 0px 5px 15px', |
| 126 | + borderRadius: '8px', |
| 127 | + }} |
| 128 | + color={hsvValue} |
| 129 | + onChange={(newColor) => { |
| 130 | + setHsvValue(colord(newColor).toHsv()) |
| 131 | + debouncedSetValue(getFormattedColorText(newColor, text)) |
| 132 | + }} |
| 133 | + onKeyDown={handleKeyPress} |
| 134 | + /> |
| 135 | + </div> |
| 136 | + ) : ( |
| 137 | + <div style={{ display: 'flex', alignItems: 'center', gap: '0.2em' }}> |
| 138 | + {originalNode} |
| 139 | + {/* Display a color swatch */} |
| 140 | + <div |
| 141 | + style={{ |
| 142 | + width: '1em', |
| 143 | + height: '1em', |
| 144 | + backgroundColor: colord(text).toHex(), |
| 145 | + borderRadius: '0.15em', |
| 146 | + ...swatchStyles, |
| 147 | + }} |
| 148 | + onDoubleClick={() => setIsEditing(true)} |
| 149 | + /> |
| 150 | + </div> |
| 151 | + )} |
| 152 | + </Suspense> |
| 153 | + ) |
| 154 | +} |
| 155 | + |
| 156 | +// Set the text value based on the selected color, keeping the |
| 157 | +// format the same as the current text input, where possible |
| 158 | +const getFormattedColorText = (color: HsvColor | HsvaColor, currentText: string): string => { |
| 159 | + let newValue = '' |
| 160 | + switch (getFormat(currentText)) { |
| 161 | + case 'rgb': |
| 162 | + newValue = colord(color).toRgbString() |
| 163 | + break |
| 164 | + case 'hsl': |
| 165 | + newValue = colord(color).toHslString() |
| 166 | + break |
| 167 | + default: |
| 168 | + newValue = colord(color).toHex() |
| 169 | + break |
| 170 | + } |
| 171 | + return newValue |
| 172 | +} |
0 commit comments