Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧷 fix: Keep Ref Cleanup and Hold Consumer Handlers to the Frontmost Dialog (#15751)

Danny Avila49b6e519e8b9
diff --git a/packages/client/src/components/OriginalDialog.tsx b/packages/client/src/components/OriginalDialog.tsx
index 2dda473ae7..115487f515 100644
--- a/packages/client/src/components/OriginalDialog.tsx
+++ b/packages/client/src/components/OriginalDialog.tsx
@@ -177,11 +177,24 @@ const DialogContent: React.ForwardRefExoticComponent<
const composedRef = React.useCallback(
(node: HTMLDivElement | null) => {
contentRef.current = node;
- if (typeof ref === 'function') {
- ref(node);
- } else if (ref) {
- (ref as React.MutableRefObject<HTMLDivElement | null>).current = node;
+ if (typeof ref !== 'function') {
+ if (ref) {
+ (ref as React.MutableRefObject<HTMLDivElement | null>).current = node;
+ }
+ return undefined;
}
+ /** React 19 lets a callback ref return a teardown, and swallowing it
+ * here would strand a consumer's observers and listeners on a replaced
+ * node. Only a real function is passed on: React 18 warns about any
+ * other return value. */
+ const cleanup: unknown = ref(node);
+ if (typeof cleanup !== 'function') {
+ return undefined;
+ }
+ return () => {
+ contentRef.current = null;
+ (cleanup as () => void)();
+ };
},
[ref],
);
@@ -217,13 +230,6 @@ const DialogContent: React.ForwardRefExoticComponent<
if (escapeBelongsToPopup(ownerDocument)) {
return;
}
- /** The consumer's own `onEscapeKeyDown` never ran: Radix only calls it
- * for the highest layer, which the toast is. Give it the say it would
- * have had, so a dialog that refuses to close on Escape still does. */
- propsOnEscapeKeyDown?.(event);
- if (event.defaultPrevented) {
- return;
- }
/** Radix Toast's own `li`. Matched whatever its `data-state`, because a
* toast that has begun closing keeps its dismissable layer registered
* until the exit animation ends — and that layer is what takes the
@@ -253,6 +259,19 @@ const DialogContent: React.ForwardRefExoticComponent<
if (frontmost !== content) {
return;
}
+ /** Only now, with this dialog established as the one the Escape belongs
+ * to. Every mounted dialog runs this listener, and calling a consumer's
+ * handler from the ones underneath would fire their side effects for
+ * someone else's keystroke — and a `preventDefault()` there would stop
+ * the frontmost dialog from closing at all.
+ *
+ * The consumer's own `onEscapeKeyDown` never ran: Radix calls it only
+ * for the highest layer, which the toast is. Give it the say it would
+ * have had, so a dialog that refuses to close on Escape still does. */
+ propsOnEscapeKeyDown?.(event);
+ if (event.defaultPrevented) {
+ return;
+ }
escapeFallbackRef.current?.click();
};
document.addEventListener('keydown', handleKeyDown);
diff --git a/packages/client/src/components/__tests__/dialogToastEscape.spec.tsx b/packages/client/src/components/__tests__/dialogToastEscape.spec.tsx
index b463dbc342..592cddb6f5 100644
--- a/packages/client/src/components/__tests__/dialogToastEscape.spec.tsx
+++ b/packages/client/src/components/__tests__/dialogToastEscape.spec.tsx
@@ -125,6 +125,38 @@ describe('dialog Escape', () => {
expect(onOpenChange).toHaveBeenCalledWith(false);
});
+ /** Every mounted dialog runs the fallback listener, so one underneath must not
+ * fire its consumer's Escape handler for a keystroke that belongs to the
+ * dialog above it. */
+ it("does not run a lower dialog's Escape handler for the frontmost dialog", async () => {
+ const lowerEscape = jest.fn();
+ const upperOpenChange = jest.fn();
+ render(
+ <RadixToast.Provider>
+ <OGDialog open>
+ <OGDialogContent onEscapeKeyDown={lowerEscape}>
+ <button type="button">lower</button>
+ </OGDialogContent>
+ </OGDialog>
+ <OGDialog open onOpenChange={upperOpenChange}>
+ <OGDialogContent style={{ zIndex: 300 }}>
+ <button type="button">upper</button>
+ </OGDialogContent>
+ </OGDialog>
+ <RadixToast.Root open duration={Infinity} className="toast-root">
+ <RadixToast.Description>Saved</RadixToast.Description>
+ </RadixToast.Root>
+ <RadixToast.Viewport />
+ </RadixToast.Provider>,
+ );
+
+ screen.getByText('upper').focus();
+ await userEvent.keyboard('{Escape}');
+
+ expect(lowerEscape).not.toHaveBeenCalled();
+ expect(upperOpenChange).toHaveBeenCalledWith(false);
+ });
+
/** A dialog that carries its z-index in a class — `ImagePreview` is
* `z-[250]` — still outranks this one, so Escape there must not close the
* dialog underneath it. */