🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
/* eslint-disable @repo/no-style-props, @repo/no-margin-on-root-elements, @repo/component-file-structure */
"use client";
import * as React from "react";
import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";
import { cva } from "class-variance-authority";
import { Check, Minus, type IconComponent } from "@langfuse/icons";
import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
import Link from "next/link";
import { cn } from "@/src/utils/tailwind";
import { useLayerContainer } from "@/src/context/LayerContext/LayerContext";
import { useScrollGradients } from "@/src/hooks/useScrollGradients";
const DropdownMenu = DropdownMenuPrimitive.Root;
const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger;
const DropdownMenuGroup = DropdownMenuPrimitive.Group;
// Route the portal into the `popover` overlay layer (above `modal`). null
// until mounted → falls back to <body>, SSR-parity. Layer order, not z-index,
// stacks it. An explicit `container` prop still overrides the default.
const DropdownMenuPortal = ({
...props
}: React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Portal>) => {
const container = useLayerContainer("popover");
return <DropdownMenuPrimitive.Portal container={container} {...props} />;
};
DropdownMenuPortal.displayName = "DropdownMenuPortal";
const DropdownMenuSub = DropdownMenuPrimitive.Sub;
const DropdownMenuRadioGroup = DropdownMenuPrimitive.RadioGroup;
const DropdownMenuSubTrigger = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.SubTrigger>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubTrigger> & {
inset?: boolean;
hasCustomIcon?: boolean;
}
>(({ className, inset, children, hasCustomIcon = false, ...props }, ref) => (
<DropdownMenuPrimitive.SubTrigger
ref={ref}
className={cn(
"focus:bg-accent data-[state=open]:bg-accent flex cursor-pointer items-center rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-disabled:cursor-not-allowed",
inset && "pl-8",
className,
)}
{...props}
>
{children}
{!hasCustomIcon && (
<span className="ml-auto flex">
<DropdownIndicator direction="right" />
</span>
)}
</DropdownMenuPrimitive.SubTrigger>
));
DropdownMenuSubTrigger.displayName =
DropdownMenuPrimitive.SubTrigger.displayName;
const DropdownMenuSubContent = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.SubContent>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubContent>
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.SubContent
ref={ref}
className={cn(
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 min-w-32 overflow-hidden rounded-md border p-1 shadow-lg",
className,
)}
{...props}
/>
));
DropdownMenuSubContent.displayName =
DropdownMenuPrimitive.SubContent.displayName;
const dropdownMenuLabelVariants = cva("px-2 py-1.5 text-sm font-bold");
const dropdownMenuContentVariants = cva(
"bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 min-w-32 overflow-hidden rounded-md border shadow-md",
{
variants: {
// Header content provides its own full-width header and padded body.
// Headerless content needs the default padding on the Radix container.
hasHeader: {
true: null,
false: "p-1",
},
},
},
);
const dropdownMenuScrollGradientVariants = cva(
"before:pointer-events-none before:sticky before:z-2 before:-mx-1 before:-mb-6 before:block before:h-6 before:bg-linear-to-b before:from-popover before:to-transparent before:content-[''] after:pointer-events-none after:sticky after:bottom-0 after:z-2 after:-mx-1 after:-mt-6 after:block after:h-6 after:bg-linear-to-t after:from-popover after:to-transparent after:content-['']",
{
variants: {
hasHeader: {
true: "before:top-[calc(2.5rem-1px)]",
false: "before:top-0",
},
showTopGradient: {
true: "before:opacity-100",
false: "before:opacity-0",
},
showBottomGradient: {
true: "after:opacity-100",
false: "after:opacity-0",
},
},
},
);
type DropdownMenuContentProps = React.ComponentPropsWithoutRef<
typeof DropdownMenuPrimitive.Content
> & {
header?: React.ReactNode;
maxHeight?: React.CSSProperties["maxHeight"];
};
const DropdownContentWrapper = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Content>,
Omit<DropdownMenuContentProps, "children"> & {
children:
| React.ReactNode
| ((gradients: { top: boolean; bottom: boolean }) => React.ReactNode);
}
>(
(
{ children, className, header, sideOffset = 4, maxHeight, style, ...props },
ref,
) => {
const container = useLayerContainer("popover");
const { register, recompute, top, bottom } = useScrollGradients<
React.ComponentRef<typeof DropdownMenuPrimitive.Content>
>(maxHeight !== undefined);
const setContentRef = React.useCallback(
(
element: React.ComponentRef<
typeof DropdownMenuPrimitive.Content
> | null,
) => {
register(element);
if (typeof ref === "function") {
ref(element);
} else if (ref) {
ref.current = element;
}
},
[ref, register],
);
const content =
typeof children === "function" ? children({ top, bottom }) : children;
return (
<DropdownMenuPrimitive.Portal container={container}>
<DropdownMenuPrimitive.Content
ref={setContentRef}
sideOffset={sideOffset}
className={cn(
dropdownMenuContentVariants({
hasHeader: header != null,
className,
}),
maxHeight !== undefined &&
header == null &&
dropdownMenuScrollGradientVariants({
hasHeader: false,
showTopGradient: top,
showBottomGradient: bottom,
}),
)}
style={
maxHeight === undefined
? style
: { ...style, maxHeight, overflowY: "auto" }
}
{...props}
onScroll={(event) => {
recompute();
props.onScroll?.(event);
}}
>
{content}
</DropdownMenuPrimitive.Content>
</DropdownMenuPrimitive.Portal>
);
},
);
DropdownContentWrapper.displayName = "DropdownContentWrapper";
const DropdownMenuContent = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Content>,
DropdownMenuContentProps
>(
(
{ children, className, header, sideOffset = 4, maxHeight, style, ...props },
ref,
) => {
// The early return is intentional: the sticky header sits outside the padded
// body so its background and border span the full scroll container. Keep the
// header and headerless paths explicit rather than consolidating them through
// local variables.
if (header != null) {
return (
<DropdownContentWrapper
ref={ref}
className={className}
header={header}
sideOffset={sideOffset}
maxHeight={maxHeight}
style={style}
{...props}
>
{({ top, bottom }) => (
<>
<div
className={cn(
dropdownMenuLabelVariants(),
"border-border bg-popover sticky top-0 z-1 border-b px-3 py-2.5",
)}
>
{header}
</div>
<div
className={cn(
"p-1",
maxHeight !== undefined &&
dropdownMenuScrollGradientVariants({
hasHeader: true,
showTopGradient: top,
showBottomGradient: bottom,
}),
)}
>
{children}
</div>
</>
)}
</DropdownContentWrapper>
);
}
return (
<DropdownContentWrapper
ref={ref}
className={className}
sideOffset={sideOffset}
maxHeight={maxHeight}
style={style}
{...props}
>
{children}
</DropdownContentWrapper>
);
},
);
DropdownMenuContent.displayName = DropdownMenuPrimitive.Content.displayName;
/**
* Owns dropdown open state while callers retain trigger and menu presentation.
* Use the supplied primitives in each render prop to preserve Radix behavior.
*/
type DropdownMenuControllerProps = {
align: React.ComponentProps<typeof DropdownMenuContent>["align"];
isActive?: boolean;
children: (control: {
isOpen: boolean;
Trigger: typeof DropdownMenuTrigger;
}) => React.ReactNode;
maxWidth?: React.CSSProperties["maxWidth"];
/** Customizes Radix focus restoration after the menu closes. */
onCloseAutoFocus?: React.ComponentProps<
typeof DropdownMenuContent
>["onCloseAutoFocus"];
renderMenu: () => React.ReactNode;
};
const DropdownMenuController = ({
align,
isActive = true,
children,
maxWidth,
onCloseAutoFocus,
renderMenu,
}: DropdownMenuControllerProps) => {
const [isOpen, setIsOpen] = React.useState(false);
return (
<DropdownMenu open={isActive && isOpen} onOpenChange={setIsOpen}>
{children({ isOpen: isActive && isOpen, Trigger: DropdownMenuTrigger })}
<DropdownMenuContent
align={align}
style={maxWidth === undefined ? undefined : { maxWidth }}
onClick={(event) => event.stopPropagation()}
onCloseAutoFocus={(event) => {
if (!isActive) event.preventDefault();
onCloseAutoFocus?.(event);
}}
>
{renderMenu()}
</DropdownMenuContent>
</DropdownMenu>
);
};
/**
* Prefer `DropdownMenuItemWithSecondaryAction` for items that do not require JSX content.
*/
const DropdownMenuItem = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Item> & {
allowPointerEventsWhenDisabled?: boolean;
inset?: boolean;
}
>(({ allowPointerEventsWhenDisabled, className, inset, ...props }, ref) => (
<DropdownMenuPrimitive.Item
ref={ref}
className={cn(
"focus:bg-accent focus:text-accent-foreground relative flex cursor-pointer items-center rounded-sm px-2 py-1.5 text-sm outline-hidden transition-colors select-none data-disabled:cursor-not-allowed data-disabled:opacity-50",
!allowPointerEventsWhenDisabled && "data-disabled:pointer-events-none",
inset && "pl-8",
className,
)}
{...props}
/>
));
DropdownMenuItem.displayName = DropdownMenuPrimitive.Item.displayName;
type DropdownMenuItemAction = {
/**
* Use `onBeforeAction` to perform minor side effects such as capturing an event.
* Do not use `onBeforeAction` to perform the main action or prevent the default action.
*/
onBeforeAction?: () => void;
} & (
| {
href: React.ComponentProps<typeof Link>["href"];
target?: React.ComponentProps<typeof Link>["target"];
onClick?: never;
}
| {
href?: never;
target?: never;
onClick: () => void;
}
);
type DropdownMenuItemWithSecondaryActionProps = {
disabled?: { reason: string };
icon?: IconComponent;
title: string;
secondaryAction?: DropdownMenuItemAction & {
ariaLabel?: string;
icon: IconComponent;
};
} & DropdownMenuItemAction;
const relForTarget = (target: React.ComponentProps<typeof Link>["target"]) =>
target === "_blank" ? "noopener noreferrer" : undefined;
const dropdownMenuItemPrimaryActionVariants = cva(
"flex min-w-0 flex-1 cursor-pointer items-center px-2 py-1.5",
);
const dropdownMenuItemSecondaryActionVariants = cva(
"hover:bg-border dark:hover:bg-white/10 flex size-6 shrink-0 cursor-pointer items-center justify-center rounded mr-1",
);
const DropdownMenuItemWithSecondaryAction = (
props: DropdownMenuItemWithSecondaryActionProps,
) => {
const secondaryAction = props.secondaryAction;
const PrimaryActionIcon = props.icon;
const SecondaryActionIcon = secondaryAction?.icon;
const isDisabled = props.disabled !== undefined;
const primaryActionRef = React.useRef<HTMLAnchorElement | HTMLButtonElement>(
null,
);
const primaryContent = (
<>
{PrimaryActionIcon && (
<PrimaryActionIcon
size="base"
color="default"
__rawClassName="mr-1.5"
aria-hidden="true"
/>
)}
<span
className="min-w-0 flex-1 overflow-hidden text-left text-ellipsis whitespace-nowrap"
title={props.disabled?.reason ?? props.title}
>
{props.title}
</span>
</>
);
const secondaryActionContent = React.useMemo(() => {
// The secondary action is intentionally pointer-only and cannot be targeted
// with the keyboard; the parent remains the row's sole menu item.
if (!secondaryAction || !SecondaryActionIcon) return null;
if (secondaryAction.href !== undefined) {
return (
<Link
href={secondaryAction.href}
target={secondaryAction.target}
rel={relForTarget(secondaryAction.target)}
aria-label={secondaryAction.ariaLabel}
aria-disabled={isDisabled}
tabIndex={isDisabled ? -1 : undefined}
className={dropdownMenuItemSecondaryActionVariants()}
onClick={(event) => {
event.stopPropagation();
if (isDisabled) {
event.preventDefault();
return;
}
secondaryAction.onBeforeAction?.();
}}
onAuxClick={(event) => {
event.stopPropagation();
if (isDisabled) {
event.preventDefault();
return;
}
if (event.button === 1) {
secondaryAction.onBeforeAction?.();
}
}}
>
<SecondaryActionIcon size="sm" color="default" />
</Link>
);
}
return (
<button
type="button"
aria-label={secondaryAction.ariaLabel}
disabled={isDisabled}
className={dropdownMenuItemSecondaryActionVariants()}
onClick={(event) => {
event.stopPropagation();
secondaryAction.onBeforeAction?.();
secondaryAction.onClick();
}}
>
<SecondaryActionIcon size="sm" color="default" />
</button>
);
}, [secondaryAction, SecondaryActionIcon, isDisabled]);
return (
<DropdownMenuItem
allowPointerEventsWhenDisabled
className="h-8 p-0 data-disabled:cursor-not-allowed"
disabled={isDisabled}
title={props.disabled?.reason}
onClick={(event) => {
if (isDisabled) return;
if (event.target !== event.currentTarget) return;
event.preventDefault();
primaryActionRef.current?.click();
}}
>
{props.href !== undefined ? (
<Link
ref={(element) => {
primaryActionRef.current = element;
}}
href={props.href}
target={props.target}
rel={relForTarget(props.target)}
aria-disabled={isDisabled}
tabIndex={isDisabled ? -1 : undefined}
className={dropdownMenuItemPrimaryActionVariants()}
onClick={(event) => {
if (isDisabled) {
event.preventDefault();
return;
}
props.onBeforeAction?.();
}}
onAuxClick={(event) => {
if (isDisabled) {
event.preventDefault();
return;
}
if (event.button === 1) {
props.onBeforeAction?.();
}
}}
>
{primaryContent}
</Link>
) : (
<button
ref={(element) => {
primaryActionRef.current = element;
}}
type="button"
disabled={isDisabled}
className={dropdownMenuItemPrimaryActionVariants()}
onClick={() => {
props.onBeforeAction?.();
props.onClick();
}}
>
{primaryContent}
</button>
)}
{secondaryActionContent}
</DropdownMenuItem>
);
};
const DropdownMenuCheckboxItem = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.CheckboxItem>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.CheckboxItem>
>(({ className, children, checked, ...props }, ref) => (
<DropdownMenuPrimitive.CheckboxItem
ref={ref}
className={cn(
"focus:bg-accent focus:text-accent-foreground relative flex cursor-pointer items-center rounded-sm py-1.5 pr-2 pl-8 text-sm outline-hidden transition-colors select-none data-disabled:pointer-events-none data-disabled:opacity-50",
className,
)}
checked={checked}
{...props}
>
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DropdownMenuPrimitive.ItemIndicator>
{checked === "indeterminate" && <Minus size="base" color="default" />}
{checked === true && <Check size="base" color="default" />}
</DropdownMenuPrimitive.ItemIndicator>
</span>
{children}
</DropdownMenuPrimitive.CheckboxItem>
));
DropdownMenuCheckboxItem.displayName =
DropdownMenuPrimitive.CheckboxItem.displayName;
const DropdownMenuRadioItem = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.RadioItem>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.RadioItem>
>(({ className, children, ...props }, ref) => (
<DropdownMenuPrimitive.RadioItem
ref={ref}
className={cn(
"focus:bg-accent focus:text-accent-foreground relative flex cursor-pointer items-center rounded-sm py-1.5 pr-2 pl-8 text-sm outline-hidden transition-colors select-none data-disabled:pointer-events-none data-disabled:opacity-50",
className,
)}
{...props}
>
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DropdownMenuPrimitive.ItemIndicator>
<span className="size-2 rounded-full bg-current" aria-hidden />
</DropdownMenuPrimitive.ItemIndicator>
</span>
{children}
</DropdownMenuPrimitive.RadioItem>
));
DropdownMenuRadioItem.displayName = DropdownMenuPrimitive.RadioItem.displayName;
const DropdownMenuLabel = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Label> & {
inset?: boolean;
}
>(({ className, inset, ...props }, ref) => (
<DropdownMenuPrimitive.Label
ref={ref}
className={cn(dropdownMenuLabelVariants(), inset && "pl-8", className)}
{...props}
/>
));
DropdownMenuLabel.displayName = DropdownMenuPrimitive.Label.displayName;
const DropdownMenuSeparator = React.forwardRef<
React.ComponentRef<typeof DropdownMenuPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Separator>
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.Separator
ref={ref}
className={cn("bg-border -mx-1 my-1 h-px", className)}
{...props}
/>
));
DropdownMenuSeparator.displayName = DropdownMenuPrimitive.Separator.displayName;
const DropdownMenuShortcut = ({
className,
...props
}: React.HTMLAttributes<HTMLSpanElement>) => {
return (
<span
className={cn("ml-auto text-xs tracking-widest opacity-60", className)}
{...props}
/>
);
};
DropdownMenuShortcut.displayName = "DropdownMenuShortcut";
export {
DropdownMenu,
DropdownMenuController,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuItemWithSecondaryAction,
DropdownMenuCheckboxItem,
DropdownMenuRadioItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuGroup,
DropdownMenuPortal,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuRadioGroup,
};