Skip to content

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

fix(phone): show the pairing QR over the whole window in Settings (#5290)

Aditya Umale254a8c4c461d
diff --git a/src/components/PhoneDevicesSection.dialog.dom.test.ts b/src/components/PhoneDevicesSection.dialog.dom.test.ts
new file mode 100644
index 0000000000..2f708b07f4
--- /dev/null
+++ b/src/components/PhoneDevicesSection.dialog.dom.test.ts
@@ -0,0 +1,87 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import type { CompanionState, PhoneSetupController } from "./PhoneSetupFlow";
+
+vi.mock("@/state/store", () => ({ useStore: () => ({ state: { config: {} } }) }));
+vi.mock("./PhoneSetupFlow", () => ({
+ PhoneSetupFlowView: () => createElement("div", { "data-qr": "" }, "QR", createElement("details", null, createElement("summary", null, "Code"))),
+}));
+const { PairDeviceDialog } = await import("./PhoneDevicesSection");
+
+const on = { on: true, since: 1 };
+const state = { enabled: true, keepAwake: false, port: 8810, devices: [], pairing: null, notices: [],
+ routes: { wifi: on, relay: on, tailscale: { on: false, since: null }, https: { on: false, since: null } } } as unknown as CompanionState;
+let root: Root;
+let settings: HTMLElement;
+let onClose = vi.fn<() => void>();
+let cancel = vi.fn<() => void>();
+let trigger: HTMLButtonElement;
+
+beforeEach(() => {
+ // Stands in for Settings: its glass pane (backdrop-filter) is the box a `fixed` child is laid out in.
+ settings = document.createElement("div");
+ settings.setAttribute("role", "dialog");
+ settings.className = "glass-surface";
+ document.body.append(settings);
+ trigger = document.createElement("button");
+ document.body.append(trigger);
+ trigger.focus();
+ onClose = vi.fn<() => void>();
+ cancel = vi.fn<() => void>();
+ const c = { busy: false, act: vi.fn(), useLocal: vi.fn(), start: vi.fn(), cancel } as unknown as PhoneSetupController;
+ root = createRoot(settings);
+ flushSync(() => root.render(createElement(PairDeviceDialog, { c, state, onClose })));
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ settings.remove();
+ trigger.remove();
+});
+
+const pair = () => document.querySelector<HTMLElement>("[data-pair-dialog]")!;
+const press = (key: string, shiftKey = false) => {
+ const event = new KeyboardEvent("keydown", { key, shiftKey, bubbles: true, cancelable: true });
+ (document.activeElement ?? document.body).dispatchEvent(event);
+ return event;
+};
+
+it("shows the pairing QR over the whole window, not inside the Settings glass pane", () => {
+ expect(pair().querySelector("[data-qr]")).not.toBeNull();
+ expect(settings.contains(pair())).toBe(false);
+ expect(pair().parentElement).toBe(document.body);
+});
+
+it("closes only the pairing dialog on Escape, so Settings stays open", () => {
+ const event = press("Escape");
+ expect(event.defaultPrevented).toBe(true);
+ expect(cancel).toHaveBeenCalledOnce();
+ expect(onClose).toHaveBeenCalledOnce();
+});
+
+it("keeps Tab inside the pairing dialog", () => {
+ const done = pair().querySelector("button")!;
+ done.getClientRects = () => [{}] as unknown as DOMRectList;
+ pair().querySelector("summary")!.getClientRects = () => [] as unknown as DOMRectList;
+ expect(press("Tab").defaultPrevented).toBe(true);
+ expect(document.activeElement).toBe(done);
+ press("Tab", true);
+ expect(document.activeElement).toBe(done);
+});
+
+it("moves focus into the pairing dialog when it opens and gives it back when it closes", () => {
+ expect(pair().contains(document.activeElement)).toBe(true);
+ flushSync(() => root.render(null));
+ expect(document.activeElement).toBe(trigger);
+});
+
+it("lets Tab reach the code details toggle", () => {
+ const summary = pair().querySelector("summary")!;
+ const done = [...pair().querySelectorAll("button")].at(-1)!;
+ for (const control of [summary, done]) control.getClientRects = () => [{}] as unknown as DOMRectList;
+ done.focus();
+ press("Tab");
+ expect(document.activeElement).toBe(summary);
+});
diff --git a/src/components/PhoneDevicesSection.tsx b/src/components/PhoneDevicesSection.tsx
index abad196c1c..32cb2d422f 100644
--- a/src/components/PhoneDevicesSection.tsx
+++ b/src/components/PhoneDevicesSection.tsx
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from "react";
+import { createPortal } from "react-dom";
import { t } from "@/lib/i18n";
import { Loader2, ShieldCheck } from "lucide-react";
import type { CompanionAccountState } from "../types/ogb";
@@ -207,6 +208,8 @@ function RouteRow({ c, state, route, blocked }: { c: PhoneSetupController; state
type PairStep = "consent" | "pairing";
+const FOCUSABLE = 'button:not([disabled]), a[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), summary, [tabindex]:not([tabindex="-1"])';
+
/** Pair a device: says what it will turn on before anything listens, then shows the code. */
export function PairDeviceDialog({ c, state, onClose, secureAddress = false }: {
c: PhoneSetupController;
@@ -229,8 +232,33 @@ export function PairDeviceDialog({ c, state, onClose, secureAddress = false }: {
setStep("pairing");
c.useLocal();
};
- return (
- <div role="dialog" aria-modal="true" aria-label={step === "consent" ? t("devices.pair.title") : t("devices.pair.titlePhone")}
+ const close = step === "consent" ? onClose : () => { c.cancel(); onClose(); };
+ const root = useRef<HTMLDivElement>(null);
+ useEffect(() => {
+ const before = document.activeElement instanceof HTMLElement ? document.activeElement : null;
+ const box = root.current;
+ if (box && !box.contains(document.activeElement)) box.querySelector<HTMLElement>(FOCUSABLE)?.focus();
+ return () => { if (before?.isConnected) before.focus(); };
+ }, []);
+ // Settings also handles Escape and Tab; capture them first so they act on this dialog only.
+ useEffect(() => {
+ const onKey = (event: KeyboardEvent) => {
+ const box = root.current;
+ if (!box || event.defaultPrevented || event.isComposing || (event.key !== "Escape" && event.key !== "Tab")) return;
+ event.preventDefault();
+ if (event.key === "Escape") { close(); return; }
+ const controls = [...box.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((control) => control.getClientRects().length);
+ if (!controls.length) return;
+ const at = controls.indexOf(document.activeElement as HTMLElement);
+ const next = at < 0 ? (event.shiftKey ? -1 : 0) : at + (event.shiftKey ? -1 : 1);
+ controls.at(next % controls.length)!.focus();
+ };
+ window.addEventListener("keydown", onKey, true);
+ return () => window.removeEventListener("keydown", onKey, true);
+ }, [close]);
+ // An ancestor with backdrop-filter becomes the containing block for `fixed`, so render into the body.
+ const dialog = (
+ <div ref={root} role="dialog" aria-modal="true" aria-label={step === "consent" ? t("devices.pair.title") : t("devices.pair.titlePhone")}
className="fixed inset-0 z-50 flex items-center justify-center bg-black/30 p-4" data-pair-dialog={step}>
<div className="flex max-h-full w-full max-w-[460px] flex-col gap-4 overflow-y-auto rounded-2xl bg-card p-5 shadow-xl">
<div className="text-[15px] font-semibold text-ink">{step === "consent" ? t("devices.pair.title") : t("devices.pair.titlePhone")}</div>
@@ -251,7 +279,7 @@ export function PairDeviceDialog({ c, state, onClose, secureAddress = false }: {
{chosen.length === 0 && <div role="alert" className="text-[12px] text-danger">{t("devices.pair.pickOne")}</div>}
</div>
<div className="flex justify-end gap-2">
- <button type="button" className={button} onClick={onClose}>{t("common.cancel")}</button>
+ <button type="button" className={button} onClick={close}>{t("common.cancel")}</button>
<button type="button" disabled={chosen.length === 0 || c.busy} onClick={() => void begin()}
className="rounded-lg bg-accent px-3 py-1.5 text-[12px] font-medium text-white disabled:opacity-40">{t("devices.route.turnOn")}</button>
</div>
@@ -261,13 +289,14 @@ export function PairDeviceDialog({ c, state, onClose, secureAddress = false }: {
<PhoneSetupFlowView controller={c} />
<div className="text-[12px] leading-relaxed text-ink-secondary">{reachLine(state)}</div>
<div className="flex justify-end">
- <button type="button" className={button} onClick={() => { c.cancel(); onClose(); }}>{t("devices.pair.done")}</button>
+ <button type="button" className={button} onClick={close}>{t("devices.pair.done")}</button>
</div>
</>
)}
</div>
</div>
);
+ return typeof document === "undefined" ? dialog : createPortal(dialog, document.body);
}
/** A pairing code still open: anyone holding it can pair until it runs out, so it can be revoked here. */