Exit animations

An element that Solid removes is gone at once. Inside a <Presence>, from @wardic/solid/hooks (on Solid 1, <AnimatePresence>), one with an exit stays on the page until that animation has played, then goes.

Live demo
presence/solid2.tsx
import { domAnimation, layout } from "@wardic/solid"
import { elements, makeWard, Presence } from "@wardic/solid/hooks"
import { createSignal, For } from "solid-js"

// This page lays out: its own Wardic instance brings `layout`, so pages that don't never load it.
const { w } = makeWard({ features: [domAnimation, layout], elements })

/** Chips that pop in and out: each plays its `exit` before it goes, and the rest close the gap. */
export default function Chips() {
  const [chips, setChips] = createSignal([1, 2, 3])
  let next = 4
  const add = () => setChips((all) => [...all, next++])
  const remove = (chip: number) => setChips((all) => all.filter((one) => one !== chip))
  return (
    <div class="sample-stack">
      <button class="sample-button" onClick={add}>
        Add
      </button>
      <ul class="sample-chips">
        {/* pop takes a leaving chip out of the row at once, so the rest move up as it goes. */}
        <Presence pop>
          <For each={chips()}>
            {(chip) => (
              <w.li class="sample-chip" layout initial={{ opacity: 0, scale: 0.6 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.6 }}>
                <button aria-label={`Remove ${chip}`} onClick={() => remove(chip)}>
                  {chip} ×
                </button>
              </w.li>
            )}
          </For>
        </Presence>
      </ul>
    </div>
  )
}
presence/solid1.tsx
import { AnimatePresence, createWardic, domAnimation, layout } from "@wardic/solid/v1"
import { createSignal, For } from "solid-js"

// This page lays out: its own Wardic instance brings `layout`, so pages that don't never load it.
const { w } = createWardic([domAnimation, layout])

/** Chips that pop in and out: each plays its `exit` before it goes, and the rest close the gap. */
export default function Chips() {
  const [chips, setChips] = createSignal([1, 2, 3])
  let next = 4
  const add = () => setChips((all) => [...all, next++])
  const remove = (chip: number) => setChips((all) => all.filter((one) => one !== chip))
  return (
    <div class="sample-stack">
      <button class="sample-button" onClick={add}>
        Add
      </button>
      <ul class="sample-chips">
        {/* popLayout takes a leaving chip out of the row at once, so the rest move up as it goes. */}
        <AnimatePresence mode="popLayout">
          <For each={chips()}>
            {(chip) => (
              <w.li class="sample-chip" layout initial={{ opacity: 0, scale: 0.6 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.6 }}>
                <button aria-label={`Remove ${chip}`} onClick={() => remove(chip)}>
                  {chip} ×
                </button>
              </w.li>
            )}
          </For>
        </AnimatePresence>
      </ul>
    </div>
  )
}
presence/foldkit.ts
import { DomLive } from "@wardic/dom"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { Features, Wardic } from "@wardic/foldkit"

// This app lays out and plays exits: its Handle names `layout` and `presence` among its Features.
const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.layout, Features.presence] })

const Model = Schema.Struct({ chips: Schema.Array(Schema.Number), next: Schema.Number })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Add: {}, Remove: { chip: Schema.Number } })
type Message = typeof Message.Type

export const init = (): Model => ({ chips: [1, 2, 3], next: 4 })
export const update = (model: Model, message: Message) =>
  Message.match(message, {
    Add: () => ({ model: { chips: [...model.chips, model.next], next: model.next + 1 } }),
    Remove: ({ chip }) => ({ model: { ...model, chips: model.chips.filter((one) => one !== chip) } }),
  })

/** Chips that pop in and out: each plays its `Exit` before it goes, and the rest close the gap. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
  const w = wardic.html(h)
  return h.div([h.Class("sample-stack")], [
    h.button([h.Class("sample-button"), h.OnClick(Message.Add())], ["Add"]),
    // popLayout takes a leaving chip out of the row at once, so the rest move up as it goes. Each
    // chip is keyed, so Foldkit's patch keeps it the same element while it leaves.
    w.ul(
      [h.Class("sample-chips"), w.Presence({ mode: "popLayout" })],
      model.chips.map((chip) =>
        w.keyed("li")(String(chip), [h.Class("sample-chip"), w.Layout(true), w.Initial({ opacity: 0, scale: 0.6 }), w.Animate({ opacity: 1, scale: 1 }), w.Exit({ opacity: 0, scale: 0.6 })], [
          h.button([h.AriaLabel(`Remove ${chip}`), h.OnClick(Message.Remove({ chip }))], [`${chip} ×`]),
        ]),
      ),
    ),
  ])
}
presence/motion.tsx
import { AnimatePresence, domMax, LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useRef, useState } from "react"

/** Chips that pop in and out: each plays its `exit` before it goes, and the rest close the gap. */
export default function Chips() {
  const [chips, setChips] = useState([1, 2, 3])
  const next = useRef(4)
  const add = () => setChips((all) => [...all, next.current++])
  const remove = (chip: number) => setChips((all) => all.filter((one) => one !== chip))
  return (
    // The chips close the gap with `layout`, so this page brings `domMax`, Motion's layout and drag, itself.
    <LazyMotion features={domMax}>
      <div className="sample-stack">
        <button className="sample-button" onClick={add}>
          Add
        </button>
        <ul className="sample-chips">
          {/* popLayout takes a leaving chip out of the row at once, so the rest move up as it goes. */}
          <AnimatePresence mode="popLayout">
            {chips.map((chip) => (
              <m.li key={chip} className="sample-chip" layout initial={{ opacity: 0, scale: 0.6 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.6 }}>
                <button aria-label={`Remove ${chip}`} onClick={() => remove(chip)}>
                  {chip} ×
                </button>
              </m.li>
            ))}
          </AnimatePresence>
        </ul>
      </div>
    </LazyMotion>
  )
}

What counts as a child

Each element directly inside is a child of its own: a <Show>'s element, or each of a <For>'s, an SVG one as much as an HTML one. For one element that swaps for another, a <Show keyed> makes each value a new child, so the last one leaves as the next one enters:

// Solid 1: <AnimatePresence mode="wait">, its own entry's.
import { Presence } from "@wardic/solid/hooks"

<Presence wait>
  <Show when={tab()} keyed>
    {(name) => <w.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>{name}</w.div>}
  </Show>
</Presence>

A key on the element, as in Motion, lets one that comes back while it's still leaving pick up from where its leaver is, rather than enter again.

Inside an <svg>

A <Presence> can sit inside an <svg>, with SVG elements for children: each point here grows in on its radius, and shrinks out before it goes.

Live demo
presence-svg/solid2.tsx
import { Presence } from "@wardic/solid/hooks"
import { w } from "~/lib/wardic.ts"
import { createSignal, For } from "solid-js"

/** Where point `id` sits on the 320 × 160 plot, spread evenly from its number, and which of four colours it takes. */
const pointAt = (id: number) => ({ cx: 20 + ((0.5 + id * 0.7549) % 1) * 280, cy: 20 + ((0.5 + id * 0.5698) % 1) * 120, tone: id % 4 })

/** Points on a plot that grow in and shrink out: a `<Presence>` inside an `<svg>`, its children SVG circles. */
export default function Plot() {
  const [points, setPoints] = createSignal([1, 2, 3, 4, 5, 6, 7, 8])
  let next = 9
  const add = () => setPoints((all) => [...all, next++])
  const remove = (point: number) => setPoints((all) => all.filter((one) => one !== point))
  return (
    <div class="sample-stack">
      <div class="sample-actions">
        <button class="sample-button" onClick={add}>
          Add
        </button>
        <button class="sample-button" disabled={points().length === 0} onClick={() => remove(points()[0])}>
          Remove
        </button>
      </div>
      <svg class="sample-plot" viewBox="0 0 320 160" role="img" aria-label={`A plot of ${points().length} points`}>
        <line x1="0" y1="53" x2="320" y2="53" />
        <line x1="0" y1="107" x2="320" y2="107" />
        {/* Each circle stays until its exit has shrunk it away. */}
        <Presence>
          <For each={points()}>
            {(point) => (
              <w.circle
                class="sample-point"
                data-tone={pointAt(point).tone}
                cx={pointAt(point).cx}
                cy={pointAt(point).cy}
                initial={{ r: 0, opacity: 0 }}
                animate={{ r: 9, opacity: 1 }}
                whileHover={{ r: 12 }}
                exit={{ r: 0, opacity: 0, transition: { duration: 0.25 } }}
                transition={{ type: "spring", visualDuration: 0.4, bounce: 0.4 }}
                onClick={() => remove(point)}
              />
            )}
          </For>
        </Presence>
      </svg>
      <p class="sample-note">Click a point to remove it.</p>
    </div>
  )
}
presence-svg/solid1.tsx
import { AnimatePresence } from "@wardic/solid/v1"
import { w } from "~/lib/wardic.ts"
import { createSignal, For } from "solid-js"

/** Where point `id` sits on the 320 × 160 plot, spread evenly from its number, and which of four colours it takes. */
const pointAt = (id: number) => ({ cx: 20 + ((0.5 + id * 0.7549) % 1) * 280, cy: 20 + ((0.5 + id * 0.5698) % 1) * 120, tone: id % 4 })

/** Points on a plot that grow in and shrink out: an `<AnimatePresence>` inside an `<svg>`, its children SVG circles. */
export default function Plot() {
  const [points, setPoints] = createSignal([1, 2, 3, 4, 5, 6, 7, 8])
  let next = 9
  const add = () => setPoints((all) => [...all, next++])
  const remove = (point: number) => setPoints((all) => all.filter((one) => one !== point))
  return (
    <div class="sample-stack">
      <div class="sample-actions">
        <button class="sample-button" onClick={add}>
          Add
        </button>
        <button class="sample-button" disabled={points().length === 0} onClick={() => remove(points()[0])}>
          Remove
        </button>
      </div>
      <svg class="sample-plot" viewBox="0 0 320 160" role="img" aria-label={`A plot of ${points().length} points`}>
        <line x1="0" y1="53" x2="320" y2="53" />
        <line x1="0" y1="107" x2="320" y2="107" />
        {/* Each circle stays until its exit has shrunk it away. */}
        <AnimatePresence>
          <For each={points()}>
            {(point) => (
              <w.circle
                class="sample-point"
                data-tone={pointAt(point).tone}
                cx={pointAt(point).cx}
                cy={pointAt(point).cy}
                initial={{ r: 0, opacity: 0 }}
                animate={{ r: 9, opacity: 1 }}
                whileHover={{ r: 12 }}
                exit={{ r: 0, opacity: 0, transition: { duration: 0.25 } }}
                transition={{ type: "spring", visualDuration: 0.4, bounce: 0.4 }}
                onClick={() => remove(point)}
              />
            )}
          </For>
        </AnimatePresence>
      </svg>
      <p class="sample-note">Click a point to remove it.</p>
    </div>
  )
}
presence-svg/foldkit.ts
import { DomLive } from "@wardic/dom"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { Features, Wardic } from "@wardic/foldkit"

// This app plays exits: its Handle names `presence` among its Features.
const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.presence] })

/** Where point `id` sits on the 320 × 160 plot, spread evenly from its number, and which of four colours it takes. */
const pointAt = (id: number) => ({ cx: 20 + ((0.5 + id * 0.7549) % 1) * 280, cy: 20 + ((0.5 + id * 0.5698) % 1) * 120, tone: id % 4 })

const Model = Schema.Struct({ points: Schema.Array(Schema.Number), next: Schema.Number })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Add: {}, Remove: { point: Schema.Number } })
type Message = typeof Message.Type

export const init = (): Model => ({ points: [1, 2, 3, 4, 5, 6, 7, 8], next: 9 })
export const update = (model: Model, message: Message) =>
  Message.match(message, {
    Add: () => ({ model: { points: [...model.points, model.next], next: model.next + 1 } }),
    Remove: ({ point }) => ({ model: { ...model, points: model.points.filter((one) => one !== point) } }),
  })

/** Points on a plot that grow in and shrink out: a `Presence` on an `<svg>`, its children SVG circles. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
  const w = wardic.html(h)
  return h.div([h.Class("sample-stack")], [
    h.div([h.Class("sample-actions")], [
      h.button([h.Class("sample-button"), h.OnClick(Message.Add())], ["Add"]),
      h.button([h.Class("sample-button"), h.Disabled(model.points.length === 0), h.OnClick(Message.Remove({ point: model.points[0] ?? 0 }))], ["Remove"]),
    ]),
    // Each circle stays until its exit has shrunk it away.
    w.svg(
      [h.Class("sample-plot"), h.ViewBox("0 0 320 160"), h.Role("img"), h.AriaLabel(`A plot of ${model.points.length} points`), w.Presence({})],
      [
        h.line([h.X1("0"), h.Y1("53"), h.X2("320"), h.Y2("53")]),
        h.line([h.X1("0"), h.Y1("107"), h.X2("320"), h.Y2("107")]),
        ...model.points.map((point) =>
          w.keyed("circle")(String(point), [
            h.Class("sample-point"),
            h.DataAttribute("tone", String(pointAt(point).tone)),
            h.Cx(String(pointAt(point).cx)),
            h.Cy(String(pointAt(point).cy)),
            w.Initial({ r: 0, opacity: 0 }),
            w.Animate({ r: 9, opacity: 1 }),
            w.WhileHover({ r: 12 }),
            w.Exit({ r: 0, opacity: 0, transition: { duration: 0.25 } }),
            w.Transition({ type: "spring", visualDuration: 0.4, bounce: 0.4 }),
            h.OnClick(Message.Remove({ point })),
          ]),
        ),
      ],
    ),
    h.p([h.Class("sample-note")], ["Click a point to remove it."]),
  ])
}
presence-svg/motion.tsx
import { AnimatePresence, domAnimation, LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useRef, useState } from "react"

/** Where point `id` sits on the 320 × 160 plot, spread evenly from its number, and which of four colours it takes. */
const pointAt = (id: number) => ({ cx: 20 + ((0.5 + id * 0.7549) % 1) * 280, cy: 20 + ((0.5 + id * 0.5698) % 1) * 120, tone: id % 4 })

/** Points on a plot that grow in and shrink out: an `<AnimatePresence>` inside an `<svg>`, its children SVG circles. */
export default function Plot() {
  const [points, setPoints] = useState([1, 2, 3, 4, 5, 6, 7, 8])
  const next = useRef(9)
  const add = () => setPoints((all) => [...all, next.current++])
  const remove = (point: number) => setPoints((all) => all.filter((one) => one !== point))
  return (
    <LazyMotion features={domAnimation}>
      <div className="sample-stack">
        <div className="sample-actions">
          <button className="sample-button" onClick={add}>
            Add
          </button>
          <button className="sample-button" disabled={points.length === 0} onClick={() => remove(points[0])}>
            Remove
          </button>
        </div>
        <svg className="sample-plot" viewBox="0 0 320 160" role="img" aria-label={`A plot of ${points.length} points`}>
          <line x1="0" y1="53" x2="320" y2="53" />
          <line x1="0" y1="107" x2="320" y2="107" />
          {/* Each circle stays until its exit has shrunk it away. */}
          <AnimatePresence>
            {points.map((point) => (
              <m.circle
                key={point}
                className="sample-point"
                data-tone={pointAt(point).tone}
                cx={pointAt(point).cx}
                cy={pointAt(point).cy}
                initial={{ r: 0, opacity: 0 }}
                animate={{ r: 9, opacity: 1 }}
                whileHover={{ r: 12 }}
                exit={{ r: 0, opacity: 0, transition: { duration: 0.25 } }}
                transition={{ type: "spring", visualDuration: 0.4, bounce: 0.4 }}
                onClick={() => remove(point)}
              />
            ))}
          </AnimatePresence>
        </svg>
        <p className="sample-note">Click a point to remove it.</p>
      </div>
    </LazyMotion>
  )
}

Modes

How a child enters while another leaves, by <Presence>'s props (on Solid 1, <AnimatePresence>'s mode, in brackets):

  • Neither, the default ("sync"): the leaving and the entering children animate at once.
  • wait ("wait"): the entering child waits until the leaving one has gone.
  • pop ("popLayout"): a leaving child is taken out of the layout as it starts to leave, so the others move into its place as it goes, with layout on them. It keeps its top left corner while it's popped, or the one it's given, pop="bottom-right" (on Solid 1, anchorX and anchorY say which edges). An icon's <svg> is popped too, where Motion pops only an HTML element; the SVG elements inside an <svg> leave where they are.

Here a play button's icon swaps for a pause one. The leaving <svg> is popped, so it turns out over the new icon rather than beside it, and the button keeps its size. Motion's tab wraps each icon in an m.span to pop it.

Live demo
presence-icon/solid2.tsx
import { Presence } from "@wardic/solid/hooks"
import { w } from "~/lib/wardic.ts"
import { createSignal, Show } from "solid-js"

/** Each icon turns in a quarter turn from half its size, and turns out the same way on. */
const TURN = {
  initial: { opacity: 0, scale: 0.5, rotate: -90 },
  animate: { opacity: 1, scale: 1, rotate: 0 },
  exit: { opacity: 0, scale: 0.5, rotate: 90 },
  transition: { type: "spring", visualDuration: 0.3, bounce: 0.3 },
} as const

/** A play button whose icon swaps for a pause one: each icon an `<svg>`, the leaving one popped out of the flow. */
export default function PlayButton() {
  const [playing, setPlaying] = createSignal(false)
  return (
    <div class="sample-stack">
      <button class="sample-button sample-icon-button" aria-label={playing() ? "Pause" : "Play"} onClick={() => setPlaying((on) => !on)}>
        {/* The leaving icon is popped: it turns out over the new one, not beside it, and the button keeps its size. */}
        <Presence pop enter={false}>
          <Show
            when={playing()}
            fallback={
              <w.svg viewBox="0 0 24 24" width="22" height="22" initial={TURN.initial} animate={TURN.animate} exit={TURN.exit} transition={TURN.transition}>
                <path d="M7 5 L19 12 L7 19 Z" />
              </w.svg>
            }
          >
            <w.svg viewBox="0 0 24 24" width="22" height="22" initial={TURN.initial} animate={TURN.animate} exit={TURN.exit} transition={TURN.transition}>
              <rect x="6" y="5" width="4" height="14" rx="1" />
              <rect x="14" y="5" width="4" height="14" rx="1" />
            </w.svg>
          </Show>
        </Presence>
      </button>
      <p class="sample-note">{playing() ? "Playing" : "Paused"}</p>
    </div>
  )
}
presence-icon/solid1.tsx
import { AnimatePresence } from "@wardic/solid/v1"
import { w } from "~/lib/wardic.ts"
import { createSignal, Show } from "solid-js"

/** Each icon turns in a quarter turn from half its size, and turns out the same way on. */
const TURN = {
  initial: { opacity: 0, scale: 0.5, rotate: -90 },
  animate: { opacity: 1, scale: 1, rotate: 0 },
  exit: { opacity: 0, scale: 0.5, rotate: 90 },
  transition: { type: "spring", visualDuration: 0.3, bounce: 0.3 },
} as const

/** A play button whose icon swaps for a pause one: each icon an `<svg>`, the leaving one popped out of the flow. */
export default function PlayButton() {
  const [playing, setPlaying] = createSignal(false)
  return (
    <div class="sample-stack">
      <button class="sample-button sample-icon-button" aria-label={playing() ? "Pause" : "Play"} onClick={() => setPlaying((on) => !on)}>
        {/* The leaving icon is popped: it turns out over the new one, not beside it, and the button keeps its size. */}
        <AnimatePresence mode="popLayout" initial={false}>
          <Show
            when={playing()}
            fallback={
              <w.svg viewBox="0 0 24 24" width="22" height="22" initial={TURN.initial} animate={TURN.animate} exit={TURN.exit} transition={TURN.transition}>
                <path d="M7 5 L19 12 L7 19 Z" />
              </w.svg>
            }
          >
            <w.svg viewBox="0 0 24 24" width="22" height="22" initial={TURN.initial} animate={TURN.animate} exit={TURN.exit} transition={TURN.transition}>
              <rect x="6" y="5" width="4" height="14" rx="1" />
              <rect x="14" y="5" width="4" height="14" rx="1" />
            </w.svg>
          </Show>
        </AnimatePresence>
      </button>
      <p class="sample-note">{playing() ? "Playing" : "Paused"}</p>
    </div>
  )
}
presence-icon/foldkit.ts
import { DomLive } from "@wardic/dom"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { Features, Wardic } from "@wardic/foldkit"

// This app plays exits: its Handle names `presence` among its Features.
const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.presence] })

const Model = Schema.Struct({ playing: Schema.Boolean })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Toggle: {} })
type Message = typeof Message.Type

export const init = (): Model => ({ playing: false })
export const update = (model: Model, message: Message) => Message.match(message, { Toggle: () => ({ model: { playing: !model.playing } }) })

/** Each icon turns in a quarter turn from half its size, and turns out the same way on. */
const TURN = {
  initial: { opacity: 0, scale: 0.5, rotate: -90 },
  animate: { opacity: 1, scale: 1, rotate: 0 },
  exit: { opacity: 0, scale: 0.5, rotate: 90 },
  transition: { type: "spring", visualDuration: 0.3, bounce: 0.3 },
} as const

/** A play button whose icon swaps for a pause one: each icon an `<svg>`, keyed, the leaving one popped out of the flow. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
  const w = wardic.html(h)
  const icon = (key: string, shapes: ReadonlyArray<Html>): Html =>
    w.keyed("svg")(key, [h.ViewBox("0 0 24 24"), h.Width("22"), h.Height("22"), w.Initial(TURN.initial), w.Animate(TURN.animate), w.Exit(TURN.exit), w.Transition(TURN.transition)], shapes)
  return h.div([h.Class("sample-stack")], [
    h.button([h.Class("sample-button sample-icon-button"), h.AriaLabel(model.playing ? "Pause" : "Play"), h.OnClick(Message.Toggle())], [
      // The leaving icon is popped: it turns out over the new one, not beside it, and the button keeps its size.
      w.span([w.Presence({ mode: "popLayout", initial: false })], [
        model.playing
          ? icon("pause", [h.rect([h.X("6"), h.Y("5"), h.Width("4"), h.Height("14"), h.Rx("1")]), h.rect([h.X("14"), h.Y("5"), h.Width("4"), h.Height("14"), h.Rx("1")])])
          : icon("play", [h.path([h.D("M7 5 L19 12 L7 19 Z")])]),
      ]),
    ]),
    h.p([h.Class("sample-note")], [model.playing ? "Playing" : "Paused"]),
  ])
}
presence-icon/motion.tsx
import { AnimatePresence, domAnimation, LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useState } from "react"

/** Each icon turns in a quarter turn from half its size, and turns out the same way on. */
const TURN = {
  initial: { opacity: 0, scale: 0.5, rotate: -90 },
  animate: { opacity: 1, scale: 1, rotate: 0 },
  exit: { opacity: 0, scale: 0.5, rotate: 90 },
  transition: { type: "spring", visualDuration: 0.3, bounce: 0.3 },
} as const

/** A play button whose icon swaps for a pause one, the leaving one popped out of the flow. */
export default function PlayButton() {
  const [playing, setPlaying] = useState(false)
  return (
    <LazyMotion features={domAnimation}>
      <div className="sample-stack">
        <button className="sample-button sample-icon-button" aria-label={playing ? "Pause" : "Play"} onClick={() => setPlaying((on) => !on)}>
          {/* Motion pops only an HTML element, so each icon turns in an `m.span` around its `<svg>`. */}
          <AnimatePresence mode="popLayout" initial={false}>
            {playing ? (
              <m.span key="pause" className="sample-icon" initial={TURN.initial} animate={TURN.animate} exit={TURN.exit} transition={TURN.transition}>
                <svg viewBox="0 0 24 24" width="22" height="22">
                  <rect x="6" y="5" width="4" height="14" rx="1" />
                  <rect x="14" y="5" width="4" height="14" rx="1" />
                </svg>
              </m.span>
            ) : (
              <m.span key="play" className="sample-icon" initial={TURN.initial} animate={TURN.animate} exit={TURN.exit} transition={TURN.transition}>
                <svg viewBox="0 0 24 24" width="22" height="22">
                  <path d="M7 5 L19 12 L7 19 Z" />
                </svg>
              </m.span>
            )}
          </AnimatePresence>
        </button>
        <p className="sample-note">{playing ? "Playing" : "Paused"}</p>
      </div>
    </LazyMotion>
  )
}

Its other props

  • enter={false} (on Solid 1, initial={false}) skips the children's entrance at the first render.
  • custom is handed to each leaving child's exit variants, for an exit that depends on something only known as it leaves, such as the direction a slider went.
  • Once every leaving child has gone: on Solid 2, presence.next("exitComplete") resolves, from a createPresence() given as <Presence ref={presence}>; on Solid 1, onExitComplete is called.
  • propagate plays the exits of nested presences when their parent leaves.

Your own exit

A child's exit can be taken over from the code: it stays on the page until you say it can go.

  • On Solid 2, a ward does it, one with no motion if it needs none: under a presence it hears its own leave, ward.exiting() is true from it, and const release = ward.hold() keeps it on the page until release().
  • On Solid 1, usePresence(element, (safeToRemove, custom) => { … }) calls you when it leaves, and useIsPresent is whether the child is still present, false from the moment it starts to leave.

In the storefront

The storefront's examples that show it, live and with their code: