aboutsummaryrefslogtreecommitdiff
path: root/test/example/src/main.gleam
diff options
context:
space:
mode:
Diffstat (limited to 'test/example/src/main.gleam')
-rw-r--r--test/example/src/main.gleam56
1 files changed, 56 insertions, 0 deletions
diff --git a/test/example/src/main.gleam b/test/example/src/main.gleam
new file mode 100644
index 0000000..7c9400f
--- /dev/null
+++ b/test/example/src/main.gleam
@@ -0,0 +1,56 @@
+// IMPORTS ---------------------------------------------------------------------
+
+import gleam/javascript/promise.{Promise}
+import gleam/string
+import lustre
+import lustre/cmd.{Cmd}
+import lustre/element.{Element}
+import lustre/event
+
+// MAIN ------------------------------------------------------------------------
+
+pub fn main() -> Promise(fn(Msg) -> Nil) {
+ let selector = "[data-lustre-container]"
+ let program = lustre.application(init(), update, render)
+
+ use _ <- promise.tap(lustre.start(program, selector))
+ Nil
+}
+
+// MODEL -----------------------------------------------------------------------
+
+type Model =
+ Int
+
+fn init() -> #(Model, Cmd(Msg)) {
+ #(0, cmd.none())
+}
+
+// UPDATE ----------------------------------------------------------------------
+
+pub opaque type Msg {
+ SetCount(Int)
+}
+
+fn update(_: Model, msg: Msg) -> #(Model, Cmd(Msg)) {
+ case msg {
+ SetCount(n) -> #(n, cmd.none())
+ }
+}
+
+// RENDER ----------------------------------------------------------------------
+
+fn render(model: Model) -> Element(Msg) {
+ element.div(
+ [],
+ [
+ element.map(
+ fn() {
+ element.button([event.on_click(model + 1)], [element.text("+")])
+ },
+ SetCount,
+ ),
+ element.text(string.inspect(model)),
+ ],
+ )
+}