yew_bs/
assets.rs

1use wasm_bindgen::prelude::*;
2use web_sys::{HtmlStyleElement, HtmlScriptElement};
3pub const BOOTSTRAP_CSS: &str = include_str!("assets/bootstrap.min.css");
4pub const BOOTSTRAP_JS: &str = include_str!("assets/bootstrap.bundle.min.js");
5pub fn inject_bootstrap_css() {
6    let window = web_sys::window().expect("no global `window` exists");
7    let document = window.document().expect("should have a document on window");
8    let style = document
9        .create_element("style")
10        .expect("Failed to create style element")
11        .dyn_into::<HtmlStyleElement>()
12        .expect("Failed to cast to HtmlStyleElement");
13    style.set_text_content(Some(BOOTSTRAP_CSS));
14    document
15        .head()
16        .expect("document should have a head")
17        .append_child(&style)
18        .expect("Failed to append style to head");
19}
20pub fn inject_bootstrap_js() {
21    let window = web_sys::window().expect("no global `window` exists");
22    let document = window.document().expect("should have a document on window");
23    let script = document
24        .create_element("script")
25        .expect("Failed to create script element")
26        .dyn_into::<HtmlScriptElement>()
27        .expect("Failed to cast to HtmlScriptElement");
28    let _ = script.set_text(BOOTSTRAP_JS);
29    document
30        .head()
31        .expect("document should have a head")
32        .append_child(&script)
33        .expect("Failed to append script to head");
34}
35pub fn init_bootstrap() {
36    inject_bootstrap_css();
37    inject_bootstrap_js();
38}