budibase/packages/bbui/src/Modal/Modal.svelte

49 lines
1.2 KiB
Svelte
Raw Normal View History

2021-03-31 11:59:07 +02:00
<script>
2021-04-08 14:47:57 +02:00
import "@spectrum-css/dialog/dist/index-vars.css"
import "@spectrum-css/modal/dist/index-vars.css"
import "@spectrum-css/underlay/dist/index-vars.css"
2021-03-31 11:59:07 +02:00
import { createEventDispatcher, setContext } from "svelte"
import { fade, fly } from "svelte/transition"
import Portal from "svelte-portal"
import Context from "../context"
const dispatch = createEventDispatcher()
let visible = false
$: dispatch(visible ? "show" : "hide")
export function show() {
if (visible) {
return
}
visible = true
}
export function hide() {
if (!visible) {
return
}
visible = false
}
function handleKey(e) {
if (visible && e.key === "Escape") {
hide()
}
}
setContext(Context.Modal, { show, hide })
</script>
<svelte:window on:keydown={handleKey} />
{#if visible}
<Portal target=".modal-container">
2021-04-08 14:47:57 +02:00
<div class="spectrum-Underlay is-open" transition:fade={{ duration: 200 }} on:click|self={hide}>
<div class="spectrum-Modal-wrapper">
<div class="spectrum-Modal is-open" transition:fly={{ y: 30, duration: 200 }}>
<slot />
2021-03-31 11:59:07 +02:00
</div>
2021-04-08 14:47:57 +02:00
</div>
2021-03-31 11:59:07 +02:00
</div>
</Portal>
2021-04-08 15:04:26 +02:00
{/if}