mod classes;
mod component;
mod conversion;
mod error;
mod listener;
use std::cell::RefCell;
use std::rc::Rc;
pub use classes::*;
pub use component::*;
pub use conversion::*;
pub use error::*;
pub use listener::*;
use wasm_bindgen::JsValue;
use web_sys::{Element, Node};
use crate::sealed::Sealed;
use crate::virtual_dom::{VNode, VPortal};
pub type Html = VNode;
pub type HtmlResult = RenderResult<Html>;
impl Sealed for HtmlResult {}
impl Sealed for Html {}
pub trait IntoHtmlResult: Sealed {
fn into_html_result(self) -> HtmlResult;
}
impl IntoHtmlResult for HtmlResult {
#[inline(always)]
fn into_html_result(self) -> HtmlResult {
self
}
}
impl IntoHtmlResult for Html {
#[inline(always)]
fn into_html_result(self) -> HtmlResult {
Ok(self)
}
}
#[derive(Default, Clone, ImplicitClone)]
pub struct NodeRef(Rc<RefCell<NodeRefInner>>);
impl PartialEq for NodeRef {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.0.as_ptr(), other.0.as_ptr())
}
}
impl std::fmt::Debug for NodeRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"NodeRef {{ references: {:?} }}",
self.get().map(|n| crate::utils::print_node(&n))
)
}
}
#[derive(PartialEq, Debug, Default, Clone)]
struct NodeRefInner {
node: Option<Node>,
}
impl NodeRef {
pub fn get(&self) -> Option<Node> {
let inner = self.0.borrow();
inner.node.clone()
}
pub fn cast<INTO: AsRef<Node> + From<JsValue>>(&self) -> Option<INTO> {
let node = self.get();
node.map(Into::into).map(INTO::from)
}
}
#[cfg(feature = "csr")]
mod feat_csr {
use super::*;
impl NodeRef {
pub(crate) fn set(&self, new_ref: Option<Node>) {
let mut inner = self.0.borrow_mut();
inner.node = new_ref;
}
}
}
pub fn create_portal(child: Html, host: Element) -> Html {
VNode::VPortal(Rc::new(VPortal::new(child, host)))
}