~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/spaces-bookclub

Book club: a Svelte demo app for atproto spaces (@atproto/lex-space)

0
0
main
99 lines · 2.3 KB Raw History
<script lang="ts">
  import type { Snippet } from 'svelte'

  /** A modal on the native <dialog>: focus trap, Escape and top layer for free. */
  let {
    open = $bindable(false),
    title,
    description,
    children,
    footer,
  }: {
    open?: boolean
    title: string
    description?: string
    children?: Snippet
    footer?: Snippet
  } = $props()

  const id = $props.id()
  let dialog: HTMLDialogElement

  $effect(() => {
    if (open && !dialog.open) dialog.showModal()
    else if (!open && dialog.open) dialog.close()
  })
</script>

<!-- Clicks on the backdrop land on the <dialog> itself, outside the panel.
     Escape closes it natively. -->
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_noninteractive_element_interactions -->
<dialog
  bind:this={dialog}
  aria-labelledby="{id}-title"
  aria-describedby={description ? `${id}-description` : undefined}
  onclose={() => (open = false)}
  onclick={(event) => {
    if (event.target === dialog) open = false
  }}
>
  <div class="panel">
    <header>
      <h2 id="{id}-title">{title}</h2>
      {#if description}<p id="{id}-description">{description}</p>{/if}
    </header>
    {#if children}<div class="body">{@render children()}</div>{/if}
    {#if footer}<footer>{@render footer()}</footer>{/if}
  </div>
</dialog>

<style>
  dialog {
    width: min(var(--dialog-width), calc(100vw - 2 * var(--space-4)));
    max-height: calc(100dvh - 2 * var(--space-8));
    padding: 0;
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-lg);
  }
  dialog[open] {
    animation: enter var(--duration-normal) var(--ease-out);
  }
  dialog::backdrop {
    background: var(--color-backdrop);
    backdrop-filter: blur(2px);
  }
  .panel {
    display: grid;
    gap: var(--space-5);
    padding: var(--space-6);
  }
  header {
    display: grid;
    gap: var(--space-2);
  }
  h2 {
    font-size: var(--text-xl);
  }
  header p {
    color: var(--color-text-muted);
    font-size: var(--text-sm);
  }
  .body {
    display: grid;
    gap: var(--space-4);
  }
  footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
  }
  @keyframes enter {
    from {
      opacity: 0;
      transform: translateY(8px) scale(0.98);
    }
  }
</style>