Skip to content

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

Pinned messages menu: close with the last pin, and finish its keyboard

luishron12384def255db3c
diff --git a/src/components/PinnedMessagesMenu.dom.test.ts b/src/components/PinnedMessagesMenu.dom.test.ts
index 1a76e0dc50..6102237334 100644
--- a/src/components/PinnedMessagesMenu.dom.test.ts
+++ b/src/components/PinnedMessagesMenu.dom.test.ts
@@ -41,21 +41,51 @@ it("lists who said the pinned message and what, and jumps to it", async () => {
});
it("reaches Unpin with the arrows, and Escape closes the menu back onto its button", async () => {
- const { root, onUnpin } = await mount();
+ const { root } = await mount();
await act(async () => trigger()!.click());
await key("ArrowDown");
expect(document.activeElement).toBe(items()[1]);
await key("Home");
expect(document.activeElement).toBe(items()[0]);
await key("End");
- await act(async () => (document.activeElement as HTMLButtonElement).click());
- expect(onUnpin).toHaveBeenCalledWith("m1");
+ expect(document.activeElement).toBe(items()[1]);
await key("Escape");
expect(trigger()!.getAttribute("aria-expanded")).toBe("false");
expect(document.activeElement).toBe(trigger());
act(() => root.unmount());
});
+it("closes for good once the last pin is gone, and hands focus to the button beside it", async () => {
+ const onUnpin = vi.fn();
+ const root = createRoot(document.body.appendChild(document.createElement("div")));
+ const render = (pins: typeof PINS) => act(async () => root.render(createElement("div", null,
+ createElement("button", { "data-beside": "" }),
+ createElement(PinnedMessagesMenu, { pins, onJump: vi.fn(), onUnpin }),
+ )));
+ await render(PINS);
+ await act(async () => trigger()!.click());
+ await act(async () => items()[1]!.click());
+ expect(onUnpin).toHaveBeenCalledWith("m1");
+ expect(document.activeElement).toBe(document.querySelector("[data-beside]"));
+ await render([]);
+ expect(trigger()).toBeNull();
+ await render(PINS);
+ expect(trigger()!.getAttribute("aria-expanded")).toBe("false");
+ expect(document.querySelector("[role=menu]:not([inert])")).toBeNull();
+ act(() => root.unmount());
+});
+
+it("jumps to a row by its first letter, and Tab closes the menu back onto its button", async () => {
+ const { root } = await mount();
+ await act(async () => trigger()!.click());
+ await key("u");
+ expect(document.activeElement).toBe(items()[1]);
+ await key("Tab");
+ expect(trigger()!.getAttribute("aria-expanded")).toBe("false");
+ expect(document.activeElement).toBe(trigger());
+ act(() => root.unmount());
+});
+
it("offers no Unpin to someone who cannot change the conversation", async () => {
const { root } = await mount({ onUnpin: undefined });
await act(async () => trigger()!.click());
diff --git a/src/components/PinnedMessagesMenu.tsx b/src/components/PinnedMessagesMenu.tsx
index 752e0b4546..db2ca34bf5 100644
--- a/src/components/PinnedMessagesMenu.tsx
+++ b/src/components/PinnedMessagesMenu.tsx
@@ -5,9 +5,11 @@ import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { peerLine } from "@/lib/peer-message";
import { citationPreviewText } from "@/lib/citations";
+import { isTypeaheadKey, typeaheadMatch, typeaheadText, type Typeahead } from "@/lib/menu-keys";
import { usePopoverDismiss } from "@/hooks/use-popover-dismiss";
import { useMenuMotion } from "./MenuMotion";
import { MENU_LABEL, MENU_SHEET, SIMPLE_MENU_META, SIMPLE_MENU_ROW } from "./menu-recipe";
+import { nextMenuIndex } from "./SidebarPopoverMenu";
export type PinnedPreview = { id: string; sender: string; text: string };
@@ -20,7 +22,6 @@ export function pinnedPreview(messages: readonly Message[], pinnedId: string | u
return text ? [{ id: pinned.id, sender, text }] : [];
}
-/** The conversation's pinned messages behind one header button: each row jumps to its message, and Unpin sits beside it. */
export function PinnedMessagesMenu({ pins, onJump, onUnpin, triggerClassName }: {
pins: readonly PinnedPreview[];
onJump: (messageId: string) => void;
@@ -32,23 +33,43 @@ export function PinnedMessagesMenu({ pins, onJump, onUnpin, triggerClassName }:
const root = useRef<HTMLDivElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const menu = useRef<HTMLDivElement>(null);
- usePopoverDismiss(open, root, () => { setOpen(false); trigger.current?.focus(); });
+ const typed = useRef<Typeahead | null>(null);
+ const close = () => { setOpen(false); trigger.current?.focus(); };
+ usePopoverDismiss(open, root, close);
useEffect(() => {
if (open) menu.current?.querySelector<HTMLElement>("[role=menuitem]")?.focus();
}, [open]);
- if (!pins.length) return null;
+ const empty = pins.length === 0;
+ useEffect(() => {
+ if (empty) setOpen(false);
+ }, [empty]);
+ if (empty) return null;
+
+ const unpin = (messageId: string) => {
+ if (pins.length === 1) {
+ setOpen(false);
+ const beside = [...(root.current?.parentElement?.querySelectorAll<HTMLElement>("button:not(:disabled)") ?? [])];
+ (beside.find((button) => !root.current?.contains(button) && button.checkVisibility?.() !== false) ?? trigger.current)?.focus();
+ }
+ onUnpin?.(messageId);
+ };
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
+ if (event.key === "Tab") {
+ event.preventDefault();
+ close();
+ return;
+ }
const items = Array.from(menu.current?.querySelectorAll<HTMLElement>("[role=menuitem]") ?? []);
const at = items.indexOf(document.activeElement as HTMLElement);
- const next = event.key === "ArrowDown" ? items[(at + 1) % items.length]
- : event.key === "ArrowUp" ? items[(at - 1 + items.length) % items.length]
- : event.key === "Home" ? items[0]
- : event.key === "End" ? items[items.length - 1]
- : undefined;
- if (!next) return;
+ let next = nextMenuIndex(event.key, at, items.length);
+ if (next === null && isTypeaheadKey(event)) {
+ typed.current = typeaheadText(typed.current, event.key, event.timeStamp);
+ next = typeaheadMatch(items.map((item) => item.getAttribute("aria-label") ?? item.textContent ?? ""), at, typed.current.text);
+ }
+ if (next === null) return;
event.preventDefault();
- next.focus();
+ items[next]?.focus();
};
return (
@@ -94,7 +115,7 @@ export function PinnedMessagesMenu({ pins, onJump, onUnpin, triggerClassName }:
role="menuitem"
aria-label={t("chat.unpinMessage")}
title={t("chat.unpin")}
- onClick={() => onUnpin(pin.id)}
+ onClick={() => unpin(pin.id)}
className="flex size-[30px] shrink-0 items-center justify-center rounded-md text-ink-secondary hover:bg-ink/[0.06] hover:text-ink focus:bg-ink/[0.06] focus:text-ink touch:size-10"
>
<PinOff size={16} />