diff --git a/apps/web/src/components/ProjectPickerMenu.browser.tsx b/apps/web/src/components/ProjectPickerMenu.browser.tsx index 25200ae46..e3b03243d 100644 --- a/apps/web/src/components/ProjectPickerMenu.browser.tsx +++ b/apps/web/src/components/ProjectPickerMenu.browser.tsx @@ -31,7 +31,7 @@ const PROJECTS = [ { name: "scratch", cwd: "/work/scratch" }, ].map((project) => ({ ...project, key: `${ENVIRONMENT_ID}:${project.cwd}` })); -function Harness(props: { onPick: (key: string) => void }) { +function Harness(props: { onPick: (key: string) => void; trailing?: boolean }) { const projectOrder = useUiStateStore((store) => store.projectOrder); const pinnedProjectCwds = useUiStateStore((store) => store.pinnedProjectCwds); const sortOrder = useSettings((settings) => settings.sidebarProjectSortOrder); @@ -52,12 +52,26 @@ function Harness(props: { onPick: (key: string) => void }) { orderKeys: [project.key], cwds: [project.cwd], selected: false, + trailing: props.trailing ? ( + + ) : undefined, }))} onPick={(row) => props.onPick(row.key)} /> ); } +async function waitForProductionStyles(): Promise { + await vi.waitFor( + () => { + expect( + getComputedStyle(document.documentElement).getPropertyValue("--background").trim(), + ).not.toBe(""); + }, + { timeout: 4_000, interval: 16 }, + ); +} + function shownNames(): string[] { return Array.from(document.querySelectorAll("[data-project-picker-row]")).map( (row) => @@ -65,11 +79,14 @@ function shownNames(): string[] { ); } -function renderHarness(onPick: (key: string) => void = vi.fn()) { +function renderHarness( + onPick: (key: string) => void = vi.fn(), + options?: { readonly trailing?: boolean }, +) { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); return render( - + , ); } @@ -148,4 +165,41 @@ describe("ProjectPickerMenu", () => { await mounted.unmount(); } }); + + it("swapping the left icon for the pin on hover never shifts menu width or trailing glyphs, and unpins pinned rows", async () => { + await waitForProductionStyles(); + const mounted = await renderHarness(vi.fn(), { trailing: true }); + try { + await page.getByTestId("picker-trigger").click(); + const popup = document.querySelector('[data-slot="menu-popup"]'); + expect(popup).not.toBeNull(); + const trailing = document.querySelector('[data-testid="trailing-gateway"]'); + expect(trailing).not.toBeNull(); + + const restWidth = popup!.getBoundingClientRect().width; + const restTrailingLeft = trailing!.getBoundingClientRect().left; + + // Hover the widest row: the pin replaces the left icon in place. + await userEvent.hover(page.getByRole("menuitem", { name: "gateway" })); + const pinButton = page.getByRole("button", { name: "Pin gateway to the top" }); + await expect.element(pinButton).toBeVisible(); + expect(popup!.getBoundingClientRect().width).toBe(restWidth); + expect(trailing!.getBoundingClientRect().left).toBe(restTrailingLeft); + + // Pinning moves it under "Pinned", and hovering there reveals Unpin without shifting width. + await pinButton.click(); + await expect.element(page.getByText("Pinned", { exact: true })).toBeVisible(); + expect(useUiStateStore.getState().pinnedProjectCwds).toEqual(["/work/apis/gateway"]); + + await userEvent.hover(page.getByRole("menuitem", { name: "gateway" })); + const unpinButton = page.getByRole("button", { name: "Unpin gateway" }); + await expect.element(unpinButton).toBeVisible(); + expect(popup!.getBoundingClientRect().width).toBe(restWidth); + + await unpinButton.click(); + expect(useUiStateStore.getState().pinnedProjectCwds).toEqual([]); + } finally { + await mounted.unmount(); + } + }); }); diff --git a/apps/web/src/components/ProjectPickerMenu.tsx b/apps/web/src/components/ProjectPickerMenu.tsx index d595e4b72..e7c7ed840 100644 --- a/apps/web/src/components/ProjectPickerMenu.tsx +++ b/apps/web/src/components/ProjectPickerMenu.tsx @@ -93,6 +93,7 @@ export function ProjectPickerMenu(props: { const { rows } = props; const [query, setQuery] = useState(""); const isCoarsePointer = useMediaQuery({ pointer: "coarse" }); + const canHoverPin = useMediaQuery("(hover: hover) and (pointer: fine)"); const showSearch = !isCoarsePointer && rows.length >= PROJECT_SEARCH_MIN_PROJECTS; const searching = showSearch && query.trim().length > 0; @@ -206,6 +207,40 @@ export function ProjectPickerMenu(props: { const pinned = isProjectPinned(row.cwds, pinnedProjectCwds); const visual = dragReorderVisual(drag, row.key); const folderHint = searching && !matchesName(row) ? folderTail(row.cwd) : null; + const pinButton = (placement: "overlay" | "trailing") => ( + + ); + return ( ) : null} - + + + {canHoverPin ? pinButton("overlay") : null} + {row.name} {folderHint ? ( @@ -259,36 +302,7 @@ export function ProjectPickerMenu(props: { ) : null} {row.trailing} - + {!canHoverPin ? pinButton("trailing") : null} ); };