Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Pop-up menus: a still pointer no longer steals the keyboard's highlight
Yashwanth Kamireddi6e244b41267d
diff --git a/.uiverify/keyboard.mjs b/.uiverify/keyboard.mjsindex 409a557..eef1ede 100644--- a/.uiverify/keyboard.mjs+++ b/.uiverify/keyboard.mjs@@ -251,6 +251,33 @@ const focused = (page) =>const second = await page.locator(".picker-menu li.is-active").innerText();check("ArrowDown moves the highlight", first !== second, `${first.trim()} → ${second.trim()}`);+ // Two presses in one go, before focus can have moved into the menu they open: the second+ // is still the menu's. It used to reopen the menu and be lost.+ await page.keyboard.press("Escape");+ await picker.focus();+ const quick = await page.evaluate(() => {+ const button = document.querySelector(".rx-detail .picker-button");+ const press = () => button.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true, cancelable: true }));+ press();+ press();+ return new Promise((r) => setTimeout(() => {+ const items = [...document.querySelectorAll(".picker-menu li")];+ r({ active: items.findIndex((li) => li.classList.contains("is-active")), chosen: items.findIndex((li) => li.getAttribute("aria-selected") === "true"), dump: items.map((li) => li.className + "/" + li.getAttribute("aria-selected")).join(" ") });+ }, 200));+ });+ check("a second key pressed at once still moves the highlight", quick.active === quick.chosen + 1, JSON.stringify(quick));+ // The pointer, once it moves, still takes the highlight to the row it is over.+ const third = page.locator(".picker-menu li").nth(2);+ const box = await third.boundingBox();+ await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2, { steps: 4 });+ check("a moving pointer highlights the row under it", (await third.getAttribute("class"))?.includes("is-active"), await third.innerText());+ await page.keyboard.press("Escape");+ await picker.focus();+ await page.keyboard.press("ArrowDown");+ await page.waitForSelector(".picker-menu");+ await page.keyboard.press("ArrowDown");+ await page.keyboard.press("ArrowUp");+await page.keyboard.press("Enter");await page.waitForTimeout(150);const dismissed = (await page.locator(".picker-menu").count()) === 0;diff --git a/src/components/Picker.tsx b/src/components/Picker.tsxindex 023a507..9a32ee5 100644--- a/src/components/Picker.tsx+++ b/src/components/Picker.tsx@@ -31,7 +31,14 @@ export function Picker({disabled?: boolean;className?: string;}) {- const [open, setOpen] = useState(false);+ const [open, setOpenState] = useState(false);+ /** Whether the menu is open as of the last key, ahead of the render that shows it: two+ * keys can arrive before React has drawn the first one's answer. */+ const isOpen = useRef(false);+ const setOpen = useCallback((to: boolean) => {+ isOpen.current = to;+ setOpenState(to);+ }, []);const [active, setActive] = useState(0);const root = useRef<HTMLDivElement | null>(null);const button = useRef<HTMLButtonElement | null>(null);@@ -95,8 +102,11 @@ export function Picker({// Opening lands the highlight on what is already chosen, not at the top — the current// value is where someone is navigating away from.+ /** Where the pointer was when it was last seen over the menu; none since it opened. */+ const pointer = useRef<{ x: number; y: number } | null>(null);const openMenu = () => {if (disabled) return;+ pointer.current = null;setActive(Math.max(0, options.findIndex((o) => o.value === value)));setOpen(true);};@@ -145,15 +155,16 @@ export function Picker({}, [open]);const onListKey = (e: React.KeyboardEvent) => {- const move = (to: number) => {+ // From the highlight as it stands after any key not yet drawn, not as last rendered.+ const move = (to: (at: number) => number) => {e.preventDefault();byKeyboard.current = true;- setActive(Math.max(0, Math.min(to, options.length - 1)));+ setActive((at) => Math.max(0, Math.min(to(at), options.length - 1)));};- if (e.key === "ArrowDown") move(active + 1);- else if (e.key === "ArrowUp") move(active - 1);- else if (e.key === "Home") move(0);- else if (e.key === "End") move(options.length - 1);+ if (e.key === "ArrowDown") move((at) => at + 1);+ else if (e.key === "ArrowUp") move((at) => at - 1);+ else if (e.key === "Home") move(() => 0);+ else if (e.key === "End") move(() => options.length - 1);else if (e.key === "Enter" || e.key === " ") {e.preventDefault();choose(active);@@ -177,6 +188,9 @@ export function Picker({disabled={disabled}onClick={() => (open ? close(false) : openMenu())}onKeyDown={(e) => {+ // Open already, with focus not yet moved in: a key pressed that quickly is the+ // menu's. Opening it again put the highlight back and the key was lost.+ if (isOpen.current) return onListKey(e);if (e.key === "ArrowDown" || e.key === "ArrowUp") {e.preventDefault();openMenu();@@ -215,7 +229,15 @@ export function Picker({className={index === active ? "is-active" : undefined}onKeyDown={onListKey}onClick={() => choose(index)}- onMouseEnter={() => {+ // The pointer highlights a row only once it moves. The menu opens over the+ // button, under a pointer that is still: the row it lands on is not chosen by+ // it, and taking it as chosen threw the keyboard's highlight back.+ onMouseMove={(e) => {+ const at = pointer.current;+ if (at && at.x === e.clientX && at.y === e.clientY) return;+ const first = at === null;+ pointer.current = { x: e.clientX, y: e.clientY };+ if (first) return;byKeyboard.current = false;setActive(index);}}