aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2022-05-14 11:19:32 +0100
committerHayleigh Thompson <me@hayleigh.dev>2022-05-14 11:19:32 +0100
commitfc1b5c00e21983b3ba13c5a374baf34e4fba834f (patch)
treee997496be413d795465f19c2577dfc3a82e33a85
parentc92e6fa297ec0b1b14c5bfc986aa71b2c59cc9c2 (diff)
downloadlustre-fc1b5c00e21983b3ba13c5a374baf34e4fba834f.tar.gz
lustre-fc1b5c00e21983b3ba13c5a374baf34e4fba834f.zip
:sparkles: Add many common events.
-rw-r--r--src/lustre/event.gleam79
1 files changed, 69 insertions, 10 deletions
diff --git a/src/lustre/event.gleam b/src/lustre/event.gleam
index f1c1fb3..341f68b 100644
--- a/src/lustre/event.gleam
+++ b/src/lustre/event.gleam
@@ -1,53 +1,90 @@
+////
+
+// IMPORTS ---------------------------------------------------------------------
+
import gleam/dynamic.{ Dynamic }
import lustre/attribute.{ Attribute }
-pub external fn ignore () -> action
- = "../ffi.mjs" "ignore"
-
+// CONSTRUCTORS ----------------------------------------------------------------
+///
pub fn on (name: String, handler: fn (Dynamic, fn (action) -> Nil) -> Nil) -> Attribute(action) {
attribute.event(name, handler)
}
+///
pub fn dispatch (action: action) -> fn (fn (action) -> Nil) -> Nil {
fn (dispatch) {
dispatch(action)
}
}
-
// 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) })
+ 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) })
+ 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) })
+ 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) })
+ 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) })
+ on("mouseover", fn (_, dispatch) { handler(dispatch) })
}
+///
pub fn on_mouse_out (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
- on("mouseOut", fn (_, dispatch) { handler(dispatch) })
+ on("mouseout", fn (_, dispatch) { handler(dispatch) })
+}
+
+// KEYBOARD EVENTS -------------------------------------------------------------
+
+pub fn on_keypress (handler: fn (String, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("keypress", fn (e, dispatch) {
+ assert Ok(key) = e |> dynamic.field("key", dynamic.string)
+
+ handler(key, dispatch)
+ })
+}
+
+pub fn on_keydown (handler: fn (String, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("keydown", fn (e, dispatch) {
+ assert Ok(key) = e |> dynamic.field("key", dynamic.string)
+
+ handler(key, dispatch)
+ })
+}
+
+pub fn on_keyup (handler: fn (String, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("keyup", fn (e, dispatch) {
+ assert Ok(key) = e |> dynamic.field("key", dynamic.string)
+
+ handler(key, dispatch)
+ })
}
// FORM EVENTS -----------------------------------------------------------------
+///
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))
@@ -55,3 +92,25 @@ pub fn on_input (handler: fn (String, fn (action) -> Nil) -> Nil) -> Attribute(a
handler(value, dispatch)
})
}
+
+pub fn on_check (handler: fn (Bool, fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("check", fn (e, dispatch) {
+ assert Ok(value) = e |> dynamic.field("target", dynamic.field("checked", dynamic.bool))
+
+ handler(value, dispatch)
+ })
+}
+
+pub fn on_submit (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("submit", fn (_, dispatch) { handler(dispatch) })
+}
+
+// FOCUS EVENTS ----------------------------------------------------------------
+
+pub fn on_focus (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("focus", fn (_, dispatch) { handler(dispatch) })
+}
+
+pub fn on_blur (handler: fn (fn (action) -> Nil) -> Nil) -> Attribute(action) {
+ on("blur", fn (_, dispatch) { handler(dispatch) })
+}