Skip to content

Commit e2e743a

Browse files
committed
✨ Use buffer resizing
1 parent eb18479 commit e2e743a

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/components/Chat/ChatContent/Message/View/EditView.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const EditView = ({
1414
content,
1515
setIsEdit,
1616
messageIndex,
17-
sticky,
17+
sticky
1818
}: {
1919
content: string;
2020
setIsEdit: React.Dispatch<React.SetStateAction<boolean>>;
@@ -28,6 +28,7 @@ const EditView = ({
2828
const [_content, _setContent] = useState<string>(content);
2929
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
3030
const textareaRef = React.createRef<HTMLTextAreaElement>();
31+
const bufferRef = React.createRef<HTMLTextAreaElement>();
3132

3233
const { t } = useTranslation();
3334

@@ -105,18 +106,23 @@ const EditView = ({
105106
handleSubmit();
106107
};
107108

108-
useEffect(() => {
109-
if (textareaRef.current) {
110-
textareaRef.current.style.height = 'auto';
111-
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
109+
const adjustTextareaHeight = () => {
110+
if (textareaRef.current && bufferRef.current) {
111+
const textarea = textareaRef.current;
112+
const buffer = bufferRef.current;
113+
114+
buffer.style.height = 'auto';
115+
buffer.style.height = buffer.scrollHeight + 'px';
116+
textarea.style.height = buffer.scrollHeight + 'px';
112117
}
118+
};
119+
120+
useEffect(() => {
121+
adjustTextareaHeight();
113122
}, [_content]);
114123

115124
useEffect(() => {
116-
if (textareaRef.current) {
117-
textareaRef.current.style.height = 'auto';
118-
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
119-
}
125+
adjustTextareaHeight();
120126
}, []);
121127

122128
return (
@@ -128,6 +134,7 @@ const EditView = ({
128134
: ''
129135
}`}
130136
>
137+
<div className="relative">
131138
<textarea
132139
ref={textareaRef}
133140
className='m-0 resize-none rounded-lg bg-transparent overflow-y-hidden focus:ring-0 focus-visible:ring-0 leading-7 w-full placeholder:text-gray-500/40'
@@ -139,6 +146,19 @@ const EditView = ({
139146
onKeyDown={handleKeyDown}
140147
rows={1}
141148
></textarea>
149+
<textarea
150+
ref={bufferRef}
151+
className='m-0 resize-none rounded-lg bg-transparent overflow-y-hidden focus:ring-0 focus-visible:ring-0 leading-7 w-full placeholder:text-gray-500/40 absolute top-0 left-0'
152+
style={{
153+
position: 'absolute',
154+
visibility: 'hidden',
155+
overflow: 'hidden'
156+
}}
157+
value={_content}
158+
readOnly
159+
rows={1}
160+
></textarea>
161+
</div>
142162
</div>
143163
<EditViewButtons
144164
sticky={sticky}

0 commit comments

Comments
 (0)