Files
immich/web/src/lib/components/shared-components/portal/portal.svelte
T
Ben McCann 907a95a746 chore(web): cleanup promise handling (#7382)
* no-misused-promises

* no-floating-promises

* format

* revert for now

* remove load function

* require-await

* revert a few no-floating-promises changes that would cause no-misused-promises failures

* format

* fix a few more

* fix most remaining errors

* executor-queue

* executor-queue.spec

* remove duplicate comments by grouping rules

* upgrade sveltekit and enforce rules

* oops. move await

* try this

* just ignore for now since it's only a test

* run in parallel

* Update web/src/routes/admin/jobs-status/+page.svelte

Co-authored-by: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com>

* remove Promise.resolve call

* rename function

* remove unnecessary warning silencing

* make handleError sync

* fix new errors from recently merged PR to main

* extract method

* use handlePromiseError

---------

Co-authored-by: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2024-02-27 10:37:37 -06:00

58 lines
1.5 KiB
Svelte

<script context="module" lang="ts">
import { handlePromiseError } from '$lib/utils';
import { tick } from 'svelte';
/**
* Usage: <div use:portal={'css selector'}> or <div use:portal={document.body}>
*/
export function portal(element: HTMLElement, target: HTMLElement | string = 'body') {
let targetElement;
async function update(newTarget: HTMLElement | string) {
target = newTarget;
if (typeof target === 'string') {
targetElement = document.querySelector(target);
if (targetElement === null) {
await tick();
targetElement = document.querySelector(target);
}
if (targetElement === null) {
throw new Error(`No element found matching css selector: "${target}"`);
}
} else if (target instanceof HTMLElement) {
targetElement = target;
} else {
throw new TypeError(
`Unknown portal target type: ${
target === null ? 'null' : typeof target
}. Allowed types: string (CSS selector) or HTMLElement.`,
);
}
targetElement.append(element);
element.hidden = false;
}
function destroy() {
if (element.parentNode) {
element.remove();
}
}
handlePromiseError(update(target));
return {
update,
destroy,
};
}
</script>
<script lang="ts">
/**
* DOM Element or CSS Selector
*/
export let target: HTMLElement | string = 'body';
</script>
<div use:portal={target} hidden>
<slot />
</div>