Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Model star: reach it from the keyboard, and show it on keyboard focus

milind-soni6767d37416eb
diff --git a/src/components/FavoriteStar.dom.test.ts b/src/components/FavoriteStar.dom.test.ts
index dd9c7608eb..30c257ddbc 100644
--- a/src/components/FavoriteStar.dom.test.ts
+++ b/src/components/FavoriteStar.dom.test.ts
@@ -4,6 +4,7 @@ import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { favoriteKey, favoriteModels, forgetFavoriteModelsForTest } from "@/lib/favorite-models";
+import { modelMenuKey } from "./model-menu-keys";
import { FavoriteStar } from "./SimpleModelMenu";
let host: HTMLDivElement;
@@ -38,4 +39,34 @@ describe("the star on a model", () => {
flushSync(() => star.click());
expect(star.getAttribute("aria-pressed")).toBe("false");
});
+
+ it("is reached from its row with the arrow keys, and Enter on it stars the model, not picks it", () => {
+ const picked: string[] = [];
+ const row = (model: string) => createElement("div", { key: model, className: "group/model relative" },
+ createElement("button", { type: "button", "data-menu-row": "", tabIndex: -1, onClick: () => picked.push(model) }, model),
+ createElement(FavoriteStar, { instanceId: "openaiCompat", model, name: model }));
+ flushSync(() => root.render(createElement("div", {
+ role: "menu", tabIndex: -1, onKeyDown: (event: Parameters<typeof modelMenuKey>[0]) => modelMenuKey(event, { query: "", typed: { current: null }, close: () => {} }),
+ }, row("a"), row("b"))));
+ const [rowA, rowB] = [...host.querySelectorAll<HTMLButtonElement>("[data-menu-row]")];
+ const [starA, starB] = [...host.querySelectorAll<HTMLButtonElement>("[data-model-favorite]")];
+ const press = (key: string) => {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true });
+ document.activeElement!.dispatchEvent(event);
+ return event;
+ };
+ rowA!.focus();
+ press("ArrowRight");
+ expect(document.activeElement).toBe(starA);
+ press("ArrowDown");
+ expect(document.activeElement).toBe(rowB);
+ press("ArrowRight");
+ expect(document.activeElement).toBe(starB);
+ expect(press("Enter").defaultPrevented, "the button's own Enter presses the star").toBe(false);
+ expect(picked).toEqual([]);
+ press("ArrowLeft");
+ expect(document.activeElement).toBe(rowB);
+ press("Enter");
+ expect(picked).toEqual(["b"]);
+ });
});
diff --git a/src/components/SimpleModelMenu.tsx b/src/components/SimpleModelMenu.tsx
index 98cf1b7ea0..b40df4948a 100644
--- a/src/components/SimpleModelMenu.tsx
+++ b/src/components/SimpleModelMenu.tsx
@@ -51,7 +51,8 @@ function RowLines({ label, children }: { label: ReactNode; children?: ReactNode
);
}
-/** Stays where it is when pressed: the row moves up the next time the menu opens, not under the pointer. */
+/** Stays where it is when pressed: the row moves up the next time the menu opens, not under the pointer. The arrow away
+ * from the start of the line moves from its row to it and back (model-menu-keys.ts); it shows while either has keyboard focus. */
export function FavoriteStar({ instanceId, model, name }: { instanceId: string; model: string; name: string }) {
const favorite = useFavoriteModels().has(favoriteKey(instanceId, model));
return (
@@ -64,7 +65,7 @@ export function FavoriteStar({ instanceId, model, name }: { instanceId: string;
title={t(favorite ? "model.favorite.remove" : "model.favorite.add", { name })}
onClick={() => toggleFavoriteModel(instanceId, model)}
className={cn("absolute end-1.5 top-1/2 flex size-6 -translate-y-1/2 items-center justify-center rounded-md text-ink-tertiary hover:text-ink",
- favorite ? "opacity-100" : "opacity-0 group-hover/model:opacity-100 touch:opacity-100")}
+ favorite ? "opacity-100" : "opacity-0 group-hover/model:opacity-100 touch:opacity-100 focus-visible:opacity-100 peer-focus-visible/row:opacity-100")}
>
<Star size={13} aria-hidden="true" className={cn(favorite && "fill-current")} />
</button>
@@ -86,7 +87,7 @@ function MenuModel({ instance, row, onPick }: { instance: InstanceInfo; row: Men
title={[modelOptionLabel(option), option.provider, row.detail, reason].filter(Boolean).join(" · ")}
tabIndex={-1}
onClick={() => onPick(instance, row.model ?? option.id, row.variant)}
- className={cn(SIMPLE_MENU_ROW, "pe-8")}
+ className={cn(SIMPLE_MENU_ROW, "peer/row pe-8")}
>
<Gutter />
<RowLines label={row.label}>
diff --git a/src/components/model-menu-keys.ts b/src/components/model-menu-keys.ts
index a3608cb9a4..19bad1619b 100644
--- a/src/components/model-menu-keys.ts
+++ b/src/components/model-menu-keys.ts
@@ -41,6 +41,26 @@ export function modelMenuKey(event: ReactKeyboardEvent<HTMLElement>, { query, ty
}
return;
}
+ // A model's star sits beside its row: the arrow away from the start of the line moves to it, the other one back.
+ const forward = rtl ? "ArrowLeft" : "ArrowRight";
+ const star = target.matches("[data-model-favorite]") ? target : null;
+ const starRow = star?.parentElement?.querySelector<HTMLElement>("[data-menu-row]") ?? null;
+ if (star) {
+ if (!starRow || event.key === "Enter" || event.key === " ") return;
+ if (event.key === (rtl ? "ArrowRight" : "ArrowLeft")) {
+ handled();
+ starRow.focus({ preventScroll: true });
+ return;
+ }
+ } else if (event.key === forward && target.matches("[data-menu-row]")) {
+ const besideRow = target.parentElement?.querySelector<HTMLElement>(":scope > [data-model-favorite]");
+ if (besideRow) {
+ handled();
+ besideRow.focus({ preventScroll: true });
+ return;
+ }
+ }
+ const at = starRow ?? target;
const search = menu.querySelector<HTMLInputElement>("[data-menu-search]");
if (target instanceof HTMLInputElement && target !== search) return;
const rows = [...menu.querySelectorAll<HTMLElement>("[data-menu-row]:not(:disabled)")];
@@ -60,14 +80,14 @@ export function modelMenuKey(event: ReactKeyboardEvent<HTMLElement>, { query, ty
}
return;
}
- const index = rows.indexOf(target);
- if (index < 0 && target !== menu) return;
+ const index = rows.indexOf(at);
+ if (index < 0 && at !== menu) return;
if (event.key === "Enter" && index >= 0) {
handled();
- target.click();
+ at.click();
return;
}
- if (search && ((event.key === "ArrowUp" && target === below[0]) || (event.key === "ArrowDown" && target === above.at(-1)))) return goTo(search);
+ if (search && ((event.key === "ArrowUp" && at === below[0]) || (event.key === "ArrowDown" && at === above.at(-1)))) return goTo(search);
const next = nextMenuIndex(event.key, index, rows.length);
if (next !== null) return goTo(rows[next]);
if (event.key === (rtl ? "ArrowLeft" : "ArrowRight") && index >= 0 && rows[index]!.matches(FORWARD_ROWS)) {