This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew/html/listener/
events.rs

1// Inspired by: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html-Events
2
3macro_rules! impl_action {
4    ($($action:ident($type:ident) -> $ret:path => $convert:path)*) => {$(
5        impl_action!($action($type, false) -> $ret => $convert);
6    )*};
7    ($($action:ident($type:ident, $passive:literal) -> $ret:path => $convert:path)*) => {$(
8        /// An abstract implementation of a listener.
9        #[doc(hidden)]
10        pub mod $action {
11            use crate::callback::Callback;
12            use crate::virtual_dom::{Listener, ListenerKind};
13            use std::rc::Rc;
14
15            /// A wrapper for a callback which attaches event listeners to elements.
16            #[derive(Clone, Debug)]
17            pub struct Wrapper {
18                callback: Callback<Event>,
19            }
20
21            impl Wrapper {
22                /// Create a wrapper for an event-typed callback
23                pub fn new(callback: Callback<Event>) -> Self {
24                    Wrapper { callback }
25                }
26
27                #[doc(hidden)]
28                #[inline]
29                pub fn __macro_new(
30                    callback: impl crate::html::IntoEventCallback<Event>,
31                ) -> Option<Rc<dyn Listener>> {
32                    let callback = callback.into_event_callback()?;
33                    Some(Rc::new(Self::new(callback)))
34                }
35            }
36
37            /// And event type which keeps the returned type.
38            pub type Event = $ret;
39
40            impl Listener for Wrapper {
41                fn kind(&self) -> ListenerKind {
42                    ListenerKind::$action
43                }
44
45                fn handle(&self, event: web_sys::Event) {
46                    self.callback.emit($convert(event));
47                }
48
49                fn passive(&self) -> bool {
50                    $passive
51                }
52            }
53        }
54    )*};
55}
56
57// Reduces repetition for common cases
58macro_rules! impl_short {
59    ($($action:ident)*) => {
60        impl_action! {
61            $(
62                $action(Event) -> web_sys::Event => std::convert::identity
63            )*
64        }
65    };
66    ($($action:ident($type:ident))*) => {
67        impl_action! {
68            $(
69                $action($type) -> web_sys::$type  => crate::html::listener::cast_event
70            )*
71        }
72    };
73}
74
75// Unspecialized event type
76impl_short! {
77    onabort
78    oncancel
79    oncanplay
80    oncanplaythrough
81    onclose
82    oncuechange
83    ondurationchange
84    onemptied
85    onended
86    onerror
87    onformdata  // web_sys doesn't have a struct for `FormDataEvent`
88    oninvalid
89
90    onload
91    onloadeddata
92    onloadedmetadata
93
94    onpause
95    onplay
96    onplaying
97
98    onratechange
99    onreset
100    onresize
101    onsecuritypolicyviolation
102
103    onseeked
104    onseeking
105
106    onselect
107    onslotchange
108    onstalled
109    onsuspend
110    ontimeupdate
111    ontoggle
112    onvolumechange
113    onwaiting
114
115    onchange
116
117    oncopy
118    oncut
119    onpaste
120
121    onpointerlockchange
122    onpointerlockerror
123    onselectionchange
124    onselectstart
125    onshow
126}
127
128// Specialized event type
129impl_short! {
130    onauxclick(MouseEvent)
131    onclick(MouseEvent)
132
133    oncontextmenu(MouseEvent)
134    ondblclick(MouseEvent)
135
136    ondrag(DragEvent)
137    ondragend(DragEvent)
138    ondragenter(DragEvent)
139    ondragexit(DragEvent)
140    ondragleave(DragEvent)
141    ondragover(DragEvent)
142    ondragstart(DragEvent)
143    ondrop(DragEvent)
144
145    onblur(FocusEvent)
146    onfocus(FocusEvent)
147    onfocusin(FocusEvent)
148    onfocusout(FocusEvent)
149
150    onkeydown(KeyboardEvent)
151    onkeypress(KeyboardEvent)
152    onkeyup(KeyboardEvent)
153
154    onloadstart(ProgressEvent)
155    onprogress(ProgressEvent)
156    onloadend(ProgressEvent)
157
158    onmousedown(MouseEvent)
159    onmouseenter(MouseEvent)
160    onmouseleave(MouseEvent)
161    onmousemove(MouseEvent)
162    onmouseout(MouseEvent)
163    onmouseover(MouseEvent)
164    onmouseup(MouseEvent)
165    onwheel(WheelEvent)
166
167    oninput(InputEvent)
168
169    onsubmit(SubmitEvent)
170
171    onanimationcancel(AnimationEvent)
172    onanimationend(AnimationEvent)
173    onanimationiteration(AnimationEvent)
174    onanimationstart(AnimationEvent)
175
176    ongotpointercapture(PointerEvent)
177    onlostpointercapture(PointerEvent)
178    onpointercancel(PointerEvent)
179    onpointerdown(PointerEvent)
180    onpointerenter(PointerEvent)
181    onpointerleave(PointerEvent)
182    onpointermove(PointerEvent)
183    onpointerout(PointerEvent)
184    onpointerover(PointerEvent)
185    onpointerup(PointerEvent)
186
187    ontouchcancel(TouchEvent)
188    ontouchend(TouchEvent)
189
190    ontransitioncancel(TransitionEvent)
191    ontransitionend(TransitionEvent)
192    ontransitionrun(TransitionEvent)
193    ontransitionstart(TransitionEvent)
194}
195
196macro_rules! impl_passive {
197    ($($action:ident($type:ident))*) => {
198        impl_action! {
199            $(
200                $action($type, true) -> web_sys::$type
201                    => crate::html::listener::cast_event
202            )*
203        }
204    };
205}
206
207// Best used with passive listeners for responsiveness
208impl_passive! {
209    onscroll(Event)
210
211    ontouchmove(TouchEvent)
212    ontouchstart(TouchEvent)
213}