Skip to content
Open
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
43 changes: 43 additions & 0 deletions site/src/pages/AgentsPage/components/AgentChatInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const stubViewport = (
});
};

const mobileDropdownVariables = [
"--mobile-dropdown-bottom",
"--mobile-dropdown-left",
"--mobile-dropdown-width",
"--mobile-dropdown-above-composer-bottom",
"--mobile-dropdown-above-composer-max-height",
];

const modelOptions = [
{
id: "model-config-1",
Expand Down Expand Up @@ -1560,4 +1568,39 @@ describe("AgentChatInput", () => {
expect(onPromoteQueuedMessage).not.toHaveBeenCalled();
}
});

it("does not write mobile dropdown variables on the root element at desktop widths", async () => {
stubViewport(false, belowMdViewportMediaQuery);
const user = userEvent.setup();
const setProperty = vi.spyOn(document.documentElement.style, "setProperty");

renderInput(<AgentChatInput {...inputProps} />);
const textbox = screen.getByRole("textbox", { name: "Chat message" });
await user.click(textbox);
await user.keyboard("when");

const rootWrites = setProperty.mock.calls.filter(([name]) =>
name.startsWith("--mobile-dropdown-"),
);
expect(rootWrites).toEqual([]);
for (const name of mobileDropdownVariables) {
expect(document.documentElement.style.getPropertyValue(name)).toBe("");
}
});

it("writes mobile dropdown variables below the md breakpoint and removes them on unmount", () => {
stubViewport(true, belowMdViewportMediaQuery);

const { unmount } = renderInput(<AgentChatInput {...inputProps} />);
for (const name of mobileDropdownVariables) {
expect(document.documentElement.style.getPropertyValue(name)).toMatch(
/^\d+px$/,
);
}

unmount();
for (const name of mobileDropdownVariables) {
expect(document.documentElement.style.getPropertyValue(name)).toBe("");
}
});
});
10 changes: 8 additions & 2 deletions site/src/pages/AgentsPage/hooks/useChatDropdownPosition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { useEffect } from "react";
import { useMediaQuery } from "#/hooks/useMediaQuery";
import { belowMdViewportMediaQuery } from "#/utils/mobile";

/** Keeps portaled mobile menus aligned with the chat composer and keyboard. */
export function useChatDropdownPosition(composerElement: HTMLElement | null) {
// A custom property change on `<html>` restyles every element in the
// document, and only the below-md `mobile-full-width-dropdown*` utilities
// read these variables.
const isBelowMd = useMediaQuery(belowMdViewportMediaQuery);
useEffect(() => {
if (!composerElement) return;
if (!composerElement || !isBelowMd) return;
// Fixed Radix menus need layout-viewport coordinates. Clamp to the
// visual viewport only for positive heights: mobile WebKit can mix
// coordinate systems while the keyboard settles.
Expand Down Expand Up @@ -133,5 +139,5 @@ export function useChatDropdownPosition(composerElement: HTMLElement | null) {
root.style.removeProperty("--mobile-dropdown-above-composer-bottom");
root.style.removeProperty("--mobile-dropdown-above-composer-max-height");
};
}, [composerElement]);
}, [composerElement, isBelowMd]);
}
Loading