Skip to content

Commit ac7bebb

Browse files
authored
fix: sync measure div top with textarea (#130)
1 parent fed544c commit ac7bebb

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/Mentions.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';
33
import toArray from 'rc-util/lib/Children/toArray';
44
import KeyCode from 'rc-util/lib/KeyCode';
55
import warning from 'rc-util/lib/warning';
6-
import * as React from 'react';
7-
import { useState, useRef } from 'react';
6+
import React, { useState, useRef, useEffect } from 'react';
87
import TextArea from 'rc-textarea';
98
import type { TextAreaProps } from 'rc-textarea';
109
import KeywordTrigger from './KeywordTrigger';
@@ -112,6 +111,8 @@ const Mentions = React.forwardRef<MentionsRef, MentionsProps>((props, ref) => {
112111
const textareaRef = useRef<TextArea>(null);
113112
const measureRef = useRef<HTMLDivElement>(null);
114113

114+
const getTextArea = () => textareaRef.current?.resizableTextArea?.textArea;
115+
115116
React.useImperativeHandle(ref, () => ({
116117
focus: () => textareaRef.current?.focus(),
117118
blur: () => textareaRef.current?.blur(),
@@ -132,6 +133,13 @@ const Mentions = React.forwardRef<MentionsRef, MentionsProps>((props, ref) => {
132133
});
133134

134135
// =============================== Open ===============================
136+
useEffect(() => {
137+
// Sync measure div top with textarea for rc-trigger usage
138+
if (measuring && measureRef.current) {
139+
measureRef.current.scrollTop = getTextArea().scrollTop;
140+
}
141+
}, [measuring]);
142+
135143
const [
136144
mergedMeasuring,
137145
mergedMeasureText,
@@ -242,8 +250,6 @@ const Mentions = React.forwardRef<MentionsRef, MentionsProps>((props, ref) => {
242250
};
243251

244252
const selectOption = (option: OptionProps) => {
245-
const getTextArea = () => textareaRef.current?.resizableTextArea?.textArea;
246-
247253
const { value: mentionValue = '' } = option;
248254
const { text, selectionLocation } = replaceWithMeasure(mergedValue, {
249255
measureLocation: mergedMeasureLocation,

0 commit comments

Comments
 (0)