Skip to content
TypeScript

fix(web): new thread shortcut focuses palette search, not the Back button (#25315)

Theo Browne9d6a2e57fce3
diff --git a/apps/web/src/components/CommandPaletteContent.browser.test.tsx b/apps/web/src/components/CommandPaletteContent.browser.test.tsx
new file mode 100644
index 00000000000..abeef942c07
--- /dev/null
+++ b/apps/web/src/components/CommandPaletteContent.browser.test.tsx
@@ -0,0 +1,47 @@
+import { useState } from "react";
+import { describe, expect, it } from "vite-plus/test";
+import { page, userEvent } from "vite-plus/test/browser";
+
+import { render } from "../../test/render";
+import { CommandPaletteContent } from "./CommandPaletteContent";
+import { CommandDialog, CommandDialogPopup, CommandItem, CommandList } from "./ui/command";
+
+function SubmenuPalette() {
+ const [open, setOpen] = useState(false);
+ return (
+ <>
+ <button type="button" onClick={() => setOpen(true)}>
+ Open
+ </button>
+ <CommandDialog open={open} onOpenChange={setOpen}>
+ <CommandDialogPopup aria-label="Command palette">
+ <CommandPaletteContent
+ items={["Project A"]}
+ inputProps={{
+ placeholder: "Search projects",
+ startAddon: (
+ <button type="button" aria-label="Back">
+ Back
+ </button>
+ ),
+ }}
+ >
+ <CommandList>
+ {(item: string) => <CommandItem value={item}>{item}</CommandItem>}
+ </CommandList>
+ </CommandPaletteContent>
+ </CommandDialogPopup>
+ </CommandDialog>
+ </>
+ );
+}
+
+describe("CommandPaletteContent", () => {
+ it("focuses the search input, not the Back button, when it opens into a submenu", async () => {
+ await using _screen = await render(<SubmenuPalette />);
+
+ await userEvent.click(page.getByRole("button", { name: "Open" }));
+
+ await expect.element(page.getByPlaceholder("Search projects")).toHaveFocus();
+ });
+});
diff --git a/apps/web/src/components/ui/command.tsx b/apps/web/src/components/ui/command.tsx
index 650b875fd49..60daa212dae 100644
--- a/apps/web/src/components/ui/command.tsx
+++ b/apps/web/src/components/ui/command.tsx
@@ -1,5 +1,6 @@
import { Dialog as CommandDialogPrimitive } from "@base-ui/react/dialog";
import { SearchIcon } from "lucide-react";
+import { useRef } from "react";
import type * as React from "react";
import { cn } from "~/lib/utils";
import {
@@ -55,11 +56,15 @@ function CommandDialogPopup({
}: CommandDialogPrimitive.Popup.Props & {
onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
}) {
+ const popupRef = useRef<HTMLDivElement>(null);
return (
<CommandDialogPortal>
<CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
<CommandDialogViewport>
<CommandDialogPrimitive.Popup
+ // The first tabbable can be an input addon, such as a submenu's Back button.
+ initialFocus={() => popupRef.current?.querySelector("input") ?? true}
+ ref={popupRef}
className={cn(
DIALOG_POPUP_CLASS,
"pointer-events-auto max-h-105 max-w-xl text-foreground",