Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(frontend): close only the innermost popup on Escape inside V3 Popover (#8839)

Andrew Huang98d5d8f90442
diff --git a/frontend/src/components/V3_MIGRATION.md b/frontend/src/components/V3_MIGRATION.md
index 83a050b958..a4534b0875 100644
--- a/frontend/src/components/V3_MIGRATION.md
+++ b/frontend/src/components/V3_MIGRATION.md
@@ -151,7 +151,7 @@ Use this recipe for every V2 `Modal`, `DeleteActionModal`, and `ConfirmActionMod
**What the V3 contract provides.** `DialogContent`, `SheetContent`, `AlertDialogContent`, and `ConfirmationAlertDialog` provide these behaviors without consumer code:
-- **Innermost layer first.** Escape or an outside click closes an open nested popup first. The next Escape or outside click reaches the overlay. Radix popups (`Select`, `DropdownMenu`, `Tooltip`, `HoverCard`, `Popover`) do this natively. `Combobox` registers its open popup with the nearest `DialogContent`, `SheetContent`, or `AlertDialogContent` (which includes `ConfirmationAlertDialog`), and an event that closed the popup cannot also close the overlay. Inside an overlay, `Combobox` defaults to `modal`.
+- **Innermost layer first.** Escape or an outside click closes an open nested popup first. The next Escape or outside click reaches the overlay. Radix popups (`Select`, `DropdownMenu`, `Tooltip`, `HoverCard`, `Popover`) do this natively. `Combobox` registers its open popup with the nearest `DialogContent`, `SheetContent`, `AlertDialogContent` (which includes `ConfirmationAlertDialog`), or `PopoverContent`, and an event that closed the popup cannot also close the overlay. Inside a `Dialog`, `Sheet`, or `AlertDialog`, `Combobox` defaults to `modal`. Inside a `Popover` that is not nested in one of those, it stays non-modal.
- **Pending close.** While `isPending` is true on `DialogContent` or `SheetContent`, Escape, outside clicks, the built-in close button, and every `DialogClose` or `SheetClose` cannot dismiss the overlay. `ConfirmationAlertDialog` locks itself while `onConfirm` is in flight, disables Cancel and the input, and shows the pending state on its confirm button.
- **Focus return.** Closing returns focus to the element that opened the overlay. When a V3 `DropdownMenuItem` opened it, focus returns to that menu's trigger. No refs or `onCloseAutoFocus` handlers are needed. If the opener no longer exists, for example a row removed by a successful delete, Radix's default applies.
- **Typed confirmation.** `ConfirmationAlertDialog` compares the typed value with `confirmKey`, resets it on every open and close, submits on Enter only when it matches, and keeps the dialog open if `onConfirm` rejects. The global mutation error toast reports the failure.
diff --git a/frontend/src/components/v3/generic/Combobox/Combobox.tsx b/frontend/src/components/v3/generic/Combobox/Combobox.tsx
index 3cb776ba3f..0efa283ac0 100644
--- a/frontend/src/components/v3/generic/Combobox/Combobox.tsx
+++ b/frontend/src/components/v3/generic/Combobox/Combobox.tsx
@@ -3,7 +3,10 @@ import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox";
import { CheckIcon, ChevronDownIcon, Loader2Icon, PlusIcon, XIcon } from "lucide-react";
import { cn } from "../../utils";
-import { useIsInsideOverlayLayer, useOverlayPopupRegistration } from "../../utils/overlay-layer";
+import {
+ useIsInsideModalOverlayLayer,
+ useOverlayPopupRegistration
+} from "../../utils/overlay-layer";
import { useScrollEdges } from "../../utils/useScrollEdges";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "../Dialog";
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "../Sheet";
@@ -926,13 +929,13 @@ const SingleCombobox = <TOption,>(props: ComboboxSingleProps<TOption>) => {
ref,
...inputProps
} = props;
- const isInsideOverlayLayer = useIsInsideOverlayLayer();
+ const isInsideModalOverlayLayer = useIsInsideModalOverlayLayer();
const inputRef = React.useRef<HTMLInputElement | null>(null);
React.useImperativeHandle(ref, () => inputRef.current as HTMLInputElement, []);
const listScrollRef = React.useRef<HTMLDivElement>(null);
const highlightedOptionValueRef = React.useRef<string | null>(null);
const [open, setOpen, markPopupDismissEvent] = useComboboxOpen(onPopupOpenChange);
- const modal = modalProp ?? isInsideOverlayLayer;
+ const modal = modalProp ?? isInsideModalOverlayLayer;
const selectedLabel = value == null ? "" : getOptionLabel(value);
const [search, setSearch] = React.useState("");
const searchRef = React.useRef(search);
@@ -1328,7 +1331,7 @@ const MultipleCombobox = <TOption,>({
ref,
...inputProps
}: ComboboxMultipleProps<TOption>) => {
- const isInsideOverlayLayer = useIsInsideOverlayLayer();
+ const isInsideModalOverlayLayer = useIsInsideModalOverlayLayer();
const inputRef = React.useRef<HTMLInputElement | null>(null);
React.useImperativeHandle(ref, () => inputRef.current as HTMLInputElement, []);
const listScrollRef = React.useRef<HTMLDivElement>(null);
@@ -1337,7 +1340,7 @@ const MultipleCombobox = <TOption,>({
singleLine ? "horizontal" : "vertical"
);
const [open, setOpen, markPopupDismissEvent] = useComboboxOpen(onPopupOpenChange);
- const modal = modalProp ?? isInsideOverlayLayer;
+ const modal = modalProp ?? isInsideModalOverlayLayer;
const openRef = React.useRef(open);
openRef.current = open;
const [search, setSearch] = React.useState("");
diff --git a/frontend/src/components/v3/generic/Popover/Popover.stories.tsx b/frontend/src/components/v3/generic/Popover/Popover.stories.tsx
index 41968399a0..be41cd0fd9 100644
--- a/frontend/src/components/v3/generic/Popover/Popover.stories.tsx
+++ b/frontend/src/components/v3/generic/Popover/Popover.stories.tsx
@@ -1,10 +1,13 @@
+import { useId, useState } from "react";
import type { Meta, StoryObj } from "@storybook/react-vite";
-import { CheckIcon, InfoIcon, PencilIcon } from "lucide-react";
+import { CheckIcon, FilterIcon, InfoIcon, PencilIcon } from "lucide-react";
import { Button } from "../Button";
+import { Combobox } from "../Combobox";
import { Field, FieldDescription, FieldLabel } from "../Field";
import { IconButton } from "../IconButton";
import { Input } from "../Input";
+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../Select";
import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "./Popover";
/**
@@ -269,3 +272,69 @@ export const WithAnchor: Story = {
</Popover>
)
};
+
+const STATUSES = [
+ { value: "active", label: "Active" },
+ { value: "expired", label: "Expired" },
+ { value: "revoked", label: "Revoked" }
+];
+
+const WithNestedPickersRender = () => {
+ const [statuses, setStatuses] = useState<(typeof STATUSES)[number][]>([]);
+ const [operator, setOperator] = useState("is");
+ const statusId = useId();
+ const operatorId = useId();
+
+ return (
+ <Popover>
+ <PopoverTrigger asChild>
+ <Button variant="outline">
+ <FilterIcon />
+ Add Filter
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="w-80" align="start">
+ <div className="flex flex-col gap-3">
+ <Field>
+ <FieldLabel htmlFor={operatorId}>Operator</FieldLabel>
+ <Select value={operator} onValueChange={setOperator}>
+ <SelectTrigger id={operatorId} className="w-full">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent position="popper">
+ <SelectItem value="is">Is</SelectItem>
+ <SelectItem value="is-not">Is Not</SelectItem>
+ </SelectContent>
+ </Select>
+ </Field>
+ <Field>
+ <FieldLabel htmlFor={statusId}>Status</FieldLabel>
+ <Combobox
+ id={statusId}
+ multiple
+ options={STATUSES}
+ value={statuses}
+ onValueChange={setStatuses}
+ getOptionValue={(status) => status.value}
+ getOptionLabel={(status) => status.label}
+ placeholder="Select statuses..."
+ />
+ </Field>
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+};
+
+export const WithNestedPickers: Story = {
+ name: "Example: With Nested Pickers",
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "`Combobox`, `Select`, and `DropdownMenu` popups inside `PopoverContent` own the first Escape or outside click, so the panel and its draft stay open. The next Escape or outside click closes the `Popover`."
+ }
+ }
+ },
+ render: () => <WithNestedPickersRender />
+};
diff --git a/frontend/src/components/v3/generic/Popover/Popover.tsx b/frontend/src/components/v3/generic/Popover/Popover.tsx
index 805a6e56a5..43d5192af2 100644
--- a/frontend/src/components/v3/generic/Popover/Popover.tsx
+++ b/frontend/src/components/v3/generic/Popover/Popover.tsx
@@ -2,6 +2,7 @@ import * as React from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";
import { cn } from "../../utils";
+import { OverlayLayerContext, useOverlayLayer } from "../../utils/overlay-layer";
function Popover({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Root>) {
return <PopoverPrimitive.Root data-slot="popover" {...props} />;
@@ -21,23 +22,33 @@ function PopoverContent({
sideOffset = 4,
collisionPadding = 8,
container,
+ onEscapeKeyDown,
+ onPointerDownOutside,
+ onInteractOutside,
...props
}: React.ComponentProps<typeof PopoverPrimitive.Content> & {
container?: React.ComponentProps<typeof PopoverPrimitive.Portal>["container"];
}) {
+ const { contextValue, guardDismiss } = useOverlayLayer(false, false);
+
return (
<PopoverPrimitive.Portal container={container}>
- <PopoverPrimitive.Content
- data-slot="popover-content"
- align={align}
- sideOffset={sideOffset}
- collisionPadding={collisionPadding}
- className={cn(
- "z-[var(--z-index-popover)] w-72 origin-(--radix-popover-content-transform-origin) rounded-lg border border-border bg-popover p-4 text-foreground shadow-md outline-hidden 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 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
- className
- )}
- {...props}
- />
+ <OverlayLayerContext.Provider value={contextValue}>
+ <PopoverPrimitive.Content
+ data-slot="popover-content"
+ align={align}
+ sideOffset={sideOffset}
+ collisionPadding={collisionPadding}
+ className={cn(
+ "z-[var(--z-index-popover)] w-72 origin-(--radix-popover-content-transform-origin) rounded-lg border border-border bg-popover p-4 text-foreground shadow-md outline-hidden 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 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
+ className
+ )}
+ onEscapeKeyDown={(event) => guardDismiss(event, onEscapeKeyDown)}
+ onPointerDownOutside={(event) => guardDismiss(event, onPointerDownOutside)}
+ onInteractOutside={(event) => guardDismiss(event, onInteractOutside)}
+ {...props}
+ />
+ </OverlayLayerContext.Provider>
</PopoverPrimitive.Portal>
);
}
diff --git a/frontend/src/components/v3/utils/overlay-layer.test.ts b/frontend/src/components/v3/utils/overlay-layer.test.ts
index 0550bc5c09..885a35c7c6 100644
--- a/frontend/src/components/v3/utils/overlay-layer.test.ts
+++ b/frontend/src/components/v3/utils/overlay-layer.test.ts
@@ -15,6 +15,21 @@ const createLayers = () => {
return layers;
};
+const createLayerChain = (configs: { isPending: boolean; isModal: boolean }[]) => {
+ const values: ReturnType<typeof useOverlayLayer>["contextValue"][] = [];
+ const Probe = ({ index }: { index: number }): ReactNode => {
+ const layer = useOverlayLayer(configs[index].isPending, configs[index].isModal);
+ values.push(layer.contextValue);
+ return createElement(
+ OverlayLayerContext.Provider,
+ { value: layer.contextValue },
+ index + 1 < configs.length ? createElement(Probe, { index: index + 1 }) : null
+ );
+ };
+ renderToString(createElement(Probe, { index: 0 }));
+ return values.map(({ isModal, isPending }) => ({ isModal, isPending }));
+};
+
const escape = () =>
Object.assign(new Event("keydown", { cancelable: true }), { key: "Escape" }) as KeyboardEvent;
@@ -87,3 +102,35 @@ describe("overlay popup bookkeeping", () => {
expect(parentHandler).toHaveBeenCalledTimes(2);
});
});
+
+describe("overlay layer context inheritance", () => {
+ it("keeps a standalone non-modal layer non-modal", () => {
+ expect(createLayerChain([{ isPending: false, isModal: false }])).toEqual([
+ { isModal: false, isPending: false }
+ ]);
+ });
+
+ it("inherits modality and pending state into a non-modal layer", () => {
+ expect(
+ createLayerChain([
+ { isPending: true, isModal: true },
+ { isPending: false, isModal: false }
+ ])
+ ).toEqual([
+ { isModal: true, isPending: true },
+ { isModal: true, isPending: true }
+ ]);
+ });
+
+ it("keeps a nested modal layer's own pending state", () => {
+ expect(
+ createLayerChain([
+ { isPending: true, isModal: true },
+ { isPending: false, isModal: true }
+ ])
+ ).toEqual([
+ { isModal: true, isPending: true },
+ { isModal: true, isPending: false }
+ ]);
+ });
+});
diff --git a/frontend/src/components/v3/utils/overlay-layer.ts b/frontend/src/components/v3/utils/overlay-layer.ts
index d52c13e2ba..d1df3fd67c 100644
--- a/frontend/src/components/v3/utils/overlay-layer.ts
+++ b/frontend/src/components/v3/utils/overlay-layer.ts
@@ -1,6 +1,7 @@
import * as React from "react";
type OverlayLayerContextValue = {
+ isModal: boolean;
isPending: boolean;
registerPopup: (
id: string,
@@ -21,8 +22,10 @@ const registerMenuItemFocusReturn = (item: Element, trigger: HTMLElement | null)
const getOriginalEvent = (event: Event) =>
(event as CustomEvent<{ originalEvent?: Event } | undefined>).detail?.originalEvent ?? event;
-const useOverlayLayer = (isPending: boolean) => {
+const useOverlayLayer = (isPending: boolean, isModal = true) => {
const parentLayer = React.useContext(OverlayLayerContext);
+ const isModalLayer = isModal || Boolean(parentLayer?.isModal);
+ const isContextPending = isPending || (!isModal && Boolean(parentLayer?.isPending));
const parentRegisterPopup = parentLayer?.registerPopup;
const parentMarkPopupDismissEvent = parentLayer?.markPopupDismissEvent;
const openPopups = React.useRef(new Map<string, ((event: KeyboardEvent) => void) | undefined>());
@@ -58,8 +61,13 @@ const useOverlayLayer = (isPending: boolean) => {
);
const contextValue = React.useMemo(
- () => ({ isPending, registerPopup, markPopupDismissEvent }),
- [isPending, registerPopup, markPopupDismissEvent]
+ () => ({
+ isModal: isModalLayer,
+ isPending: isContextPending,
+ registerPopup,
+ markPopupDismissEvent
+ }),
+ [isModalLayer, isContextPending, registerPopup, markPopupDismissEvent]
);
const guardDismiss = <TEvent extends Event>(event: TEvent, handler?: (event: TEvent) => void) => {
@@ -106,7 +114,7 @@ const useOverlayLayer = (isPending: boolean) => {
return { contextValue, guardDismiss, captureFocusReturn, restoreFocusReturn };
};
-const useIsInsideOverlayLayer = () => React.useContext(OverlayLayerContext) !== null;
+const useIsInsideModalOverlayLayer = () => Boolean(React.useContext(OverlayLayerContext)?.isModal);
const useOverlayIsPending = () => Boolean(React.useContext(OverlayLayerContext)?.isPending);
@@ -142,7 +150,7 @@ const useOverlayPopupRegistration = (
export {
OverlayLayerContext,
registerMenuItemFocusReturn,
- useIsInsideOverlayLayer,
+ useIsInsideModalOverlayLayer,
useOverlayIsPending,
useOverlayLayer,
useOverlayPopupRegistration