All files / src/components/ui/menu Menu.tsx

51.02% Statements 25/49
0% Branches 0/13
0% Functions 0/13
43.24% Lines 16/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x 1x 1x 1x                   1x                               1x               1x                     1x                                   1x                                   1x               1x       1x               1x           1x       1x                 1x          
import React, { Children, forwardRef, isValidElement, ReactNode } from "react";
import styled from "styled-components";
import { CaretRightIcon } from "ui/icons/Icons";
import {
  StyledMenu,
  StyledMenuAccelerator,
  StyledMenuDivider,
  StyledMenuGroup,
  StyledMenuItem,
  StyledMenuItemCaret,
  StyledMenuItemIcon,
} from "ui/menu/style";
 
export const extractMenuAccelerator = (children: ReactNode): string | null => {
  let accelerator: string | null = null;
 
  Children.forEach(children, (child) => {
    Iif (
      isValidElement(child) &&
      child.type === MenuAccelerator &&
      typeof child.props.accelerator === "string"
    ) {
      accelerator = child.props.accelerator;
    }
  });
 
  return accelerator;
};
 
export const normalizeMenuAccelerator = (accelerator: string) =>
  accelerator
    .replace("CommandOrControl", "ctrl")
    .split("+")
    .map((p) => p.toLowerCase())
    .sort()
    .join("+");
 
export const normalizeMenuEvent = (e: KeyboardEvent | React.KeyboardEvent) => {
  const parts: string[] = [];
  Iif (e.ctrlKey || e.metaKey) {
    parts.push("ctrl");
  }
  Iif (e.shiftKey) parts.push("shift");
  Iif (e.altKey) parts.push("alt");
  parts.push(e.key.toLowerCase());
  return parts.sort().join("+");
};
 
export const Menu = forwardRef<
  HTMLDivElement,
  React.HTMLAttributes<HTMLDivElement>
>((props, outerRef) => <StyledMenu ref={outerRef} role="menu" {...props} />);
 
export interface MenuItemProps extends React.HTMLAttributes<HTMLDivElement> {
  readonly "data-index"?: number;
  readonly "data-accelerator"?: string;
  readonly selected?: boolean;
  readonly onClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
  readonly onMouseEnter?: (
    e: React.MouseEvent<HTMLDivElement, MouseEvent>,
  ) => void;
  readonly subMenu?: React.ReactElement[];
  readonly icon?: ReactNode;
  readonly children?: ReactNode;
}
 
export const MenuItem = ({
  selected,
  subMenu,
  children,
  icon,
  ...props
}: MenuItemProps) => (
  <StyledMenuItem $selected={selected} {...props}>
    {icon && <StyledMenuItemIcon>{icon}</StyledMenuItemIcon>}
    {children}
    {subMenu && <MenuItemCaret />}
  </StyledMenuItem>
);
 
interface MenuItemIconProps {
  readonly children?: ReactNode;
}
 
export const MenuItemIcon = ({ children }: MenuItemIconProps) => (
  <StyledMenuItemIcon children={children} />
);
 
interface MenuGroupProps {
  children?: ReactNode;
}
 
export const MenuGroup = ({ children }: MenuGroupProps) => (
  <StyledMenuGroup children={children} />
);
 
export const MenuSection = styled.div`
  display: flex;
  align-items: center;
  padding: 5px 10px;
  font-size: ${(props) => props.theme.typography.menuFontSize};
  white-space: nowrap;
`;
 
export const MenuDivider = () => <StyledMenuDivider />;
 
interface MenuAcceleratorProps {
  accelerator: string;
}
 
export const MenuAccelerator = ({ accelerator }: MenuAcceleratorProps) => (
  <StyledMenuAccelerator $accelerator={accelerator} />
);
 
export const MenuOverlay = styled.div`
  position: fixed;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  z-index: 1000;
`;
 
export const MenuItemCaret = () => (
  <StyledMenuItemCaret>
    <CaretRightIcon />
  </StyledMenuItemCaret>
);