264 lines
6.7 KiB
Svelte
264 lines
6.7 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';
|
|
|
|
/**
|
|
* Drawer — the shared right-side floating layer (HEL-649 §四.3):
|
|
* slides in over 280ms with --ease-drawer and reverses mid-flight,
|
|
* focus is trapped while open, Esc / backdrop click close it, and
|
|
* reduced-motion keeps only a ≤120ms opacity fade (no slide).
|
|
*/
|
|
interface Props {
|
|
open?: boolean;
|
|
title?: string;
|
|
/** Panel width on >760px screens. */
|
|
width?: number;
|
|
dismissible?: boolean;
|
|
onclose?: () => void;
|
|
footer?: Snippet;
|
|
children: Snippet;
|
|
}
|
|
|
|
let {
|
|
open = $bindable(false),
|
|
title,
|
|
width = 380,
|
|
dismissible = true,
|
|
onclose,
|
|
footer,
|
|
children,
|
|
}: Props = $props();
|
|
|
|
const FOCUSABLE =
|
|
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
|
|
const titleId = `drawer-title-${Math.random().toString(36).slice(2, 9)}`;
|
|
|
|
let panel = $state<HTMLDivElement | null>(null);
|
|
const layer = Symbol('drawer');
|
|
|
|
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="drawer-backdrop" onclick={handleBackdropClick}>
|
|
<div
|
|
bind:this={panel}
|
|
class="drawer-panel"
|
|
style:--drawer-w="{width}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="drawer-header">
|
|
{#if title}
|
|
<h2 class="drawer-title" id={titleId}>{title}</h2>
|
|
{/if}
|
|
{#if dismissible}
|
|
<button
|
|
type="button"
|
|
class="drawer-close"
|
|
aria-label={t('modal.closeDialog')}
|
|
onclick={requestClose}
|
|
>
|
|
<Icon name="close" size={16} />
|
|
</button>
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
<div class="drawer-body">
|
|
{@render children()}
|
|
</div>
|
|
{#if footer}
|
|
<div class="drawer-footer">
|
|
{@render footer()}
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
{/if}
|
|
|
|
<style>
|
|
.drawer-backdrop {
|
|
position: fixed;
|
|
inset: 0;
|
|
z-index: 900;
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
background: var(--overlay);
|
|
animation: drawer-fade var(--d-state) ease-out;
|
|
}
|
|
.drawer-panel {
|
|
width: min(var(--drawer-w), 100vw);
|
|
height: 100dvh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
background: var(--float-bg);
|
|
border-radius: var(--r-lg) 0 0 var(--r-lg);
|
|
box-shadow: var(--shadow-float);
|
|
outline: none;
|
|
animation: var(--drawer-animation, drawer-slide) var(--d-drawer) var(--ease-drawer) both;
|
|
}
|
|
.drawer-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 12px;
|
|
padding: 16px 20px 0;
|
|
flex-shrink: 0;
|
|
}
|
|
.drawer-title {
|
|
margin: 0;
|
|
font-size: var(--fs-title);
|
|
font-weight: 600;
|
|
color: var(--text);
|
|
}
|
|
.drawer-close {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 28px;
|
|
height: 28px;
|
|
margin-left: auto;
|
|
border: none;
|
|
border-radius: var(--r-sm);
|
|
background: transparent;
|
|
color: var(--text-3);
|
|
cursor: pointer;
|
|
transition:
|
|
background-color var(--d-state) ease,
|
|
color var(--d-state) ease;
|
|
}
|
|
@media (hover: hover) and (pointer: fine) {
|
|
.drawer-close:hover {
|
|
background: var(--bg-4);
|
|
color: var(--text);
|
|
}
|
|
}
|
|
.drawer-close:focus-visible {
|
|
outline: 2px solid var(--accent);
|
|
outline-offset: 2px;
|
|
}
|
|
.drawer-body {
|
|
padding: 14px 20px 20px;
|
|
overflow-y: auto;
|
|
flex: 1;
|
|
min-height: 0;
|
|
font-size: 13px;
|
|
line-height: 1.55;
|
|
color: var(--text);
|
|
}
|
|
.drawer-footer {
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
gap: 8px;
|
|
padding: 12px 20px 16px;
|
|
border-top: 1px solid var(--line);
|
|
flex-shrink: 0;
|
|
}
|
|
@keyframes drawer-fade {
|
|
from {
|
|
opacity: 0;
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
}
|
|
}
|
|
@keyframes -global-drawer-calm {
|
|
from { opacity: 0; transform: translateY(var(--pop-enter-y)) scale(var(--pop-enter-scale)); }
|
|
to { opacity: 1; transform: none; }
|
|
}
|
|
@keyframes -global-drawer-slide {
|
|
from {
|
|
transform: translateX(100%);
|
|
}
|
|
to {
|
|
transform: translateX(0);
|
|
}
|
|
}
|
|
/* 减少动态效果 (§五):去位移,只留 ≤120ms 透明度 */
|
|
:global(:root[data-reduce='1']) .drawer-panel {
|
|
animation: drawer-fade var(--d-drawer) ease-out both;
|
|
}
|
|
@media (prefers-reduced-motion: reduce) {
|
|
.drawer-panel {
|
|
animation: drawer-fade var(--d-drawer) ease-out both;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 760px) {
|
|
.drawer-panel {
|
|
width: 100vw;
|
|
border-radius: 0;
|
|
}
|
|
}
|
|
</style>
|