Skip to content

Commit fa6326b

Browse files
fix(web): closing a composer menu on a touch tablet no longer opens the keyboard (#36325)
1 parent 3521634 commit fa6326b

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

‎apps/web/src/components/chat/ComposerPermissionsSection.browser.test.tsx‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { render } from "../../../test/render";
77
import { ComposerHandleContext, type ComposerHandleRef } from "../../composerHandleContext";
88
import { ComposerContextPermissions } from "../ComposerContextPermissions";
99
import { Menu, MenuPopup, MenuTrigger } from "../ui/menu";
10+
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
1011
import {
1112
focusRequestedPermission,
1213
markOpenToPermissions,
1314
useComposerMenuProps,
1415
} from "./composerEventScope";
1516
import { ComposerFooterModeControls, ComposerPermissionsSection } from "./ComposerFooterControls";
17+
import { ComposerPickerFooter } from "./ComposerModelEffort";
1618
import { runtimeModeConfig, runtimeModeOptions } from "./runtimeModeConfig";
1719

1820
const options = runtimeModeOptions.map((mode) => ({ mode, ...runtimeModeConfig[mode] }));
@@ -95,6 +97,49 @@ it("returns focus to the editor after a pick", async () => {
9597
await vi.waitFor(() => expect(handle.focusAtEnd).toHaveBeenCalled());
9698
});
9799

100+
// Mirrors the model picker: a composer popover whose footer nests the Permissions menu.
101+
function PickerWithPermissions() {
102+
const [mode, setMode] = useState<RuntimeMode>("full-access");
103+
const composerMenuProps = useComposerMenuProps();
104+
return (
105+
<>
106+
<p>Timeline</p>
107+
<Popover>
108+
<PopoverTrigger>Model</PopoverTrigger>
109+
<PopoverPopup {...composerMenuProps}>
110+
<ComposerPickerFooter
111+
traits={null}
112+
runtimeMode={mode}
113+
runtimeModeOptions={options}
114+
onRuntimeModeChange={setMode}
115+
/>
116+
</PopoverPopup>
117+
</Popover>
118+
</>
119+
);
120+
}
121+
122+
it("on a touch tablet, tapping outside the Permissions menu leaves the editor unfocused", async () => {
123+
const realMatchMedia = window.matchMedia.bind(window);
124+
using _touch = vi
125+
.spyOn(window, "matchMedia")
126+
.mockImplementation((query) => realMatchMedia(query === "(pointer: coarse)" ? "all" : query));
127+
const { handle, ui } = withComposer(<PickerWithPermissions />);
128+
await using screen = await render(ui, { router: false }); // no routes involved
129+
await userEvent.click(screen.getByRole("button", { name: "Model" }));
130+
await userEvent.click(page.getByRole("button", { name: "Permissions: Full access" }));
131+
const fullAccess = page.getByRole("menuitemradio", { name: "Full access" });
132+
await expect.element(fullAccess).toBeInTheDocument();
133+
const permissionsTrigger = page.getByRole("button", { name: "Permissions: Full access" });
134+
// The first outside tap closes the Permissions menu, the second the picker around it.
135+
await userEvent.click(document.body, { position: { x: 1, y: 1 } });
136+
await expect.element(fullAccess).not.toBeInTheDocument();
137+
await userEvent.click(screen.getByText("Timeline"));
138+
await expect.element(permissionsTrigger).not.toBeInTheDocument();
139+
// Focusing the editor after a tap opens the on-screen keyboard on Android tablets.
140+
expect(handle.focusAtEnd).not.toHaveBeenCalled();
141+
});
142+
98143
it("opens on the checked permission when composer.mode marked the trigger", async () => {
99144
await using screen = await render(<OverflowMenu />, { router: false }); // no routes involved
100145
const trigger = screen.getByRole("button", { name: "More" });

‎apps/web/src/components/chat/composerEventScope.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useComposerHandleContext } from "../../composerHandleContext";
2+
import { useMediaQuery } from "../../hooks/useMediaQuery";
23

34
const COMPOSER_FLOATING_LAYER_SELECTOR = [
45
'[data-composer-drawer-layer="true"]',
@@ -11,19 +12,22 @@ export const composerFloatingLayerProps = {
1112

1213
export function useComposerMenuProps() {
1314
const composerRef = useComposerHandleContext();
15+
const isCoarsePointer = useMediaQuery({ pointer: "coarse" });
1416

1517
return {
1618
...composerFloatingLayerProps,
17-
finalFocus: composerRef
18-
? () => {
19-
const activeElement = document.activeElement;
20-
if (activeElement !== document.body && !isInsideComposerFloatingLayer(activeElement)) {
19+
// Focusing the editor on a touch device opens the on-screen keyboard, so the menu keeps its own focus return.
20+
finalFocus:
21+
composerRef && !isCoarsePointer
22+
? () => {
23+
const activeElement = document.activeElement;
24+
if (activeElement !== document.body && !isInsideComposerFloatingLayer(activeElement)) {
25+
return false;
26+
}
27+
composerRef.current?.focusAtEnd();
2128
return false;
2229
}
23-
composerRef.current?.focusAtEnd();
24-
return false;
25-
}
26-
: undefined,
30+
: undefined,
2731
};
2832
}
2933

0 commit comments

Comments
 (0)