You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
2 years ago
|
import { Island } from "../Island";
|
||
|
import { useDevice } from "../App";
|
||
|
import clsx from "clsx";
|
||
|
import Stack from "../Stack";
|
||
2 years ago
|
import React, { useRef } from "react";
|
||
2 years ago
|
import { DropdownMenuContentPropsContext } from "./common";
|
||
2 years ago
|
import { useOutsideClick } from "../../hooks/useOutsideClick";
|
||
2 years ago
|
|
||
|
const MenuContent = ({
|
||
|
children,
|
||
|
onClickOutside,
|
||
|
className = "",
|
||
2 years ago
|
onSelect,
|
||
2 years ago
|
style,
|
||
|
}: {
|
||
|
children?: React.ReactNode;
|
||
|
onClickOutside?: () => void;
|
||
|
className?: string;
|
||
2 years ago
|
/**
|
||
|
* Called when any menu item is selected (clicked on).
|
||
|
*/
|
||
|
onSelect?: (event: Event) => void;
|
||
2 years ago
|
style?: React.CSSProperties;
|
||
|
}) => {
|
||
|
const device = useDevice();
|
||
2 years ago
|
const menuRef = useRef<HTMLDivElement>(null);
|
||
|
|
||
|
useOutsideClick(menuRef, () => {
|
||
2 years ago
|
onClickOutside?.();
|
||
|
});
|
||
|
|
||
|
const classNames = clsx(`dropdown-menu ${className}`, {
|
||
1 year ago
|
"dropdown-menu--mobile": device.editor.isMobile,
|
||
2 years ago
|
}).trim();
|
||
2 years ago
|
|
||
2 years ago
|
return (
|
||
2 years ago
|
<DropdownMenuContentPropsContext.Provider value={{ onSelect }}>
|
||
|
<div
|
||
|
ref={menuRef}
|
||
|
className={classNames}
|
||
|
style={style}
|
||
|
data-testid="dropdown-menu"
|
||
|
>
|
||
|
{/* the zIndex ensures this menu has higher stacking order,
|
||
2 years ago
|
see https://github.com/excalidraw/excalidraw/pull/1445 */}
|
||
1 year ago
|
{device.editor.isMobile ? (
|
||
2 years ago
|
<Stack.Col className="dropdown-menu-container">{children}</Stack.Col>
|
||
|
) : (
|
||
|
<Island
|
||
|
className="dropdown-menu-container"
|
||
|
padding={2}
|
||
2 years ago
|
style={{ zIndex: 2 }}
|
||
2 years ago
|
>
|
||
|
{children}
|
||
|
</Island>
|
||
|
)}
|
||
|
</div>
|
||
|
</DropdownMenuContentPropsContext.Provider>
|
||
2 years ago
|
);
|
||
|
};
|
||
|
MenuContent.displayName = "DropdownMenuContent";
|
||
2 years ago
|
|
||
|
export default MenuContent;
|