1
- # Alpine.js
1
+ # Alpine.js - Your new, lightweight, JavaScript framework.
2
2
3
- - Don't need bundle or build tooling
4
- - simple - 15 attributes, 6 properties, and 2 methods.
5
- - lightweight - cdn
6
- - Vue reactivity system
7
- - extensions / plugins / components
8
- - https://alpinejs.dev/components / https://www.alpinetoolbox.com/ / https://js.hyperui.dev/
3
+ <!-- column_layout: [1, 1] -->
9
4
10
- https://www.codemotion.com/magazine/frontend/ten-reasons-why-i-think-alpine-js-can-do-magic/
11
- https://medium.com/@jogarcia/alpinejs-for-the-vuejs-developer-5b39fd21c2c1
5
+ <!-- column: 0 -->
12
6
13
- ```
14
- <script src="//unpkg.com/alpinejs " defer></script >
15
-
7
+ ``` html
8
+ <script src =" alpine.js " defer >
9
+ </ script >
16
10
<div x-data =" { open: false }" >
17
- <button @click="open = true">Expand</button>
11
+ <button @click =" open = true" >
12
+ Expand
13
+ </button >
18
14
19
15
<span x-show =" open" >
20
16
Content...
21
17
</span >
22
18
</div >
23
19
```
24
20
21
+ <!-- column: 1 -->
22
+
23
+ <!-- pause -->
24
+ - Don't need bundle or build tooling
25
+ <!-- pause -->
26
+ - ` defer ` : parallel downloading & executed after the page has finished parsing.
27
+ <!-- pause -->
28
+
29
+ - simple - js version TailwindCSS
30
+ <!-- pause -->
31
+ - 18 directives / attributes
32
+ - 9 magics / properties
33
+ - 3 globals / methods.
34
+ <!-- pause -->
35
+ - lightweight - cdn 43k/16k(gz)
36
+ <!-- pause -->
37
+ - Vue reactivity system
38
+ - extensions / plugins / components
39
+
40
+ <!-- reset_layout -->
41
+
42
+
43
+
44
+ <!-- end_slide-->
45
+
46
+ ## Doc & Components
47
+
48
+ Doc:
49
+ - [ Alpine] ( https://alpinejs.dev/start-here )
50
+ - [ Alpine.js 繁體中文文件] ( https://hackmd.io/@monkenWu/S14j-NqsU ) 中文文件v2
51
+ - [ 10_reasons] ( https://www.codemotion.com/magazine/frontend/ten-reasons-why-i-think-alpine-js-can-do-magic/ )
52
+ - [ from_vue] ( https://medium.com/@jogarcia/alpinejs-for-the-vuejs-developer-5b39fd21c2c1 )
53
+
54
+ ---
55
+
56
+ Components:
57
+ - [ Alpine UI Components] ( https://alpinejs.dev/components )
58
+ - [ Toolbox] ( https://www.alpinetoolbox.com/ )
59
+ - [ HyperUI] ( https://js.hyperui.dev/ )
60
+
61
+
62
+ <!-- end_slide -->
63
+
64
+ ### x-data - defines reactive data for that component to reference.
65
+
66
+ <!-- column_layout: [1, 1] -->
67
+
68
+ <!-- column: 0 -->
69
+
70
+ ``` html
71
+ <div x-data =" {
72
+ foo: 'bar',
73
+ open: false,
74
+ get isOpen() { return this.open },
75
+ toggle() {
76
+ this.open = ! this.open
77
+ }
78
+ }" >
79
+ <span x-text =" foo" ></span >
80
+ <div x-data =" { bar: 'baz' }" >
81
+ <span x-text =" foo" ></span >
82
+ <div x-data =" { foo: 'bob' }" >
83
+ <span x-text =" foo" ></span >
84
+ </div >
85
+ </div >
86
+ <button @click =" toggle()" >Toggle</button >
87
+ <div x-show =" isOpen" >
88
+ Content...
89
+ </div >
90
+ </div >
91
+ ```
92
+ <!-- column: 1 -->
93
+
94
+ - Scope - available to all element children (even nested)
95
+ - Single-element components
96
+ - Methods
97
+ - x-data is evaluated as a normal JavaScript object
98
+ - Getters
99
+ - ``` get isOpen() { return this.open } ```
100
+ - Data-less components
101
+ - ``` <div x-data> ```
102
+
103
+
104
+
105
+ <!-- end_slide-->
106
+
107
+
108
+
109
+
110
+
111
+
25
112
## compare to other frameworks
26
113
27
- - vs React:
28
- - vs Vue:
29
- - vs htmx:
30
- - vs astrojs/nextjs:
31
- - old school is back / TALL stack
114
+ - vs React - (-) JSX (-) VDOM (-) SPA
115
+ - vs Vue - (-) VDOM (-) SPA
116
+ - (=) template-wise (=) MPA (=) Reactivity
117
+ - (* ) component scope
118
+ - vs htmx(* ) - (-) make HTTP requests/response (=) MPA
119
+ - vs astrojs(* )/nextjs - (-) SSG/SSR (=) MPA
32
120
33
121
122
+ <!-- end_slide-->
123
+
34
124
### vs React
35
125
36
126
```
37
127
const root = ReactDOM.createRoot(document.getElementById('root'));
38
128
root.render(<h1>Hello, world!</h1>);
129
+
130
+ function MyButton() {
131
+ return (
132
+ <button>I'm a button</button>
133
+ );
134
+ }
135
+ ```
136
+
137
+ <!-- column_layout: [1, 1] -->
138
+
139
+ <!-- column: 0 -->
140
+
141
+ ```
142
+ <div>
143
+ {isLoggedIn ? (
144
+ <AdminPanel />
145
+ ) : (
146
+ <LoginForm />
147
+ )}
148
+ </div>
39
149
```
150
+ <!-- column: 1 -->
151
+ - JSX
152
+
153
+ <!-- reset_layout -->
154
+
155
+ <!-- end_slide-->
40
156
41
157
### vs Vue
42
158
@@ -56,10 +172,59 @@ const msg = ref('Hello World!')
56
172
</template>
57
173
```
58
174
175
+ <!-- end_slide-->
176
+
59
177
### vs htmx
60
178
179
+ ```
180
+ <script src="https://unpkg.com/htmx.org@1.9.10"></script>
181
+ <!-- have a button POST a click via AJAX -->
182
+ <button hx-post="/clicked" hx-swap="outerHTML">
183
+ Click Me
184
+ </button>
185
+ ```
186
+
187
+ - to AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes
188
+ - simplicity and power of hypertext
189
+
190
+ <!-- end_slide-->
61
191
62
192
### vs astrojs
63
193
194
+ <!-- column_layout: [1, 1] -->
195
+
196
+ <!-- column: 0 -->
197
+ - Component Islands
198
+ - Zero JavaScript - Input JS Output Html
199
+ - Highly customizable
200
+ - UI- agnostic (meaning it is interoperable with a lot of UI frameworks)
201
+ - Server-first API (SSR/SSG)
202
+ - Server Side Rendering
203
+ - Static Site Generator
204
+ - Edge ready
205
+
206
+ <!-- column: 1 -->
207
+ ```
208
+ ---
209
+ const pageTitle = "About Me";
210
+ ---
211
+ <html lang="en">
212
+ <head>
213
+ <meta charset="utf-8" />
214
+ <title>{pageTitle}</title>
215
+ </head>
216
+ <body>
217
+ ...
218
+ </body>
219
+ </html>
220
+ ```
221
+
222
+ <!-- reset_layout -->
223
+
224
+ <!-- end_slide-->
225
+
64
226
227
+ old school is coming back...
228
+ <!-- pause -->
229
+ TALL stack - Tailwind CSS / AlpineJS / Laravel / Livewire
65
230
0 commit comments