Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I create a Web Component #3119

Closed
9876691 opened this issue Oct 22, 2022 · 3 comments
Closed

How do I create a Web Component #3119

9876691 opened this issue Oct 22, 2022 · 3 comments
Labels

Comments

@9876691
Copy link

9876691 commented Oct 22, 2022

I can't find any examples (that work) of creating a web component in rust.

So I want to do something like

use wasm_bindgen::prelude::*;

#[wasm_bindgen(extends=web_sys::HtmlElement)]    
pub struct HelloWorld;

#[wasm_bindgen]
impl HelloWorld {
    #[wasm_bindgen(constructor)]
    pub fn new() -> HelloWorld {

        HelloWorld {}
    }
}

#[wasm_bindgen(start)]        
pub fn main() {

    let window = web_sys::window().unwrap();
    let document = window.document().unwrap();

    window.custom_elements().define(
        "hello-world",
        &HelloWorld::new(),

    ).unwrap();
}

I'm not sure how to get the &HelloWorld::new() into a js_sys::Function so that it can be passed into custom_elements().define

and then how would I handle the call backs?

if anyone has a minimal working example that would be great.

@jonboj
Copy link

jonboj commented Oct 29, 2022

Also looked into this once. There are a bunch of open issues related to construction/inheritance of JS objects #2853, #1721 and especially #621, #210 involving designers of wasm-bindgen. So looks like that defining Custom Elements from Rust wasn't supported by the framework.

@9876691
Copy link
Author

9876691 commented Oct 29, 2022

@jonboj Thanks. In the end I found a library that works. https://github.com/gbj/custom-elements

I'll close this out.

@9876691 9876691 closed this as completed Oct 29, 2022
@martinitus
Copy link

Funny enough I started today with the goal to port this example to the current version of web-sys and wasm-bindgen. I got stuck at the same point. However, if above example ever worked, then it at least used to be possible without any javascript glue like done in (https://github.com/gbj/custom-elements). I assume some of the features where dropped from wasm-bindgen because they were to experimental/hacky?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants