Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<Message[]>([]);
const [walletError, setWalletError] = useState<string | null>(null);
const [limitsOpen, setLimitsOpen] = useState(false);
Expand Down
25 changes: 25 additions & 0 deletions web/src/lib/keyboardClose.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
51 changes: 51 additions & 0 deletions web/src/lib/keyboardClose.ts
Original file line number Diff line number Diff line change
@@ -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);
}, []);
}
Loading