aboutsummaryrefslogtreecommitdiff
path: root/test-apps/no-keyed-diff/src/app.gleam
blob: 400e8660bd11134134e71c77ae0f7c8721274fbb (plain)
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
import gleam/list
import lustre
import lustre/attribute
import lustre/element.{type Element, element}
import lustre/element/html
import lustre/event
// These examples are written with `lustre/ui` in mind. They'll work regardless,
// but to see what `lustre/ui` can do make sure to run each of these examples with
// the `--use-example-styles` flag:
//
//   $ gleam run -m lustre/dev start --use-example-styles
//
// In your own apps, make sure to add the `lustre/ui` dependency and include the
// stylesheet somewhere.
import lustre/ui

// MAIN ------------------------------------------------------------------------

pub fn main() {
  let app = lustre.simple(init, update, view)
  let assert Ok(_) = lustre.start(app, "#app", Nil)
}

// MODEL -----------------------------------------------------------------------

type Model =
  Bool

fn init(_flags) -> Model {
  False
}

// UPDATE ----------------------------------------------------------------------

pub opaque type Msg {
  Swap
}

fn update(model: Model, msg: Msg) -> Model {
  case msg {
    Swap -> !model
  }
}

// VIEW ------------------------------------------------------------------------

const names = ["hayleigh", "jak", "john"]

fn view(model: Model) -> Element(Msg) {
  let styles = [#("width", "100vw"), #("height", "100vh"), #("padding", "1rem")]

  ui.centre(
    [attribute.style(styles)],
    ui.stack([], [
      ui.button([event.on_click(Swap)], [element.text("toggle content")]),
      case model {
        True ->
          element.keyed(
            ui.stack([], _),
            list.map(names, fn(name) {
              #(name, html.span([], [html.text(name)]))
            }),
          )
        False -> element.none()
      },
    ]),
  )
}