A svelte use directive for click/tap outside an element.
npm install svelte-outside
Click outside
<script lang="ts">
import { clickOutside } from "svelte-outside"
</script>
<div use:clickOutside={(e) => console.log("Clicked outside!")}>
Click outside me!
</div>
Tap outside
<script lang="ts">
import { tapOutside } from "svelte-outside"
</script>
<div use:tapOutside={(e) => console.log("Tap outside!")}>
Tap outside me!
</div>