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

106 lines
2.4 KiB
Svelte
Raw Normal View History

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