Description: The x-transition directive in AlpineJS allows you to add smooth enter and leave animations for elements whose visibility is controlled by x-show or x-if. It works by applying specified CSS classes at different stages of the element's appearance or disappearance.
x-show or x-if (to control visibility):
These directives are fundamental for conditionally displaying elements. x-show toggles the display: none; style, while x-if (often on a <template> tag) conditionally adds or removes the element from the DOM. Transitions work with both.
x-transition:enter="classes-during-enter-from":
CSS classes applied for the entire duration of the enter (appear) transition. Typically, these define the CSS transition properties like transition-property, transition-duration, and transition-timing-function. Example: transition ease-out duration-300.
x-transition:enter-start="classes-at-start-of-enter":
CSS classes defining the initial state of the element before it starts entering. Example: opacity-0 transform scale-95 (making it transparent and slightly smaller).
x-transition:enter-end="classes-at-end-of-enter":
CSS classes defining the final state of the element after it has finished entering. Example: opacity-100 transform scale-100 (making it fully opaque and normal size).
x-transition:leave="classes-during-leave-from":
CSS classes applied for the entire duration of the leave (disappear) transition. Example: transition ease-in duration-200.
x-transition:leave-start="classes-at-start-of-leave":
CSS classes defining the initial state of the element as it begins to leave. Example: opacity-100 transform scale-100.
x-transition:leave-end="classes-at-end-of-leave":
CSS classes defining the final state of the element after it has finished leaving (just before it's hidden or removed). Example: opacity-0 transform scale-95.
AlpineJS provides convenient shorthands for common transitions:
x-transition: Applies a default subtle fade and scale transition. This is equivalent to a set of predefined enter, enter-start, etc. classes.x-transition.opacity: A simple opacity fade transition.x-transition.scale: A simple scale transition..duration.500ms (e.g., x-transition.opacity.duration.500ms) can be chained to customize the duration. Similarly, .delay.Nms can be used.While shorthands are quick, explicitly defining the enter-* and leave-* classes with utilities (like those from Tailwind CSS) gives you maximum control and clarity.
Alpine's x-transition directives only add and remove CSS classes. Your CSS (whether custom or from a framework like Tailwind CSS) must define what those classes do. Crucially, the classes specified in x-transition:enter and x-transition:leave must include the actual CSS transition properties (e.g., transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out; or Tailwind's transition duration-300 ease-in-out). Without these, the change between -start and -end states will be instantaneous.
<!-- Correct: Tailwind classes provide transition properties -->
<div x-show="open"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0 scale-90"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition ease-in duration-200"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-90">
Content
</div>
x-transition with x-if:
When an element is managed by x-if, it's truly added to or removed from the DOM. For transitions to work, Alpine needs to apply the leave classes and wait for the CSS transition to complete before actually removing the element. Ensure the x-transition directives are placed on the element directly controlled by x-if. If x-if is on a <template> tag, the transition directives must be on the single root element immediately inside that template. Using a :key attribute is also crucial when x-if is used within an x-for loop to help Alpine track elements correctly.
Keep your transition classes focused. If an element has many other utility classes (e.g., from Tailwind CSS) that also affect properties like opacity or transform, they might conflict with or override your intended x-transition behavior. It's often best to use dedicated classes for transition states that are distinct from general styling classes. For example, if an element normally has opacity-75, your enter-start might be opacity-0 and enter-end might be opacity-75 specifically for the transition, rather than just opacity-100.
x-transition shorthand.
No tasks yet. Add one above!