97 lines
3.3 KiB
Svelte
97 lines
3.3 KiB
Svelte
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
|
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
|
|
<script lang="ts">
|
|
import type { Snippet } from "svelte";
|
|
import { onMount } from "svelte";
|
|
import Icon from "./Icon.svelte";
|
|
|
|
// Handoff §2.9: a native <dialog>, modal (showModal) or not (the engine popover). Focus goes
|
|
// to the first field (or the element marked data-autofocus), and back to the opener on close.
|
|
let {
|
|
title,
|
|
kind = "dialog",
|
|
modal = true,
|
|
role,
|
|
onclose,
|
|
escape = true,
|
|
closable = true,
|
|
toggle,
|
|
children,
|
|
footer,
|
|
}: {
|
|
title: string;
|
|
kind?: "dialog" | "panel" | "popover";
|
|
modal?: boolean;
|
|
role?: "dialog" | "alertdialog";
|
|
onclose: () => void;
|
|
escape?: boolean;
|
|
closable?: boolean;
|
|
/** A popover's opener (a CSS selector): a click there is the opener's, which toggles it. */
|
|
toggle?: string;
|
|
children?: Snippet;
|
|
footer?: Snippet;
|
|
} = $props();
|
|
|
|
let el: HTMLDialogElement | undefined = $state();
|
|
const id = "dlg-" + Math.random().toString(36).slice(2, 9);
|
|
|
|
onMount(() => {
|
|
const opener = document.activeElement;
|
|
if (el) {
|
|
if (modal && typeof el.showModal === "function") el.showModal();
|
|
else if (typeof el.show === "function") el.show();
|
|
else el.setAttribute("open", "");
|
|
const first =
|
|
el.querySelector<HTMLElement>("[data-autofocus]") ??
|
|
el.querySelector<HTMLElement>("input:not([type=hidden]):not([readonly]):not([type=radio]), textarea:not([readonly]), select");
|
|
(first ?? el.querySelector<HTMLElement>(".ia-dialog-title"))?.focus();
|
|
}
|
|
// A dialog opened with show() (the popover) gets no cancel event: Esc and a click outside
|
|
// close it here, like the modal ones.
|
|
const key = (e: KeyboardEvent) => {
|
|
if (!modal && e.key === "Escape" && escape) {
|
|
e.preventDefault();
|
|
onclose();
|
|
}
|
|
};
|
|
const down = (e: PointerEvent) => {
|
|
if (!modal && escape && el && e.target instanceof Node && !el.contains(e.target)) {
|
|
if (toggle && e.target instanceof Element && e.target.closest(toggle)) return;
|
|
onclose();
|
|
}
|
|
};
|
|
document.addEventListener("keydown", key);
|
|
// On the next turn, so the click that opened the popover does not close it.
|
|
const t = setTimeout(() => document.addEventListener("pointerdown", down), 0);
|
|
return () => {
|
|
clearTimeout(t);
|
|
document.removeEventListener("keydown", key);
|
|
document.removeEventListener("pointerdown", down);
|
|
if (opener instanceof HTMLElement && document.contains(opener)) opener.focus();
|
|
};
|
|
});
|
|
|
|
function cancel(e: Event) {
|
|
e.preventDefault();
|
|
if (escape) onclose();
|
|
}
|
|
</script>
|
|
|
|
<dialog
|
|
bind:this={el}
|
|
class="ia-dialog {kind === 'panel' ? 'ia-panel' : kind === 'popover' ? 'ia-popover' : ''}"
|
|
aria-labelledby={id}
|
|
{role}
|
|
oncancel={cancel}
|
|
>
|
|
<div class="ia-dialog-head">
|
|
<h2 class="ia-h3 ia-dialog-title" id={id} tabindex="-1">{title}</h2>
|
|
{#if closable}
|
|
<button type="button" class="ia-icon-btn" aria-label="Close" onclick={() => onclose()}><Icon name="close" /></button>
|
|
{/if}
|
|
</div>
|
|
<div class="ia-panel-body">{@render children?.()}</div>
|
|
{#if footer}<div class="ia-dialog-foot">{@render footer()}</div>{/if}
|
|
</dialog>
|