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
|
import gleam/int
import lustre
import lustre/attribute
import lustre/element.{type Element}
import lustre/element/html
import lustre/event
import lustre/ui
// MAIN ------------------------------------------------------------------------
pub fn main() {
let app = lustre.simple(init, update, view)
let assert Ok(_) = lustre.start(app, "#app", 0)
Nil
}
// MODEL -----------------------------------------------------------------------
type Model =
Int
fn init(initial_count: Int) -> Model {
case initial_count < 0 {
True -> 0
False -> initial_count
}
}
// UPDATE ----------------------------------------------------------------------
pub opaque type Msg {
Incr
Decr
}
fn update(model: Model, msg: Msg) -> Model {
case msg {
Incr -> model + 1
Decr -> model - 1
}
}
// VIEW ------------------------------------------------------------------------
fn view(model: Model) -> Element(Msg) {
let styles = [#("width", "100vw"), #("height", "100vh"), #("padding", "1rem")]
let count = int.to_string(model)
ui.centre(
[attribute.style(styles)],
ui.stack([], [
ui.button([event.on_click(Incr)], [element.text("+")]),
html.p([attribute.style([#("text-align", "center")])], [
element.text(count),
]),
ui.button([event.on_click(Decr)], [element.text("-")]),
]),
)
}
|