diff --git a/src/app/components/HexColorPickerPopOut.tsx b/src/app/components/HexColorPickerPopOut.tsx index ee35665f2e..77d183a3f8 100644 --- a/src/app/components/HexColorPickerPopOut.tsx +++ b/src/app/components/HexColorPickerPopOut.tsx @@ -1,18 +1,32 @@ import FocusTrap from 'focus-trap-react'; import type { RectCords } from 'folds'; import { Box, Button, config, Menu, Text } from 'folds'; +import { HexColorPicker } from 'react-colorful'; import { PopOut } from '$components/overlay-stack'; import type { MouseEventHandler, ReactNode } from 'react'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { stopPropagation } from '$utils/keyboard'; type HexColorPickerPopOutProps = { children: (onOpen: MouseEventHandler, opened: boolean) => ReactNode; - picker: ReactNode; + color: string; + onChange: (color: string) => void; onRemove?: () => void; }; -export function HexColorPickerPopOut({ picker, onRemove, children }: HexColorPickerPopOutProps) { +const isValidHexColor = (color: string) => /^#[0-9A-F]{6}$/i.test(color); + +export function HexColorPickerPopOut({ + color, + onChange, + onRemove, + children, +}: HexColorPickerPopOutProps) { const [cords, setCords] = useState(); + const [pickerColor, setPickerColor] = useState(isValidHexColor(color) ? color : '#FFFFFF'); + + useEffect(() => { + if (isValidHexColor(color)) setPickerColor(color); + }, [color]); const handleOpen: MouseEventHandler = (evt) => { setCords(evt.currentTarget.getBoundingClientRect()); @@ -39,7 +53,13 @@ export function HexColorPickerPopOut({ picker, onRemove, children }: HexColorPic }} > - {picker} + { + setPickerColor(newColor); + onChange(newColor); + }} + /> {onRemove && ( )} - } - > + {(onOpen, opened) => (