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
use super::Props;
use crate::{
callback::PersistedCallback, create_element, hooks::JsRefContainer, VNode,
VNodeList,
};
use wasm_bindgen::{
convert::{FromWasmAbi, IntoWasmAbi},
JsValue,
};
use web_sys::Element;
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct HtmlTag<'a>(pub &'a str);
impl<'a> AsRef<str> for HtmlTag<'a> {
fn as_ref(&self) -> &str {
&self.0
}
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct ImportedComponent<'a>(pub &'a JsValue);
impl<'a> AsRef<JsValue> for ImportedComponent<'a> {
fn as_ref(&self) -> &JsValue {
&self.0
}
}
mod private {
use super::{HtmlTag, ImportedComponent};
pub trait Sealed {}
impl<'a> Sealed for ImportedComponent<'a> {}
impl<'a> Sealed for HtmlTag<'a> {}
}
pub trait HType: private::Sealed {
#[doc(hidden)]
fn with_js<T>(&self, f: impl FnOnce(&JsValue) -> T) -> T;
}
impl<'a> HType for ImportedComponent<'a> {
fn with_js<T>(&self, f: impl FnOnce(&JsValue) -> T) -> T {
f(&self.as_ref())
}
}
impl<'a> HType for HtmlTag<'a> {
fn with_js<T>(&self, f: impl FnOnce(&JsValue) -> T) -> T {
f(&self.as_ref().into())
}
}
#[derive(Debug, Clone)]
pub struct H<T: HType> {
pub(crate) typ: T,
pub(crate) props: Props,
}
impl<T: HType> H<T> {
pub fn new(typ: T) -> Self {
Self {
typ,
props: Props::new(),
}
}
pub fn key(mut self, value: Option<&str>) -> Self {
self.props = self.props.key(value);
self
}
pub fn ref_container(
mut self,
ref_container: &JsRefContainer<Element>,
) -> Self {
self.props = self.props.ref_container(ref_container);
self
}
pub fn ref_callback(
mut self,
ref_callback: &PersistedCallback<Option<Element>>,
) -> Self {
self.props = self.props.ref_callback(ref_callback);
self
}
pub fn attr(mut self, key: &str, value: &JsValue) -> Self {
self.props = self.props.insert(key, value);
self
}
pub fn attr_callback<U, V>(
mut self,
key: &str,
f: &PersistedCallback<U, V>,
) -> Self
where
U: FromWasmAbi + 'static,
V: IntoWasmAbi + 'static,
{
self.props = self.props.insert_callback(key, f);
self
}
pub fn build(self, children: VNodeList) -> VNode {
self
.typ
.with_js(|typ| create_element(typ, &self.props, children))
}
}