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

Need a good way to load javascript or css dynamically #426

Open
piaoger opened this issue May 27, 2022 · 2 comments
Open

Need a good way to load javascript or css dynamically #426

piaoger opened this issue May 27, 2022 · 2 comments
Labels
C-enhancement Category: new feature or improvement to existing feature

Comments

@piaoger
Copy link

piaoger commented May 27, 2022

I am trying to append a script node into HEAD node for a child router. I tried to 2 methods but each one has its own shortcomings.

Blow are my tests:

  1. It can works as desired but seems not very simple by using raw web_sys apis and DomNode

    I skipped the helpers to get head element and use head() in my code snippt.

AppRoutes::Edit => {

    let script = DomNode::element_from_tag("script");
    script.set_attribute(
        "src",
        "https://abc.com/d.js",
    );
    script.set_attribute("type", "text/javascript");
    let head = DomNode::from_web_sys(head().into());
    head.append_child(&script);
    script.event(cx, "load", |_| web_sys::console::log_1(&"javascript loaded...".into()));

    view! {cx,
        components::editor::edit()
    }
}
  1. A script element is just appended to parent element(DIV in my case) though it works.
AppRoutes::Edit=> {
    view! {cx,
        script(src="https://abc.com/d.js")
        components::editor::edit()
    }
}
@piaoger piaoger added the C-enhancement Category: new feature or improvement to existing feature label May 27, 2022
@lukechu10
Copy link
Member

Yeah it would be nice to have something like use_script and a use_stylesheet hook although I think it should be in something like https://github.com/lukechu10/sycamore-hooks instead of the main sycamore repo.

@piaoger
Copy link
Author

piaoger commented May 30, 2022

Yes. sycamore-hook can be good place for it. I tried use_websocket before though did not including it in my in house tool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: new feature or improvement to existing feature
Projects
None yet
Development

No branches or pull requests

2 participants