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

124 lines
3.0 KiB
Svelte
Raw Normal View History

2023-10-08 15:38:42 -07:00
<script lang="ts">
2024-08-21 15:42:33 +02:00
import { onDestroy, onMount } from 'svelte';
2024-02-22 11:54:55 +01:00
import { fade } from 'svelte/transition';
2023-10-08 15:38:42 -07:00
2024-03-24 15:43:03 -07:00
import { flyAndScale } from '$lib/utils/transitions';
2025-05-08 13:29:24 +02:00
import * as FocusTrap from 'focus-trap';
2023-10-08 15:38:42 -07:00
export let show = true;
2024-01-05 20:59:56 -08:00
export let size = 'md';
2024-11-28 23:49:24 -08:00
export let containerClassName = 'p-3';
2025-04-02 19:01:39 -07:00
export let className = 'bg-white dark:bg-gray-900 rounded-2xl';
2024-01-05 20:59:56 -08:00
2024-04-07 00:57:23 -07:00
let modalElement = null;
2023-10-08 15:38:42 -07:00
let mounted = false;
// Create focus trap to trap user tabs inside modal
// https://www.w3.org/WAI/WCAG21/Understanding/focus-order.html
// https://www.w3.org/WAI/WCAG21/Understanding/keyboard.html
let focusTrap: FocusTrap.FocusTrap | null = null;
2023-10-08 15:38:42 -07:00
2024-01-05 20:59:56 -08:00
const sizeToWidth = (size) => {
2024-10-20 00:36:43 -07:00
if (size === 'full') {
return 'w-full';
}
2024-01-17 21:01:30 -08:00
if (size === 'xs') {
return 'w-[16rem]';
} else if (size === 'sm') {
2024-01-05 20:59:56 -08:00
return 'w-[30rem]';
2024-04-20 18:24:18 -05:00
} else if (size === 'md') {
2024-11-18 05:17:35 -08:00
return 'w-[42rem]';
2025-07-19 18:06:59 +04:00
} else if (size === 'lg') {
2024-05-19 00:17:40 -07:00
return 'w-[56rem]';
2025-07-19 18:06:59 +04:00
} else if (size === 'xl') {
return 'w-[70rem]';
} else if (size === '2xl') {
return 'w-[84rem]';
} else if (size === '3xl') {
return 'w-[100rem]';
2024-01-05 20:59:56 -08:00
}
};
2024-04-07 00:57:23 -07:00
const handleKeyDown = (event: KeyboardEvent) => {
2024-06-24 17:39:43 -07:00
if (event.key === 'Escape' && isTopModal()) {
2024-04-07 00:57:23 -07:00
console.log('Escape');
show = false;
}
};
2024-06-24 17:39:43 -07:00
const isTopModal = () => {
const modals = document.getElementsByClassName('modal');
return modals.length && modals[modals.length - 1] === modalElement;
};
2023-10-08 15:38:42 -07:00
onMount(() => {
mounted = true;
});
2025-05-08 13:29:24 +02:00
2024-06-18 15:39:50 -07:00
$: if (show && modalElement) {
document.body.appendChild(modalElement);
focusTrap = FocusTrap.createFocusTrap(modalElement);
focusTrap.activate();
2024-06-18 15:39:50 -07:00
window.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
} else if (modalElement) {
focusTrap.deactivate();
2024-06-18 15:39:50 -07:00
window.removeEventListener('keydown', handleKeyDown);
2024-06-24 17:39:43 -07:00
document.body.removeChild(modalElement);
2024-06-18 15:39:50 -07:00
document.body.style.overflow = 'unset';
2023-10-08 15:38:42 -07:00
}
2024-08-21 15:42:33 +02:00
onDestroy(() => {
show = false;
if (focusTrap) {
focusTrap.deactivate();
}
2024-08-21 15:42:33 +02:00
if (modalElement) {
document.body.removeChild(modalElement);
}
});
2023-10-08 15:38:42 -07:00
</script>
{#if show}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
2025-06-27 04:35:28 -04:00
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
2023-10-08 15:38:42 -07:00
<div
2024-04-07 00:57:23 -07:00
bind:this={modalElement}
aria-modal="true"
role="dialog"
2025-02-15 19: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 00:47:54 -08:00
in:fade={{ duration: 10 }}
on:mousedown={() => {
2024-02-23 01:21:22 -08:00
show = false;
}}
2023-10-08 15:38:42 -07:00
>
<div
2025-02-20 01: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-24 15:43:03 -07:00
in:flyAndScale
on:mousedown={(e) => {
2023-10-08 15:38:42 -07:00
e.stopPropagation();
}}
>
<slot />
</div>
</div>
{/if}
2024-02-22 15:20:48 +01:00
<style>
.modal-content {
animation: scaleUp 0.1s ease-out forwards;
}
@keyframes scaleUp {
from {
2024-02-23 01:23:06 -08:00
transform: scale(0.985);
2024-02-22 15:20:48 +01:00
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
</style>