From f03d1641d92d46aa0ec60b09fd784f0df80d2408 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 22 Nov 2023 17:34:16 -0500 Subject: [PATCH] Add an attach shadow method --- macros/Cargo.toml | 2 +- macros/src/lib.rs | 13 +++++++++++++ wasm-web-component/Cargo.toml | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/macros/Cargo.toml b/macros/Cargo.toml index bac2bf3..2537990 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasm-web-component-macros" -version = "0.2.0" +version = "0.3.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/macros/src/lib.rs b/macros/src/lib.rs index aa72959..5b0c538 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -224,6 +224,19 @@ fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl { Self::default() } + #[::wasm_bindgen::prelude::wasm_bindgen] + #[doc = "Attach an open shadowroot to our element."] + pub fn attach_shadow(&self, element: &web_sys::HtmlElement, root: &str) { + self.attach_shadow_with_mode(element, root, web_sys::ShadowRootMode::Open); + } + + #[::wasm_bindgen::prelude::wasm_bindgen] + #[doc = "Attach a shadowroot with the given mode to our element."] + pub fn attach_shadow_with_mode(&self, element: &web_sys::HtmlElement, root: &str, mode: web_sys::ShadowRootMode) { + let shadow_root = element.attach_shadow(&web_sys::ShadowRootInit::new(mode)).unwrap(); + shadow_root.set_inner_html(root); + } + #[::wasm_bindgen::prelude::wasm_bindgen] pub fn init_impl(&mut self, element: &web_sys::HtmlElement) { use #trait_path; diff --git a/wasm-web-component/Cargo.toml b/wasm-web-component/Cargo.toml index 84a8873..05e42b8 100644 --- a/wasm-web-component/Cargo.toml +++ b/wasm-web-component/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasm-web-component" -version = "0.2.0" +version = "0.3.0" edition = "2021" [lib]