入力欄と直近発言が画面下にあるようなチャットアプリにおいて、iOSのバーチャルキーボードが被って不都合が起きていた
解決方法
const [logs, setLogs] = useState(...)useEffect(scrollToBottom, [logs])でレンダリング後にスクロールsetTimeout(scrollToBottom);するconst scrollToBottom = () => {
const e = document.getElementById("bottom") as HTMLElement;
const y = e.offsetTop - document.documentElement.clientHeight + 300;
if (y > 0) {
window.scrollTo(0, y);
}
};
- 300はバーチャルキーボードの高さ。デバイスから取得する方法がわからなかった
- 参考資料(*1)によれば301で、手元での11ProMaxでの実験でもまあ問題なさそうだったからこうした
- bottomは入力欄の下にhrを置いてる
関連 テキスト入力後にフォーカスが外れる 参考資料
https://github.com/mui-org/material-ui/blob/next/packages/material-ui/src/TextareaAutosize/TextareaAutosize.js pKeicho
window.innerHeightで取得する場合、上にスクロールをしてアドレスバーが表示されているときと、下にスクロールしてアドレスバーが表示されていないときでは取得できる値が異なる。 document.documentElement.clientHeightで取得する場合はアドレスバーによる影響はなく、常に一定の値が取得できる。
ios - What is the height of iPhone's onscreen keyboard? - Stack Overflow