1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-19 13:27:58 +00:00
rolens/frontend/src/components/modal.svelte
2023-01-22 15:30:27 +01:00

128 lines
2.4 KiB
Svelte

<script context="module">
let numberOfModalsOpen = 0;
</script>
<script>
import { fade, fly } from 'svelte/transition';
import Icon from './icon.svelte';
export let show = false;
export let title = undefined;
export let contentPadding = true;
export let width = '80vw';
export let overflow = true;
const level = numberOfModalsOpen + 1;
let isNew = true;
$: if (show) {
numberOfModalsOpen++;
}
else if (!isNew) {
numberOfModalsOpen--;
}
else {
isNew = false;
}
function keydown(event) {
if ((event.key === 'Escape') && (level === numberOfModalsOpen)) {
event.preventDefault();
show = false;
}
}
</script>
<svelte:window on:keydown={keydown} />
{#if show}
<div class="modal outer" on:mousedown|self={() => show = false} transition:fade>
<div class="inner" style:max-width={width || '80vw'} transition:fly={{ y: 100 }}>
{#if title}
<header>
<div class="title">{title}</div>
<button class="btn close" on:click={() => show = false} title="close" type="button">
<Icon name="x" />
</button>
</header>
{/if}
<div class="slot content" class:padded={contentPadding} class:overflow> <slot /> </div>
{#if $$slots.footer}
<footer> <slot name="footer" /> </footer>
{/if}
</div>
</div>
{/if}
<style>
.outer {
position: fixed;
display: flex;
z-index: 100;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
margin: 0;
padding-top: 50px;
cursor: pointer;
}
:global(#root.platform-darwin) .outer {
margin-top: var(--darwin-titlebar-height);
}
.inner {
max-height: 80vh;
background-color: #fff;
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
width: 100%;
border-radius: 10px;
display: flex;
flex-flow: column;
cursor: auto;
}
.inner > :global(*:first-child) {
margin-top: 0;
}
.inner > :global(*:last-child) {
margin-bottom: 0;
}
header {
display: flex;
align-items: center;
padding: 1rem;
}
header .title {
font-size: 1.5rem;
}
.close {
margin-left: auto;
}
.content {
max-height: 100%;
}
.content.overflow {
overflow-y: auto;
}
.content.padded {
padding: 1rem;
}
header + .content.padded {
border-top: 1px solid #ccc;
}
footer {
padding: 1rem;
border-top: 1px solid #ccc;
}
</style>