Modal dialog

A dialog rendered into <body> with a portal, with enter and leave transitions.

App.kn
<script lang="ts">
  import Modal from './Modal.kn'

  const open = state(false)
  const name = state('Ada')
</script>

<p>Hello, <strong>{name}</strong>.</p>
<button onClick={() => open.set(true)}>Edit profile</button>

<Modal open={open} title="Edit profile" onClose={() => open.set(false)}>
  <label>
    Display name
    <input bind:value={name} />
  </label>
  <p class="hint">Changes apply as you type. Press Esc or click outside to close.</p>
</Modal>

<style>
  label {
    display: grid;
    gap: 0.35rem;
    font-size: 0.85rem;
  }
  /* The modal renders in <body>, outside any page styles: style what goes in it here. */
  label {
    color: var(--muted);
    font-weight: 500;
  }
  input {
    min-height: 2.25rem;
    padding: 0 0.75rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    background: var(--bg);
    color: var(--fg);
    font: inherit;
    outline: none;
  }
  input:focus-visible {
    border-color: var(--faint);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--fg) 10%, transparent);
  }
  .hint {
    color: var(--muted);
    font-size: 0.8rem;
  }
</style>