Skip to content

Commit

Permalink
Hotfix: 댓글 및 대댓글 하단 공백 오류 수정 (#80)
Browse files Browse the repository at this point in the history
* refactor: 불필요한 로직 제거 및 스크롤 포커싱 focus 로직으로 기본으로 동작하도록 변경

* fix: 댓글 입력창이 하단에 거의 붙어 있는 상태에서 클릭 시 포커싱이 제대로 되지 않는 오류 수정
  • Loading branch information
semnil5202 authored Mar 23, 2024
1 parent c04b956 commit 949db5d
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions src/pages/FeedDetail/contexts/CommentFocusContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useContext, useState, createContext, MutableRefObject, useRef, ReactNode } from 'react';

import { useMobileViewRefContext } from '../../../layouts/contexts/MobileViewContext';

interface CommentFocusContextType {
isFocusComment: boolean;
openCommentTextarea: () => void;
Expand All @@ -19,44 +17,27 @@ interface Props {
children: ReactNode;
}

interface FocusTextareaRefProps {
textareaRef: MutableRefObject<HTMLTextAreaElement | null>;
mobileViewRef: MutableRefObject<HTMLElement | null>;
}

const CommentFocusContext = createContext<CommentFocusContextType | null>(null);
const RATIO = window.innerHeight / window.innerWidth;

const focusTextareaRef = ({ textareaRef, mobileViewRef }: FocusTextareaRefProps) => {
const focusTextareaRef = (textareaRef: MutableRefObject<HTMLTextAreaElement | null>) => {
if (!textareaRef.current) return;

textareaRef.current.focus();

if (!mobileViewRef.current || !window.visualViewport) return;

const textareaRect = textareaRef.current.getBoundingClientRect();
const mobileViewHeight = mobileViewRef.current.getBoundingClientRect().height;
const currentRatioDiff = window.innerHeight / window.innerWidth - RATIO;
const keyboardHeight = window.innerHeight * currentRatioDiff;

if (currentRatioDiff > 0 && mobileViewHeight - textareaRect.y < keyboardHeight) {
textareaRef.current.scrollIntoView({ block: 'center', behavior: 'smooth' });
}
textareaRef.current.scrollIntoView({ block: 'center', behavior: 'smooth' });
};

const initTextareaRef = (textareaRef: MutableRefObject<HTMLTextAreaElement | null>) => {
textareaRef.current = null;
};

export const CommentFocusProvider = ({ children }: Props) => {
const mobileViewRef = useMobileViewRefContext();
const [isFocusComment, setIsFocusComment] = useState<boolean>(false);
const commentTextareaRef = useRef<HTMLTextAreaElement | null>(null);
const recommentTextareaRef = useRef<HTMLTextAreaElement | null>(null);
const editCommentTextareaRef = useRef<HTMLTextAreaElement | null>(null);

const openCommentTextarea = () => {
focusTextareaRef({ textareaRef: commentTextareaRef, mobileViewRef });
focusTextareaRef(commentTextareaRef);

setIsFocusComment(true);
};
Expand All @@ -66,15 +47,15 @@ export const CommentFocusProvider = ({ children }: Props) => {
};

const focusRecommentTextarea = () => {
focusTextareaRef({ textareaRef: recommentTextareaRef, mobileViewRef });
focusTextareaRef(recommentTextareaRef);
};

const initRecommentTextarea = () => {
initTextareaRef(recommentTextareaRef);
};

const focusEditCommentTextarea = () => {
focusTextareaRef({ textareaRef: editCommentTextareaRef, mobileViewRef });
focusTextareaRef(editCommentTextareaRef);
};

const initEditCommentTextarea = () => {
Expand Down

0 comments on commit 949db5d

Please sign in to comment.