1use super::Props;
2use crate::{
3 Callback, create_element, hooks::JsRefContainer, KeyType, VNode,
4};
5use std::borrow::Cow;
6use wasm_bindgen::{
7 convert::{FromWasmAbi, IntoWasmAbi},
8 intern, JsValue,
9};
10use web_sys::Element;
11
12#[doc(hidden)]
13#[derive(Debug, Clone, Copy)]
14pub struct HtmlTag<'a>(pub &'a str);
15
16impl AsRef<str> for HtmlTag<'_> {
17 fn as_ref(&self) -> &str {
18 &self.0
19 }
20}
21
22pub trait HType {
26 fn as_js(&self) -> Cow<'_, JsValue>;
28}
29
30impl HType for HtmlTag<'_> {
31 fn as_js(&self) -> Cow<'_, JsValue> {
32 Cow::Owned(intern(self.0).into())
33 }
34}
35
36#[derive(Debug, Clone)]
42pub struct H<T> {
43 pub(crate) typ: T,
44 pub(crate) props: Props,
45}
46
47impl<T: HType> H<T> {
48 pub fn new(typ: T) -> Self {
51 Self {
52 typ,
53 props: Props::new(),
54 }
55 }
56
57 pub fn key(mut self, value: Option<impl KeyType>) -> Self {
61 self.props = self.props.key(value);
62 self
63 }
64
65 pub fn ref_container(
70 mut self,
71 ref_container: &JsRefContainer<Element>,
72 ) -> Self {
73 self.props = self.props.ref_container(ref_container);
74 self
75 }
76
77 pub fn ref_callback(
81 mut self,
82 ref_callback: &Callback<Option<Element>>,
83 ) -> Self {
84 self.props = self.props.ref_callback(ref_callback);
85 self
86 }
87
88 pub fn attr(mut self, key: &str, value: &JsValue) -> Self {
90 self.props = self.props.insert(key, value);
91 self
92 }
93
94 pub fn attr_callback<U, V>(mut self, key: &str, f: &Callback<U, V>) -> Self
96 where
97 U: FromWasmAbi + 'static,
98 V: IntoWasmAbi + 'static,
99 {
100 self.props = self.props.insert_callback(key, f);
101 self
102 }
103
104 pub fn build(self, children: impl Into<VNode>) -> VNode {
106 create_element(&self.typ.as_js(), &self.props, children.into())
107 }
108}