diff --git a/e2e/navigationBarPopupLayout.spec.ts b/e2e/navigationBarPopupLayout.spec.ts new file mode 100644 index 000000000..6ae5dc2e3 --- /dev/null +++ b/e2e/navigationBarPopupLayout.spec.ts @@ -0,0 +1,138 @@ +import { expect, stubDesktop, test } from "./fixtures"; + +test("the floating navigation list stays beside the visible segment and scrolls to keyboard selection", async ({ + workbench: page, +}) => { + await page.setViewportSize({ width: 600, height: 560 }); + const folder = "billing"; + const parent = + "/dev/acme-api/quarterly-settlement-and-reconciliation/merchant-accounts-with-exchange-rate-adjustments"; + const directory = `${parent}/${folder}`; + const filename = "SettlementSummary.ts"; + const path = `${directory}/${filename}`; + const long = "QuarterlyReconciliationForPendingSettlementAndCrossBorderMerchantAdjustments.ts"; + const last = "YearEndTotals.ts"; + await page.evaluate(async (path) => { + const backend = "/src/editor/core/backend.ts"; + (await import(/* @vite-ignore */ backend)).docBackend().files.set(path, "export const pending = 1;\n"); + const service = "/src/workbench/contributions.ts"; + await (await import(/* @vite-ignore */ service)).editorService().openFile(path); + }, path); + await expect(page.locator(".cm-content")).toContainText("pending"); + await stubDesktop(page); + await page.evaluate( + async ({ directory, filename, long, last }) => { + window.__ipc.handlers.fs_read_dir = ({ path }) => + path === directory + ? ["archive", long, ...Array.from({ length: 24 }, (_, i) => `Report-${i + 1}.ts`), filename, last].map( + (name) => ({ + name, + path: `${directory}/${name}`, + kind: name === "archive" ? "dir" : "file", + size: name === "archive" ? 0 : 1, + }), + ) + : []; + const actions = "/src/platform/actions.ts"; + await (await import(/* @vite-ignore */ actions)).executeAction("navigate.navigationBar"); + }, + { directory, filename, long, last }, + ); + const bar = page.getByRole("toolbar", { name: "Navigation bar" }); + await expect(bar).toBeVisible(); + await page.keyboard.press("ArrowLeft"); + const segment = bar.getByRole("button", { name: folder, exact: true }); + await expect(segment).toHaveAttribute("aria-current", "location"); + const barBounds = (await bar.boundingBox())!; + await page.keyboard.press("ArrowDown"); + const list = page.getByRole("listbox", { name: "Children" }); + await expect(list).toBeVisible(); + await expect(list.getByRole("option", { name: filename, exact: true })).toHaveAttribute("aria-selected", "true"); + await expect + .poll(async () => { + const bounds = (await list.boundingBox())!; + return bounds.x >= 5 && bounds.x + bounds.width <= 595; + }) + .toBe(true); + expect((await list.boundingBox())!.y).toBeGreaterThanOrEqual(barBounds.y + barBounds.height - 4); + expect((await bar.boundingBox())!.height).toBeCloseTo(barBounds.height); + + await page.keyboard.press("Home"); + const folderRow = list.getByRole("option", { name: "archive" }); + await expect(folderRow).toHaveAttribute("aria-selected", "true"); + await expect(folderRow).toBeInViewport({ ratio: 1 }); + await expect(list.getByRole("option", { name: long, exact: true }).getByTitle(long)).toBeVisible(); + expect(await list.evaluate((el) => el.scrollWidth - el.clientWidth)).toBeLessThanOrEqual(1); + await page.keyboard.press("End"); + const lastRow = list.getByRole("option", { name: last, exact: true }); + await expect(lastRow).toHaveAttribute("aria-selected", "true"); + await expect(lastRow).toBeInViewport({ ratio: 1 }); + await page.keyboard.press("Escape"); + await expect(list).toBeHidden(); + await expect(bar).toBeFocused(); + await page.keyboard.press("Escape"); + await expect(bar).toBeHidden(); +}); + +for (const position of ["top", "statusBar"] as const) { + test(`the ${position} navigation list opens toward the editor and keeps folder navigation`, async ({ + workbench: page, + }) => { + const directory = "/dev/acme-api/src"; + const path = `${directory}/Overview.ts`; + await page.evaluate(async (path) => { + const backend = "/src/editor/core/backend.ts"; + (await import(/* @vite-ignore */ backend)).docBackend().files.set(path, "export const overview = 1;\n"); + const service = "/src/workbench/contributions.ts"; + await (await import(/* @vite-ignore */ service)).editorService().openFile(path); + }, path); + await expect(page.locator(".cm-content")).toContainText("overview"); + await stubDesktop(page); + await page.evaluate( + async ({ directory, position }) => { + const archive = `${directory}/archive`; + window.__ipc.handlers.fs_read_dir = ({ path }) => + path === directory + ? [ + { path: archive, name: "archive", kind: "dir", size: 0 }, + { path: `${directory}/Overview.ts`, name: "Overview.ts", kind: "file", size: 1 }, + ] + : path === archive + ? [{ path: `${archive}/Previous.ts`, name: "Previous.ts", kind: "file", size: 1 }] + : []; + const source = await (await fetch("/src/workbench/Workbench.tsx")).text(); + const project = source.match(/["'](\/src\/platform\/project\.ts(?:\?[^"']+)?)["']/)?.[1]; + if (!project) throw new Error("Missing project store"); + const { useProject } = await import(/* @vite-ignore */ project); + const settings = useProject.getState().settings; + useProject.setState({ + settings: { ...settings, appearance: { ...settings?.appearance, navigationBar: position } }, + }); + }, + { directory, position }, + ); + const anchor = page + .getByRole("navigation", { name: "Breadcrumbs" }) + .getByRole("button", { name: "src", exact: true }); + await anchor.click(); + const list = page.getByRole("listbox", { name: "src", exact: true }); + const archive = list.getByRole("option", { name: "archive" }); + await expect(archive).toHaveAttribute("aria-selected", "true"); + await expect(list).toBeFocused(); + await expect + .poll(async () => { + const bounds = (await anchor.boundingBox())!; + const popup = (await list.boundingBox())!; + return position === "top" ? popup.y >= bounds.y + bounds.height : popup.y + popup.height <= bounds.y; + }) + .toBe(true); + await page.keyboard.press("ArrowRight"); + const nested = page.getByRole("listbox", { name: "archive" }); + await expect(nested.getByRole("option", { name: "Previous.ts" })).toHaveAttribute("aria-selected", "true"); + await page.keyboard.press("ArrowLeft"); + await expect(archive).toHaveAttribute("aria-selected", "true"); + await page.keyboard.press("Escape"); + await expect(list).toBeHidden(); + await expect(anchor).toBeFocused(); + }); +} diff --git a/src/editor/core/view/NavBarPopup.module.css b/src/editor/core/view/NavBarPopup.module.css index 603ad112b..ac5db0408 100644 --- a/src/editor/core/view/NavBarPopup.module.css +++ b/src/editor/core/view/NavBarPopup.module.css @@ -1,18 +1,22 @@ -.popup { - max-width: min(90vw, 900px); - padding: 0; +.barPopup, +.childPopup { + width: max-content; + overflow: hidden; + font-size: var(--fs-chrome); } .root { outline: none; display: flex; flex-direction: column; + max-width: 900px; } .bar { display: flex; align-items: center; - gap: 0; - padding: 3px 4px; + min-width: 0; + padding: var(--space-1); overflow-x: auto; + scrollbar-width: thin; white-space: nowrap; } .segmentWrap { @@ -22,20 +26,20 @@ } .sep { color: var(--fg-faint); - margin: 0 1px; + margin: 0 var(--space-0-5); } .segment { display: inline-flex; align-items: center; - gap: 5px; + gap: var(--space-1); + max-width: min(288px, 55vw); height: var(--h-control-small); - padding: 0 5px; + padding: 0 var(--space-1-5); border: none; border-radius: var(--radius-small); background: transparent; color: var(--fg); font: inherit; - font-size: var(--fs-ui); cursor: default; } .segment:hover { @@ -46,22 +50,28 @@ background: var(--bg-list-selected); color: var(--fg-strong); } +.childPopup { + display: flex; + flex-direction: column; + min-width: min(200px, calc(100vw - 12px)); +} .list { - align-self: flex-start; - min-width: 200px; - max-height: 360px; - overflow-y: auto; - margin: 0 4px 4px; - padding: 4px; - border-top: 1px solid var(--border-popup); + min-height: 0; + min-width: 0; + max-width: min(480px, calc(100vw - 14px)); + max-height: min(360px, 62vh); + padding: var(--space-1); + overflow: auto; + overflow-x: hidden; + outline: none; } .row { display: flex; align-items: center; - gap: var(--space-1-5); + gap: var(--space-1); height: var(--h-list-row); - padding: 0 6px; - border-radius: var(--radius-row); + padding: 0 var(--space-2); + border-radius: var(--radius-selection); color: var(--fg); white-space: nowrap; } @@ -70,31 +80,31 @@ color: var(--fg-strong); } .rowLabel { + min-width: 0; overflow: hidden; text-overflow: ellipsis; + white-space: nowrap; +} +.rowIcon { + flex: none; } .empty { padding: 4px 6px; color: var(--fg-muted); } -.childList { - min-width: 200px; - max-height: 360px; - overflow-y: auto; - padding: 4px; - outline: none; -} .childHeader { display: flex; + flex: none; align-items: center; - gap: var(--space-1-5); - height: var(--h-list-row); - padding: 0 6px; - margin-bottom: 2px; + gap: var(--space-1); + max-width: min(480px, calc(100vw - 14px)); + height: calc(var(--h-list-row) + var(--space-2)); + padding: 0 var(--space-3); border-bottom: 1px solid var(--border-popup); color: var(--fg-muted); } .rowChevron { + flex: none; margin-left: auto; color: var(--fg-faint); } diff --git a/src/editor/core/view/NavBarPopup.tsx b/src/editor/core/view/NavBarPopup.tsx index 496afcda7..254c44b76 100644 --- a/src/editor/core/view/NavBarPopup.tsx +++ b/src/editor/core/view/NavBarPopup.tsx @@ -4,13 +4,13 @@ * (folder contents, file / symbol members); Enter on a child opens it (a * folder becomes the next segment), Escape closes the list, then the bar. */ -import { useEffect, useLayoutEffect, useMemo, useRef, useState, type KeyboardEvent } from "react"; +import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type KeyboardEvent } from "react"; import clsx from "clsx"; import { create } from "zustand"; import { call, commands, isTauri, type OutlineItem } from "@/platform/ipc"; import { projectFolder, useProject } from "@/platform/project"; import { useProjectViewLead } from "@/workbench/contributions"; -import { Icon, Popup, T } from "@/ui"; +import { Icon, listNavigate, Popup, revealHorizontally, revealVertically, T } from "@/ui"; import { editorController, useEditorStatus } from "../controller"; import { useOutline } from "../model/document"; import { symbolChain } from "./outline"; @@ -64,6 +64,74 @@ function anchorPoint(): { x: number; y: number } { return r ? { x: r.left + 8, y: r.top + 4 } : { x: 80, y: 80 }; } +function NavChildList({ + label, + id, + items, + active, + onSelect, + onChoose, + onKeyDown, +}: { + label: string; + id: string; + items: readonly NavChild[] | null; + active: number; + onSelect: (index: number) => void; + onChoose: (child: NavChild) => void; + onKeyDown?: (e: KeyboardEvent) => void; +}) { + const ref = useRef(null); + useLayoutEffect(() => { + const scroller = ref.current; + const row = scroller?.querySelector('[aria-selected="true"]'); + if (!scroller || !row) return; + const reveal = () => revealVertically(scroller, row); + reveal(); + if (typeof ResizeObserver === "undefined") return; + const resize = new ResizeObserver(reveal); + resize.observe(scroller); + return () => resize.disconnect(); + }, [active, items]); + + return ( +
+ {items?.length === 0 && ( +
+ Nothing here +
+ )} + {items?.map((child, i) => ( +
onSelect(i)} + onClick={() => onChoose(child)} + > + + + {child.label} + + {child.kind === "dir" && } +
+ ))} +
+ ); +} + function NavBar({ path, line, col, directory }: { path: string; line: number; col: number; directory: boolean }) { const project = useProject((s) => s.project); const outline = useOutline((s) => s[path]); @@ -77,11 +145,19 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co const [active, setActive] = useState(() => Math.max(0, directory ? initial.length - 1 : initial.findIndex((s) => s.kind === "file")), ); - const [list, setList] = useState<{ seg: number; items: NavChild[]; active: number; left: number } | null>(null); + const [list, setList] = useState<{ seg: number; items: NavChild[]; active: number } | null>(null); + const [childAnchor, setChildAnchor] = useState(null); + const listId = useId(); const [anchor] = useState(anchorPoint); + const strip = useRef(null); const buttons = useRef>([]); - const listRef = useRef(null); const request = useRef(0); + const listSegment = list?.seg; + + const closeList = () => { + request.current++; + setList(null); + }; const openList = async (index: number, segs = segments) => { const seg = segs[index]; @@ -92,7 +168,7 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co // Start on the child that leads to the next segment, like IDEA. const next = segs[index + 1]; const at = next ? items.findIndex((c) => c.path === next.path && c.label === next.label) : -1; - setList({ seg: index, items, active: Math.max(0, at), left: buttons.current[index]?.offsetLeft ?? 0 }); + setList({ seg: index, items, active: Math.max(0, at) }); }; const choose = (child: NavChild) => { @@ -125,15 +201,14 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co let handled = true; if (e.key === "ArrowLeft") move(-1); else if (e.key === "ArrowRight") move(1); - else if (e.key === "ArrowDown" && !list) void openList(active); - else if (e.key === "ArrowDown" && list) - setList({ ...list, active: Math.min(list.items.length - 1, list.active + 1) }); - else if (e.key === "ArrowUp" && list) setList({ ...list, active: Math.max(0, list.active - 1) }); - else if (e.key === "Enter" && list) { - const child = list.items[list.active]; - if (child) choose(child); - } else if (e.key === "Enter") void openList(active); - else if (e.key === "Escape" && list) setList(null); + else if (list) { + const next = listNavigate(e.key, list); + if (next !== null) setList({ ...list, active: next }); + else if (e.key === "Enter") { + const child = list.items[list.active]; + if (child) choose(child); + } else handled = false; + } else if (e.key === "ArrowDown" || e.key === "Enter") void openList(active); else handled = false; if (handled) { e.preventDefault(); @@ -142,11 +217,19 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co }; useLayoutEffect(() => { - buttons.current[active]?.scrollIntoView?.({ block: "nearest", inline: "nearest" }); - }, [active]); - useEffect(() => { - listRef.current?.querySelector('[aria-selected="true"]')?.scrollIntoView?.({ block: "nearest" }); - }, [list]); + const row = strip.current; + const current = buttons.current[active]; + if (!row || !current) return; + const reveal = () => revealHorizontally(row, current); + reveal(); + if (typeof ResizeObserver === "undefined") return; + const resize = new ResizeObserver(reveal); + resize.observe(row); + return () => resize.disconnect(); + }, [active, segments]); + useLayoutEffect(() => { + setChildAnchor(listSegment === undefined ? null : (buttons.current[listSegment] ?? null)); + }, [listSegment, segments]); return (
-
+
{segments.map((s, i) => ( {i > 0 && } @@ -176,44 +259,39 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co tabIndex={-1} className={clsx(styles.segment, i === active && styles.active)} aria-current={i === active ? "location" : undefined} + aria-haspopup="listbox" + aria-expanded={i === listSegment} + aria-controls={i === listSegment ? listId : undefined} + title={s.label} onClick={() => { setActive(i); void openList(i); }} > - - {s.label} + + {s.label} ))}
- {list && ( -
- {list.items.length === 0 && ( -
- Nothing here -
- )} - {list.items.map((c, i) => ( -
setList({ ...list, active: i })} - onClick={() => choose(c)} - > - - {c.label} -
- ))} -
+ setList({ ...list, active: i })} + onChoose={choose} + /> + )}
@@ -228,10 +306,12 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co export function NavChildrenPopup({ segment, anchor, + openAbove = true, onClose, }: { segment: NavSegment; anchor: HTMLElement; + openAbove?: boolean; onClose: () => void; }) { const [stack, setStack] = useState([segment]); @@ -239,7 +319,7 @@ export function NavChildrenPopup({ const outline = useOutline((s) => s[top.path]); const [loaded, setLoaded] = useState<{ top: NavSegment; items: NavChild[] } | null>(null); const [active, setActive] = useState(0); - const listRef = useRef(null); + const listId = useId(); const items = loaded?.top === top ? loaded.items : null; useEffect(() => { @@ -255,10 +335,6 @@ export function NavChildrenPopup({ alive = false; }; }, [top, outline]); - useEffect(() => { - listRef.current?.querySelector('[aria-selected="true"]')?.scrollIntoView?.({ block: "nearest" }); - }, [active, items]); - const choose = (child: NavChild) => { if (child.kind === "dir") return setStack((s) => [...s, { kind: "dir", path: child.path, label: child.label }]); onClose(); @@ -270,10 +346,9 @@ export function NavChildrenPopup({ }; const onKeyDown = (e: KeyboardEvent) => { - const n = items?.length ?? 0; + const next = items ? listNavigate(e.key, { items, active, wrap: true }) : null; let handled = true; - if (e.key === "ArrowDown" && n) setActive((a) => (a + 1) % n); - else if (e.key === "ArrowUp" && n) setActive((a) => (a - 1 + n) % n); + if (next !== null) setActive(next); else if (e.key === "Enter" || (e.key === "ArrowRight" && items?.[active]?.kind === "dir")) { const child = items?.[active]; if (child) choose(child); @@ -286,44 +361,31 @@ export function NavChildrenPopup({ }; return ( - -
+ {stack.length > 1 && ( +
+ + + {top.label} + +
+ )} + - {stack.length > 1 && ( -
- - {top.label} -
- )} - {items?.length === 0 && ( -
- Nothing here -
- )} - {items?.map((c, i) => ( -
setActive(i)} - onClick={() => choose(c)} - > - - {c.label} - {c.kind === "dir" && } -
- ))} -
+ />
); } diff --git a/src/editor/core/view/StatusItems.tsx b/src/editor/core/view/StatusItems.tsx index 51a896e44..ffcef4136 100644 --- a/src/editor/core/view/StatusItems.tsx +++ b/src/editor/core/view/StatusItems.tsx @@ -226,7 +226,15 @@ function BreadcrumbStrip({ } /** A row of crumbs; a segment with `nav` opens a popup of its children. */ -function CrumbNav({ segments, className }: { segments: Crumb[]; className?: string }) { +function CrumbNav({ + segments, + className, + inStatusBar, +}: { + segments: Crumb[]; + className?: string; + inStatusBar: boolean; +}) { const [popup, setPopup] = useState<{ nav: NavSegment; anchor: HTMLElement } | null>(null); return ( ); })} - {popup && setPopup(null)} />} + {popup && ( + setPopup(null)} + /> + )} ); } @@ -311,7 +327,7 @@ export function Breadcrumbs({ className, inStatusBar = false }: { className?: st segments.push(projectCrumb(project ?? currentProject)); } if (!segments.length) return null; - return ; + return ; } /** The status bar instance of the navigation bar; renders only for `statusBar`. */