From ff3a5e7ce450f1007ae39a47f4e1b431da04a23d Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Fri, 22 Dec 2023 01:37:39 +0000 Subject: :recycle: Refactor example apps to use lustre/try instead of vite. --- examples/counter/gleam.toml | 7 +++++ examples/counter/manifest.toml | 11 ++++++++ examples/counter/src/counter.gleam | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 examples/counter/gleam.toml create mode 100644 examples/counter/manifest.toml create mode 100644 examples/counter/src/counter.gleam (limited to 'examples/counter') diff --git a/examples/counter/gleam.toml b/examples/counter/gleam.toml new file mode 100644 index 0000000..2a5f0f5 --- /dev/null +++ b/examples/counter/gleam.toml @@ -0,0 +1,7 @@ +name = "counter" +version = "1.0.0" +target = "javascript" + +[dependencies] +gleam_stdlib = "~> 0.34" +lustre = { path = "../../" } \ No newline at end of file diff --git a/examples/counter/manifest.toml b/examples/counter/manifest.toml new file mode 100644 index 0000000..715dadc --- /dev/null +++ b/examples/counter/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, + { name = "lustre", version = "3.0.12", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "../.." }, +] + +[requirements] +gleam_stdlib = { version = "~> 0.34" } +lustre = { path = "../../" } diff --git a/examples/counter/src/counter.gleam b/examples/counter/src/counter.gleam new file mode 100644 index 0000000..37af39a --- /dev/null +++ b/examples/counter/src/counter.gleam @@ -0,0 +1,53 @@ +// IMPORTS --------------------------------------------------------------------- + +import gleam/int +import lustre +import lustre/element.{type 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, view) + let assert Ok(_) = lustre.start(app, "[data-lustre-app]", Nil) +} + +// 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 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")]), + p([], [text(int.to_string(model))]), + ]) +} -- cgit v1.2.3