flotilla/src/app/keyboard.ts

30 lines
1 KiB
TypeScript
Raw Normal View History

2026-05-06 19:48:15 +00:00
import {App} from "@capacitor/app"
2025-11-26 01:06:21 +00:00
import {Capacitor} from "@capacitor/core"
2025-11-25 23:30:40 +00:00
import {Keyboard} from "@capacitor/keyboard"
2025-11-26 01:06:21 +00:00
import {noop} from "@welshman/lib"
2025-11-25 23:30:40 +00:00
export const syncKeyboard = () => {
if (!Capacitor.isPluginAvailable("Keyboard")) return noop
2025-11-25 23:30:40 +00:00
const showListener = Keyboard.addListener("keyboardWillShow", () => {
document.body.classList.add("keyboard-open")
})
const hideListener = Keyboard.addListener("keyboardWillHide", () => {
document.body.classList.remove("keyboard-open")
})
2026-05-06 19:48:15 +00:00
// On Android, system-dismissing the IME during pause doesn't fire keyboardWillHide,
// so on resume we force a hide to re-sync native insets and clear our CSS state.
const resumeListener = App.addListener("appStateChange", ({isActive}) => {
if (isActive) Keyboard.hide()
})
2025-11-25 23:30:40 +00:00
return () => {
showListener.then(listener => listener.remove())
hideListener.then(listener => listener.remove())
2026-05-06 19:48:15 +00:00
resumeListener.then(listener => listener.remove())
2025-11-25 23:30:40 +00:00
document.body.classList.remove("keyboard-open")
}
}