Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): Resolve dropdown component issues introduced in a186a9d (#15275)

Ben Bachem6d59330b7ca8
diff --git a/web/src/components/ProjectDropdownMenu/ProjectDropdownMenu.stories.tsx b/web/src/components/ProjectDropdownMenu/ProjectDropdownMenu.stories.tsx
index 07dbe752f3..2afccd6bb3 100644
--- a/web/src/components/ProjectDropdownMenu/ProjectDropdownMenu.stories.tsx
+++ b/web/src/components/ProjectDropdownMenu/ProjectDropdownMenu.stories.tsx
@@ -1,4 +1,5 @@
import type { ComponentProps } from "react";
+import { expect, fn, userEvent, waitFor, within } from "storybook/test";
import preview from "../../../.storybook/preview";
import {
DropdownMenu,
@@ -76,3 +77,95 @@ export const Loading = meta.story({
state: "loading",
},
});
+
+// TODO: Move these regression tests to a generic dropdown component story once one exists.
+const getAnalyticsActions = async (canvasElement: HTMLElement) => {
+ const body = within(canvasElement.ownerDocument.body);
+ const primaryAction = await body.findByRole("link", { name: "Analytics" });
+ const secondaryAction = body.getByRole("link", {
+ name: "Go to settings for Analytics",
+ });
+ const menuItem = primaryAction.closest('[role="menuitem"]');
+
+ // Every primary action must be owned by a Radix menu item for keyboard navigation.
+ await expect(menuItem).not.toBeNull();
+ if (!(menuItem instanceof HTMLElement)) {
+ throw new Error("Expected Analytics to be rendered inside a menu item");
+ }
+
+ return { menuItem, primaryAction, secondaryAction };
+};
+
+export const TestSecondaryActionLayout = meta.story({
+ name: "(Test) Secondary action layout",
+ args: {
+ state: "loaded",
+ projects,
+ },
+ play: async ({ canvasElement }) => {
+ const { menuItem, secondaryAction } =
+ await getAnalyticsActions(canvasElement);
+
+ // Both actions must share one menu item so its highlight spans the full row.
+ await expect(menuItem).toContainElement(secondaryAction);
+
+ const menuItemRect = menuItem.getBoundingClientRect();
+ const secondaryActionRect = secondaryAction.getBoundingClientRect();
+ const centerDifference = Math.abs(
+ menuItemRect.top +
+ menuItemRect.height / 2 -
+ (secondaryActionRect.top + secondaryActionRect.height / 2),
+ );
+ // The secondary action must remain vertically centered in the row.
+ await expect(centerDifference).toBeLessThanOrEqual(1);
+
+ await userEvent.hover(secondaryAction);
+
+ // Hovering the secondary action must highlight the shared menu item.
+ await waitFor(() => expect(menuItem).toHaveAttribute("data-highlighted"));
+ },
+});
+
+export const TestActionsRemainIndependent = meta.story({
+ name: "(Test) Actions remain independent",
+ args: {
+ state: "loaded",
+ projects,
+ },
+ play: async ({ canvasElement }) => {
+ const { primaryAction, secondaryAction } =
+ await getAnalyticsActions(canvasElement);
+ const onPrimaryClick = fn((event: MouseEvent) => event.preventDefault());
+ const onSecondaryClick = fn((event: MouseEvent) => event.preventDefault());
+ primaryAction.addEventListener("click", onPrimaryClick);
+ secondaryAction.addEventListener("click", onSecondaryClick);
+
+ await userEvent.click(secondaryAction);
+
+ // The secondary action handles the click itself without triggering the primary link.
+ await expect(onSecondaryClick).toHaveBeenCalledOnce();
+ await expect(onPrimaryClick).not.toHaveBeenCalled();
+ },
+});
+
+export const TestKeyboardActivation = meta.story({
+ name: "(Test) Keyboard activation",
+ args: {
+ state: "loaded",
+ projects,
+ },
+ play: async ({ canvasElement }) => {
+ const { menuItem, primaryAction } =
+ await getAnalyticsActions(canvasElement);
+ const onPrimaryClick = fn((event: MouseEvent) => event.preventDefault());
+ primaryAction.addEventListener("click", onPrimaryClick);
+
+ menuItem.focus();
+ await userEvent.keyboard("{Enter}");
+ menuItem.focus();
+ await userEvent.keyboard(" ");
+
+ // Radix forwards both supported activation keys to the primary action exactly once.
+ await expect(onPrimaryClick).toHaveBeenCalledTimes(2);
+ },
+});
diff --git a/web/src/components/ui/dropdown-menu.tsx b/web/src/components/ui/dropdown-menu.tsx
index 062df28756..92f33f10f5 100644
--- a/web/src/components/ui/dropdown-menu.tsx
+++ b/web/src/components/ui/dropdown-menu.tsx
@@ -191,9 +191,11 @@ const DropdownMenuContent = React.forwardRef<
{ 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) {
- // The sticky header sits outside the padded body so its background and
- // border cover the full width of the scroll container.
return (
<DropdownContentWrapper
ref={ref}
@@ -310,6 +312,9 @@ const DropdownMenuItemWithSecondaryAction = (
const secondaryAction = props.secondaryAction;
const PrimaryActionIcon = props.icon;
const SecondaryActionIcon = secondaryAction?.icon;
+ const primaryActionRef = React.useRef<HTMLAnchorElement | HTMLButtonElement>(
+ null,
+ );
const primaryContent = (
<>
{PrimaryActionIcon && (
@@ -325,6 +330,8 @@ const DropdownMenuItemWithSecondaryAction = (
);
let secondaryActionContent: React.ReactNode = null;
+ // 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) {
if (secondaryAction.href !== undefined) {
secondaryActionContent = (
@@ -365,41 +372,51 @@ const DropdownMenuItemWithSecondaryAction = (
}
return (
- <div className="flex h-8">
+ <DropdownMenuItem
+ className="h-8 p-0"
+ onClick={(event) => {
+ if (event.target !== event.currentTarget) return;
+
+ event.preventDefault();
+ primaryActionRef.current?.click();
+ }}
+ >
{props.href !== undefined ? (
- <DropdownMenuItem asChild className="h-8 min-w-0 flex-1 p-0">
- <Link
- href={props.href}
- className={dropdownMenuItemPrimaryActionVariants()}
- onClick={() => {
+ <Link
+ ref={(element) => {
+ primaryActionRef.current = element;
+ }}
+ href={props.href}
+ className={dropdownMenuItemPrimaryActionVariants()}
+ onClick={() => {
+ props.onBeforeAction?.();
+ }}
+ onAuxClick={(event) => {
+ if (event.button === 1) {
props.onBeforeAction?.();
- }}
- onAuxClick={(event) => {
- if (event.button === 1) {
- props.onBeforeAction?.();
- }
- }}
- >
- {primaryContent}
- </Link>
- </DropdownMenuItem>
+ }
+ }}
+ >
+ {primaryContent}
+ </Link>
) : (
- <DropdownMenuItem asChild className="h-8 min-w-0 flex-1 p-0">
- <button
- type="button"
- className={dropdownMenuItemPrimaryActionVariants()}
- onClick={() => {
- props.onBeforeAction?.();
- props.onClick();
- }}
- >
- {primaryContent}
- </button>
- </DropdownMenuItem>
+ <button
+ ref={(element) => {
+ primaryActionRef.current = element;
+ }}
+ type="button"
+ className={dropdownMenuItemPrimaryActionVariants()}
+ onClick={() => {
+ props.onBeforeAction?.();
+ props.onClick();
+ }}
+ >
+ {primaryContent}
+ </button>
)}
{secondaryActionContent}
- </div>
+ </DropdownMenuItem>
);
};