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
24 changes: 24 additions & 0 deletions apps/web/src/components/chat/ComposerModelEffort.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,27 @@ it("draws an agent's reasoning as bars and keeps its mode out of the levels", as
.toBeInTheDocument();
await expect.element(page.getByRole("menuitemradio")).not.toBeInTheDocument();
});

it("focuses a reasoning level by its first letter without selecting it", async () => {
await using screen = await render(<Column />, { router: false }); // no routes involved
await userEvent.tab();
await expect.element(screen.getByRole("radio", { name: "Medium" })).toHaveFocus();
await userEvent.keyboard("l");
await expect.element(screen.getByRole("radio", { name: "Low" })).toHaveFocus();
await expect
.element(screen.getByRole("radio", { name: "Medium" }))
.toHaveAttribute("aria-checked", "true");
for (const key of ["{Control>}h{/Control}", "{Meta>}h{/Meta}", "{Alt>}h{/Alt}", "{Delete}"]) {
await userEvent.keyboard(key);
await expect.element(screen.getByRole("radio", { name: "Low" })).toHaveFocus();
}
await userEvent.keyboard("H");
await expect.element(screen.getByRole("radio", { name: "High" })).toHaveFocus();
// Wraps past the last level back to the first match.
await userEvent.keyboard("m");
await expect.element(screen.getByRole("radio", { name: "Medium" })).toHaveFocus();
await userEvent.keyboard("h{Enter}");
await expect
.element(screen.getByRole("radio", { name: "High" }))
.toHaveAttribute("aria-checked", "true");
});
23 changes: 18 additions & 5 deletions apps/web/src/components/chat/ComposerModelEffort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ function ColumnRow(props: {
"aria-checked": props.selected,
disabled: props.disabled,
"data-effort-row": true,
// Focus lands on a level after a model pick.
"data-effort-level": true,
// Focus lands on a level after a model pick, and a typed letter finds one by its label.
"data-effort-level": props.label,
// Up and Down move between rows.
tabIndex: props.tabStop ? 0 : -1,
className: COLUMN_ROW,
Expand Down Expand Up @@ -244,15 +244,28 @@ function ColumnRow(props: {
}

function moveBetweenRows(event: KeyboardEvent<HTMLElement>) {
if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
const arrow = event.key === "ArrowDown" || event.key === "ArrowUp";
if (
!arrow &&
(event.key.length !== 1 ||
event.key.trim() === "" ||
event.ctrlKey ||
event.altKey ||
event.metaKey ||
event.nativeEvent.isComposing)
)
return;
// An option's side menu is a React child here, but it moves through its own items.
if (!event.currentTarget.contains(event.target as Node)) return;
const rows = Array.from(
event.currentTarget.querySelectorAll<HTMLButtonElement>("[data-effort-row]:not(:disabled)"),
);
const index = rows.findIndex((row) => row === document.activeElement);
const next =
rows[Math.min(rows.length - 1, Math.max(0, index + (event.key === "ArrowDown" ? 1 : -1)))];
const next = arrow
? rows[Math.min(rows.length - 1, Math.max(0, index + (event.key === "ArrowDown" ? 1 : -1)))]
: [...rows.slice(index + 1), ...rows.slice(0, index + 1)].find((row) =>
row.dataset.effortLevel?.toLowerCase().startsWith(event.key.toLowerCase()),
);
if (!next) return;
event.preventDefault();
event.stopPropagation();
Expand Down
Loading