diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-01-27 02:24:50 +0000 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-01-27 02:24:50 +0000 |
commit | c34d1a0e9003d483752720466def696c30f705c8 (patch) | |
tree | b6e693c3b3d971a9d449daeb461b1a867177ad39 /examples/02-interactivity/src/app.gleam | |
parent | 235fcd2ae0229272ebb0927806d301c8b887c1ba (diff) | |
download | lustre-c34d1a0e9003d483752720466def696c30f705c8.tar.gz lustre-c34d1a0e9003d483752720466def696c30f705c8.zip |
:construction: Start replacing old test examples with structured examples.
Diffstat (limited to 'examples/02-interactivity/src/app.gleam')
-rw-r--r-- | examples/02-interactivity/src/app.gleam | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/02-interactivity/src/app.gleam b/examples/02-interactivity/src/app.gleam new file mode 100644 index 0000000..e47b7a4 --- /dev/null +++ b/examples/02-interactivity/src/app.gleam @@ -0,0 +1,66 @@ +import gleam/int +import lustre +import lustre/attribute +import lustre/element.{type 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 `--include-styles` flag: +// +// $ gleam run -m lustre/try -- --include-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", 0) +} + +// 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 count = int.to_string(model) + let styles = [#("width", "100vw"), #("height", "100vh")] + + 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("-")]), + ]), + ) +} |