aboutsummaryrefslogtreecommitdiff
path: root/examples/components.gleam
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2023-09-22 10:42:33 +0100
committerHayleigh Thompson <me@hayleigh.dev>2023-09-22 10:42:33 +0100
commit60ca120b030de72aedd675e16961f0ece2fe8063 (patch)
treee3ab36c3ce430b2dd9c205b04ffe646cd7102075 /examples/components.gleam
parent5e1dd775e7c1974e88df0494eb2e3effb7bcb9f2 (diff)
downloadlustre-60ca120b030de72aedd675e16961f0ece2fe8063.tar.gz
lustre-60ca120b030de72aedd675e16961f0ece2fe8063.zip
:truck: Move examples out of test so CI doesn't break.
Diffstat (limited to 'examples/components.gleam')
-rw-r--r--examples/components.gleam100
1 files changed, 100 insertions, 0 deletions
diff --git a/examples/components.gleam b/examples/components.gleam
new file mode 100644
index 0000000..b87cf84
--- /dev/null
+++ b/examples/components.gleam
@@ -0,0 +1,100 @@
+// IMPORTS ---------------------------------------------------------------------
+
+import gleam/dynamic
+import gleam/function
+import gleam/int
+import gleam/list
+import gleam/map
+import gleam/result
+import lustre
+import lustre/attribute
+import lustre/effect
+import lustre/element.{element, text}
+import lustre/element/html.{button, div, li, ol, p, slot}
+import lustre/event
+
+// MAIN ------------------------------------------------------------------------
+
+pub fn main() {
+ let assert Ok(_) =
+ lustre.component(
+ "custom-counter",
+ counter_init,
+ counter_update,
+ counter_view,
+ map.from_list([
+ #(
+ "count",
+ fn(attr) {
+ dynamic.int(attr)
+ |> result.map(GotCount)
+ },
+ ),
+ ]),
+ )
+
+ // 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)
+
+ Nil
+}
+
+fn init(_) {
+ []
+}
+
+fn update(history, msg) {
+ case msg {
+ "reset" -> []
+ _ -> [msg, ..history]
+ }
+}
+
+fn view(history) {
+ let on_custom_click = event.on("custom-click", function.constant(Ok("click")))
+
+ div(
+ [],
+ [
+ button([event.on_click("reset")], [text("Reset")]),
+ ol([], list.map(history, fn(msg) { li([], [text(msg)]) })),
+ element(
+ "custom-counter",
+ [on_custom_click, attribute.property("count", list.length(history))],
+ [ol([], list.map(history, fn(msg) { li([], [text(msg)]) }))],
+ ),
+ ],
+ )
+}
+
+// COUNTER ---------------------------------------------------------------------
+
+fn counter_init() {
+ #(0, effect.none())
+}
+
+type CounterMsg {
+ GotCount(Int)
+ Clicked
+}
+
+fn counter_update(count, msg) {
+ case msg {
+ GotCount(count) -> #(count, effect.none())
+ Clicked -> #(count, event.emit("custom-click", Nil))
+ }
+}
+
+fn counter_view(count) {
+ div(
+ [],
+ [
+ button([event.on_click(Clicked)], [text("Click me!")]),
+ p([], [text("Count: "), text(int.to_string(count))]),
+ slot([]),
+ ],
+ )
+}