InvokeAI/invokeai/frontend/web/src/common/components/IAISimpleMenu.tsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-19 00:17:45 +00:00
import {
Menu,
MenuButton,
MenuItem,
MenuList,
MenuProps,
MenuButtonProps,
MenuListProps,
MenuItemProps,
2023-02-19 00:17:45 +00:00
} from '@chakra-ui/react';
import { MouseEventHandler, ReactNode } from 'react';
import { MdArrowDropDown, MdArrowDropUp } from 'react-icons/md';
import IAIButton from './IAIButton';
import IAIIconButton from './IAIIconButton';
2023-02-19 00:17:45 +00:00
interface IAIMenuItem {
item: ReactNode | string;
onClick: MouseEventHandler<HTMLButtonElement> | undefined;
}
interface IAIMenuProps {
menuType?: 'icon' | 'regular';
buttonText?: string;
iconTooltip?: string;
menuItems: IAIMenuItem[];
menuProps?: MenuProps;
menuButtonProps?: MenuButtonProps;
2023-02-19 00:17:45 +00:00
menuListProps?: MenuListProps;
menuItemProps?: MenuItemProps;
}
export default function IAISimpleMenu(props: IAIMenuProps) {
const {
menuType = 'icon',
iconTooltip,
buttonText,
menuItems,
menuProps,
menuButtonProps,
menuListProps,
menuItemProps,
} = props;
const renderMenuItems = () => {
const menuItemsToRender: ReactNode[] = [];
2023-02-19 00:25:01 +00:00
menuItems.forEach((menuItem, index) => {
2023-02-19 00:17:45 +00:00
menuItemsToRender.push(
<MenuItem
2023-02-19 00:25:01 +00:00
key={index}
2023-02-19 00:17:45 +00:00
onClick={menuItem.onClick}
fontSize="0.9rem"
color="var(--text-color-secondary)"
backgroundColor="var(--background-color-secondary)"
_focus={{
color: 'var(--text-color)',
backgroundColor: 'var(--border-color)',
}}
2023-02-19 00:17:45 +00:00
{...menuItemProps}
>
{menuItem.item}
</MenuItem>
);
});
return menuItemsToRender;
};
return (
<Menu {...menuProps}>
2023-02-19 00:17:45 +00:00
{({ isOpen }) => (
<>
<MenuButton
as={menuType === 'icon' ? IAIIconButton : IAIButton}
2023-02-19 00:17:45 +00:00
tooltip={iconTooltip}
icon={isOpen ? <MdArrowDropUp /> : <MdArrowDropDown />}
padding={menuType === 'regular' ? '0 0.5rem' : 0}
backgroundColor="var(--btn-base-color)"
_hover={{
backgroundColor: 'var(--btn-base-color-hover)',
}}
minWidth="1rem"
minHeight="1rem"
fontSize="1.5rem"
2023-02-19 00:17:45 +00:00
{...menuButtonProps}
>
{menuType === 'regular' && buttonText}
</MenuButton>
<MenuList
zIndex={15}
padding={0}
borderRadius="0.5rem"
backgroundColor="var(--background-color-secondary)"
color="var(--text-color-secondary)"
borderColor="var(--border-color)"
{...menuListProps}
>
2023-02-19 00:17:45 +00:00
{renderMenuItems()}
</MenuList>
</>
)}
</Menu>
);
}