~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
main
66 lines · 1.8 KB Raw History
<script lang="ts">
  import { m } from '#lib/paraglide/messages.js';
  import type { Snippet } from 'svelte';
  import { empty } from '#lib/forms.ts';
  /**
   * A dialog over the dimmed page, opened by a button whose
   * `popovertarget` is its `id`: its `title`, a
   * button to close it, then `children`. Light dismiss closes it too, as a
   * popover does, with no script. `wide` for one with much in it. Closed,
   * however it closes, what was typed into it goes, and `onclose` is told.
   * `opened` opens it whenever it turns true, as the page is drawn or
   * drawn again, for a page that is asked for with it open.
   */
  let {
    id,
    title,
    wide = false,
    opened = false,
    onclose,
    children
  }: {
    id: string;
    title: string;
    wide?: boolean;
    opened?: boolean;
    onclose?: () => void;
    children: Snippet;
  } = $props();
  $effect(() => {
    const el = document.getElementById(id);
    if (opened && !el?.matches(':popover-open')) el?.showPopover();
  });
  // Opened, the first thing to fill in is the one being typed into.
  function toggled(e: ToggleEvent) {
    if (e.newState !== 'open') {
      (e.currentTarget as HTMLElement).querySelectorAll('form').forEach(empty);
      onclose?.();
      return;
    }
    const into = (e.currentTarget as HTMLElement).querySelector<HTMLInputElement>(
      'input:not([type=hidden]),textarea'
    );
    into?.focus();
  }
</script>

<div
  class="dialog"
  class:dialog-wide={wide}
  {id}
  popover="auto"
  role="dialog"
  aria-modal="true"
  aria-labelledby="{id}-title"
  ontoggle={toggled}
>
  <h2 id="{id}-title" class="dialog-title">{title}</h2>
  <button
    class="close"
    type="button"
    popovertarget={id}
    popovertargetaction="hide"
    aria-label={m.common_close()}>×</button
  >
  {@render children()}
</div>