Skip to content

Commit

Permalink
post: add doc about scroll of mobile device
Browse files Browse the repository at this point in the history
  • Loading branch information
gloriaJun committed Jun 28, 2023
1 parent 4fad294 commit 056119a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: 모바일 디바이스에서 키패드 활성화 여부 확인하기
tags: ['scroll', 'mobile-devie', 'keypad']
date: 2023-06-26T19:38
---

<!--truncate-->

모바일 디바이스에서 키패드의 활성화 여부를 체크하기 위해서는 사용자에게 보여지는 화면의 영역에 대한 뷰포트의 사이즈를 체크하여 확인할 있다.
해당 값은 `window.innerHeight`의 값을 확인할 있는데,
aos의 경우, 키패드가 활성화된 경우 뷰포트가 키패드 위로 줄어들면서 해당 값이 변화하게 되지만,
ios의 경우에는 키패드가 활성화되더라도 해당 값은 동일하게 되지만, `window.visualViewport.height` 값은 키패드가 제외된 화면의 영영의 사이즈를 의미한다

그러므로 아래와 같이 작성하여 키패드 활성화 유무를 판단할 있다.

```javascript
const webViewHeight = useRef < number > 0;

const isIOS = () => {
const agent = navigator.userAgent || navigator.vendor;
return /iPad|iPhone|iPod/i.test(agent);
};

useEffect(() => {
const handleVisualViewportResize = () => {
const viewportHeight = isIOS
? window.visualViewport.height
: window.innerHeight;

if (webViewHeight.current > viewportHeight) {
// keypad open
}
};

webViewHeight.current = window.innerHeight;
window.visualViewport.addEventListener('resize', handleVisualViewportResize);

return () => {
visualViewport.removeEventListener('resize', handleVisualViewportResize);
};
}, []);
```
## References
- [VisualViewport | MDN](https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport)
- [(WebView) aOS, iOS에서 키패드 유무 파악하기](https://velog.io/@dongkyun/WebView-aOS-iOS%EC%97%90%EC%84%9C-%ED%82%A4%ED%8C%A8%EB%93%9C-%EC%9C%A0%EB%AC%B4-%ED%8C%8C%EC%95%85%ED%95%98%EA%B8%B0)
2 changes: 0 additions & 2 deletions apps/doc/blog/frontend/2023/2023-06-27-scrollRestoration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ window.history.scrollRestoration = 'manual';
## References

- [History: scrollRestoration property | MDN](https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration)
- [(WebView) aOS, iOS에서 키패드 유무 파악하기](https://velog.io/@dongkyun/WebView-aOS-iOS%EC%97%90%EC%84%9C-%ED%82%A4%ED%8C%A8%EB%93%9C-%EC%9C%A0%EB%AC%B4-%ED%8C%8C%EC%95%85%ED%95%98%EA%B8%B0)
- [이슈) Fixed DOM과 가상 키보드](https://nuhends.tistory.com/2)

0 comments on commit 056119a

Please sign in to comment.