Reka UI logoReka
backdrop
Utilities

Presence

Manages mounting and unmounting of element with transition support.
Question

How is this component different from Vue Transition?

A: The biggest difference is it waits for whatever CSS transition or animation the element itself runs, without transition classes, and controls the visibility of the element.

Presence component provides enhanced control over element mounting/unmounting. It ensures animations and transitions complete before removing elements from the DOM, making it perfect for animated UI components.

It renders two attributes on its child element, so CSS transitions can animate both phases:

  • data-starting-style for the first frame after the element is mounted (or shown again) by present turning true.
  • data-ending-style from present turning false until every transition and animation of the element has finished (read with element.getAnimations()); the element then unmounts.

When nothing is running (no transition or animation, or prefers-reduced-motion styles that remove them), the element unmounts right away. Infinite animations and animations of descendants are not waited for. Everything else running on the element when it closes is: an unrelated transition (a hover colour change) delays the unmount until it ends, a paused animation holds the element mounted, and a close in a background tab completes when the browser runs the tab's animations again. There is no timeout. In environments without Web Animations (jsdom), no attribute is rendered and changes settle right away.

API Reference

PropDefaultType
present*
boolean

Conditional to mount or unmount the child element. Similar to v-if

forceMount
boolean

Force the element to render all the time. Useful for programmatically render grandchild component with the exposed present

appear
boolean

Treat an initial present of true as an open: render the child with data-starting-style on its first frame, like Vue Transition with appear. Without it only a later change to present does. Read once, when the component is set up.

EmitPayload
complete
[present: boolean]

Emitted once the enter (true) or exit (false) transitions and animations of the child element have finished; on exit, right before the element unmounts. A change interrupted by another one is not reported.

enter
CustomEvent

Event handler called when the enter animation has started

after-enter
CustomEvent

Event handler called when the enter animation has finished

leave
CustomEvent

Event handler called when the leave animation has started

after-leave
CustomEvent

Event handler called when the leave animation has finished

tip

Read our Animation Guide to learn more about implementing animations with Presence component.

Example

vue
<template>
  <Presence :present="isVisible">
    <div
      class="transition-opacity duration-200 data-[starting-style]:opacity-0 data-[ending-style]:opacity-0"
    >
      <slot />
    </div>
  </Presence>
</template>

Keyframe animations work too:

vue
<template>
  <Presence :present="isVisible">
    <div
      :data-state="isVisible ? 'open' : 'closed'"
      class="data-[state=open]:animate-fadeIn data-[state=closed]:animate-fadeOut"
    >
      <slot />
    </div>
  </Presence>
</template>

Force Mount

When you need to ensure content is always rendered regardless of the present state:

vue
<template>
  <Presence v-slot="{ present }" :present="isVisible" :force-mount="true">
    <div>
      This content will always be rendered

      <div v-if="present">
        This content is hidden
      </div>
    </div>
  </Presence>
</template>