Skip to content

Commit

Permalink
fix(component): fix lit portal not re-rendering in inline links case
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 26, 2024
1 parent 37e44e0 commit badcb3a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ type PortalEvent = {
type PortalListener = (event: PortalEvent) => void;

export function createLitPortalAnchor(callback: (event: PortalEvent) => void) {
const id = nanoid();
return html`<lit-react-portal
.notify=${callback}
portalId=${id}
portalId=${nanoid()}
></lit-react-portal>`;
}

Expand Down Expand Up @@ -119,24 +118,26 @@ export const useLitPortalFactory = () => {
}

const prevId = event.previousPortalId;
// Ignore first `willUpdate`
if (!prevId) {
return;
}

// No re-rendering allowed
// Used in `pdf embed view` scenario
if (!rerendering) {
// Ignores first `willUpdate`
if (!prevId) {
return;
}

setPortals(portals => {
const portal = portals.find(p => p.id === prevId);
if (!portal) return portals;
if (!portal) return [...portals];

// Updates `ID`
portal.id = id;
portal.portal.key = id;
portal.portal.children = element;

// Re-rendering
// true: `inline link`
// false: `pdf embed view`
if (rerendering) {
portal.portal = ReactDOM.createPortal(element, target, id);
}

return [...portals];
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export const BiDirectionalLinkPanel = () => {
{
<>
{portals.map(p => (
<Fragment key={p.id}>{p.portal}</Fragment>
<Fragment key={p.portal.key}>{p.portal}</Fragment>
))}
</>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const usePatchSpecs = (shared: boolean, mode: DocMode) => {
() => (
<>
{portals.map(p => (
<Fragment key={p.id}>{p.portal}</Fragment>
<Fragment key={p.portal.key}>{p.portal}</Fragment>
))}
</>
),
Expand Down

0 comments on commit badcb3a

Please sign in to comment.