import { __ } from "@wordpress/i18n";
import {
__experimentalColorGradientControl as ColorGradientControl,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
} from "@wordpress/block-editor";
import {
Button,
ColorIndicator,
Dropdown,
FlexItem,
__experimentalHStack as HStack,
__experimentalDropdownContentWrapper as DropdownContentWrapper,
} from "@wordpress/components";
import { reset as resetIcon } from "@wordpress/icons";
import { useRef } from "react";
import classNames from "classnames";
import { useColorPalettes } from "./useColorPalettes";
import "./color-dropdown-style.scss";
interface ColorDropdownProps {
label: string;
colorValue: string | undefined | null;
gradientValue?: string | null;
onColorChange: (newValue: string | undefined) => void;
onGradientChange?: (newValue: string | undefined) => void;
onDeselect?: () => void;
allowGradient?: boolean;
enableAlpha?: boolean;
}
const LabeledColorIndicator = ({
colorValue,
label,
}: {
colorValue: string | undefined | null;
label: string;
}) => (
{label}
);
function ColorDropdown({
label,
colorValue,
gradientValue,
onColorChange,
onGradientChange,
onDeselect,
allowGradient = false,
enableAlpha = true,
}: ColorDropdownProps) {
const colorGradientSettings = useMultipleOriginColorsAndGradients();
// Same grouped origins the border picker shows; the WordPress hook drops
// the Default palette when a theme opts out of it.
const colorPalettes = useColorPalettes();
const colorButtonRef = useRef(null);
const lastColor = useRef(colorValue);
const lastGradient = useRef(gradientValue);
const value = colorValue ?? gradientValue;
const clearable = !!value;
const clearValue = () => {
if (colorValue) {
onColorChange(undefined);
} else if (gradientValue && onGradientChange) {
onGradientChange(undefined);
}
onDeselect?.();
};
const handleColorChange = (newColor: string | undefined) => {
if (lastColor.current !== newColor) {
lastColor.current = newColor;
onColorChange(newColor);
}
};
const handleGradientChange = (newGradient: string | undefined) => {
if (onGradientChange && lastGradient.current !== newGradient) {
lastGradient.current = newGradient;
onGradientChange(newGradient);
}
};
return (
(
)}
renderContent={() => (
)}
/>
{clearable && (
);
}
export default ColorDropdown;