Skip to content

Commit

Permalink
Fix popover positioning bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on committed Nov 1, 2024
1 parent cf3d3b2 commit 19bf4db
Showing 1 changed file with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default function CreateTemplatePopoverPlugin({
const [editor] = useLexicalComposerContext()

const [popoverStyle, setPopoverStyle] = useState<CSSProperties | null>(null)
const [popoverWidth, setPopoverWidth] = useState(0)
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
const [isDialogOpen, setIsDialogOpen] = useState(false)

Expand All @@ -38,10 +37,6 @@ export default function CreateTemplatePopoverPlugin({
}, [editor])

const updatePopoverPosition = useCallback(() => {
if (popoverRef.current) {
setPopoverWidth(popoverRef.current.offsetWidth)
}

if (!anchorElement || !contentEditableElement) return
const nativeSelection = document.getSelection()
const range = nativeSelection?.getRangeAt(0)
Expand All @@ -62,7 +57,7 @@ export default function CreateTemplatePopoverPlugin({
const idealLeft = rects[rects.length - 1].right - anchorRect.left
const paddingX = 8
const paddingY = 4
const minLeft = popoverWidth + paddingX
const minLeft = (popoverRef.current?.offsetWidth ?? 0) + paddingX
const finalLeft = Math.max(minLeft, idealLeft)
setPopoverStyle({
top: rects[rects.length - 1].bottom - anchorRect.top + paddingY,
Expand All @@ -76,12 +71,7 @@ export default function CreateTemplatePopoverPlugin({
return
}
setIsPopoverOpen(true)
}, [
anchorElement,
contentEditableElement,
getSelectedSerializedNodes,
popoverWidth,
])
}, [anchorElement, contentEditableElement, getSelectedSerializedNodes])

useEffect(() => {
const handleSelectionChange = () => {
Expand Down Expand Up @@ -120,17 +110,16 @@ export default function CreateTemplatePopoverPlugin({
onOpenChange={setIsDialogOpen}
>
<Dialog.Trigger asChild>
{isPopoverOpen ? (
<button
ref={popoverRef}
style={{
position: 'absolute',
...popoverStyle,
}}
>
Create template
</button>
) : null}
<button
ref={popoverRef}
style={{
position: 'absolute',
visibility: isPopoverOpen ? 'visible' : 'hidden',
...popoverStyle,
}}
>
Create template
</button>
</Dialog.Trigger>
<CreateTemplateDialogContent
selectedSerializedNodes={getSelectedSerializedNodes()}
Expand Down

0 comments on commit 19bf4db

Please sign in to comment.