Skip to content

Commit a539564

Browse files
Fuphoenixesphoenizhao
andauthored
fix(mention): panel has wrong position if component has padding or placement is set to top (#6242)
Co-authored-by: phoenizhao <[email protected]>
1 parent 2d34e3a commit a539564

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/mention/src/Mention.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export default defineComponent({
153153
const inputInstRef = ref<InputInst | null>(null)
154154
const cursorRef = ref<HTMLElement | null>(null)
155155
const followerRef = ref<FollowerInst | null>(null)
156+
const wrapperElRef = ref<HTMLElement | null>(null)
156157
const partialPatternRef = ref<string>('')
157158
let cachedPrefix: string | null = null
158159
// cached pattern end is for partial pattern
@@ -277,9 +278,11 @@ export default defineComponent({
277278
top: number
278279
height: number
279280
} = getRelativePosition(inputEl)
280-
cursorPos.left += inputEl.parentElement!.offsetLeft
281-
cursorAnchor.style.left = `${cursorPos.left}px`
282-
cursorAnchor.style.top = `${cursorPos.top + cursorPos.height}px`
281+
const inputRect = inputEl.getBoundingClientRect()
282+
const wrapperRect = wrapperElRef.value!.getBoundingClientRect()
283+
cursorAnchor.style.left = `${cursorPos.left + inputRect.left - wrapperRect.left}px`
284+
cursorAnchor.style.top = `${cursorPos.top + inputRect.top - wrapperRect.top}px`
285+
cursorAnchor.style.height = `${cursorPos.height}px`
283286
}
284287
function syncPosition(): void {
285288
if (!showMenuRef.value)
@@ -419,6 +422,7 @@ export default defineComponent({
419422
inputInstRef,
420423
cursorRef,
421424
followerRef,
425+
wrapperElRef,
422426
showMenu: showMenuRef,
423427
adjustedTo: useAdjustedTo(props),
424428
isMounted: useIsMounted(),
@@ -439,7 +443,7 @@ export default defineComponent({
439443
render() {
440444
const { mergedTheme, mergedClsPrefix, $slots } = this
441445
return (
442-
<div class={`${mergedClsPrefix}-mention`}>
446+
<div class={`${mergedClsPrefix}-mention`} ref="wrapperElRef">
443447
<NInput
444448
status={this.mergedStatus}
445449
themeOverrides={mergedTheme.peerOverrides.Input}
@@ -466,12 +470,10 @@ export default defineComponent({
466470
default: () => {
467471
const style: CSSProperties = {
468472
position: 'absolute',
469-
width: 0,
470-
height: 0
473+
width: 0
471474
}
472475
if (__DEV__ && this.internalDebug) {
473476
style.width = '1px'
474-
style.height = '1px'
475477
style.background = 'red'
476478
}
477479
return <div style={style} ref="cursorRef" />

0 commit comments

Comments
 (0)