FIX: Simplify ColorPicker code a bit

* Removes "renderMenuItem"
* Color is no longer optional
* Removes 'index' unused variable
This commit is contained in:
Nicolás Hatcher
2025-03-18 23:00:03 +01:00
committed by Nicolás Hatcher Andrés
parent 08b3d71e9e
commit e2a466c500
2 changed files with 14 additions and 40 deletions

View File

@@ -1,7 +1,7 @@
import styled from "@emotion/styled"; import styled from "@emotion/styled";
import { Menu, MenuItem } from "@mui/material"; import { Menu, MenuItem } from "@mui/material";
import { Plus } from "lucide-react"; import { Plus } from "lucide-react";
import { type JSX, useRef, useState } from "react"; import { useRef, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { theme } from "../../theme"; import { theme } from "../../theme";
import ColorPicker from "./ColorPicker"; import ColorPicker from "./ColorPicker";
@@ -11,10 +11,6 @@ type ColorMenuProps = {
anchorEl: React.RefObject<HTMLButtonElement | null>; anchorEl: React.RefObject<HTMLButtonElement | null>;
open: boolean; open: boolean;
onClose: () => void; onClose: () => void;
renderMenuItem: (
color: string,
handleColorSelect: (color: string | null) => void,
) => JSX.Element;
}; };
const ColorMenu = ({ const ColorMenu = ({
@@ -22,9 +18,7 @@ const ColorMenu = ({
anchorEl, anchorEl,
open, open,
onClose, onClose,
renderMenuItem,
}: ColorMenuProps) => { }: ColorMenuProps) => {
const [color, setColor] = useState<string | null>(theme.palette.common.black);
const recentColors = useRef<string[]>([]); const recentColors = useRef<string[]>([]);
const [isPickerOpen, setPickerOpen] = useState(false); const [isPickerOpen, setPickerOpen] = useState(false);
const { t } = useTranslation(); const { t } = useTranslation();
@@ -107,7 +101,6 @@ const ColorMenu = ({
/> />
) : ( ) : (
<StyledMenu anchorEl={anchorEl.current} open={open} onClose={onClose}> <StyledMenu anchorEl={anchorEl.current} open={open} onClose={onClose}>
{renderMenuItem(theme.palette.common.black, handleColorSelect)}
<HorizontalDivider /> <HorizontalDivider />
<ColorsWrapper> <ColorsWrapper>
<ColorList> <ColorList>
@@ -116,14 +109,13 @@ const ColorMenu = ({
key={presetColor} key={presetColor}
$color={presetColor} $color={presetColor}
onClick={(): void => { onClick={(): void => {
setColor(presetColor);
handleColorSelect(presetColor); handleColorSelect(presetColor);
}} }}
/> />
))} ))}
</ColorList> </ColorList>
<ColorGrid> <ColorGrid>
{toneArrays.map((tones, index) => ( {toneArrays.map((tones) => (
<ColorGridCol key={tones.join("-")}> <ColorGridCol key={tones.join("-")}>
{tones.map((presetColor) => ( {tones.map((presetColor) => (
<RecentColorButton <RecentColorButton
@@ -146,7 +138,6 @@ const ColorMenu = ({
key={recentColor} key={recentColor}
$color={recentColor} $color={recentColor}
onClick={(): void => { onClick={(): void => {
setColor(recentColor);
handleColorSelect(recentColor); handleColorSelect(recentColor);
}} }}
/> />

View File

@@ -1,38 +1,33 @@
import styled from "@emotion/styled"; import styled from "@emotion/styled";
import { Menu, MenuItem, Popover, type PopoverOrigin } from "@mui/material"; import { Menu, MenuItem, Popover, type PopoverOrigin } from "@mui/material";
import { Check, Plus } from "lucide-react"; import { Check, Plus } from "lucide-react";
import { type JSX, useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { HexColorInput, HexColorPicker } from "react-colorful"; import { HexColorInput, HexColorPicker } from "react-colorful";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { theme } from "../../theme"; import { theme } from "../../theme";
// Types // Types
type ColorPickerProps = { type ColorPickerProps = {
color?: string; color: string;
onChange: (color: string | null) => void; onChange: (color: string) => void;
onClose?: () => void; onClose: () => void;
anchorEl: React.RefObject<HTMLElement | null>; anchorEl: React.RefObject<HTMLElement | null>;
anchorOrigin?: PopoverOrigin; anchorOrigin?: PopoverOrigin;
transformOrigin?: PopoverOrigin; transformOrigin?: PopoverOrigin;
open: boolean; open: boolean;
renderMenuItem?: (
color: string,
handleColorSelect: (color: string | null) => void,
) => JSX.Element;
}; };
const colorPickerWidth = 240; const colorPickerWidth = 240;
// Main Component // Main Component
const ColorPicker = ({ const ColorPicker = ({
color = theme.palette.common.black, color,
onChange, onChange,
onClose, onClose,
anchorEl, anchorEl,
anchorOrigin, anchorOrigin,
transformOrigin, transformOrigin,
open, open,
renderMenuItem,
}: ColorPickerProps) => { }: ColorPickerProps) => {
const [selectedColor, setSelectedColor] = useState<string>(color); const [selectedColor, setSelectedColor] = useState<string>(color);
const [isPickerOpen, setPickerOpen] = useState(false); const [isPickerOpen, setPickerOpen] = useState(false);
@@ -48,8 +43,8 @@ const ColorPicker = ({
setMenuOpen(open && !isPickerOpen); setMenuOpen(open && !isPickerOpen);
}, [open, isPickerOpen]); }, [open, isPickerOpen]);
const handleColorSelect = (color: string | null) => { const handleColorSelect = (color: string) => {
if (color && !recentColors.current.includes(color)) { if (!recentColors.current.includes(color)) {
const maxRecentColors = 14; const maxRecentColors = 14;
recentColors.current = [color, ...recentColors.current].slice( recentColors.current = [color, ...recentColors.current].slice(
0, 0,
@@ -118,17 +113,6 @@ const ColorPicker = ({
blueTones, blueTones,
]; ];
// Default menu item renderer
const defaultRenderMenuItem = (
color: string,
handleColorSelect: (color: string | null) => void,
) => (
<MenuItemWrapper onClick={() => handleColorSelect(color)}>
<MenuItemSquare />
<MenuItemText>{t("color_picker.default")}</MenuItemText>
</MenuItemWrapper>
);
// Render color picker or menu // Render color picker or menu
return ( return (
<> <>
@@ -191,10 +175,10 @@ const ColorPicker = ({
open={isMenuOpen} open={isMenuOpen}
onClose={handleClose} onClose={handleClose}
> >
{(renderMenuItem || defaultRenderMenuItem)( <MenuItemWrapper onClick={() => handleColorSelect(color)}>
theme.palette.common.black, <MenuItemSquare style={{ backgroundColor: color }} />
handleColorSelect, <MenuItemText>{t("color_picker.default")}</MenuItemText>
)} </MenuItemWrapper>
<HorizontalDivider /> <HorizontalDivider />
<ColorsWrapper> <ColorsWrapper>
<ColorList> <ColorList>
@@ -210,7 +194,7 @@ const ColorPicker = ({
))} ))}
</ColorList> </ColorList>
<ColorGrid> <ColorGrid>
{toneArrays.map((tones, index) => ( {toneArrays.map((tones) => (
<ColorGridCol key={tones.join("-")}> <ColorGridCol key={tones.join("-")}>
{tones.map((presetColor) => ( {tones.map((presetColor) => (
<RecentColorButton <RecentColorButton
@@ -301,7 +285,6 @@ const MenuItemText = styled("div")`
const MenuItemSquare = styled.div` const MenuItemSquare = styled.div`
width: 16px; width: 16px;
height: 16px; height: 16px;
background-color: ${theme.palette.common.black};
box-sizing: border-box; box-sizing: border-box;
margin-top: 0px; margin-top: 0px;
border-radius: 4px; border-radius: 4px;