From 5baa76603bb27f050eac00e7d63b38deb981445a Mon Sep 17 00:00:00 2001 From: benwis Date: Sun, 14 Apr 2024 14:33:11 -0700 Subject: [PATCH] Add id to Body --- meta/src/body.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/meta/src/body.rs b/meta/src/body.rs index e0a2aaf49..f67cfa314 100644 --- a/meta/src/body.rs +++ b/meta/src/body.rs @@ -11,6 +11,8 @@ pub struct BodyContext { #[cfg(feature = "ssr")] class: Rc>>, #[cfg(feature = "ssr")] + id: Rc>>, + #[cfg(feature = "ssr")] attributes: Rc>>, } @@ -24,6 +26,13 @@ impl BodyContext { leptos::leptos_dom::ssr::escape_attr(&val.get()) ) }); + + let id = self.id.borrow().as_ref().map(|val| { + format!( + "id=\"{}\"", + leptos::leptos_dom::ssr::escape_attr(&val.get()) + ) + }); let attributes = self.attributes.borrow(); let attributes = (!attributes.is_empty()).then(|| { attributes @@ -41,7 +50,7 @@ impl BodyContext { .join(" ") }); - let mut val = [class, attributes] + let mut val = [id, class, attributes] .into_iter() .flatten() .collect::>() @@ -92,6 +101,9 @@ pub fn Body( /// The `class` attribute on the ``. #[prop(optional, into)] class: Option, + /// The `id` attribute on the ``. + #[prop(optional, into)] + id: Option, /// Arbitrary attributes to add to the `` #[prop(attrs)] attributes: Vec<(&'static str, Attribute)>, @@ -112,15 +124,27 @@ pub fn Body( }); } + + if let Some(id) = id { + create_render_effect({ + let el = el.clone(); + move |_| { + let value = id.get(); + _ = el.set_attribute("id", &value); + } + }); + } for (name, value) in attributes { leptos::leptos_dom::attribute_helper(el.unchecked_ref(), name.into(), value); } } else if #[cfg(feature = "ssr")] { let meta = crate::use_head(); *meta.body.class.borrow_mut() = class; + *meta.body.id.borrow_mut() = id; meta.body.attributes.borrow_mut().extend(attributes); } else { _ = class; + _ = id; _ = attributes; #[cfg(debug_assertions)]