Description: The x-transition directive in AlpineJS allows you to add smooth enter (appear) and leave (disappear) animations for elements whose visibility is controlled by x-show or x-if. This is achieved by applying CSS classes at different stages of the transition, which in turn trigger CSS transitions or animations defined in your stylesheets.
x-show="condition" or x-if="condition":
These directives control the visibility of an element. x-transition works in conjunction with them.
x-show: Toggles the display: none; style. The element remains in the DOM, making it ideal for frequent toggles where state needs to be preserved.x-if: Conditionally renders an element. If the condition is false, the element (and its children) are completely removed from the DOM. If true, they are added. Transitions also work with x-if, but the transition directives must be on the element itself or its immediate child (if x-if is on a <template> tag).x-transition Directives:
These allow fine-grained control over the classes applied during each phase of the transition. The classes you specify should correspond to CSS that defines actual transition properties (like transition-property, transition-duration, transition-timing-function) and the visual states (like opacity, transform).
x-transition:enter="classes-during-enter": Classes applied for the entire duration of the element's entrance. Typically includes CSS like transition-opacity duration-300 ease-out.x-transition:enter-start="classes-at-start-of-enter": Classes defining the initial state of the entering element (e.g., opacity-0 transform scale-90).x-transition:enter-end="classes-at-end-of-enter": Classes defining the final state of the fully entered element (e.g., opacity-100 transform scale-100).x-transition:leave="classes-during-leave": Classes applied for the entire duration of the element's departure. Similar to enter, e.g., transition-opacity duration-200 ease-in.x-transition:leave-start="classes-at-start-of-leave": Classes defining the initial state of the leaving element (e.g., opacity-100 transform scale-100).x-transition:leave-end="classes-at-end-of-leave": Classes defining the final state of the fully departed element (e.g., opacity-0 transform scale-90).Example using Tailwind CSS classes:
<div x-show="isOpen"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0 transform scale-95"
x-transition:enter-end="opacity-100 transform scale-100"
x-transition:leave="transition ease-in duration-200"
x-transition:leave-start="opacity-100 transform scale-100"
x-transition:leave-end="opacity-0 transform scale-95">
Modal Content
</div>
Alpine provides convenient shorthands for common transition effects, which apply default classes. These can be modified.
x-transition: A default fade and scale transition.x-transition.opacity: Applies only an opacity fade.x-transition.scale: Applies only a scale transform..duration.Nms (e.g., .duration.500ms) can be chained to customize the shorthands: x-transition.opacity.duration.700ms.x-transition:enter.duration.500ms x-transition:leave.duration.300ms.Example using a shorthand:
<div x-show="isVisible" x-transition.opacity.duration.300ms>
Fading content...
</div>
Alpine's x-transition directives only add/remove CSS classes. Your actual CSS must define the transition property (e.g., Tailwind's transition-opacity duration-300 ease-out or custom CSS like transition: opacity 0.3s ease;) and the styles for the start/end states (e.g., Tailwind's opacity-0 and opacity-100 or custom CSS opacity: 0; and opacity: 1;). Without these CSS rules, the classes will change, but no visual transition will occur.
x-transition with x-if:
For x-if, the element is truly added/removed from the DOM. The transition directives must be on the element with x-if itself, OR if x-if is on a <template> tag, the directives should be on the single root element *inside* that template. Alpine needs to apply "leave" classes just before removal and "enter" classes upon insertion.
Ensure your transition classes are focused. If other utility classes (e.g., from Tailwind CSS) also manipulate transform, opacity, or display, they might conflict with or override your intended x-transition behavior. Debugging can become hard. Using dedicated transition classes or carefully auditing applied utilities can help. Browser developer tools are invaluable for inspecting applied classes and styles during the animation.
This example demonstrates various uses of x-transition, including a modal dialog, an element toggled with x-if, and an element using shorthand transitions.
x-showx-if Transition Examplex-if and has its own transition. It is completely added/removed from the DOM.
x-transition Example