aboutsummaryrefslogtreecommitdiff
path: root/examples/counter
diff options
context:
space:
mode:
Diffstat (limited to 'examples/counter')
-rw-r--r--examples/counter/gleam.toml7
-rw-r--r--examples/counter/manifest.toml11
-rw-r--r--examples/counter/src/counter.gleam53
3 files changed, 71 insertions, 0 deletions
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))]),
+ ])
+}