Skip to content
Closed
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
2 changes: 2 additions & 0 deletions TESTING_COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ keyboard filtering, removal of plugin/website/app creation actions from menus,
discovery of their blank starters on a fresh drive, and file/upload searches.
Existing app, plugin, and website browser flows create through the catalog,
including a website seeded from its selected parent document.
The resource menu check simulates a shrinking visual viewport and requires the
open menu, including its filter, to remain above the keyboard boundary.

Mobile AI chat navigation: Chromium covers opening the left sidebar above chat,
Back dismissal without leaving the page, the AI settings link, an empty composer
Expand Down
168 changes: 95 additions & 73 deletions browser/data-browser/src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,99 @@ export function DropdownMenu({
// if the keyboard is used to navigate the menu items
const [useKeys, setUseKeys] = useState(true);

const positionMenu = useCallback(() => {
const menu = dropdownRef.current;
const trigger = triggerRef.current;

if (!menu || !trigger) return;

const dialog = menu.closest('dialog');
const viewport = window.visualViewport;
const visibleTop = viewport?.scale === 1 ? viewport.offsetTop : 0;
const visibleHeight =
viewport?.scale === 1 ? viewport.height : window.innerHeight;
const visibleBottom = visibleTop + visibleHeight;

// Keyboard animation resizes the visual viewport without necessarily
// changing CSS viewport units. Keep the menu scrollable inside that area.
if (!dialog) {
menu.style.maxHeight = `${Math.max(0, Math.min(window.innerHeight * 0.8, visibleHeight - 16))}px`;
}

const menuRect = menu.getBoundingClientRect();

if (anchorPoint) {
const left =
anchorPoint.x + menuRect.width > window.innerWidth
? anchorPoint.x - menuRect.width
: anchorPoint.x;
const preferredTop =
anchorPoint.y + menuRect.height > visibleBottom
? anchorPoint.y - menuRect.height
: anchorPoint.y;

menu.style.left = `${Math.max(0, left)}px`;
menu.style.top = `${Math.max(visibleTop + 8, Math.min(preferredTop, visibleBottom - menuRect.height - 8))}px`;

return;
}

const triggerRect = trigger.getBoundingClientRect();

if (dialog) {
const dialogRect = dialog.getBoundingClientRect();
const relativeTop = triggerRect.y - dialogRect.y;
const relativeLeft = triggerRect.x - dialogRect.x;
const topPos = relativeTop - menuRect.height;

menu.style.top = `${topPos < 0 ? relativeTop + triggerRect.height : topPos}px`;

const leftPos = relativeLeft - menuRect.width;
menu.style.left = `${leftPos < 0 ? relativeLeft : relativeLeft - menuRect.width + triggerRect.width}px`;

return;
}

// Prefer above the trigger; clamp to the visible viewport if the keyboard
// has covered the trigger or reduced the available space.
const above = triggerRect.y - menuRect.height - MENU_TRIGGER_GAP;
const preferredTop =
above < visibleTop ? triggerRect.bottom + MENU_TRIGGER_GAP : above;
menu.style.top = `${Math.max(visibleTop + 8, Math.min(preferredTop, visibleBottom - menuRect.height - 8))}px`;

const leftPos = triggerRect.x - menuRect.width;
menu.style.left = `${leftPos < 0 ? triggerRect.x : triggerRect.x - menuRect.width + triggerRect.width}px`;
}, [anchorPoint]);

useEffect(() => {
if (!isActive) return;

let frame = 0;

const schedulePosition = () => {
cancelAnimationFrame(frame);
frame = requestAnimationFrame(positionMenu);
};

const viewport = window.visualViewport;
const observer = new ResizeObserver(schedulePosition);

if (dropdownRef.current) observer.observe(dropdownRef.current);
viewport?.addEventListener('resize', schedulePosition);
viewport?.addEventListener('scroll', schedulePosition);
window.addEventListener('resize', schedulePosition);
window.addEventListener('scroll', schedulePosition, true);

return () => {
cancelAnimationFrame(frame);
observer.disconnect();
viewport?.removeEventListener('resize', schedulePosition);
viewport?.removeEventListener('scroll', schedulePosition);
window.removeEventListener('resize', schedulePosition);
window.removeEventListener('scroll', schedulePosition, true);
};
}, [isActive, positionMenu]);

const handleToggle = useCallback(() => {
if (isActive) {
handleClose();
Expand All @@ -224,7 +317,7 @@ export function DropdownMenu({
return;
}

const menuRect = dropdownRef.current.getBoundingClientRect();
positionMenu();

// The menu is positioned while visibility:hidden, so the entrance
// transition must start AFTER it becomes visible — one frame later —
Expand All @@ -247,80 +340,9 @@ export function DropdownMenu({
});
};

// A right-click / context menu: position at the cursor point with the
// usual convention (below-right, flipping left/up when it would overflow
// the viewport, clamped to stay on-screen).
if (anchorPoint) {
const left =
anchorPoint.x + menuRect.width > window.innerWidth
? anchorPoint.x - menuRect.width
: anchorPoint.x;
const top =
anchorPoint.y + menuRect.height > window.innerHeight
? anchorPoint.y - menuRect.height
: anchorPoint.y;

dropdownRef.current.style.left = `${Math.max(0, left)}px`;
dropdownRef.current.style.top = `${Math.max(0, top)}px`;
reveal();

return;
}

const triggerRect = triggerRef.current.getBoundingClientRect();

// Check if we're inside a dialog
const dialog = dropdownRef.current.closest('dialog');

// TODO: Use CSS anchor positioning instead.
if (dialog) {
// For dialogs, use absolute positioning relative to the dialog
const dialogRect = dialog.getBoundingClientRect();
const relativeTop = triggerRect.y - dialogRect.y;
const relativeLeft = triggerRect.x - dialogRect.x;

const topPos = relativeTop - menuRect.height;

// If the top is outside of the dialog, render it below
if (topPos < 0) {
dropdownRef.current.style.top = `${relativeTop + triggerRect.height}px`;
} else {
dropdownRef.current.style.top = `${topPos}px`;
}

const leftPos = relativeLeft - menuRect.width;

// If the left is outside of the dialog, render it to the right
if (leftPos < 0) {
dropdownRef.current.style.left = `${relativeLeft}px`;
} else {
dropdownRef.current.style.left = `${relativeLeft - menuRect.width + triggerRect.width}px`;
}
} else {
// Prefer opening above the trigger, below when there's no room. A
// small gap instead of overlapping the trigger — covering it half-way
// made it unclickable for toggling the menu closed.
const topPos = triggerRect.y - menuRect.height - MENU_TRIGGER_GAP;

if (topPos < 0) {
dropdownRef.current.style.top = `${triggerRect.bottom + MENU_TRIGGER_GAP}px`;
} else {
dropdownRef.current.style.top = `${topPos}px`;
}

const leftPos = triggerRect.x - menuRect.width;

// If the left is outside of the screen, render it to the right
if (leftPos < 0) {
dropdownRef.current.style.left = `${triggerRect.x}px`;
} else {
dropdownRef.current.style.left = `${triggerRect.x - menuRect.width + triggerRect.width}px`;
}
}

reveal();
});
}, [isActive, setIsActive, anchorPoint, searchable]);
}, [isActive, handleClose, setIsActive, positionMenu, searchable]);

const handleMouseOverMenu = useCallback(() => {
setUseKeys(false);
Expand Down
34 changes: 34 additions & 0 deletions browser/e2e/tests/resource-context-menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ async function openContextMenu(page: Page, target: Locator, items: Locator[]) {
test.describe('resource context menu', () => {
test.beforeEach(before);

test('open menu follows the visible viewport as a keyboard appears', async ({
page,
}) => {
await page.setViewportSize({ width: 390, height: 844 });
await page.getByRole('button', { name: 'More', exact: true }).click();
const menu = page.getByRole('menu');
await expect(menu).toBeVisible();
const originalTop = (await menu.boundingBox())!.y;

for (const height of [620, 480]) {
await page.evaluate(visibleHeight => {
const viewport = window.visualViewport!;
Object.defineProperty(viewport, 'height', {
configurable: true,
value: visibleHeight,
});
viewport.dispatchEvent(new Event('resize'));
}, height);

await expect
.poll(async () => {
const bounds = (await menu.boundingBox())!;

return bounds.y + bounds.height;
})
.toBeLessThanOrEqual(height - 8);
}

expect((await menu.boundingBox())!.y).toBeLessThan(originalTop);
await expect(
page.getByRole('textbox', { name: 'Filter actions' }),
).toBeFocused();
});

test('sidebar link + table cell open the resource menu on right-click', async ({
page,
}: {
Expand Down
20 changes: 15 additions & 5 deletions browser/e2e/tests/right-panel-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ async function expectLeftSidebarClosed(page: Page) {

await expect
.poll(async () =>
sidebar.evaluate(element => element.parentElement!.getBoundingClientRect().width),
sidebar.evaluate(
element => element.parentElement!.getBoundingClientRect().width,
),
)
.toBe(0);
await expect
.poll(() => sidebar.evaluate(element => Number(getComputedStyle(element).opacity)))
.poll(() =>
sidebar.evaluate(element => Number(getComputedStyle(element).opacity)),
)
.toBe(0);
}

Expand All @@ -26,7 +30,9 @@ for (const [name, trigger, panel] of [
const sidebar = page.getByTestId('sidebar');
await expect
.poll(() =>
sidebar.evaluate(element => element.parentElement!.getBoundingClientRect().width),
sidebar.evaluate(
element => element.parentElement!.getBoundingClientRect().width,
),
)
.toBeGreaterThan(0);
await page.mouse.move(700, 400);
Expand All @@ -35,7 +41,9 @@ for (const [name, trigger, panel] of [
await expectLeftSidebarClosed(page);
await page.mouse.move(2, 400);
await expect
.poll(() => sidebar.evaluate(element => Number(getComputedStyle(element).opacity)))
.poll(() =>
sidebar.evaluate(element => Number(getComputedStyle(element).opacity)),
)
.toBe(0);
});
}
Expand All @@ -49,7 +57,9 @@ test('opening meeting chat closes the left sidebar at tablet width', async ({
.poll(() =>
page
.getByTestId('sidebar')
.evaluate(element => element.parentElement!.getBoundingClientRect().width),
.evaluate(
element => element.parentElement!.getBoundingClientRect().width,
),
)
.toBeGreaterThan(0);
await page.getByRole('button', { name: 'New Meeting' }).first().click();
Expand Down
2 changes: 1 addition & 1 deletion browser/lib/src/client-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1154,8 +1154,8 @@ export class ClientDbWorker {
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private sendToLeader(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
msg: Record<string, any>,
retries = 1,
): Promise<unknown> {
Expand Down
Loading