import React, { CSSProperties } from "react"; import classNames from "classnames"; import { ColorLike } from "color"; import { utils } from "../../common"; import { getEnabledColorsProps } from "../../common/utils/colorHash"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { ContextOverlay } from "../ContextOverlay"; import { FieldSet } from "../Form"; import { RadioButton } from "../RadioButton/RadioButton"; import { Spacing } from "../Separation/Spacing"; import { Tag, TagList } from "../Tag"; import { TextField, TextFieldProps } from "../TextField"; import { Tooltip } from "../Tooltip/Tooltip"; import { WhiteSpaceContainer } from "../Typography"; type ColorPresets = [string, string][] | [string, ColorLike][]; type ColorPresetConfiguration = Pick; export interface ColorFieldProps extends Omit { /** * Any color can be selected, not only from the color presets. */ allowCustomColor?: boolean; /** * List of named colors that are used a selectable color options. */ colorPresets?: ColorPresets; } /** * Color input field that provides resets from the configured color palette. * Use `includeColorWeight` and `includePaletteGroup` to filter them. */ export const ColorField = ({ className = "", allowCustomColor = false, colorPresets = listColorPalettePresets(), defaultValue, value, onChange, fullWidth = false, ...otherTextFieldProps }: ColorFieldProps) => { const ref = React.useRef(null); const [colorValue, setColorValue] = React.useState(defaultValue || value || "#000000"); if (value && value !== colorValue) { setColorValue(value); } const disableNativePicker = colorPresets.length > 0; const disabled = (!disableNativePicker && !allowCustomColor) || otherTextFieldProps.disabled; const forwardOnChange = (forwardedEvent: React.ChangeEvent) => { setColorValue(forwardedEvent.target.value); if (onChange) { onChange(forwardedEvent); } }; const colorInput = ( ) => { forwardOnChange(e); } : undefined } style={{ ...otherTextFieldProps.style, [`--eccgui-colorfield-background`]: colorValue } as CSSProperties} /> ); return disableNativePicker && !disabled ? ( {allowCustomColor && ( <> ) => { forwardOnChange(e); }} /> )}
{colorPresets!.map((color: [string, ColorLike], idx: number) => [ ) => { forwardOnChange(e); }} > {typeof color[1] === "string" ? color[1] : color[1].toString()} , // Looks like we cannot force some type of line break in the tag list via CSS only (idx + 1) % 8 === 0 && ( <>
), ])}
} > {colorInput}
) : ( colorInput ); }; const defaultColorPaletteSet: ColorPresetConfiguration = { // on default, we only include color weights that can have enough contrasts to black/white includeColorWeight: [100, 300, 700, 900], // on default, we only include layout colors includePaletteGroup: ["layout"], }; /** * Simple helper function to get a list of colors defined in the color palette. */ const listColorPalettePresets = (colorPaletteSet = defaultColorPaletteSet) => { return utils .getEnabledColorPropertiesFromPalette({ ...colorPaletteSet, minimalColorDistance: 0, // we use all allowed colors, and do not check distances between them }) .map((color: [string, ColorLike]) => [ color[0].replace(`${eccgui}-color-palette-`, ""), color[1], ]) as ColorPresets; }; ColorField.listColorPalettePresets = listColorPalettePresets; type calculateColorHashValueProps = Pick & ColorPresetConfiguration; /** * Simple helper function that provide simple access to color hash calculation. */ ColorField.calculateColorHashValue = ( text: string, options: calculateColorHashValueProps = { ...defaultColorPaletteSet, allowCustomColor: false, }, ) => { const hash = utils.textToColorHash({ text, options: { returnValidColorsDirectly: options.allowCustomColor as boolean, enabledColors: utils.getEnabledColorsFromPalette({ includePaletteGroup: options.includePaletteGroup, includeColorWeight: options.includeColorWeight, minimalColorDistance: 0, }), }, }); return hash ? hash : undefined; }; export default ColorField;