diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2023-07-10 23:11:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-10 23:11:32 +0100 |
commit | f350db196bcab490b8a6b67f9536f0b9b7322073 (patch) | |
tree | 59664ca671be2cfdb473424ca1bf737cf12622e8 /test/counter.gleam | |
parent | 6d314230346336ba5b452b1df39b908ffa666f45 (diff) | |
download | lustre-f350db196bcab490b8a6b67f9536f0b9b7322073.tar.gz lustre-f350db196bcab490b8a6b67f9536f0b9b7322073.zip |
♻️ Replace React with diffhtml (#10)
* :wrench: Remove react dependency, add vite for running examples.
* :heavy_plus_sign: Update stdlib version to 0.29
* :fire: Remove old examples.
* :sparkles: Vendor diffhtml and update runtime ffi code to replace react.
* :recycle: Refactor all the things now react is gone.
* :memo: Remove references to react in the readme.
* :sparkles: Create a simple counter example.
Diffstat (limited to 'test/counter.gleam')
-rw-r--r-- | test/counter.gleam | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/counter.gleam b/test/counter.gleam new file mode 100644 index 0000000..5a5326b --- /dev/null +++ b/test/counter.gleam @@ -0,0 +1,73 @@ +// IMPORTS --------------------------------------------------------------------- + +import gleam/int +import gleam/io +import gleam/option.{None} +import lustre +import lustre/element.{Element, button, div, text} +import lustre/event + +// MAIN ------------------------------------------------------------------------ + +pub fn main() { + // A `simple` lustre application doesn't produce `Cmd`s. These are best to + // start with if you're just getting started with lustre or you know you don't + // need the runtime to manage any side effects. + let app = lustre.simple(init, update, view) + let assert Ok(dispatch) = lustre.start(app, "body") + + dispatch(Incr) + dispatch(Incr) + dispatch(Incr) +} + +// MODEL ----------------------------------------------------------------------- + +type Model = + Int + +fn init() -> Model { + 0 +} + +// UPDATE ---------------------------------------------------------------------- + +type Msg { + Incr + Decr + Reset +} + +fn update(model: Model, msg: Msg) -> Model { + case msg { + Incr -> model + 1 + Decr -> model - 1 + Reset -> 0 + } +} + +// VIEW ------------------------------------------------------------------------ + +fn view(model: Model) -> Element(Msg) { + div( + [], + [ + button([event.on_click(Incr)], [text("+")]), + button([event.on_click(Decr)], [text("-")]), + button([event.on_click(Reset)], [text("Reset")]), + button( + [ + event.on( + "click", + fn(_) { + io.println("Do nothing") + None + }, + ), + ], + [text("Do Nothing")], + ), + div([], [text(int.to_string(model))]), + ], + ) +} |