Transitions With Headless UI
Use the Transition component to animate enter and leave states of dialogs, dropdowns, and drawers using Tailwind's transition utilities.
Why Animate With Headless UI's Transition?
Tailwind's transition-* utilities handle smooth state changes on persistent elements, but they cannot animate elements that mount and unmount from the DOM. When a dialog or dropdown appears, it goes from non-existent to visible in one frame — there is no transition. Headless UI's Transition component solves this by keeping the element mounted briefly during the exit animation, allowing Tailwind transition utilities to complete before the element is removed.
The Transition Component API
The Transition component from Headless UI accepts a show boolean and a set of class props: enter, enterFrom, enterTo for the entering animation, and leave, leaveFrom, leaveTo for the exit. Classes in enter and leave are active during the entire transition. Classes in enterFrom/leaveFrom define the start state, and enterTo/leaveTo define the end state.
import { Transition } from '@headlessui/react';
<Transition
show={isVisible}
enter='transition-opacity duration-200 ease-out'
enterFrom='opacity-0'
enterTo='opacity-100'
leave='transition-opacity duration-150 ease-in'
leaveFrom='opacity-100'
leaveTo='opacity-0'
>
<div className='bg-white rounded-xl p-6 shadow-lg'>
Content that fades in and out
</div>
</Transition>All lessons in this course
- Introduction to Headless UI
- Styling Headless Menu and Dropdown
- Building Accessible Dialogs
- Transitions With Headless UI