313 lines
7.2 KiB
Svelte
313 lines
7.2 KiB
Svelte
<script lang="ts">
|
|
import type { Snippet } from 'svelte';
|
|
import Icon from './Icon.svelte';
|
|
import { enterModal, leaveModal, topModal } from './modalLayers';
|
|
import { t } from '$lib/i18n.svelte';
|
|
|
|
interface Props {
|
|
open?: boolean;
|
|
title?: string;
|
|
size?: 'md' | 'lg' | 'xl';
|
|
dismissible?: boolean;
|
|
onclose?: () => void;
|
|
footer?: Snippet;
|
|
children: Snippet;
|
|
/** Open above a dock, retaining the shared dialog focus and keyboard behavior. */
|
|
anchor?: HTMLElement | null;
|
|
headerActions?: Snippet;
|
|
}
|
|
|
|
let {
|
|
open = $bindable(false),
|
|
title,
|
|
size = 'md',
|
|
dismissible = true,
|
|
onclose,
|
|
footer,
|
|
children,
|
|
anchor = null,
|
|
headerActions,
|
|
}: Props = $props();
|
|
|
|
const FOCUSABLE =
|
|
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
|
|
const titleId = `modal-title-${Math.random().toString(36).slice(2, 9)}`;
|
|
|
|
let panel = $state<HTMLDivElement | null>(null);
|
|
let anchorBox = $state({ left: 0, bottom: 0, width: 620, available: 560 });
|
|
$effect(() => {
|
|
if (!open || !anchor) return;
|
|
const source = anchor;
|
|
const place = () => {
|
|
const r = source.getBoundingClientRect();
|
|
anchorBox = {
|
|
left: r.left + 12,
|
|
bottom: innerHeight - r.top + 8,
|
|
width: r.width - 24,
|
|
available: Math.max(100, r.top - 64),
|
|
};
|
|
};
|
|
place();
|
|
const observer = new ResizeObserver(place);
|
|
observer.observe(source);
|
|
window.addEventListener('resize', place);
|
|
return () => {
|
|
observer.disconnect();
|
|
window.removeEventListener('resize', place);
|
|
};
|
|
});
|
|
const layer = Symbol('dialog');
|
|
|
|
function requestClose() {
|
|
if (!dismissible) return;
|
|
open = false;
|
|
}
|
|
|
|
function handleKeydown(event: KeyboardEvent) {
|
|
if (!topModal(layer)) return;
|
|
if (event.key === 'Escape') {
|
|
event.preventDefault();
|
|
requestClose();
|
|
return;
|
|
}
|
|
if (event.key === 'Tab') trapFocus(event);
|
|
}
|
|
|
|
function trapFocus(event: KeyboardEvent) {
|
|
if (!panel) return;
|
|
const focusables = Array.from(
|
|
panel.querySelectorAll<HTMLElement>(FOCUSABLE),
|
|
).filter((el) => el.getClientRects().length > 0);
|
|
if (focusables.length === 0) {
|
|
event.preventDefault();
|
|
panel.focus();
|
|
return;
|
|
}
|
|
const first = focusables[0];
|
|
const last = focusables[focusables.length - 1];
|
|
const active = document.activeElement;
|
|
const focusInside = panel.contains(active);
|
|
if (event.shiftKey && (!focusInside || active === first)) {
|
|
event.preventDefault();
|
|
last.focus();
|
|
} else if (!event.shiftKey && (!focusInside || active === last)) {
|
|
event.preventDefault();
|
|
first.focus();
|
|
}
|
|
}
|
|
|
|
function handleBackdropClick(event: MouseEvent) {
|
|
if (event.target === event.currentTarget) requestClose();
|
|
}
|
|
|
|
$effect(() => {
|
|
if (!open) return;
|
|
|
|
const invoker = document.activeElement as HTMLElement | null;
|
|
enterModal(layer);
|
|
document.addEventListener('keydown', handleKeydown, true);
|
|
|
|
requestAnimationFrame(() => {
|
|
if (!topModal(layer) || !panel || panel.contains(document.activeElement))
|
|
return;
|
|
const first = panel.querySelector<HTMLElement>(FOCUSABLE);
|
|
(first ?? panel).focus();
|
|
});
|
|
|
|
return () => {
|
|
const wasTop = topModal(layer);
|
|
leaveModal(layer);
|
|
document.removeEventListener('keydown', handleKeydown, true);
|
|
if (wasTop && invoker?.isConnected) invoker.focus();
|
|
onclose?.();
|
|
};
|
|
});
|
|
</script>
|
|
|
|
{#if open}
|
|
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
|
<div
|
|
class="modal-backdrop"
|
|
class:anchored={!!anchor}
|
|
onclick={handleBackdropClick}
|
|
>
|
|
<div
|
|
bind:this={panel}
|
|
class="modal-panel size-{size}"
|
|
style:left={anchor ? `${anchorBox.left}px` : undefined}
|
|
style:bottom={anchor ? `${anchorBox.bottom}px` : undefined}
|
|
style:width={anchor
|
|
? `min(var(--desk-pop-width), ${anchorBox.width}px)`
|
|
: undefined}
|
|
style:--anchor-available={`${anchorBox.available}px`}
|
|
role="dialog"
|
|
aria-modal="true"
|
|
aria-labelledby={title ? titleId : undefined}
|
|
aria-label={title ? undefined : t('modal.dialog')}
|
|
tabindex="-1"
|
|
>
|
|
{#if title || dismissible}
|
|
<div class="modal-header">
|
|
{#if title}
|
|
<h2 class="modal-title" id={titleId}>{title}</h2>
|
|
{/if}
|
|
{@render headerActions?.()}
|
|
{#if dismissible}
|
|
<button
|
|
type="button"
|
|
class="modal-close"
|
|
aria-label={t('modal.closeDialog')}
|
|
onclick={requestClose}
|
|
>
|
|
<Icon name="close" size={16} />
|
|
</button>
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
<div class="modal-body">
|
|
{@render children()}
|
|
</div>
|
|
{#if footer}
|
|
<div class="modal-footer">
|
|
{@render footer()}
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
{/if}
|
|
|
|
<style>
|
|
.modal-backdrop {
|
|
position: fixed;
|
|
inset: 0;
|
|
z-index: 900;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 24px;
|
|
background: var(--overlay);
|
|
animation: modal-fade var(--d-state) ease-out;
|
|
}
|
|
.modal-panel {
|
|
width: min(520px, 100%);
|
|
max-height: calc(100vh - 96px);
|
|
display: flex;
|
|
flex-direction: column;
|
|
background: var(--float-bg);
|
|
border-radius: var(--r-lg);
|
|
box-shadow: var(--shadow-float);
|
|
outline: none;
|
|
animation: modal-rise var(--d-pop) var(--ease-out);
|
|
}
|
|
.modal-panel.size-lg {
|
|
width: min(920px, 100%);
|
|
}
|
|
.modal-panel.size-xl {
|
|
width: min(1180px, 100%);
|
|
}
|
|
.anchored {
|
|
background: transparent;
|
|
padding: 0;
|
|
}
|
|
.anchored .modal-panel {
|
|
box-sizing: border-box;
|
|
position: absolute;
|
|
height: var(--desk-pop-height);
|
|
max-height: min(64vh, var(--anchor-available));
|
|
border: 1px solid var(--line-2);
|
|
border-radius: var(--desk-dock-radius);
|
|
background: var(--desk-pop-bg);
|
|
box-shadow: var(--desk-pop-shadow);
|
|
backdrop-filter: blur(14px);
|
|
transform-origin: bottom left;
|
|
}
|
|
.anchored .modal-header {
|
|
padding: 12px 14px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
.anchored .modal-title {
|
|
font-size: var(--fs-body);
|
|
}
|
|
.anchored .modal-body {
|
|
flex: 1;
|
|
min-height: 0;
|
|
padding: 12px 14px;
|
|
}
|
|
.anchored .modal-footer {
|
|
padding: 10px 14px;
|
|
border-top: 1px solid var(--line);
|
|
}
|
|
.modal-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 12px;
|
|
padding: 18px 20px 0;
|
|
}
|
|
.modal-title {
|
|
margin: 0;
|
|
font-family: var(--font-display);
|
|
font-size: var(--fs-title);
|
|
font-weight: 600;
|
|
color: var(--text-primary);
|
|
}
|
|
.modal-close {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 28px;
|
|
height: 28px;
|
|
margin-left: auto;
|
|
border: none;
|
|
border-radius: var(--radius-sm);
|
|
background: transparent;
|
|
color: var(--text-muted);
|
|
cursor: pointer;
|
|
transition:
|
|
background-color var(--d-state) ease,
|
|
color var(--d-state) ease;
|
|
}
|
|
@media (hover: hover) and (pointer: fine) {
|
|
.modal-close:hover {
|
|
background: var(--bg-4);
|
|
color: var(--text-primary);
|
|
}
|
|
}
|
|
.modal-close:focus-visible {
|
|
outline: 2px solid var(--accent);
|
|
outline-offset: 2px;
|
|
}
|
|
.modal-body {
|
|
padding: 16px 20px 20px;
|
|
overflow-y: auto;
|
|
font-size: 14px;
|
|
line-height: 1.5;
|
|
color: var(--text-primary);
|
|
}
|
|
.modal-footer {
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
gap: 8px;
|
|
padding: 0 20px 20px;
|
|
}
|
|
@keyframes modal-fade {
|
|
from {
|
|
opacity: 0;
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
}
|
|
}
|
|
@keyframes modal-rise {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(8px);
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
}
|
|
</style>
|