import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu"; import { LocationDescriptor } from "history"; import * as React from "react"; import styled from "styled-components"; import { depths, s } from "@shared/styles"; import Scrollable from "~/components/Scrollable"; import Tooltip from "~/components/Tooltip"; import { fadeAndScaleIn } from "~/styles/animations"; import { MenuButton, MenuDisclosure, MenuExternalLink, MenuHeader, MenuInternalLink, MenuLabel, MenuSeparator, MenuSubTrigger, SelectedIconWrapper, } from "./components/Menu"; import { CheckmarkIcon } from "outline-icons"; const DropdownMenu = DropdownMenuPrimitive.Root; const DropdownSubMenu = DropdownMenuPrimitive.Sub; const DropdownMenuTrigger = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, ref) => { const { children, ...rest } = props; return ( {children} ); }); DropdownMenuTrigger.displayName = DropdownMenuPrimitive.Trigger.displayName; const DropdownMenuContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, ref) => { const { children, ...rest } = props; return ( {children} ); }); DropdownMenuContent.displayName = DropdownMenuPrimitive.Content.displayName; type DropdownSubMenuTriggerProps = BaseDropdownItemProps & React.ComponentPropsWithoutRef; const DropdownSubMenuTrigger = React.forwardRef< React.ElementRef, DropdownSubMenuTriggerProps >((props, ref) => { const { label, icon, disabled, ...rest } = props; return ( {icon} {label} ); }); DropdownSubMenuTrigger.displayName = DropdownMenuPrimitive.SubTrigger.displayName; const DropdownSubMenuContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, ref) => { const { children, ...rest } = props; return ( {children} ); }); DropdownSubMenuContent.displayName = DropdownMenuPrimitive.SubContent.displayName; type DropdownMenuGroupProps = { label: string; items: React.ReactNode[]; } & Omit< React.ComponentPropsWithoutRef, "children" | "asChild" >; const DropdownMenuGroup = React.forwardRef< React.ElementRef, DropdownMenuGroupProps >((props, ref) => { const { label, items, ...rest } = props; return ( {label} {items} ); }); DropdownMenuGroup.displayName = DropdownMenuPrimitive.Group.displayName; type BaseDropdownItemProps = { label: string; icon?: React.ReactElement; disabled?: boolean; }; type DropdownMenuButtonProps = BaseDropdownItemProps & { onClick: (event: React.MouseEvent) => void; tooltip?: React.ReactChild; selected?: boolean; dangerous?: boolean; } & Omit< React.ComponentPropsWithoutRef, "children" | "asChild" | "onClick" >; const DropdownMenuButton = React.forwardRef< React.ElementRef, DropdownMenuButtonProps >((props, ref) => { const { label, icon, tooltip, disabled, selected, dangerous, onClick, ...rest } = props; const button = ( {icon} {label} {selected !== undefined && ( {selected ? : null} )} ); return tooltip ? (
{button}
) : ( <>{button} ); }); DropdownMenuButton.displayName = "DropdownMenuButton"; type DropdownMenuInternalLinkProps = BaseDropdownItemProps & { to: LocationDescriptor; } & Omit< React.ComponentPropsWithoutRef, "children" | "asChild" | "onClick" >; const DropdownMenuInternalLink = React.forwardRef< React.ElementRef, DropdownMenuInternalLinkProps >((props, ref) => { const { label, icon, disabled, to, ...rest } = props; return ( {icon} {label} ); }); DropdownMenuInternalLink.displayName = "DropdownMenuInternalLink"; type DropdownMenuExternalLinkProps = BaseDropdownItemProps & { href: string; target?: string; } & Omit< React.ComponentPropsWithoutRef, "children" | "asChild" | "onClick" >; const DropdownMenuExternalLink = React.forwardRef< React.ElementRef, DropdownMenuExternalLinkProps >((props, ref) => { const { label, icon, disabled, href, target, ...rest } = props; return ( {icon} {label} ); }); DropdownMenuExternalLink.displayName = "DropdownMenuExternalLink"; const DropdownMenuSeparator = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, ref) => ( )); DropdownMenuSeparator.displayName = "DropdownMenuSeparator"; const DropdownMenuLabel = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( {children} )); DropdownMenuLabel.displayName = DropdownMenuPrimitive.Label.displayName; /** Styled components */ const StyledScrollable = styled(Scrollable)` z-index: ${depths.menu}; min-width: 180px; max-width: 276px; min-height: 44px; max-height: min(85vh, var(--radix-dropdown-menu-content-available-height)); font-weight: normal; background: ${s("menuBackground")}; box-shadow: ${s("menuShadow")}; border-radius: 6px; padding: 6px; outline: none; transform-origin: var(--radix-dropdown-menu-content-transform-origin); &[data-state="open"] { animation: ${fadeAndScaleIn} 150ms ease-out; } @media print { display: none; } `; export { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuButton, DropdownMenuInternalLink, DropdownMenuExternalLink, DropdownMenuSeparator, DropdownMenuGroup, DropdownMenuLabel, DropdownSubMenu, DropdownSubMenuTrigger, DropdownSubMenuContent, };