webfluent 0.5.0-alpha

The Web-First Language — compiles to HTML, CSS, JavaScript, and PDF. 50+ built-in components, reactivity, routing, i18n, SSG, and template engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Language Guide</title>
    <meta name="description" content="A programming language that compiles to HTML, CSS, and JavaScript. Build SPAs and static sites with built-in components, reactivity, routing, i18n, and animations.">
    <link rel="stylesheet" href="../styles.css">
</head>
<body>
    <div id="app">
        <!--wf-component-->
        <div class="wf-row">
            <!--wf-component-->
            <div class="wf-container wf-animate-fadeIn">
                <div class="wf-spacer"></div>
                <h1 class="wf-heading wf-heading--h1">Language Guide</h1><p class="wf-text wf-text--muted">Learn the core concepts of WebFluent.</p><div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Pages</h2><p class="wf-text">Pages are top-level route targets. Each page defines a URL path and contains the UI tree for that route.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">Page Home (path: &quot;/&quot;, title: &quot;Home&quot;) {
    Container {
        Heading(&quot;Welcome&quot;, h1)
        Text(&quot;This is the home page.&quot;)
    }
}</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--bold">Page attributes:</p><table class="wf-table">
                    <thead>
                        <td>Attribute</td><td>Type</td><td>Description</td></thead><tr>
                        <td>path</td><td>String</td><td>URL route for this page (required)</td></tr><tr>
                        <td>title</td><td>String</td><td>Document title</td></tr><tr>
                        <td>guard</td><td>Expression</td><td>Navigation guard — redirects if false</td></tr><tr>
                        <td>redirect</td><td>String</td><td>Redirect target when guard fails</td></tr></table><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Components</h2><p class="wf-text">Reusable UI blocks that accept props and can have internal state.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">Component UserCard (name: String, role: String, active: Bool = true) {
    Card(elevated) {
        Row(align: center, gap: md) {
            Avatar(initials: &quot;U&quot;, primary)
            Stack {
                Text(name, bold)
                Text(role, muted)
            }
            if active {
                Badge(&quot;Active&quot;, success)
            }
        }
    }
}

// Usage
UserCard(name: &quot;Monzer&quot;, role: &quot;Developer&quot;)</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--muted">Props support types: String, Number, Bool, List, Map. Optional props use ?, defaults use =.</p><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">State and Reactivity</h2><p class="wf-text">State is declared with the state keyword. It is reactive — any UI that reads it updates automatically when it changes.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">Page Counter (path: &quot;/counter&quot;) {
    state count = 0

    Container {
        Text(&quot;Count: {count}&quot;)
        Button(&quot;+1&quot;, primary) { count = count + 1 }
        Button(&quot;-1&quot;) { count = count - 1 }
    }
}</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--bold">Derived state:</p><div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">state items = [{name: &quot;A&quot;, price: 3}, {name: &quot;B&quot;, price: 2}]
derived total = items.map(i =&gt; i.price).sum()
derived isEmpty = items.length == 0</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Events</h2><p class="wf-text">Event handlers are declared with on:event or via shorthand blocks on buttons.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">Button(&quot;Submit&quot;) {
    on:click {
        submitForm()
    }
}

Input(text, placeholder: &quot;Search...&quot;) {
    on:input {
        searchQuery = value
    }
    on:keydown {
        if key == &quot;Enter&quot; {
            performSearch()
        }
    }
}

// Shorthand: block on Button defaults to on:click
Button(&quot;Save&quot;) { save() }</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--muted">Supported events: on:click, on:submit, on:input, on:change, on:focus, on:blur, on:keydown, on:keyup, on:mouseover, on:mouseout, on:mount, on:unmount</p><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Control Flow</h2><p class="wf-text wf-text--bold">Conditionals:</p><div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">if isLoggedIn {
    Text(&quot;Welcome back!&quot;)
} else if isGuest {
    Text(&quot;Hello, guest&quot;)
} else {
    Button(&quot;Log In&quot;) { navigate(&quot;/login&quot;) }
}</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--bold">Loops:</p><div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">for user in users {
    UserCard(name: user.name, role: user.role)
}

// With index
for item, index in items {
    Text(&quot;{index + 1}. {item}&quot;)
}</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--bold">Show/Hide (keeps element in DOM, toggles visibility):</p><div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">show isExpanded {
    Card { Text(&quot;Expanded content&quot;) }
}</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Stores</h2><p class="wf-text">Stores hold shared state accessible from any page or component.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">Store CartStore {
    state items = []

    derived total = items.map(i =&gt; i.price * i.quantity).sum()
    derived count = items.length

    action addItem(product: Map) {
        items.push({ id: product.id, name: product.name, price: product.price, quantity: 1 })
    }

    action removeItem(id: Number) {
        items = items.filter(i =&gt; i.id != id)
    }
}

// Usage in a page
Page Cart (path: &quot;/cart&quot;) {
    use CartStore

    Text(&quot;Total: ${CartStore.total}&quot;)
    Button(&quot;Clear&quot;) { CartStore.clear() }
}</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Routing</h2><p class="wf-text">SPA routing is declared in the App file.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">App {
    Navbar {
        Navbar.Brand { Text(&quot;My App&quot;, heading) }
        Navbar.Links {
            Link(to: &quot;/&quot;) { Text(&quot;Home&quot;) }
            Link(to: &quot;/about&quot;) { Text(&quot;About&quot;) }
        }
    }

    Router {
        Route(path: &quot;/&quot;, page: Home)
        Route(path: &quot;/about&quot;, page: About)
        Route(path: &quot;/user/:id&quot;, page: UserProfile)
        Route(path: &quot;*&quot;, page: NotFound)
    }
}

// Programmatic navigation
Button(&quot;Go Home&quot;) { navigate(&quot;/&quot;) }

// Dynamic routes access params
Page UserProfile (path: &quot;/user/:id&quot;) {
    Text(&quot;User ID: {params.id}&quot;)
}</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Data Fetching</h2><p class="wf-text">Built-in async data loading with automatic loading, error, and success states.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">fetch users from &quot;/api/users&quot; {
    loading {
        Spinner()
    }
    error (err) {
        Alert(&quot;Failed to load users&quot;, danger)
    }
    success {
        for user in users {
            UserCard(name: user.name, role: user.role)
        }
    }
}

// With options
fetch result from &quot;/api/submit&quot; (method: &quot;POST&quot;, body: { name: name, email: email }) {
    success {
        Alert(&quot;Saved!&quot;, success)
    }
}</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Return Values</h2><p class="wf-text">Store actions can return values using the return keyword.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">Store AuthStore {
    state accessToken = &quot;&quot;

    action getHeaders() {
        h = {}
        h[&quot;Authorization&quot;] = &quot;Bearer &quot; + accessToken
        return h
    }
}</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Browser Globals</h2><p class="wf-text">Standard browser APIs are available directly without any special syntax. They compile to their JavaScript equivalents.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">// Storage
localStorage.setItem(&quot;token&quot;, tok)
sessionStorage.getItem(&quot;key&quot;)

// Window &amp; Document
window.open(&quot;https://example.com&quot;)

// JSON
data = JSON.parse(responseText)
text = JSON.stringify(obj)

// Console
console.log(&quot;debug info&quot;)

// Timers
setTimeout(callback, 1000)</code></div></div><div class="wf-spacer"></div>
                <p class="wf-text wf-text--muted">Available globals: window, document, console, localStorage, sessionStorage, JSON, Math, Date, setTimeout, setInterval, parseInt, parseFloat, Array, Object, Promise, Error, fetch, alert, confirm, prompt, and more.</p><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Map Literals</h2><p class="wf-text">Map literals support quoted string keys for HTTP headers and special field names. Reserved words also work as map keys.</p><div class="wf-spacer"></div>
                <div class="wf-card wf-card--outlined">
                    <div class="wf-card__body">
                        <code class="wf-code">// Quoted keys for headers
headers: { &quot;Content-Type&quot;: &quot;application/json&quot;, &quot;X-Api-Key&quot;: apiKey }

// Reserved words as keys
body: { action: &quot;create&quot;, token: sessionToken, state: &quot;active&quot; }</code></div></div><div class="wf-spacer"></div>
                <hr class="wf-divider">
                <div class="wf-spacer"></div>
                <h2 class="wf-heading wf-heading--h2">Operators</h2><p class="wf-text">WebFluent supports all common comparison and logical operators.</p><div class="wf-spacer"></div>
                <table class="wf-table">
                    <thead>
                        <td>Operator</td><td>Description</td></thead><tr>
                        <td>==</td><td>Equal</td></tr><tr>
                        <td>!=</td><td>Not equal</td></tr><tr>
                        <td>!==</td><td>Strict not equal (alias for !=)</td></tr><tr>
                        <td>&lt; &gt; &lt;= &gt;=</td><td>Comparison</td></tr><tr>
                        <td>&amp;&amp; ||</td><td>Logical AND / OR</td></tr><tr>
                        <td>!</td><td>Logical NOT</td></tr><tr>
                        <td>+ - * / %</td><td>Arithmetic</td></tr></table><div class="wf-spacer"></div>
                <div class="wf-row">
                    <button class="wf-btn wf-btn--primary">
                        Components Reference
                    </button><button class="wf-btn">
                        Styling Guide
                    </button></div><div class="wf-spacer"></div>
            </div></div>
        <!--wf-component-->
    </div>
    <script src="../app.js"></script>
</body>
</html>