Bug: Safari-specific rendering issues with FloatingLinkEditor on image links
Lexical version: 0.44(latest) (tested on https://playground.lexical.dev)
Two related issues observed only on Safari (macOS) when interacting with image links via FloatingLinkEditor. Both reproduce with the official playground.
Issue 1: URL input is mispositioned when adding a link to an image
Steps To Reproduce
- Open the playground in Safari.
- Insert an image and click it to select.
- Click the link button in the toolbar (or press Cmd/Ctrl+K) to open the URL input.
The current behavior
The URL input box overlaps the image (around its vertical middle) instead of being positioned below or above. The input covers the image content while the user is typing.
This happens before the link is committed — at the moment the URL input first appears (no LinkNode exists in the editor model yet). Chrome and Firefox position the input correctly below the image.
The expected behavior
The URL input should appear below the image (or above when there isn't room below).
Issue 2: Image briefly disappears when the popup mounts/unmounts
Steps To Reproduce
- Open the playground in Safari.
- Insert an image and add a link to it.
- Click the link button or the trash icon to open/close the FloatingLinkEditor popup.
- Watch the image while the popup mounts/unmounts.
The current behavior
The image briefly disappears (single-frame flicker) when the FloatingLinkEditor popup is mounted or unmounted. The image returns immediately but the flicker is noticeable.
Chrome and Firefox do not exhibit this behavior.
The expected behavior
The image should remain visible during popup mount/unmount.
Notes
The image is rendered as a Lexical DecoratorNode (React-managed). The FloatingLinkEditor popup is rendered via createPortal with position: absolute (@floating-ui/react). Both issues likely share a root cause related to WebKit's reconciliation/layout timing for portal-mounted absolutely-positioned elements adjacent to React-managed decorator content.
PR #8419 addresses related FloatingLinkEditor mispositioning for multi-line and decorator-only links, but the symptoms above happen specifically on Safari before the LinkNode is materialized (Issue 1) or during portal mount/unmount (Issue 2), neither of which is covered by that PR's RangeSelection-branch fix.
Dug into both of these and they look like the same root cause.
When a link wraps an image, the image's React decorator briefly remounts (parent goes from paragraph to link), causing the 1-frame disappearance in Issue 2. floating-ui measures the reference during that window and gets a much smaller rect (line height without the image), which puts the toolbar at the wrong position in Issue 1. Once the image reappears, subsequent measurements are fine.
Things I tried that didn't help:
- removed
will-change: transformandtransition: opacityfrom.link-editor strategy: 'fixed', portal target ondocument.bodyuseLayoutEffect+update(),requestAnimationFrameto delay the popupisPositionedfromuseFloating- replaced the conditional render with
display: contents/none
Nothing keeps the image stable during the link transition on Safari. Still digging — probably need to either avoid the decorator remount on wrap, or cache a known-good rect to fall back on while the image is gone.
I dug into this and ended up tracing it through to the reconciler. Sharing what I found and pointing at a candidate PR.
Root cause
The 1-frame flicker (sub-issue 2) and the wrong-position-on-first-click (sub-issue 1) turn out to be the same underlying issue.
When $toggleLink wraps an image, the paragraph's children change from [ImageNode] to [LinkNode → [ImageNode]]. The image node itself keeps the same __key, only its parent changes. But the reconciler's $reconcileChildren (the prev=1, next=1 fast path) doesn't have a "moved across parents" branch. It sees a different child key and goes through $createNode + $destroyNode. Inside $createNode for the new link, the recursive call for the image goes through $createDOM again, producing a fresh <span> wrapper. storeDOMWithKey then overwrites _keyToDOMMap[imageKey] with the new wrapper.
useDecorators re-reads getElementByKey(imageKey) and calls createPortal(component, newWrapper, imageKey). Same portal key but a different container, so React unmounts the old portal and mounts a fresh one. <ImageComponent> remounts, and during the remount window the <img> isn't painted.
That explains both:
- The flicker (sub-issue 2): the unmount/remount blanks the image for one frame on Safari. Chrome's paint scheduling happens to hide it.
- The wrong position (sub-issue 1):
floating-ui'suseFloatingmeasures the reference span during the same window. The wrapper is briefly empty, sogetBoundingClientRectreturns a line-height-tall rect, and the toolbar lands on top of where the image used to be.
This is also why nothing on the floating-UI side reliably fixed it. Delaying with rAF, swapping middlewares, etc. were all just trying to dodge the flicker window.
Approach
Make the reconciler aware of cross-parent moves. In $createNode, if the same key existed in the previous tree under a different parent and activePrevKeyToDOMMap still holds its DOM, reuse that DOM (insert it into the new slot, then route through $reconcileNode for any internal updates). The DOM identity is preserved, so the React portal container stays the same and there's no unmount.
The change is in the reconciler diff algorithm. Same-parent reorders already had a "Move next" branch; cross-parent moves were the missing case.
PR
#8441 — covers the reconciler change, the supporting $destroyNode short-circuit and direction re-evaluation, plus two latent fragilities in lexical-list that the reuse path exposed (className insertion order and a conditional aria-checked set).
Open to feedback if a different fix shape is preferred.