---
title: createTransitionPresence
description: Keep an element mounted until its CSS exit animation finishes.
package: moraine
version: 0.11.0
repository: https://github.com/subframe7536/moraine
---

# createTransitionPresence

> Keep an element mounted until its CSS exit animation finishes.

`createTransitionPresence` keeps conditionally rendered elements mounted while their CSS exit animation runs. When `open` becomes `false`, `present()` stays `true` until the registered elements' exit animations settle. Render the element with `<Show when={present()}>` so it unmounts only after the animation ends.

## Import

```tsx
import { createTransitionPresence } from 'moraine/utils'
```

## Signature

```ts
function createTransitionPresence(options: CreateTransitionPresenceOptions): TransitionPresenceState

interface CreateTransitionPresenceOptions {
  open: Accessor<boolean>
  onExitComplete?: () => void
}

interface TransitionPresenceState {
  present: Accessor<boolean>
  dataAttrs: {
    readonly 'data-closed'?: string
    readonly 'data-expanded'?: string
  }
  /** Tracks an element until its exit animation settles; call the returned function to release it. */
  registerElement: (element: HTMLElement) => () => void
}
```

## Usage Example

```tsx title="src/AnimatedDialog.tsx"
import { Show, createEffect, createSignal, on, onCleanup } from 'solid-js'
import { createTransitionPresence } from 'moraine/utils'

export function AnimatedDialog() {
  const [open, setOpen] = createSignal(false)
  const presence = createTransitionPresence({ open })
  const [panel, setPanel] = createSignal<HTMLDivElement>()

  createEffect(
    on(panel, (element) => {
      if (element) {
        onCleanup(presence.registerElement(element))
      }
    }),
  )

  return (
    <>
      <button onClick={() => setOpen(!open())}>Toggle Modal</button>

      <Show when={presence.present()}>
        <div
          ref={setPanel}
          {...presence.dataAttrs}
          class="data-expanded:animate-mo-enter data-closed:animate-mo-exit p-6 bg-card border rounded-xl"
        >
          This panel stays mounted until its exit animation finishes.
        </div>
      </Show>
    </>
  )
}
```

## Details

- Register each animating element with `registerElement`, which returns a release function. Register from the component that renders the element, not inside its `ref` callback, so the owner releases the registration on unmount. The hook listens for `animationstart`, `animationend`, and `animationcancel` on the element.
- `present()` starts as the initial `open()` value. It turns `true` as soon as `open` is `true`.
- With no registered element, `present()` turns `false` as soon as `open` is `false`.
- `onExitComplete` runs once the exit settles and `present()` turns `false`.
- Zero-duration and cancelled animations complete the exit immediately, so a missing or interrupted animation does not leave the element mounted.

