Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto z-index to Modal instances #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/lib/components/Modal.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script context="module">
let counter = 0;
</script>

<script lang="ts">
import { tick } from "svelte";

Expand All @@ -9,19 +13,21 @@
show = false;
tick().finally(cleanup);
}

$: count = show ? ++counter : -1;
</script>

<slot name="backdrop" {close}>
{#if closeOnClickOutside}
<div role="presentation" class="fixed inset-0 transition duration-1000" class:show class:pointer-events-none={!show} on:click={close} />
<div role="presentation" class="fixed inset-0 transition duration-1000" class:show style:z-index={count} class:pointer-events-none={!show} on:click={close} />
{:else}
<div class="pointer-events-none fixed inset-0 transition duration-1000" class:show />
<div class="pointer-events-none fixed inset-0 transition duration-1000" class:show style:z-index={count} />
{/if}
</slot>

{#if show}
<slot {close}>
<div class="pointer-events-none fixed inset-0 grid place-items-center [&>*]:pointer-events-auto">
<div class="pointer-events-none fixed inset-0 grid place-items-center [&>*]:pointer-events-auto" style:z-index={count}>
<slot name="content" {close} />
</div>
</slot>
Expand Down
22 changes: 22 additions & 0 deletions src/routes/test/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import Modal from "$lib/components/Modal.svelte";
import { onMount } from "svelte";

let show = false;

onMount(() => {
setTimeout(() => {
show = true;
}, 100);
});
</script>

<Modal {show}>
<div slot="content" class="h-30 w-70 bg-cyan" />
</Modal>

<Modal show>
<div slot="content" class="h-60 w-60 bg-red">
</Modal>

<div class="fixed bottom-20 self-center">the cyan square should be on top of the red one</div>
Loading