Skip to content

Can I use externally created HtmlElement in leptos? #2009

Answered by gbj
liukaizheng asked this question in Q&A
Discussion options

You must be logged in to vote

Sure. Probably the easiest way to do this is on a NodeRef containing whatever the parent element should be:

use leptos::*;

#[component]
pub fn App() -> impl IntoView {
    // here, I just create a canvas
    // but this can be the canvas you get from winit
    let some_canvas = document().create_element("canvas").unwrap();

    let parent = NodeRef::<html::Div>::new();
    create_effect(move |_| {
        if let Some(parent) = parent.get() {
            parent.append_child(&some_canvas);
        }
    });

    view! {
        <div node_ref=parent/>
    }
}

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@liukaizheng
Comment options

@wrm65
Comment options

@gbj
Comment options

gbj Dec 28, 2023
Maintainer

@wrm65
Comment options

Answer selected by liukaizheng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants