Skip to content

Commit 2209e93

Browse files
committed
Improve Floater positioning
1 parent f5a0ade commit 2209e93

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

packages/react/Floater.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import './style/floater.css'
22

3-
import React, { useMemo, useRef } from 'react'
3+
import React, { useEffect, useMemo, useRef, useState } from 'react'
44

55
import { useEditorState, useEditorView } from './EditorProvider'
66

77
export const Floater: React.FC = ({ children }) => {
88
const view = useEditorView()
99
const state = useEditorState()
10+
const [needsUpdate, setNeedsUpdate] = useState(Date.now())
11+
12+
// trigger redraw on resize and scroll events
13+
useEffect(() => {
14+
let handle: number
15+
16+
const handleEvent = () => {
17+
if (handle) {
18+
window.clearTimeout(handle)
19+
}
20+
21+
handle = window.setTimeout(() => {
22+
setNeedsUpdate(Date.now())
23+
}, 50)
24+
}
25+
26+
window.addEventListener('resize', handleEvent)
27+
window.addEventListener('scroll', handleEvent)
28+
29+
return () => {
30+
window.removeEventListener('resize', handleEvent)
31+
window.removeEventListener('scroll', handleEvent)
32+
}
33+
}, [])
1034

1135
const menuRef = useRef<HTMLDivElement>(null)
1236

@@ -26,7 +50,8 @@ export const Floater: React.FC = ({ children }) => {
2650
: coords.left,
2751
top: coords.top - 30 > 0 ? coords.top - 30 : coords.top + 30,
2852
}
29-
}, [menuRef, state, view])
53+
// eslint-disable-next-line react-hooks/exhaustive-deps
54+
}, [menuRef, state, view, needsUpdate])
3055

3156
return (
3257
<div ref={menuRef} className="prosemirror-floater" style={style}>

packages/react/style/floater.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
}
44

55
.prosemirror-floater {
6-
position: absolute;
6+
position: fixed;
77
z-index: 10;
88
display: flex;
99
flex-wrap: nowrap;

0 commit comments

Comments
 (0)