Skip to content

Commit 9d6a2e5

Browse files
t3dotggclaude
andauthored
fix(web): new thread shortcut focuses palette search, not the Back button (#25315)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent beeb215 commit 9d6a2e5

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { useState } from "react";
2+
import { describe, expect, it } from "vite-plus/test";
3+
import { page, userEvent } from "vite-plus/test/browser";
4+
5+
import { render } from "../../test/render";
6+
import { CommandPaletteContent } from "./CommandPaletteContent";
7+
import { CommandDialog, CommandDialogPopup, CommandItem, CommandList } from "./ui/command";
8+
9+
function SubmenuPalette() {
10+
const [open, setOpen] = useState(false);
11+
return (
12+
<>
13+
<button type="button" onClick={() => setOpen(true)}>
14+
Open
15+
</button>
16+
<CommandDialog open={open} onOpenChange={setOpen}>
17+
<CommandDialogPopup aria-label="Command palette">
18+
<CommandPaletteContent
19+
items={["Project A"]}
20+
inputProps={{
21+
placeholder: "Search projects",
22+
startAddon: (
23+
<button type="button" aria-label="Back">
24+
Back
25+
</button>
26+
),
27+
}}
28+
>
29+
<CommandList>
30+
{(item: string) => <CommandItem value={item}>{item}</CommandItem>}
31+
</CommandList>
32+
</CommandPaletteContent>
33+
</CommandDialogPopup>
34+
</CommandDialog>
35+
</>
36+
);
37+
}
38+
39+
describe("CommandPaletteContent", () => {
40+
it("focuses the search input, not the Back button, when it opens into a submenu", async () => {
41+
await using _screen = await render(<SubmenuPalette />);
42+
43+
await userEvent.click(page.getByRole("button", { name: "Open" }));
44+
45+
await expect.element(page.getByPlaceholder("Search projects")).toHaveFocus();
46+
});
47+
});

‎apps/web/src/components/ui/command.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Dialog as CommandDialogPrimitive } from "@base-ui/react/dialog";
22
import { SearchIcon } from "lucide-react";
3+
import { useRef } from "react";
34
import type * as React from "react";
45
import { cn } from "~/lib/utils";
56
import {
@@ -55,11 +56,15 @@ function CommandDialogPopup({
5556
}: CommandDialogPrimitive.Popup.Props & {
5657
onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
5758
}) {
59+
const popupRef = useRef<HTMLDivElement>(null);
5860
return (
5961
<CommandDialogPortal>
6062
<CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
6163
<CommandDialogViewport>
6264
<CommandDialogPrimitive.Popup
65+
// The first tabbable can be an input addon, such as a submenu's Back button.
66+
initialFocus={() => popupRef.current?.querySelector("input") ?? true}
67+
ref={popupRef}
6368
className={cn(
6469
DIALOG_POPUP_CLASS,
6570
"pointer-events-auto max-h-105 max-w-xl text-foreground",

0 commit comments

Comments
 (0)