Search on this site

Blog ✌️

AlpineJS: a Framework for Building Fast and Functional Frontends
@danestves
--- views

AlpineJS: a Framework for Building Fast and Functional Frontends

Published

When we need to make an interactive application and React or Vue is too much for us, AlpineJS comes in to make our work faster. Basically, when we need to make an interactive application and React or Vue is too much for us (not development-wise but time-wise) we can easily use AlpineJS which uses all the features of Vue for HTML behavior.

What is AlpineJS?

A rugged, minimal framework for composing JavaScript behavior in your markup.

Basically when we need to make an interactive application and React or Vue is too much for us (not development-wise but time-wise) we can easily use AlpineJS which uses all the features of Vue for HTML behavior.

We can think of AlpineJS as the TailwindCSS for JavaScript.

What can AlpineJS do? 🧐

  1. Dropdown:
  1. Tabs:

These are basic examples, but they illustrate very well the power of Alpine to add interactivity to our web sites. Now, how can we add animations? Very easy, we add TailwindCSS to our project and we can do something like this πŸ‘‡

Weird and easy right? Well let's explain the magic behind it

  1. We have our basic HTML:

    1<div x-data="{ open: false }">
    2 <button @click="open = true">Open Dropdown</button>
    3Β 
    4 <ul x-show="open" @click.away="open = false">
    5 Dropdown Body
    6 </ul>
    7</div>

    x-data would be our state, it can be anything, an array, an object, a string, numbers; any kind of value. @click would be our action and what it will do. x-show is a property that will display the element when its state is equal to true, but by default the element will have as property display: none; @click.away is a bind in which if we click outside the element it will close.

  2. Now, the animations can be added through the property x-transition in this way:

    1<ul
    2 x-show="open"
    3 @click.away="open = false"
    4 x-transition:enter="transition ease-out duration-300"
    5 x-transition:enter-start="opacity-0 transform scale-90"
    6 x-transition:enter-end="opacity-100 transform scale-100"
    7 x-transition:leave="transition ease-in duration-300"
    8 x-transition:leave-start="opacity-100 transform scale-100"
    9 x-transition:leave-end="opacity-0 transform scale-90"
    10>
    11 Dropdown Body
    12</ul>

    As we can see step by step we are telling it with TailwindCSS classes what it is going to do when entering and exiting in its different states, and AlpineJS will know when to apply these classes of x-transition based on the property of x-show

Now, how we would make an animation for a tab πŸ§πŸ‘‡


Thanks to AlpineJS properties we can manipulate JavaScript and interactivity through HTML.

I've been using it for about two months now and I'd like to show you all what I've learned in this time, so if you'd like more content on how to do more complex things with AlpineJS subscribe to the newsletter below this blog and stay tuned for more updates as they become available 😎

See you at code πŸ€“