diff --git a/web/src/App.tsx b/web/src/App.tsx index c9cabf1..ce9f9f3 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -5,6 +5,7 @@ import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from import { PromptInput } from "@/components/ui/ai-chat-input"; import { Button } from "@/components/ui/button"; import { IntiMascot } from "@/components/ui/inti-mascot"; +import { useBlurOnKeyboardClose } from "@/lib/keyboardClose"; import { cn } from "@/lib/utils"; import { AgentError, askAgent } from "./chat/agentClient"; import { ChatMessage, initialsOf, TypingIndicator } from "./chat/ChatMessage"; @@ -70,6 +71,8 @@ export function App() { const { theme } = useTheme(); const { createWallet } = useCreateWallet(); const { exportWallet } = useExportWallet(); + // The header and tabs hide while typing on a phone; they come back when the keyboard closes. + useBlurOnKeyboardClose(); const [messages, setMessages] = useState([]); const [walletError, setWalletError] = useState(null); const [limitsOpen, setLimitsOpen] = useState(false); diff --git a/web/src/lib/keyboardClose.test.ts b/web/src/lib/keyboardClose.test.ts new file mode 100644 index 0000000..6bc4ad5 --- /dev/null +++ b/web/src/lib/keyboardClose.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; +import { keyboardCloseWatcher } from "./keyboardClose"; + +describe("keyboardCloseWatcher", () => { + it("says closed once when the height comes back after a keyboard", () => { + const closed = keyboardCloseWatcher(800, 400); + expect(closed(500, 400)).toBe(false); + expect(closed(650, 400)).toBe(false); + expect(closed(800, 400)).toBe(true); + expect(closed(800, 400)).toBe(false); + }); + + it("ignores small changes from the browser's own bars", () => { + const closed = keyboardCloseWatcher(800, 400); + expect(closed(740, 400)).toBe(false); + expect(closed(800, 400)).toBe(false); + }); + + it("starts over when the phone turns", () => { + const closed = keyboardCloseWatcher(800, 400); + expect(closed(500, 400)).toBe(false); + expect(closed(400, 800)).toBe(false); + expect(closed(400, 800)).toBe(false); + }); +}); diff --git a/web/src/lib/keyboardClose.ts b/web/src/lib/keyboardClose.ts new file mode 100644 index 0000000..c32e29a --- /dev/null +++ b/web/src/lib/keyboardClose.ts @@ -0,0 +1,51 @@ +import { useEffect } from "react"; + +/** Height a phone keyboard takes at least; smaller changes are the browser's own bars. */ +const KEYBOARD_MIN_PX = 150; + +/** + * Follows the visible height of the page and says when a phone keyboard has just closed: the + * height came back near its full size after dropping by a keyboard's height. A width change + * (the phone turned) starts over, since the full height changes with it. + */ +export function keyboardCloseWatcher(height: number, width: number): (height: number, width: number) => boolean { + let full = height; + let lastWidth = width; + let open = false; + return (h, w) => { + if (w !== lastWidth) { + lastWidth = w; + full = h; + open = false; + return false; + } + full = Math.max(full, h); + if (full - h >= KEYBOARD_MIN_PX) { + open = true; + return false; + } + if (!open) return false; + open = false; + return true; + }; +} + +/** + * On Android, closing the keyboard with the back or hide button leaves the text box focused, so + * anything hidden while typing would stay hidden. When the keyboard closes, the text box lets go + * of its focus and the menu comes back. + */ +export function useBlurOnKeyboardClose(): void { + useEffect(() => { + const viewport = window.visualViewport; + if (!viewport) return; + const closed = keyboardCloseWatcher(viewport.height, viewport.width); + const onResize = () => { + if (!closed(viewport.height, viewport.width)) return; + const focused = document.activeElement; + if (focused instanceof HTMLTextAreaElement) focused.blur(); + }; + viewport.addEventListener("resize", onResize); + return () => viewport.removeEventListener("resize", onResize); + }, []); +}