Description: AlpineJS's x-transition system allows you to add smooth enter and leave animations for elements that are shown or hidden using x-show or x-if. It works by applying CSS classes at different stages of the transition, which you then style using CSS.
x-show or x-if (to control visibility):
These directives are fundamental for toggling the visibility or existence of DOM elements. x-transition hooks into these changes.
x-show toggles the display: none; style on an element. The element remains in the DOM.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.x-transition:enter="classes-during-enter":
Specifies the CSS classes to apply for the entire duration of the element's "enter" (appearance) animation. Typically, this includes CSS transition properties like transition-opacity, transition-transform, duration-*, and ease-* (using Tailwind CSS class names as examples).
<div x-show="open" x-transition:enter="transition ease-out duration-300">...</div>
x-transition:enter-start="classes-at-start-of-enter":
Defines the CSS classes for the initial state of the "enter" animation (what the element looks like just before it starts appearing). For example, opacity-0 or transform scale-95.
<div x-show="open" ... x-transition:enter-start="opacity-0 transform scale-90">...</div>
x-transition:enter-end="classes-at-end-of-enter":
Defines the CSS classes for the final state of the "enter" animation (what the element looks like when it has fully appeared). For example, opacity-100 or transform scale-100.
<div x-show="open" ... x-transition:enter-end="opacity-100 transform scale-100">...</div>
x-transition:leave="classes-during-leave":
Similar to x-transition:enter, but for the "leave" (disappearance) animation.
<div x-show="open" ... x-transition:leave="transition ease-in duration-200">...</div>
x-transition:leave-start="classes-at-start-of-leave":
Initial state for the "leave" animation (element is fully visible). Example: opacity-100.
<div x-show="open" ... x-transition:leave-start="opacity-100 transform scale-100">...</div>
x-transition:leave-end="classes-at-end-of-leave":
Final state for the "leave" animation (element is fully hidden). Example: opacity-0.
<div x-show="open" ... x-transition:leave-end="opacity-0 transform scale-90">...</div>
x-transition: Applying this directive alone provides a default fade and scale transition.
<div x-show="open" x-transition>...</div>
x-transition.property.duration: Alpine provides modifiers for common transitions. For example, x-transition.opacity, x-transition.scale, x-transition.duration.500ms, x-transition.delay.200ms. These can be chained.
<div x-show="open" x-transition.opacity.duration.500ms>Fades over 500ms</div>
<div x-show="open" x-transition.scale.origin.top.duration.300ms>Scales from top</div>
These shorthands apply predefined classes. For custom animations or more complex scenarios, the explicit enter/leave stages are more flexible.
Alpine's x-transition directives primarily add and remove CSS classes. Your CSS must define the actual transition properties (e.g., transition-property: opacity, transform;, transition-duration: 0.3s;, transition-timing-function: ease-in-out;) and the styles for the start/end states (e.g., opacity: 0; vs opacity: 1;). Tailwind CSS utility classes like transition, duration-300, ease-in-out, opacity-0, opacity-100, scale-95, scale-100 are designed for this and make it easier.
If your transition classes (e.g., my-fade-enter) don't have corresponding CSS rules that specify what should transition and how, nothing will animate.
x-transition with x-if:
When an element is controlled by x-if, it is truly added to or removed from the DOM. The x-transition directives need to be on the element itself that has x-if, or on its direct child if x-if is on a <template> tag. Alpine handles applying the classes correctly as the element is inserted or just before it's removed. Ensure the lifecycle of classes (enter, enter-start, enter-end, etc.) aligns with the element's DOM manipulation.
Keep your transition classes focused. If you have many other utility classes (especially from frameworks like Tailwind CSS) that also affect properties like transform or opacity, ensure they don't conflict with your intended x-transition behavior. Using dedicated, well-named transition classes (e.g., fade-in-start, fade-in-end) can make debugging clearer. Sometimes, a global style or a utility class might unintentionally override part of your transition.
x-showx-showx-transition.opacity.duration.750ms shorthand.
x-transition.scale.origin.top.duration.500ms.delay.200ms.
x-for and x-transitionNo items yet. Add some!