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: Close Only the Dropdown List on Escape Inside Settings (#17503)

Marco Berettaa775ca03f14f
diff --git a/e2e/specs/mock/scenarios/settings-dropdown-escape.spec.ts b/e2e/specs/mock/scenarios/settings-dropdown-escape.spec.ts
new file mode 100644
index 0000000000..b3bef2c6a0
--- /dev/null
+++ b/e2e/specs/mock/scenarios/settings-dropdown-escape.spec.ts
@@ -0,0 +1,34 @@
+import { expect, test } from '@playwright/test';
+import { NEW_CHAT_PATH } from '../helpers';
+import { openSidebar } from './sidebar';
+
+/**
+ * Escape walks back one level at a time in the Settings dialog: the first press closes an open
+ * dropdown list and leaves Settings where it was, the second closes Settings.
+ */
+test('Escape in a Settings dropdown closes the list before the dialog @scenario:settings-dropdown-escape', async ({
+ page,
+}) => {
+ await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
+ /** Mobile keeps the account menu in the drawer. */
+ await openSidebar(page);
+ await page.getByTestId('nav-user').filter({ visible: true }).first().click();
+ await page.getByRole('menuitem', { name: 'Settings' }).click();
+
+ const dialog = page.getByRole('dialog');
+ const clock = dialog.getByRole('combobox', { name: /clock/i }).first();
+ await dialog.getByRole('tab', { name: /general/i }).click();
+ await expect(clock).toBeVisible();
+
+ await clock.click();
+ const listbox = page.locator('[role="listbox"]:not([hidden])');
+ await expect(listbox).toBeVisible();
+
+ await page.keyboard.press('Escape');
+ await expect(listbox).toBeHidden();
+ await expect(clock).toBeVisible();
+ await expect(clock).toBeFocused();
+
+ await page.keyboard.press('Escape');
+ await expect(clock).toBeHidden();
+});
diff --git a/packages/client/src/components/Dropdown.tsx b/packages/client/src/components/Dropdown.tsx
index 4f751b072b..974d48fbd4 100644
--- a/packages/client/src/components/Dropdown.tsx
+++ b/packages/client/src/components/Dropdown.tsx
@@ -252,6 +252,17 @@ const Dropdown: React.FC<DropdownProps> = ({
portal={portal ?? (portalElement != null || !inDialog)}
portalElement={portalElement}
store={selectProps}
+ /* Escape belongs to the list while it is open. Marking it handled is how a container
+ that also closes on Escape, such as a Headless UI dialog, knows to stay open. A list
+ still animating closed after a pick is no longer open, so that Escape is left for
+ the container. */
+ hideOnEscape={(event) => {
+ if (!selectProps.getState().open) {
+ return false;
+ }
+ event.preventDefault();
+ return true;
+ }}
/* Inside a dialog the list layers over it; elsewhere `z-40` applies. Pointer
events are already re-enabled by the class below. */
style={{
diff --git a/packages/client/src/components/__tests__/dropdownEscape.spec.tsx b/packages/client/src/components/__tests__/dropdownEscape.spec.tsx
new file mode 100644
index 0000000000..0eda097645
--- /dev/null
+++ b/packages/client/src/components/__tests__/dropdownEscape.spec.tsx
@@ -0,0 +1,77 @@
+import '@testing-library/jest-dom';
+import { Dialog, DialogPanel, DialogTitle } from '@headlessui/react';
+import { act, fireEvent, render, screen } from '@testing-library/react';
+import { DialogLayer } from '../OriginalDialog';
+import Dropdown from '../Dropdown';
+
+/**
+ * Escape in an open Dropdown list belongs to the list. Containers that close on Escape, such as
+ * a Headless UI dialog, skip an Escape something already handled, so the list closing must mark
+ * the keystroke handled or one press closes the list and its dialog together.
+ */
+function SettingsLikeDialog({ onClose }: { onClose: () => void }) {
+ return (
+ <Dialog open onClose={onClose}>
+ <DialogPanel>
+ <DialogTitle>Settings</DialogTitle>
+ <DialogLayer>
+ <Dropdown
+ value="system"
+ onChange={() => undefined}
+ ariaLabel="Clock format"
+ options={[
+ { value: 'system', label: 'System' },
+ { value: '12h', label: '12-hour' },
+ ]}
+ />
+ </DialogLayer>
+ </DialogPanel>
+ </Dialog>
+ );
+}
+
+it('closes only the list on Escape, leaving the dialog it sits in open', async () => {
+ const onClose = jest.fn();
+ render(<SettingsLikeDialog onClose={onClose} />);
+
+ await act(async () => {
+ fireEvent.click(screen.getByRole('combobox', { name: /Clock format/ }));
+ });
+ const listbox = await screen.findByRole('listbox');
+
+ await act(async () => {
+ fireEvent.keyDown(listbox, { key: 'Escape' });
+ });
+
+ expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
+ expect(onClose).not.toHaveBeenCalled();
+});
+
+it('still lets Escape close the dialog once the list is closed', async () => {
+ const onClose = jest.fn();
+ render(<SettingsLikeDialog onClose={onClose} />);
+
+ await act(async () => {
+ fireEvent.keyDown(screen.getByRole('combobox', { name: /Clock format/ }), { key: 'Escape' });
+ });
+
+ expect(onClose).toHaveBeenCalledTimes(1);
+});
+
+it('lets Escape close the dialog right after an option is picked', async () => {
+ const onClose = jest.fn();
+ render(<SettingsLikeDialog onClose={onClose} />);
+
+ await act(async () => {
+ fireEvent.click(screen.getByRole('combobox', { name: /Clock format/ }));
+ });
+ await act(async () => {
+ fireEvent.click(await screen.findByRole('option', { name: '12-hour' }));
+ });
+ /** The list is closing, not open: the Escape is the dialog's. */
+ await act(async () => {
+ fireEvent.keyDown(document.activeElement ?? document.body, { key: 'Escape' });
+ });
+
+ expect(onClose).toHaveBeenCalledTimes(1);
+});