Skip to content

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

improvement(ui): observe effective combobox popup state (#8440)

Andrew Huang43582fe994d2
diff --git a/frontend/src/components/v3/generic/Combobox/Combobox.stories.tsx b/frontend/src/components/v3/generic/Combobox/Combobox.stories.tsx
index 2eb7b4561b..3d59aca794 100644
--- a/frontend/src/components/v3/generic/Combobox/Combobox.stories.tsx
+++ b/frontend/src/components/v3/generic/Combobox/Combobox.stories.tsx
@@ -3,8 +3,10 @@ import {
createContext,
type FormEvent,
type ReactNode,
+ useCallback,
useContext,
useEffect,
+ useRef,
useState
} from "react";
import type { Meta, StoryObj } from "@storybook/react-vite";
@@ -631,7 +633,11 @@ export const ChipLayouts: Story = {
)
};
-const NestedDialogCreationField = () => {
+const NestedDialogCreationField = ({
+ onPopupOpenChange
+}: {
+ onPopupOpenChange?: (open: boolean) => void;
+}) => {
const [options, setOptions] = useState<string[]>([]);
const [value, setValue] = useState<string | null>(null);
@@ -641,6 +647,7 @@ const NestedDialogCreationField = () => {
<StoryCombobox
id="combobox-nested-dialog-tag"
modal
+ onPopupOpenChange={onPopupOpenChange}
options={options}
value={value}
onValueChange={setValue}
@@ -685,13 +692,30 @@ const InDialogRender = () => {
ORGANIZATION_ROLES[1]
);
const [parentSubmitCount, setParentSubmitCount] = useState(0);
+ const openPopups = useRef(new Set<string>());
+ const onPopupOpenChange = useCallback((field: string, open: boolean) => {
+ if (open) openPopups.current.add(field);
+ else openPopups.current.delete(field);
+ }, []);
+ const onRolePopupOpenChange = useCallback(
+ (open: boolean) => onPopupOpenChange("role", open),
+ [onPopupOpenChange]
+ );
+ const onTagPopupOpenChange = useCallback(
+ (open: boolean) => onPopupOpenChange("tag", open),
+ [onPopupOpenChange]
+ );
return (
<Dialog>
<DialogTrigger asChild>
<Button variant="outline">Open Role Picker</Button>
</DialogTrigger>
- <DialogContent>
+ <DialogContent
+ onEscapeKeyDown={(event) => {
+ if (openPopups.current.size > 0) event.preventDefault();
+ }}
+ >
<form
className="flex flex-col gap-6"
onSubmit={(event) => {
@@ -717,6 +741,7 @@ const InDialogRender = () => {
searchPlaceholder="Search roles..."
searchAriaLabel="Search organization roles"
modal
+ onPopupOpenChange={onRolePopupOpenChange}
renderOption={(option) => (
<div className="min-w-0">
<p className="truncate">{option.name}</p>
@@ -727,7 +752,7 @@ const InDialogRender = () => {
)}
/>
</Field>
- <NestedDialogCreationField />
+ <NestedDialogCreationField onPopupOpenChange={onTagPopupOpenChange} />
<p role="status">Parent form submits: {parentSubmitCount}</p>
</form>
</DialogContent>
@@ -741,7 +766,7 @@ export const InDialog: Story = {
docs: {
description: {
story:
- "Set `modal` when the combobox is rendered inside a modal Dialog. Base UI then preserves focus containment and scroll access for its body-portalled option list. A dialog creation form stops its submit event at the shared creation-dialog boundary, so React portal propagation cannot submit an ancestor form."
+ "Set `modal` inside a modal Dialog to preserve focus containment and scroll access. Use `onPopupOpenChange` to track each option popup in a caller-owned registry, and prevent the parent's `onEscapeKeyDown` only while an option popup is open. Base UI still owns option-popup dismissal; after the option popup closes, the next Escape reaches the parent. This composition does not coordinate creation-dialog admission or native exit lifetimes. The latest committed callback receives logical state changes and initial attachment, including attachment while already open. Explicit observer removal or unmount reports false; recreating an inline callback does not manufacture close/open events. Semantic owner changes require a committed detach or a React-key remount, not callback identity; setting undefined and a new callback in one batch is not a committed detach. A dialog creation form stops its submit event at the shared creation-dialog boundary, so React portal propagation cannot submit an ancestor form."
}
}
},
diff --git a/frontend/src/components/v3/generic/Combobox/Combobox.tsx b/frontend/src/components/v3/generic/Combobox/Combobox.tsx
index a6e967a3af..033777bd6d 100644
--- a/frontend/src/components/v3/generic/Combobox/Combobox.tsx
+++ b/frontend/src/components/v3/generic/Combobox/Combobox.tsx
@@ -86,6 +86,7 @@ type ComboboxSharedProps<TOption> = {
isLoading?: boolean;
isError?: boolean;
modal?: boolean;
+ onPopupOpenChange?: (open: boolean) => void;
portalContainer?: HTMLElement | React.RefObject<HTMLElement | null> | null;
contentClassName?: string;
onInputValueChange?: (inputValue: string) => void;
@@ -159,6 +160,31 @@ const clearSingleComboboxValue = <TOption,>(props: ComboboxSingleProps<TOption>)
if (props.isClearable !== false) props.onValueChange(null);
};
+const useComboboxOpen = (onPopupOpenChange?: (open: boolean) => void) => {
+ const [open, setOpen] = React.useState(false);
+ const observerRef = React.useRef(onPopupOpenChange);
+ const hasObserver = Boolean(onPopupOpenChange);
+
+ React.useLayoutEffect(() => {
+ const previousObserver = observerRef.current;
+ observerRef.current = onPopupOpenChange;
+ if (previousObserver && !onPopupOpenChange) previousObserver(false);
+ }, [onPopupOpenChange]);
+
+ React.useLayoutEffect(() => {
+ observerRef.current?.(open);
+ }, [open, hasObserver]);
+
+ React.useLayoutEffect(
+ () => () => {
+ observerRef.current?.(false);
+ },
+ []
+ );
+
+ return [open, setOpen] as const;
+};
+
const SINGLE_LIST_MAX_HEIGHT = "min(18.75rem, var(--available-height, 50dvh))";
const MULTIPLE_LIST_MAX_HEIGHT = "min(18.75rem, var(--available-height, 50dvh))";
@@ -815,6 +841,7 @@ const SingleCombobox = <TOption,>(props: ComboboxSingleProps<TOption>) => {
isLoading = false,
isError = false,
modal = false,
+ onPopupOpenChange,
portalContainer: portalContainerProp,
variant = "default",
className,
@@ -832,7 +859,7 @@ const SingleCombobox = <TOption,>(props: ComboboxSingleProps<TOption>) => {
const inputRef = React.useRef<HTMLInputElement | null>(null);
const listScrollRef = React.useRef<HTMLDivElement>(null);
const highlightedOptionValueRef = React.useRef<string | null>(null);
- const [open, setOpen] = React.useState(false);
+ const [open, setOpen] = useComboboxOpen(onPopupOpenChange);
const selectedLabel = value == null ? "" : getOptionLabel(value);
const [search, setSearch] = React.useState("");
const searchRef = React.useRef(search);
@@ -1204,6 +1231,7 @@ const MultipleCombobox = <TOption,>({
isLoading = false,
isError = false,
modal = false,
+ onPopupOpenChange,
portalContainer: portalContainerProp,
className,
contentClassName,
@@ -1223,7 +1251,7 @@ const MultipleCombobox = <TOption,>({
const { scrollEdges, setViewportRef } = useScrollEdges<HTMLDivElement>(
singleLine ? "horizontal" : "vertical"
);
- const [open, setOpen] = React.useState(false);
+ const [open, setOpen] = useComboboxOpen(onPopupOpenChange);
const openRef = React.useRef(open);
openRef.current = open;
const [search, setSearch] = React.useState("");