-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
event.gleam
180 lines (139 loc) · 4.07 KB
/
event.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//// To read the full documentation for this module, please visit
//// [https://lustre.build/api/lustre/event](https://lustre.build/api/lustre/event)
// IMPORTS ---------------------------------------------------------------------
import gleam/dynamic.{type DecodeError, type Dynamic}
import gleam/result
import lustre/attribute.{type Attribute}
import lustre/effect.{type Effect}
// TYPES -----------------------------------------------------------------------
type Decoded(a) =
Result(a, List(DecodeError))
// EFFECTS ---------------------------------------------------------------------
///
pub fn emit(event: String, data: any) -> Effect(msg) {
effect.event(event, data)
}
// CUSTOM EVENTS ---------------------------------------------------------------
///
pub fn on(
name: String,
handler: fn(Dynamic) -> Result(msg, error),
) -> Attribute(msg) {
attribute.on(name, handler)
}
// MOUSE EVENTS ----------------------------------------------------------------
///
pub fn on_click(msg: msg) -> Attribute(msg) {
use _ <- on("click")
Ok(msg)
}
///
pub fn on_mouse_down(msg: msg) -> Attribute(msg) {
use _ <- on("mousedown")
Ok(msg)
}
///
pub fn on_mouse_up(msg: msg) -> Attribute(msg) {
use _ <- on("mouseup")
Ok(msg)
}
///
pub fn on_mouse_enter(msg: msg) -> Attribute(msg) {
use _ <- on("mouseenter")
Ok(msg)
}
///
pub fn on_mouse_leave(msg: msg) -> Attribute(msg) {
use _ <- on("mouseleave")
Ok(msg)
}
///
pub fn on_mouse_over(msg: msg) -> Attribute(msg) {
use _ <- on("mouseover")
Ok(msg)
}
///
pub fn on_mouse_out(msg: msg) -> Attribute(msg) {
use _ <- on("mouseout")
Ok(msg)
}
// KEYBOARD EVENTS -------------------------------------------------------------
/// Listens for key presses on an element, and dispatches a message with the
/// current key being pressed.
///
pub fn on_keypress(msg: fn(String) -> msg) -> Attribute(msg) {
use event <- on("keypress")
event
|> dynamic.field("key", dynamic.string)
|> result.map(msg)
}
/// Listens for key dow events on an element, and dispatches a message with the
/// current key being pressed.
///
pub fn on_keydown(msg: fn(String) -> msg) -> Attribute(msg) {
use event <- on("keydown")
event
|> dynamic.field("key", dynamic.string)
|> result.map(msg)
}
/// Listens for key up events on an element, and dispatches a message with the
/// current key being released.
///
pub fn on_keyup(msg: fn(String) -> msg) -> Attribute(msg) {
use event <- on("keyup")
event
|> dynamic.field("key", dynamic.string)
|> result.map(msg)
}
// FORM EVENTS -----------------------------------------------------------------
///
pub fn on_input(msg: fn(String) -> msg) -> Attribute(msg) {
use event <- on("input")
value(event)
|> result.map(msg)
}
pub fn on_check(msg: fn(Bool) -> msg) -> Attribute(msg) {
use event <- on("change")
checked(event)
|> result.map(msg)
}
pub fn on_submit(msg: msg) -> Attribute(msg) {
use event <- on("submit")
let _ = prevent_default(event)
Ok(msg)
}
// FOCUS EVENTS ----------------------------------------------------------------
pub fn on_focus(msg: msg) -> Attribute(msg) {
use _ <- on("focus")
Ok(msg)
}
pub fn on_blur(msg: msg) -> Attribute(msg) {
use _ <- on("blur")
Ok(msg)
}
// DECODERS --------------------------------------------------------------------
///
pub fn value(event: Dynamic) -> Decoded(String) {
event
|> dynamic.field("target", dynamic.field("value", dynamic.string))
}
///
pub fn checked(event: Dynamic) -> Decoded(Bool) {
event
|> dynamic.field("target", dynamic.field("checked", dynamic.bool))
}
///
pub fn mouse_position(event: Dynamic) -> Decoded(#(Float, Float)) {
use x <- result.then(dynamic.field("clientX", dynamic.float)(event))
use y <- result.then(dynamic.field("clientY", dynamic.float)(event))
Ok(#(x, y))
}
// UTILS -----------------------------------------------------------------------
@external(javascript, "../lustre.ffi.mjs", "prevent_default")
pub fn prevent_default(_event: Dynamic) -> Nil {
Nil
}
@external(javascript, "../lustre.ffi.mjs", "stop_propagation")
pub fn stop_propagation(_event: Dynamic) -> Nil {
Nil
}