Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions apps/web/src/components/ProjectPickerMenu.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -52,24 +52,41 @@ function Harness(props: { onPick: (key: string) => void }) {
orderKeys: [project.key],
cwds: [project.cwd],
selected: false,
trailing: props.trailing ? (
<span data-testid={`trailing-${project.name}`} className="inline-block size-3 shrink-0" />
) : undefined,
}))}
onPick={(row) => props.onPick(row.key)}
/>
);
}

async function waitForProductionStyles(): Promise<void> {
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) =>
PROJECTS.find((project) => project.key === row.getAttribute("data-project-picker-row"))!.name,
);
}

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(
<QueryClientProvider client={queryClient}>
<Harness onPick={onPick} />
<Harness onPick={onPick} trailing={options?.trailing ?? false} />
</QueryClientProvider>,
);
}
Expand Down Expand Up @@ -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<HTMLElement>('[data-slot="menu-popup"]');
expect(popup).not.toBeNull();
const trailing = document.querySelector<HTMLElement>('[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();
}
});
});
76 changes: 45 additions & 31 deletions apps/web/src/components/ProjectPickerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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") => (
<button
type="button"
data-testid={`project-picker-pin-${row.key}`}
aria-label={pinned ? `Unpin ${row.name}` : `Pin ${row.name} to the top`}
aria-pressed={pinned}
title={pinned ? "Unpin (⌥P)" : "Pin to the top (⌥P)"}
className={cn(
"size-5 cursor-pointer items-center justify-center rounded-sm text-muted-foreground hover:bg-foreground/10 hover:text-foreground focus-ring",
placement === "overlay"
? // Swaps into the project icon's footprint on row hover so the menu
// width and trailing glyphs never shift. Keyboard users have ⌥P.
"absolute inset-1/2 hidden -translate-x-1/2 -translate-y-1/2 group-hover/project-row:inline-flex"
: "-me-1 inline-flex shrink-0",
)}
onKeyDown={keepActivationKeys}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
setProjectPinned(row.cwds, !pinned);
}}
>
{pinned ? (
placement === "overlay" ? (
<PinOffIcon className="size-3.5" />
) : (
<PinIcon className="size-3 text-foreground" />
)
) : (
<PinIcon className={placement === "overlay" ? "size-3.5" : "size-3"} />
)}
</button>
);

return (
<MenuItem
key={row.key}
Expand Down Expand Up @@ -251,44 +286,23 @@ export function ProjectPickerMenu(props: {
<GripVerticalIcon className="size-3.5" />
</span>
) : null}
<ProjectFavicon cwd={row.cwd} environmentId={row.environmentId} name={row.name} />
<span className="relative inline-flex size-3.5 shrink-0 items-center justify-center">
<ProjectFavicon
cwd={row.cwd}
environmentId={row.environmentId}
name={row.name}
className={cn(canHoverPin && "group-hover/project-row:opacity-0")}
/>
{canHoverPin ? pinButton("overlay") : null}
</span>
<span className="max-w-56 min-w-0 flex-1 truncate">{row.name}</span>
{folderHint ? (
<span className="max-w-32 shrink truncate font-mono text-[10px] text-muted-foreground">
{folderHint}
</span>
) : null}
{row.trailing}
<button
type="button"
data-testid={`project-picker-pin-${row.key}`}
aria-label={pinned ? `Unpin ${row.name}` : `Pin ${row.name} to the top`}
aria-pressed={pinned}
title={pinned ? "Unpin (⌥P)" : "Pin to the top (⌥P)"}
className={cn(
"-me-1 size-5 shrink-0 cursor-pointer items-center justify-center rounded-sm text-muted-foreground hover:bg-foreground/10 hover:text-foreground focus-ring",
// A pin shows where it holds; on other rows only under the pointer
// or the keyboard, and always on touch, which has no hover.
pinned
? "inline-flex"
: "hidden group-data-highlighted/project-row:inline-flex pointer-coarse:inline-flex",
)}
onKeyDown={keepActivationKeys}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
setProjectPinned(row.cwds, !pinned);
}}
>
{pinned ? (
<>
<PinIcon className="size-3 group-hover/project-row:hidden" />
<PinOffIcon className="hidden size-3 group-hover/project-row:block" />
</>
) : (
<PinIcon className="size-3" />
)}
</button>
{!canHoverPin ? pinButton("trailing") : null}
</MenuItem>
);
};
Expand Down
Loading