StealThis .dev
Patterns Medium

Animate Presence

Elements that animate in AND out of the DOM with smooth enter/exit transitions. Add and remove list items with fade+slide animations that play before DOM removal.

Open in Lab
css javascript vue svelte
Targets: TS JS HTML React Vue Svelte

Code

Animate Presence

Demonstrates elements that animate both in and out of the DOM. Unlike simple CSS transitions that only handle enter states, this pattern plays a full exit animation before removing the element.

How it works

  1. Enter animation — newly added items get a fadeSlideIn keyframe that runs on mount
  2. Exit animation — before removal, the fadeSlideOut keyframe plays; an animationend listener then removes the node
  3. The vanilla JS version manages a simple item list with add/remove buttons
  4. The React version wraps children in an AnimatePresence component that tracks mounting/unmounting

Use cases

  • Toast / notification stacks
  • Dynamic list items (todos, tags, chat messages)
  • Modal and dialog open/close
  • Accordion expand/collapse content