aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2022-05-14 06:26:30 +0100
committerHayleigh Thompson <me@hayleigh.dev>2022-05-14 06:26:30 +0100
commit25a287bf529b66d8235b387301881dca0b1e9063 (patch)
tree04001d8d2b5802c1917d185f5cf75bb46858a13d
parent63c28635c57bf8d75030947b987a599e6c277bb0 (diff)
downloadlustre-25a287bf529b66d8235b387301881dca0b1e9063.tar.gz
lustre-25a287bf529b66d8235b387301881dca0b1e9063.zip
:recycle: Explicitly pass 'dispatch' callback to event handlers.
-rw-r--r--src/lustre/attribute.gleam6
-rw-r--r--src/lustre/event.gleam53
2 files changed, 43 insertions, 16 deletions
diff --git a/src/lustre/attribute.gleam b/src/lustre/attribute.gleam
index c294b00..745e834 100644
--- a/src/lustre/attribute.gleam
+++ b/src/lustre/attribute.gleam
@@ -3,7 +3,7 @@ import gleam/dynamic.{ Dynamic }
pub opaque type Attribute(action) {
Attribute(name: String, value: String)
Property(name: String, value: Dynamic)
- Event(on: String, handler: fn (Dynamic) -> action)
+ Event(name: String, handler: fn (Dynamic, fn (action) -> Nil) -> Nil)
}
// CONSTRUCTORS ----------------------------------------------------------------
@@ -16,6 +16,6 @@ pub fn property (name: String, value: Dynamic) -> Attribute(action) {
Property(name, value)
}
-pub fn event (on: String, handler: fn (Dynamic) -> action) -> Attribute(action) {
- Event(on, handler)
+pub fn event (name: String, handler: fn (Dynamic, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ Event(name, handler)
} \ No newline at end of file
diff --git a/src/lustre/event.gleam b/src/lustre/event.gleam
index 23c9f49..75c540b 100644
--- a/src/lustre/event.gleam
+++ b/src/lustre/event.gleam
@@ -1,24 +1,51 @@
import gleam/dynamic.{ Dynamic }
import lustre/attribute.{ Attribute }
-pub fn on (event: String, handler: fn (Dynamic) -> action) -> Attribute(action) {
- attribute.event(event, handler)
+pub external fn ignore () -> action
+ = "./ffi.mjs" "ignore"
+
+
+pub fn on (name: String, handler: fn (Dynamic, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ attribute.event(name, handler)
+}
+
+
+// MOUSE EVENTS ----------------------------------------------------------------
+
+pub fn on_click (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("click", fn (_, dispatch) { handler(dispatch) })
+}
+
+pub fn on_mouse_down (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("mouseDown", fn (_, dispatch) { handler(dispatch) })
}
-//
+pub fn on_mouse_up (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("mouseUp", fn (_, dispatch) { handler(dispatch) })
+}
+
+pub fn on_mouse_enter (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("mouseEnter", fn (_, dispatch) { handler(dispatch) })
+}
+
+pub fn on_mouse_leave (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("mouseLeave", fn (_, dispatch) { handler(dispatch) })
+}
+
+pub fn on_mouse_over (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("mouseOver", fn (_, dispatch) { handler(dispatch) })
+}
-pub fn on_click (handler: action) -> Attribute(action) {
- on("onClick", fn (_) { handler })
+pub fn on_mouse_out (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("mouseOut", fn (_, dispatch) { handler(dispatch) })
}
-pub fn on_input (handler: fn (String) -> action) -> Attribute(action) {
- let decoder = dynamic.field("target", dynamic.field("value", dynamic.string))
+// FORM EVENTS -----------------------------------------------------------------
- on("onInput", fn (e) {
- // If this fails then there's probably some sort of hideous browser bug
- // that is way beyond the concern of our Lustre apps!
- assert Ok(value) = decoder(e)
+pub fn on_input (handler: fn (String, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("input", fn (e, dispatch) {
+ assert Ok(value) = e |> dynamic.field("target", dynamic.field("value", dynamic.string))
- handler(value)
+ handler(value, dispatch)
})
-} \ No newline at end of file
+}