import React, { useState, useEffect } from 'react'; import ButtonBase from '@mui/material/ButtonBase'; import Menu from '@mui/material/Menu'; import MenuItem from '@mui/material/MenuItem'; import { colorKeys, getBackgroundColor } from './MaterialColors'; import { useTheme } from '@mui/styles'; const colors: string[] = colorKeys; export enum DropdownAlignment { SE, SW } interface ColorDropdownButtonProps { currentColor?: string; onColorChange: (color: string) => void; dropdownAlignment: DropdownAlignment; } const ColorDropdownButton: React.FC = ({ currentColor = colors[0], onColorChange, dropdownAlignment = DropdownAlignment.SE }) => { const [anchorEl, setAnchorEl] = useState(null); const [selectedColor, setSelectedColor] = useState(currentColor); const theme = useTheme(); useEffect(() => { setSelectedColor(currentColor); }, [currentColor]); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; const handleColorSelect = (color: string) => { handleClose(); setSelectedColor(color); onColorChange(color); }; return (
{colors.map((color) => { let selected = color === selectedColor; return ( handleColorSelect(color)} style={{ width: 48, height: 48, borderRadius: 6, margin: 4, borderStyle: "solid", borderWidth: selected ? 2 : 0.25, borderColor: color === selectedColor ? theme.palette.text.primary : theme.palette.text.secondary, backgroundColor: getBackgroundColor(color) }} > ) } )}
); }; export default ColorDropdownButton;