Skip to content

Commit

Permalink
fix: portal fails gracefully if target doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-8 committed Jul 26, 2024
1 parent cb464dc commit 4f87cfe
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/lib/actions/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
* Usage: `<div use:portal>` or `<div use:portal={'#direction'}>`, add `hidden` if SSR rendered (requires updating action with node.hidden = false and true)
*/
export function portal(node: HTMLElement, target = 'body') {
const portal = document.createElement('div');
document.querySelector(target).appendChild(portal);
portal.appendChild(node);
const target_div = document.querySelector(target)
if (!target_div)
return

const portal = document.createElement('div')
target_div.appendChild(portal)
portal.appendChild(node)

return {
destroy() {
portal.parentElement.removeChild(portal);
portal.parentElement.removeChild(portal)
},
};
}
}
// from https://github.com/romkor/svelte-portal

0 comments on commit 4f87cfe

Please sign in to comment.