euv-core 0.5.6

A declarative, cross-platform UI framework for Rust with virtual DOM, reactive signals, and HTML macros for WebAssembly.
Documentation
use crate::*;

/// Mounts the given virtual DOM tree to a specific element matched by a CSS selector.
///
/// Supported selector syntax:
/// - `"#id"` — select by element ID
/// - `".class"` — select by class name (uses the first match)
/// - `"tag"` — select by tag name (uses the first match)
///
/// # Arguments
///
/// - `&str` - A CSS selector string to locate the target element.
/// - `FnOnce() -> VirtualNode + 'static` - A closure that returns the virtual DOM tree to render.
///
pub fn mount<F>(selector: &str, render_fn: F)
where
    F: FnOnce() -> VirtualNode,
{
    init_event_delegation();
    let window: Window = match window() {
        Some(w) => w,
        None => return,
    };
    let document: Document = match window.document() {
        Some(d) => d,
        None => return,
    };
    let target: Element = if selector == BODY_TAG {
        match document.body() {
            Some(body) => body.into(),
            None => return,
        }
    } else if let Some(id) = selector.strip_prefix(ID_SELECTOR_PREFIX) {
        match document.get_element_by_id(id) {
            Some(el) => el,
            None => return,
        }
    } else if let Some(class) = selector.strip_prefix(CLASS_SELECTOR_PREFIX) {
        match document.get_elements_by_class_name(class).item(0) {
            Some(el) => el,
            None => return,
        }
    } else {
        match document.get_elements_by_tag_name(selector).item(0) {
            Some(el) => el,
            None => return,
        }
    };
    let mut renderer: Renderer = Renderer::new(target);
    let vnode: VirtualNode = render_fn();
    renderer.render(vnode);
}