Alpine.js: the outsider Javascript framework

commitsoftware 364 views 23 slides Feb 25, 2022
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Alpine.js ti offre la natura reattiva e dichiarativa di grandi framework come Vue o React a un costo molto inferiore. La sua forza sta nella facilità di manipolazione del DOM (document object model).
Alpine.js non prevede alcun build step e fornisce tutti gli strumenti necessari per creare un'i...


Slide Content

PRIMARY
#2D3442
SECONDARY
#F90032
TERTIARY
#F3F3F3
use for text creation and items such as shapes and forms only use shapes and background items
delete this slide after read or if you don't need it

the outsider javascript framework

Claudio Bisconti

Principal Engineer @ MotorK

Technologyfanatic
Appleaddicted
Alpine.js
Tailwind%20CSS
lover
Vue.js
React
Laravel
expert
WordPressunfortunately expert

Alpine.js

Simple.
Lightweight.
Powerful as hell.

Alpine.js

The origins

State (JavaScript data that Alpine watches for changes)
How alpine.js work?
<section x-data="{ open: false }">
<button @click.prevent="open = !open">
Toggle button
</button>

<div x-show="open" x-cloak x-data="{ label: 'Content:' } ">
<span x-text="label"></span>

<p x-ignore>
Whathever you write here is ignored by Alpine.js lifecycle
</p>
</div>
</section>

Alpine is a collection of
●18 attributes
●9 magic properties
●3 global methods

Let’s check the docs together
Documentation
Documentation showcase

Official must to use plugins
Feature name NPM package name Alpine feature
intersection observer @alpinejs/intersect x-intersect
persist @alpinejs/persist
Save the Alpine.Store into the
localStorage
focus @alpinejs/focus x-trap
collapse @alpinejs/collapse x-collapse

Adding a new “Component”
window.myTabs = () => ({
selectedTab: 0,
setTabIndex: (i) => {
this.selectedTab = i
},
init: (i = 0) => {
this.setTabIndex(i)
}
})


<section x-data="myTabs(4)" x-init="init(2)">
<div class="tabs-header">
<button class="tab" @click="setTabIndex(0)">

</div>

<div class="tabs-content">
<div class="tab-0" x-show="selectedTab == 0">
...
</div>
...
</div>
</section>

Adding a new “Component”
const myTabs = () => ({
selectedTab: 0,
setTabIndex: (i) => {
this.selectedTab = i
},
init: (i = 0) => {
this.setTabIndex(i)
}
})

Alpine.directive(‘tabs’, myTabs);
<section x-tabs x-init="init(2)">
<div class="tabs-header">
<button class="tab" @click="setTabIndex(0)">

</div>

<div class="tabs-content">
<div class="tab-0" x-show="selectedTab == 0">
...
</div>
...
</div>
</section>

Adding a new “magic” component
Alpine.magic('clipboard', () => {
return subject => navigator.clipboard.writeText(subject)
})

// HTML usage

<button @click="$clipboard('hello world')">
Copy "Hello World"
</button>

Using in a ES6 context
import Alpine from 'alpinejs'

// Optional
window.Alpine = Alpine

// Register plugins and custom features
Alpine.start()

When you add HTML into the DOM,
all the Alpine.js components
will be automatically initialized
Surprised by the MutationObserver

Since Alpine it works directly into the HTML without bundlers…
You can use it also in production without hacks.

https://chrome.google.com/webstore/detail/alpinejs-devtools/fopaemeedckajflibkpifppcankfmbhk
Devtools

Livewire is a full-stack framework
for Laravel that makes building
dynamic front-ends as simple as
writing vanilla PHP (literally).

How we changed frontend
approach with alpine.js

We added the x-defer directive to Alpine
Dynamic modules
window.components = window.components || [];
window.components.push = function (obj) {
window.components[window.components.length] = obj;
loadModule(obj);
}

window.components.forEach(obj => loadModule(obj));

Alpine.js

Conclusions

Pro
●No bundlers needed
●Easy to use
●Near to Vue syntax
●It simplify the work on
small projects
Cons
●No SPA/Routing support
●It requires discipline
(to avoid spaghetti code)
●Is not a magic wand
Pro and cons table

That’s all.
Questions?

Software developer
Claudio Bisconti
[email protected]