Skip to main content

topcoat_runtime/
event_handler.rs

1use topcoat_core::context::Cx;
2use topcoat_view::{Attribute, AttributeKeyViewParts, AttributeViewParts, PartsWriter, Unescaped};
3
4use crate::{Event, Expr};
5
6pub trait EventHandlerFn {}
7
8impl<T, R> EventHandlerFn for T where T: FnOnce(Event) -> R {}
9
10/// An attribute that attaches a browser event handler to an element.
11pub struct EventHandler<K, F> {
12    key: K,
13    value: Expr<F>,
14}
15
16impl<K, F> EventHandler<K, F>
17where
18    F: EventHandlerFn,
19{
20    #[inline]
21    pub fn new(key: K, value: Expr<F>) -> Self {
22        Self { key, value }
23    }
24}
25
26impl<K, F> AttributeViewParts for EventHandler<K, F>
27where
28    K: AttributeKeyViewParts,
29{
30    #[inline]
31    fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>) {
32        Attribute::new(
33            (Unescaped::new_unchecked("data-topcoat-on:"), self.key),
34            self.value.js,
35        )
36        .into_view_parts(cx, parts);
37    }
38}