In chat apps where the input field and most recent statement are at the bottom of the screen, the iOS virtual keyboard was causing an inconvenience.
solution
const [logs, setLogs] = useState(...)useEffect(scrollToBottom, [logs]) to scroll after renderingsetTimeout(scrollToBottom); doconst scrollToBottom = () => {
const e = document.getElementById("bottom") as HTMLElement;
const y = e.offsetTop - document.documentElement.clientHeight + 300;
if (y > 0) {
window.scrollTo(0, y);
}
};
- 300 is the height of the virtual keyboard. Could not figure out how to get it from the device.
- According to the reference material (*1), it was 301, and the experiment with 11ProMax at hand seemed to be OK, so this is what we did.
- BOTTOM has hr under the input field.
Related Focus is lost after text entry. reference data
https://github.com/mui-org/material-ui/blob/next/packages/material-ui/src/TextareaAutosize/TextareaAutosize.js pKeicho
When retrieving with window.innerHeight, the value that can be obtained is different when the address bar is displayed by scrolling up and when the address bar is not displayed by scrolling down. When retrieving with document.documentElement.clientHeight, there is no effect by the address bar and a constant value can always be obtained.
Get screen size in Safari on iOS | chocolateorange.github.io
ios - What is the height of iPhone's onscreen keyboard? - Stack Overflow
This page is auto-translated from /nishio/入力欄と直近発言がキーボードとかぶる using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.