Files
open-webui/src/lib/components/common/Drawer.svelte
T
2026-10-24 08:49:32 +09:00

112 lines
2.9 KiB
Svelte

<script lang="ts">
import { onDestroy, onMount } from 'svelte';
import { flyAndScale } from '$lib/utils/transitions';
import { fade, fly, slide } from 'svelte/transition';
import { settings } from '$lib/stores';
import { matchKeybinding, Shortcut } from '$lib/shortcuts';
export let show = false;
export let className = '';
export let zIndexClass = 'z-999';
export let panelStyle = '';
export let backdropStyle = '';
export let swipeSide: 'left' | 'right' | undefined = undefined;
export let onClose = () => {};
export let onRequestClose: (() => void) | null = null;
const requestClose = () => (onRequestClose ? onRequestClose() : (show = false));
let modalElement = null;
let mounted = false;
const handleKeyDown = (event: KeyboardEvent) => {
if (
(event.key === 'Escape' ||
($settings?.keyboardShortcuts !== false &&
matchKeybinding(event) === Shortcut.CLOSE_MODAL)) &&
isTopModal()
) {
requestClose();
}
};
const isTopModal = () => {
const modals = document.getElementsByClassName('modal');
return modals.length && modals[modals.length - 1] === modalElement;
};
onMount(() => {
mounted = true;
});
$: if (show && modalElement) {
document.body.appendChild(modalElement);
window.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
} else if (modalElement) {
onClose();
window.removeEventListener('keydown', handleKeyDown);
if (document.body.contains(modalElement)) {
document.body.removeChild(modalElement);
document.body.style.overflow = 'unset';
}
}
onDestroy(() => {
window.removeEventListener('keydown', handleKeyDown);
show = false;
if (modalElement) {
if (document.body.contains(modalElement)) {
document.body.removeChild(modalElement);
document.body.style.overflow = 'unset';
}
}
});
</script>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
{#if show}
<div
bind:this={modalElement}
data-swipe-panel={swipeSide}
class="modal fixed right-0 bottom-0 left-0 {zIndexClass} flex h-screen max-h-[100dvh] w-full justify-center overflow-hidden overscroll-contain {swipeSide
? ''
: 'bg-black/60'}"
in:fly={{ y: swipeSide ? 0 : 100, duration: swipeSide ? 0 : 100 }}
on:mousedown={requestClose}
>
{#if swipeSide}
<div class="absolute inset-0 bg-black" style={backdropStyle}></div>
{/if}
<div
class="{swipeSide
? 'relative'
: ''} mt-auto w-full bg-gray-50 dark:bg-gray-900 dark:text-gray-100 {className} scrollbar-hidden max-h-[100dvh] overflow-y-auto"
style={panelStyle}
on:mousedown={(e) => {
e.stopPropagation();
}}
>
<slot />
</div>
</div>
{/if}
<style>
.modal-content {
animation: scaleUp 0.1s ease-out forwards;
}
@keyframes scaleUp {
from {
transform: scale(0.985);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
</style>