

As a result, you can lose the filesize, wizardry and cognitive-load of larger framework but retain the declarative programming methodology.
#Alpine s 10 code#
Like Vue and React, Alpine.js allows us to write declarative code but it uses the “real” DOM amending the contents and attributes of the same nodes that you and I might edit when we crack open a text editor or dev-tools. Especially when all you want to do is toggle tabs! And that is where Alpine.js steps in. Typically, the bundle size of these frameworks is large and for those coming from a jQuery background, the learning curve feels incredibly steep.

This allows us to write more declarative code that is cleaner and easier to read. The result is that when you ‘hide’ a DOM element (like a tab) in one of these frameworks it doesn’t add a display:none style attribute, but instead it literally disappears from the ‘real’ DOM. These frameworks allow us to write cleaner code by utilizing the virtual DOM: a kind of mirror of the UI stored in the browser memory. We’d quickly get into some serious spaghetti code.ĭevelopers have solved this issue by reaching for frameworks like Vue, Angular and React.


Imagine if we wanted to disable the button after it has been clicked, or wanted to change the background color of the page. But this style of coding where we tell the browser exactly what to do (imperative coding) quickly gets us in a mess. See the Pen Showing / hiding with jQuery by Phil on CodePen. In that situation, we create an event listener on the tab, and when a user clicks, the event fires and we tell the browser what to do. To create a better experience over the years, developers have reached for jQuery and/or Bootstrap. But the page refresh on every tab click isn’t very ‘reactive’. If we come from a PHP background, we could easily achieve this server side. Our ultimate goal is that when a user clicks on a tab, the tab contents displays. Let’s consider a basic UI pattern like Tabs. You get to keep your DOM, and sprinkle in behavior as you see fit.” What Is Alpine.js?Īccording to project author Caleb Porzio: “Alpine.js offers you the reactive and declarative nature of big frameworks like Vue or React at a much lower cost. In fact, part of what makes Alpine.js great is that you barely need to know any JavaScript at all. Throughout the article, I refer to Vue.js, but don’t worry if you have no experience of Vue - that is not required. If you regularly build sites that require a sprinkling on Javascript to alter the UI based on some user interaction, then this article is for you. In this article, we’re going to take a closer look at Alpine.js and how it can replace JQuery or larger JavaScript libraries to build interactive websites. Recently, the rise of utility based tools like Tailwind CSS have done this for CSS, and now Alpine.js promises something similar for JavaScript. Why use CSS when you can use CSS-in-JS? Why use Grunt when you can use Gulp? Why use Gulp when you can use Webpack? Why use a traditional CMS when you can go headless? Every so often though, the new-hotness makes life simpler. Like most developers, I have a bad tendency to over-complicate my workflow, especially if there’s some new hotness on the horizon. Ever built a website and reached for jQuery, Bootstrap, Vue.js or React to acheive some basic user interaction? Alpine.js is a fraction of the size of these frameworks because it involves no build steps and provides all of the tools you need to build a basic user interface.
