import React from "react" import { Checkbox } from "baseui/checkbox" import { StatefulPopover, PLACEMENT } from "baseui/popover" import { HexColorPicker } from "react-colorful" import { Slider } from "baseui/slider" import { Input } from "baseui/input" import { useActiveObject, useEditor } from "@layerhub-io/react" interface Options { angle: number colors: string[] enabled: boolean } function Gradient() { const editor = useEditor() const activeObject = useActiveObject() const [options, setOptions] = React.useState({ angle: 0, colors: ["#24C6DC", "#514A9D"], enabled: false, }) const handleChange = (key: any, value: any) => { setOptions({ ...options, [key]: value }) if (key === "enabled") { if (value) { editor.objects.setGradient({ ...options, [key]: value }) } else { editor.objects.update({ fill: "#000000", }) } } else { if (options.enabled) { editor.objects.setGradient({ ...options, [key]: value }) } } } const initialOptions = { angle: 0, colors: ["#24C6DC", "#514A9D"], enabled: false, } const getGradientOptions = (object: any) => { const isNotGradient = typeof object?.fill === "string" || object?.fill instanceof String if (!isNotGradient) { const colorStops = object.fill.colorStops const colors = [colorStops[0].color, colorStops[1].color] return { angle: 0, colors: colors, enabled: true, } } else { return initialOptions } } React.useEffect(() => { if (activeObject) { const initialOptions = getGradientOptions(activeObject) setOptions({ ...options, ...initialOptions }) } }, [activeObject]) const handleGradientColorChange = (index: number, color: string) => { const updatedColors = [...options.colors] updatedColors[index] = color handleChange("colors", updatedColors) } return (
handleChange("enabled", (e.target as any).checked)} > Gradient
Colors
handleGradientColorChange(0, color)} /> handleGradientColorChange(0, (e.target as any).value)} placeholder="#000000" clearOnEscape />
} accessibilityType={"tooltip"} >
handleGradientColorChange(1, color)} /> handleGradientColorChange(1, (e.target as any).value)} placeholder="#000000" clearOnEscape />
} accessibilityType={"tooltip"} >
Direction
null, ThumbValue: () => null, TickBar: () => null, Thumb: { style: { height: "12px", width: "12px", paddingLeft: 0, }, }, Track: { style: { paddingLeft: 0, paddingRight: 0, }, }, }} max={360} value={[options.angle]} onChange={({ value }) => handleChange("angle", value[0])} />
) } export default Gradient