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
50 changes: 49 additions & 1 deletion mobile/src/platform/keyboard-close-blur.web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function shellReports(height: number): void {
}
}

const ANDROID = 'Mozilla/5.0 (Linux; Android 16) AppleWebKit/537.36'

function focusedField(userAgent: string): HTMLInputElement {
Object.defineProperty(navigator, 'userAgent', { configurable: true, value: userAgent })
const field = document.createElement('input')
Expand All @@ -35,6 +37,7 @@ function focusedField(userAgent: string): HTMLInputElement {

describe('a keyboard the phone closes', () => {
beforeEach(() => {
vi.useFakeTimers({ toFake: ['setTimeout', 'performance'] })
publishShellKeyboardSource({
read: () => shellHeight,
subscribe: (listener) => {
Expand All @@ -45,14 +48,15 @@ describe('a keyboard the phone closes', () => {
})

afterEach(() => {
vi.useRealTimers()
shellListeners.clear()
shellHeight = 0
publishShellKeyboardSource(null)
document.body.replaceChildren()
})

it("ends its field's focus on Android, so the next touch cannot raise it again", () => {
const field = focusedField('Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36')
const field = focusedField(ANDROID)
startBlurringOnKeyboardClose()
shellReports(300)
expect(document.activeElement).toBe(field)
Expand All @@ -62,6 +66,50 @@ describe('a keyboard the phone closes', () => {
expect(document.activeElement).not.toBe(field)
})

// The second case is tapping the underlined word after Back: that tap both focuses the field and opens the menu.
it.each([
['already focused', false],
['focused by the tap', true]
] as const)(
'keeps focus and reopens the keyboard when Chromium hides it for a tapped autocorrected word in a field %s',
(_case, tapFocuses) => {
const field = focusedField(ANDROID)
if (tapFocuses) {
field.blur()
}
startBlurringOnKeyboardClose()

field.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }))
TextInputState.focusTextInput(field)
shellReports(300)
const focusSessions = vi.fn()
field.addEventListener('focus', focusSessions)
vi.advanceTimersByTime(400)
shellReports(0)
vi.advanceTimersByTime(100)

expect(document.activeElement).toBe(field)
expect(focusSessions).toHaveBeenCalledOnce()
}
)

it.each([
['a touch elsewhere', 'body', 400],
['a Back long after a touch on the field', 'field', 5000]
] as const)('still ends focus on a close after %s', (_case, touched, waitMs) => {
const field = focusedField(ANDROID)
startBlurringOnKeyboardClose()
shellReports(300)

const target = touched === 'field' ? field : document.body
target.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }))
vi.advanceTimersByTime(waitMs)
shellReports(0)
vi.advanceTimersByTime(100)

expect(document.activeElement).not.toBe(field)
})

it('leaves focus alone on iOS, where only a blur closes the keyboard', () => {
const field = focusedField('Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X)')
startBlurringOnKeyboardClose()
Expand Down
46 changes: 41 additions & 5 deletions mobile/src/platform/keyboard-close-blur.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,48 @@ import { Keyboard } from 'react-native'
import { hostOs } from './host-os.web'
import { subscribeSoftKeyboard } from './keyboard-occlusion.web'

// Chromium hides the IME itself to show a tapped autocorrected word's menu; Back comes with no touch.
const FIELD_TAP_HIDE_WINDOW_MS = 1000
const KEYBOARD_REOPEN_DELAY_MS = 50

// Android raises the IME only for a new focus session, as for the terminal live input.
function reopenKeyboard(field: HTMLElement): void {
if (document.activeElement === field) {
field.blur()
field.focus()
}
}

// Android Back hides the IME without blurring; blur now so the next terminal touch cannot reopen it.
export function startBlurringOnKeyboardClose(): void {
if (hostOs() === 'android') {
subscribeSoftKeyboard(
() => {},
() => Keyboard.dismiss()
)
if (hostOs() !== 'android') {
return
}
let lastTouch: { target: Node; at: number } | null = null
document.addEventListener(
'pointerdown',
(event) => {
lastTouch =
event.target instanceof Node ? { target: event.target, at: performance.now() } : null
},
{ capture: true }
)
subscribeSoftKeyboard(
() => {},
() => {
const field = document.activeElement
const touch = lastTouch
lastTouch = null
if (
field instanceof HTMLElement &&
touch !== null &&
field.contains(touch.target) &&
performance.now() - touch.at <= FIELD_TAP_HIDE_WINDOW_MS
) {
setTimeout(() => reopenKeyboard(field), KEYBOARD_REOPEN_DELAY_MS)
} else {
Keyboard.dismiss()
}
}
)
}
Loading