fix(component): lit portal not re-rendering in inline links case (#9321)

* uses `portal.key` in templates
* updates `portal.id`  for use in queries
This commit is contained in:
fundon
2024-12-26 13:11:31 +00:00
committed by Fangdun Tsai
parent 835e7c434e
commit 6fcdd015aa
7 changed files with 177 additions and 26 deletions

View File

@@ -2,4 +2,4 @@ export {
type ElementOrFactory,
useLitPortal,
useLitPortalFactory,
} from './lite-portal';
} from './lit-portal';

View File

@@ -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>`;
}
@@ -119,24 +118,27 @@ 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`
// Used for portal queries in `disconnectedCallback`
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];
});
});