open-webui/src/lib/components/common/Modal.svelte

93 lines
2.0 KiB
Svelte
Raw Normal View History

2023-10-08 22:38:42 +00:00
<script lang="ts">
import { onMount } from 'svelte';
2024-02-22 10:54:55 +00:00
import { fade } from 'svelte/transition';
2023-10-08 22:38:42 +00:00
2024-03-24 22:43:03 +00:00
import { flyAndScale } from '$lib/utils/transitions';
2023-10-08 22:38:42 +00:00
export let show = true;
2024-01-06 04:59:56 +00:00
export let size = 'md';
2024-04-07 07:57:23 +00:00
let modalElement = null;
2023-10-08 22:38:42 +00:00
let mounted = false;
2024-01-06 04:59:56 +00:00
const sizeToWidth = (size) => {
2024-01-18 05:01:30 +00:00
if (size === 'xs') {
return 'w-[16rem]';
} else if (size === 'sm') {
2024-01-06 04:59:56 +00:00
return 'w-[30rem]';
2024-04-20 23:24:18 +00:00
} else if (size === 'md') {
return 'w-[48rem]';
2024-05-15 22:55:13 +00:00
} else {
2024-05-19 07:17:40 +00:00
return 'w-[56rem]';
2024-01-06 04:59:56 +00:00
}
};
2024-04-07 07:57:23 +00:00
const handleKeyDown = (event: KeyboardEvent) => {
2024-06-25 00:39:43 +00:00
if (event.key === 'Escape' && isTopModal()) {
2024-04-07 07:57:23 +00:00
console.log('Escape');
show = false;
}
};
2024-06-25 00:39:43 +00:00
const isTopModal = () => {
const modals = document.getElementsByClassName('modal');
return modals.length && modals[modals.length - 1] === modalElement;
};
2023-10-08 22:38:42 +00:00
onMount(() => {
mounted = true;
});
2024-06-18 22:39:50 +00:00
$: if (show && modalElement) {
document.body.appendChild(modalElement);
window.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
} else if (modalElement) {
window.removeEventListener('keydown', handleKeyDown);
2024-06-25 00:39:43 +00:00
document.body.removeChild(modalElement);
2024-06-18 22:39:50 +00:00
document.body.style.overflow = 'unset';
2023-10-08 22:38:42 +00:00
}
</script>
{#if show}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
2024-04-07 07:57:23 +00:00
bind:this={modalElement}
2024-06-25 00:39:43 +00:00
class="modal fixed top-0 right-0 left-0 bottom-0 bg-black/60 w-full min-h-screen h-screen flex justify-center z-[9999] overflow-hidden overscroll-contain"
2024-02-23 08:47:54 +00:00
in:fade={{ duration: 10 }}
on:mousedown={() => {
2024-02-23 09:21:22 +00:00
show = false;
}}
2023-10-08 22:38:42 +00:00
>
<div
2024-03-24 22:43:03 +00:00
class=" m-auto rounded-2xl max-w-full {sizeToWidth(
2024-01-06 04:59:56 +00:00
size
2024-07-09 02:26:31 +00:00
)} mx-2 bg-gray-50 dark:bg-gray-900 shadow-3xl max-h-[100dvh] overflow-y-auto scrollbar-hidden"
2024-03-24 22:43:03 +00:00
in:flyAndScale
on:mousedown={(e) => {
2023-10-08 22:38:42 +00:00
e.stopPropagation();
}}
>
<slot />
</div>
</div>
{/if}
2024-02-22 14:20:48 +00:00
<style>
.modal-content {
animation: scaleUp 0.1s ease-out forwards;
}
@keyframes scaleUp {
from {
2024-02-23 09:23:06 +00:00
transform: scale(0.985);
2024-02-22 14:20:48 +00:00
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
</style>