дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-27 13:06:07 +02:00
Keep navigation bar child lists within the viewport (#4740)
The floating navigation bar keeps its path visible while a segment's child list is open: the list anchors to that segment and scrolls within the viewport. Both navigation bar placements share compact row spacing and font size, keep full names on hover, and open toward the editor.
Цей коміт міститься в:
1 джерело
4ce05ff14e
коміт
2dd72d4c36
4 files changed
+352
-126
No files matched your search
@@ -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();
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<HTMLDivElement>(null);
|
||||
useLayoutEffect(() => {
|
||||
const scroller = ref.current;
|
||||
const row = scroller?.querySelector<HTMLElement>('[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 (
|
||||
<div
|
||||
ref={ref}
|
||||
id={id}
|
||||
className={styles.list}
|
||||
role="listbox"
|
||||
aria-label={label}
|
||||
aria-activedescendant={items?.[active] ? `${id}-${active}` : undefined}
|
||||
tabIndex={0}
|
||||
data-autofocus
|
||||
onKeyDown={onKeyDown}
|
||||
>
|
||||
{items?.length === 0 && (
|
||||
<div className={styles.empty}>
|
||||
<T>Nothing here</T>
|
||||
</div>
|
||||
)}
|
||||
{items?.map((child, i) => (
|
||||
<div
|
||||
key={`${child.path}:${child.label}:${i}`}
|
||||
id={`${id}-${i}`}
|
||||
role="option"
|
||||
aria-selected={i === active}
|
||||
className={clsx(styles.row, i === active && styles.rowActive)}
|
||||
onMouseEnter={() => onSelect(i)}
|
||||
onClick={() => onChoose(child)}
|
||||
>
|
||||
<Icon name={iconOf(child)} size={16} className={styles.rowIcon} />
|
||||
<span className={styles.rowLabel} title={child.label}>
|
||||
{child.label}
|
||||
</span>
|
||||
{child.kind === "dir" && <Icon name="chevron-right" size={12} className={styles.rowChevron} />}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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<HTMLButtonElement | null>(null);
|
||||
const listId = useId();
|
||||
const [anchor] = useState(anchorPoint);
|
||||
const strip = useRef<HTMLDivElement>(null);
|
||||
const buttons = useRef<Array<HTMLButtonElement | null>>([]);
|
||||
const listRef = useRef<HTMLDivElement>(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<HTMLElement>('[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 (
|
||||
<Popup
|
||||
@@ -156,7 +239,7 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co
|
||||
offset={0}
|
||||
onClose={close}
|
||||
aria-label="Navigation bar"
|
||||
className={styles.popup}
|
||||
className={styles.barPopup}
|
||||
>
|
||||
<div
|
||||
className={styles.root}
|
||||
@@ -166,7 +249,7 @@ function NavBar({ path, line, col, directory }: { path: string; line: number; co
|
||||
role="toolbar"
|
||||
aria-label={tx("Navigation bar")}
|
||||
>
|
||||
<div className={styles.bar}>
|
||||
<div className={styles.bar} ref={strip}>
|
||||
{segments.map((s, i) => (
|
||||
<span key={`${i}:${s.path}:${s.label}`} className={styles.segmentWrap}>
|
||||
{i > 0 && <Icon name="chevron-right" size={12} className={styles.sep} />}
|
||||
@@ -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);
|
||||
}}
|
||||
>
|
||||
<Icon name={iconOf(s)} size={14} />
|
||||
<span>{s.label}</span>
|
||||
<Icon name={iconOf(s)} size={14} className={styles.rowIcon} />
|
||||
<span className={styles.rowLabel}>{s.label}</span>
|
||||
</button>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
{list && (
|
||||
<div
|
||||
ref={listRef}
|
||||
className={styles.list}
|
||||
style={{ marginLeft: list.left }}
|
||||
role="listbox"
|
||||
{list && childAnchor && (
|
||||
<Popup
|
||||
open
|
||||
anchor={childAnchor}
|
||||
placement="bottom-start"
|
||||
onClose={closeList}
|
||||
aria-label={tx("Children")}
|
||||
className={styles.childPopup}
|
||||
>
|
||||
{list.items.length === 0 && (
|
||||
<div className={styles.empty}>
|
||||
<T>Nothing here</T>
|
||||
</div>
|
||||
)}
|
||||
{list.items.map((c, i) => (
|
||||
<div
|
||||
key={`${c.path}:${c.label}:${i}`}
|
||||
role="option"
|
||||
aria-selected={i === list.active}
|
||||
className={clsx(styles.row, i === list.active && styles.rowActive)}
|
||||
onMouseEnter={() => setList({ ...list, active: i })}
|
||||
onClick={() => choose(c)}
|
||||
>
|
||||
<Icon name={iconOf(c)} size={14} />
|
||||
<span className={styles.rowLabel}>{c.label}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<NavChildList
|
||||
id={listId}
|
||||
label={tx("Children")}
|
||||
items={list.items}
|
||||
active={list.active}
|
||||
onSelect={(i) => setList({ ...list, active: i })}
|
||||
onChoose={choose}
|
||||
/>
|
||||
</Popup>
|
||||
)}
|
||||
</div>
|
||||
</Popup>
|
||||
@@ -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<NavSegment[]>([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<HTMLDivElement>(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<HTMLElement>('[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 (
|
||||
<Popup open anchor={anchor} placement="top-start" onClose={onClose} aria-label={top.label} className={styles.popup}>
|
||||
<div
|
||||
ref={listRef}
|
||||
className={styles.childList}
|
||||
role="listbox"
|
||||
aria-label={top.label}
|
||||
tabIndex={0}
|
||||
data-autofocus
|
||||
aria-activedescendant={items?.length ? `navbar-child-${active}` : undefined}
|
||||
<Popup
|
||||
open
|
||||
anchor={anchor}
|
||||
placement={openAbove ? "top-start" : "bottom-start"}
|
||||
onClose={onClose}
|
||||
aria-label={top.label}
|
||||
className={styles.childPopup}
|
||||
>
|
||||
{stack.length > 1 && (
|
||||
<div className={styles.childHeader}>
|
||||
<Icon name={iconOf(top)} size={16} className={styles.rowIcon} />
|
||||
<span className={styles.rowLabel} title={top.label}>
|
||||
{top.label}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
<NavChildList
|
||||
id={listId}
|
||||
label={top.label}
|
||||
items={items}
|
||||
active={active}
|
||||
onSelect={setActive}
|
||||
onChoose={choose}
|
||||
onKeyDown={onKeyDown}
|
||||
>
|
||||
{stack.length > 1 && (
|
||||
<div className={styles.childHeader}>
|
||||
<Icon name={iconOf(top)} size={14} />
|
||||
<span className={styles.rowLabel}>{top.label}</span>
|
||||
</div>
|
||||
)}
|
||||
{items?.length === 0 && (
|
||||
<div className={styles.empty}>
|
||||
<T>Nothing here</T>
|
||||
</div>
|
||||
)}
|
||||
{items?.map((c, i) => (
|
||||
<div
|
||||
key={`${c.path}:${c.label}:${i}`}
|
||||
id={`navbar-child-${i}`}
|
||||
role="option"
|
||||
aria-selected={i === active}
|
||||
className={clsx(styles.row, i === active && styles.rowActive)}
|
||||
onMouseEnter={() => setActive(i)}
|
||||
onClick={() => choose(c)}
|
||||
>
|
||||
<Icon name={iconOf(c)} size={14} />
|
||||
<span className={styles.rowLabel}>{c.label}</span>
|
||||
{c.kind === "dir" && <Icon name="chevron-right" size={12} className={styles.rowChevron} />}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
/>
|
||||
</Popup>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<BreadcrumbStrip
|
||||
@@ -261,7 +269,15 @@ function CrumbNav({ segments, className }: { segments: Crumb[]; className?: stri
|
||||
</Fragment>
|
||||
);
|
||||
})}
|
||||
{popup && <NavChildrenPopup segment={popup.nav} anchor={popup.anchor} onClose={() => setPopup(null)} />}
|
||||
{popup && (
|
||||
<NavChildrenPopup
|
||||
key={`${popup.nav.kind}:${popup.nav.path}:${popup.nav.label}`}
|
||||
segment={popup.nav}
|
||||
anchor={popup.anchor}
|
||||
openAbove={inStatusBar}
|
||||
onClose={() => setPopup(null)}
|
||||
/>
|
||||
)}
|
||||
</BreadcrumbStrip>
|
||||
);
|
||||
}
|
||||
@@ -311,7 +327,7 @@ export function Breadcrumbs({ className, inStatusBar = false }: { className?: st
|
||||
segments.push(projectCrumb(project ?? currentProject));
|
||||
}
|
||||
if (!segments.length) return null;
|
||||
return <CrumbNav segments={segments} className={className ?? styles.status} />;
|
||||
return <CrumbNav segments={segments} className={className ?? styles.status} inStatusBar={inStatusBar} />;
|
||||
}
|
||||
|
||||
/** The status bar instance of the navigation bar; renders only for `statusBar`. */
|
||||
|
||||
Reference in new issue
Заблокувати користувача