Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 28 ยท Alpine.js Going Further

Magic Properties, x-effect and Transitions

Go further with Alpine.js: watch data with $watch, run code after updates with $nextTick, use x-effect, animate with x-transition, avoid flashes with x-cloak, and build modals with x-teleport.

Alpine's basics (state, bindings, loops, events) cover a lot. This lesson adds the tools that make components feel polished: code that reacts when data changes, code that waits until the page has re-rendered, smooth show and hide animations, and a way to render a modal at the top of the page no matter where you wrote it.

Alpine calls the helpers that start with a dollar sign magic properties. You have already met $event, $refs, $el and $dispatch in events and refs. Here you add $watch, $nextTick and $store, plus the directives x-effect, x-transition, x-cloak and x-teleport.

Reacting to changes with $watch

$watch('property', (newValue, oldValue) => { ... }) runs a function every time a piece of data changes. Put it in x-init so it is registered once when the component starts. The property name is a string and can use dots for nested data ('user.name').

$watch a counter

Use $watch for side effects: calling a function, dispatching an event, saving a draft. If you just need a derived value for display, a getter is simpler and cheaper (see reactive state).

Waiting for the DOM with $nextTick

When you change data, Alpine does not update the page that same instant. It batches the work and updates a moment later. If your next line needs the updated page, for example to focus an input that x-if has just created, wrap it in $nextTick.

Focus after an element appears

The input is created by x-if only during the update. Without $nextTick, $refs.box would not exist yet when the click handler runs, and the call would fail with an error.

Reading global state with $store

$store.name reads global data registered with Alpine.store. The next lesson, reusable components and stores, shows how to create stores; for now remember that $store is the magic you use to reach them from any component on the page.

Running code automatically with x-effect

x-effect="expression" runs an expression once at start and then again whenever any reactive value it used changes. You do not list the dependencies; Alpine detects them from what the code reads.

x-effect keeps a title in sync

Avoid writing to a value that the same effect reads: it can cause loops or confusing results. Prefer a getter for pure calculations, and keep x-effect for work that must happen when data changes, such as updating document.title.

Transitions with x-transition

Elements shown with x-show snap in and out by default. Add x-transition and Alpine fades and scales them instead. Without arguments it uses sensible defaults; modifiers tune them: x-transition.duration.500ms, x-transition.opacity (fade only), x-transition.scale.80 (start at 80%), x-transition.origin.top.

For full control, supply your own CSS classes for each phase:

Directive When it applies
x-transition:enter The whole entering phase (put transition properties here)
x-transition:enter-start The first frame of entering
x-transition:enter-end The last frame of entering
x-transition:leave The whole leaving phase
x-transition:leave-start The first frame of leaving
x-transition:leave-end The last frame of leaving
Default and custom transitions

Use x-transition together with x-show. Elements removed by x-if or x-for are not a good fit for animation.

Avoiding flashes with x-cloak

Alpine starts after the HTML has loaded, so for a moment a hidden dropdown or a {{ }}-style placeholder might show unstyled. Add x-cloak to elements that should stay hidden until Alpine is ready, and add one CSS rule:

HTML
<style>[x-cloak] { display: none !important; }</style>

<div x-data="{ open: false }">
  <div x-show="open" x-cloak>I never flash on page load.</div>
</div>

Alpine removes the attribute once it initializes the component. Put the style in your <head>, so it applies before the first paint.

Modals with x-teleport

A modal written deep inside a card can be clipped by an ancestor's overflow: hidden or trapped under another element's stacking context. x-teleport="selector" moves the element to the end of the element that matches the selector (usually body) while it keeps working with the data of the component where you wrote it. It must be placed on a <template> tag with a single root element inside.

A teleported modal

The dialog uses the card's open variable even though it now lives under <body>. A production dialog should also move focus into itself and trap Tab; the official Focus plugin in Alpine plugins does that.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where is the best place to register a $watch?

    x-init runs once when the component starts, so the watcher is registered a single time.
  2. 2. Why would you use $nextTick?

    Alpine batches DOM updates. $nextTick runs your function after the page reflects the new data, for example so a newly shown input can be focused.
  3. 3. What does x-cloak need in order to work?

    x-cloak is only an attribute that Alpine removes later. The CSS rule is what hides the element until then.
  4. 4. Which element type must carry x-teleport?

    x-teleport is placed on a <template>; the single child element inside is moved to the target selector.

Summary

  • $watch reacts to data changes (register it in x-init); $nextTick waits for the DOM update.
  • $store reaches global stores; x-effect reruns an expression whenever the values it reads change.
  • x-transition animates x-show, with modifiers for quick tuning or six class directives for full control.
  • x-cloak plus one CSS rule prevents unstyled flashes; x-teleport moves modal markup to another place in the page while keeping its data.

Next, reusable components, Alpine.data and stores.