aboutsummaryrefslogtreecommitdiff
path: root/examples/svg.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/svg.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/svg.gleam')
-rw-r--r--examples/svg.gleam107
1 files changed, 107 insertions, 0 deletions
diff --git a/examples/svg.gleam b/examples/svg.gleam
new file mode 100644
index 0000000..e895f1a
--- /dev/null
+++ b/examples/svg.gleam
@@ -0,0 +1,107 @@
+// IMPORTS ---------------------------------------------------------------------
+
+import gleam/int
+import lustre
+import lustre/attribute.{attribute}
+import lustre/element.{Element, text}
+import lustre/element/html.{button, div, p, svg}
+import lustre/element/svg.{path}
+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)],
+ [plus([attribute.style([#("color", "red")])])],
+ ),
+ button([event.on_click(Decr)], [minus([])]),
+ button([event.on_click(Reset)], [text("Reset")]),
+ p([], [text(int.to_string(model))]),
+ ],
+ )
+}
+
+fn plus(attrs) {
+ svg(
+ [
+ attribute("width", "15"),
+ attribute("height", "15"),
+ attribute("viewBox", "0 0 15 15"),
+ attribute("fill", "none"),
+ ..attrs
+ ],
+ [
+ path([
+ attribute(
+ "d",
+ "M8 2.75C8 2.47386 7.77614 2.25 7.5 2.25C7.22386 2.25 7 2.47386 7 2.75V7H2.75C2.47386 7 2.25 7.22386 2.25 7.5C2.25 7.77614 2.47386 8 2.75 8H7V12.25C7 12.5261 7.22386 12.75 7.5 12.75C7.77614 12.75 8 12.5261 8 12.25V8H12.25C12.5261 8 12.75 7.77614 12.75 7.5C12.75 7.22386 12.5261 7 12.25 7H8V2.75Z",
+ ),
+ attribute("fill", "currentColor"),
+ attribute("fill-rule", "evenodd"),
+ attribute("clip-rule", "evenodd"),
+ ]),
+ ],
+ )
+}
+
+fn minus(attrs) {
+ svg(
+ [
+ attribute("width", "15"),
+ attribute("height", "15"),
+ attribute("viewBox", "0 0 15 15"),
+ attribute("fill", "none"),
+ ..attrs
+ ],
+ [
+ path([
+ attribute(
+ "d",
+ "M2.25 7.5C2.25 7.22386 2.47386 7 2.75 7H12.25C12.5261 7 12.75 7.22386 12.75 7.5C12.75 7.77614 12.5261 8 12.25 8H2.75C2.47386 8 2.25 7.77614 2.25 7.5Z",
+ ),
+ attribute("fill", "currentColor"),
+ attribute("fill-rule", "evenodd"),
+ attribute("clip-rule", "evenodd"),
+ ]),
+ ],
+ )
+}