With a component library

Solid 2

A size guide in Kobalte's dialog

Open the size guide: the shop dims and the guide rises in. Escape, a click outside or ✕ takes it away as smoothly.

Live demo

Reduced motion: The guide and the shade fade in and out; the guide doesn't rise.

Why

A dialog's hard part isn't its motion. It's moving focus in and back out, keeping Tab inside, closing on Escape, hiding the page from screen readers, and stopping the page scrolling underneath. A component library does all of that, and on Solid that's Kobalte. The guide is Kobalte's Dialog; its motion is Wardic's.

w.create(Dialog.Overlay) and w.create(Dialog.Content) give Kobalte's parts the motion props. Wardic puts its ward on the element each part renders, through the ref Kobalte forwards. Kobalte's own presence waits only for CSS animations, so the dialog is forceMounted, and Wardic's <AnimatePresence>, around a <Show> of the open state, keeps the parts on the page while they leave. Kobalte still hands focus back to the button the moment it closes.

On Solid 2 it's Kobalte 2.0, still an alpha: the console shows its own development warnings.

The code

examples/size-guide/Demo.tsx
import { Dialog } from "@kobalte/core/dialog"
import { AnimatePresence } from "@wardic/solid"
import { w } from "~/lib/wardic.ts"
import { createMemo, createSignal, For, Loading, Show } from "solid-js"
import { products } from "~/lib/store.ts"

// Kobalte's parts, taking Wardic's motion props: the ward goes on the element each one renders,
// through the ref Kobalte forwards to it.
const Shade = w.create(Dialog.Overlay)
const Guide = w.create(Dialog.Content)

const shade = {
  initial: { opacity: 0 },
  animate: { opacity: 1 },
  exit: { opacity: 0 },
  transition: { type: "tween", duration: 0.2 },
} as const

// The guide rises a little as it fades in, and sinks back, quicker, as it goes.
const rise = {
  initial: { opacity: 0, y: 24, scale: 0.98 },
  animate: { opacity: 1, y: 0, scale: 1 },
  exit: { opacity: 0, y: 12, scale: 0.98, transition: { type: "tween", duration: 0.15 } },
  transition: { type: "spring", stiffness: 380, damping: 32 },
} as const

export default function SizeGuide() {
  const pieces = createMemo(() => products())
  const [open, setOpen] = createSignal(false)
  return (
    // forceMount: Kobalte keeps its parts mounted, and the presence below decides when they go, so
    // they're still on the page while Wardic plays their exits. Kobalte's own presence waits only
    // for CSS animations. It still moves focus in, keeps Tab inside, and hands focus back on close.
    <Dialog open={open()} onOpenChange={setOpen} forceMount>
      <Dialog.Trigger class="button size-guide-button">Size guide</Dialog.Trigger>
      <Dialog.Portal>
        <AnimatePresence>
          <Show when={open()}>
            <Shade class="backdrop" {...shade} />
            <Guide class="size-guide" {...rise}>
              <header>
                <Dialog.Title>Size guide</Dialog.Title>
                <Dialog.CloseButton class="icon-button" aria-label="Close">
                  ✕
                </Dialog.CloseButton>
              </header>
              <Dialog.Description class="muted">Height × width, or across for plates and bowls.</Dialog.Description>
              <Loading fallback={<p class="muted">Loading the sizes…</p>}>
                <table class="size-table">
                  <tbody>
                    <For each={pieces()}>
                      {(piece) => (
                        <tr>
                          <th scope="row">{piece.name}</th>
                          <td>{piece.details[0]}</td>
                        </tr>
                      )}
                    </For>
                  </tbody>
                </table>
              </Loading>
            </Guide>
          </Show>
        </AnimatePresence>
      </Dialog.Portal>
    </Dialog>
  )
}