Skip to content

Commit

Permalink
fix(form): optimize AnnotationToolbarPopover
Browse files Browse the repository at this point in the history
The selection change listener caused a lot of re-renders, and should just return if the annotation currently isn't focused
  • Loading branch information
skogsmaskin authored and rexxars committed Sep 23, 2022
1 parent fc9ef8c commit 11b3a6e
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
return cursorRect
},
}
}, [cursorRect])
}, [cursorRect]) as HTMLElement

useEffect(() => {
if (!open) {
Expand Down Expand Up @@ -114,6 +114,7 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
// Detect selection changes
useEffect(() => {
function handleSelectionChange() {
if (!focused) return
if (!textElement) return
const winSelection = window.getSelection()

Expand All @@ -136,7 +137,7 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
return () => {
document.removeEventListener('selectionchange', handleSelectionChange)
}
}, [textElement])
}, [focused, textElement])

// Open popover when selection is within annotations
useEffect(() => {
Expand Down Expand Up @@ -165,6 +166,10 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
}
}, [focused, selection, annotationElement])

if (!open) {
return null
}

return (
<ToolbarPopover
boundaryElement={scrollElement}
Expand Down Expand Up @@ -200,7 +205,7 @@ export function AnnotationToolbarPopover(props: AnnotationToolbarPopoverProps) {
open={open}
placement="top"
portal="editor"
referenceElement={cursorElement as HTMLElement}
referenceElement={cursorElement}
scheme={popoverScheme}
/>
)
Expand Down

0 comments on commit 11b3a6e

Please sign in to comment.