Skip to content

Commit b7a6475

Browse files
committed
feat: keyboard resize 추가
1 parent c39beae commit b7a6475

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/components/SelectTag/TagSheet/TagSheet.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Text from "@/components/ui/Text/Text";
1212

1313
import useOnClickOutside from "@/hooks/common/useClickOutside";
1414
import { useFocus } from "@/hooks/common/useFocus";
15+
import { useKeyboardResize } from "@/hooks/common/useKeyboardResize";
1516

1617
interface TagSheetProps {
1718
isOpen: boolean;
@@ -23,6 +24,8 @@ interface TagSheetProps {
2324
const TagSheet = ({ isOpen, tagList, handleClose, handleTagAdd }: TagSheetProps) => {
2425
const { isFocus, onFocus, onBlur } = useFocus({ defaultFocus: true });
2526

27+
const { keyboardVisible, keyboardHeight } = useKeyboardResize();
28+
2629
const [newTag, setNewTag] = useState("");
2730

2831
const modalRef = useRef<HTMLDivElement>(null);
@@ -103,7 +106,14 @@ const TagSheet = ({ isOpen, tagList, handleClose, handleTagAdd }: TagSheetProps)
103106
)}
104107
</div>
105108

106-
<Button text="추가하기" disabled={isInputError || isInputEmpty} onClick={handleAddTag} />
109+
<Button
110+
text="추가하기"
111+
disabled={isInputError || isInputEmpty}
112+
onClick={handleAddTag}
113+
style={{
114+
transform: `translateY(-${keyboardVisible ? keyboardHeight : 0}px)`,
115+
}}
116+
/>
107117
</Dialog.Content>
108118
</Dialog.Portal>
109119
</Dialog.Root>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useState, useEffect } from "react";
2+
3+
export const useKeyboardResize = () => {
4+
const [keyboardVisible, setKeyboardVisible] = useState(false);
5+
const [keyboardHeight, setKeyboardHeight] = useState(0);
6+
7+
useEffect(() => {
8+
let timeoutId: NodeJS.Timeout;
9+
10+
const handleVisualViewportChange = () => {
11+
clearTimeout(timeoutId);
12+
timeoutId = setTimeout(() => {
13+
if (window.visualViewport) {
14+
const isKeyboardVisible = window.visualViewport.height < window.innerHeight;
15+
setKeyboardVisible(isKeyboardVisible);
16+
setKeyboardHeight(
17+
isKeyboardVisible ? window.innerHeight - window.visualViewport.height : 0,
18+
);
19+
}
20+
}, 100);
21+
};
22+
23+
if (window.visualViewport) {
24+
window.visualViewport.addEventListener("resize", handleVisualViewportChange);
25+
handleVisualViewportChange();
26+
}
27+
28+
return () => {
29+
if (window.visualViewport) {
30+
window.visualViewport.removeEventListener("resize", handleVisualViewportChange);
31+
}
32+
clearTimeout(timeoutId);
33+
};
34+
}, []);
35+
36+
return { keyboardVisible, keyboardHeight };
37+
};

0 commit comments

Comments
 (0)