Skip to content

Commit d628227

Browse files
authored
Fix top position below caret
1 parent d08304e commit d628227

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

src/text-expander-element.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ type Key = {
2020

2121
const states = new WeakMap()
2222

23-
function isTopLayer(el: Element) {
24-
try {
25-
if (el.matches(':popover-open')) return true
26-
} catch {
27-
/* fall through */
28-
}
29-
try {
30-
if (el.matches('dialog:modal')) return true
31-
} catch {
32-
/* fall through */
33-
}
34-
try {
35-
if (el.matches(':fullscreen')) return true
36-
} catch {
37-
/* fall through */
38-
}
39-
return false
40-
}
41-
4223
class TextExpander {
4324
expander: TextExpanderElement
4425
input: HTMLInputElement | HTMLTextAreaElement
@@ -103,18 +84,20 @@ class TextExpander {
10384

10485
this.expander.dispatchEvent(new Event('text-expander-activate'))
10586

106-
let {top, left} = new InputRange(this.input, match.position).getBoundingClientRect()
107-
if (isTopLayer(menu)) {
108-
const rect = this.input.getBoundingClientRect()
109-
top += rect.top
110-
left += rect.left
111-
if (getComputedStyle(menu).position === 'absolute') {
112-
top += window.scrollY
113-
left += window.scrollX
114-
}
115-
}
116-
menu.style.top = `${top}px`
117-
menu.style.left = `${left}px`
87+
const caretRect = new InputRange(this.input, match.position).getBoundingClientRect()
88+
const nominalPosition = {top: caretRect.top + caretRect.height, left: caretRect.left}
89+
menu.style.top = `${nominalPosition.top}px`
90+
menu.style.left = `${nominalPosition.left}px`
91+
92+
// Nominal position is relative to entire document, but the menu could be positioned relative to a container if
93+
// it is not `fixed` or on the top layer
94+
const actualPosition = menu.getBoundingClientRect()
95+
96+
const topDelta = actualPosition.top - nominalPosition.top
97+
if (topDelta !== 0) menu.style.top = `${nominalPosition.top - topDelta}px`
98+
99+
const leftDelta = actualPosition.left - nominalPosition.left
100+
if (leftDelta !== 0) menu.style.left = `${nominalPosition.left - leftDelta}px`
118101

119102
this.combobox.start()
120103
menu.addEventListener('combobox-commit', this.oncommit)

0 commit comments

Comments
 (0)