diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2023-08-19 22:21:19 +0100 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2023-08-19 22:21:19 +0100 |
commit | 9919bc2702c89168d1805eaa0db9e4baff091260 (patch) | |
tree | ef5f1fd360d90ec8433aa0fccc2709bed0e8e9d2 /lib/test/examples/counter.gleam | |
parent | e4aa0e04e54105395d5f6f5e3f7e2d9a4f7851e0 (diff) | |
download | lustre-9919bc2702c89168d1805eaa0db9e4baff091260.tar.gz lustre-9919bc2702c89168d1805eaa0db9e4baff091260.zip |
:truck: Shift things around to accomodate a monorepo.
Diffstat (limited to 'lib/test/examples/counter.gleam')
-rw-r--r-- | lib/test/examples/counter.gleam | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/test/examples/counter.gleam b/lib/test/examples/counter.gleam new file mode 100644 index 0000000..759ebdf --- /dev/null +++ b/lib/test/examples/counter.gleam @@ -0,0 +1,56 @@ +// IMPORTS --------------------------------------------------------------------- + +import gleam/int +import lustre +import lustre/element.{Element, text} +import lustre/element/html.{button, div, p} +import lustre/event + +// MAIN ------------------------------------------------------------------------ + +pub fn main() { + // A `simple` lustre application doesn't produce `Effect`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, render) + let assert Ok(_) = lustre.start(app, "[data-lustre-app]") +} + +// MODEL ----------------------------------------------------------------------- + +pub type Model = + Int + +pub fn init() -> Model { + 0 +} + +// UPDATE ---------------------------------------------------------------------- + +pub opaque type Msg { + Incr + Decr + Reset +} + +pub fn update(model: Model, msg: Msg) -> Model { + case msg { + Incr -> model + 1 + Decr -> model - 1 + Reset -> 0 + } +} + +// VIEW ------------------------------------------------------------------------ + +pub fn render(model: Model) -> Element(Msg) { + div( + [], + [ + button([event.on_click(Incr)], [text("+")]), + button([event.on_click(Decr)], [text("-")]), + button([event.on_click(Reset)], [text("Reset")]), + p([], [text(int.to_string(model))]), + ], + ) +} |