2023-11-24 10:04:54 +08:00
|
|
|
use cfg_if::cfg_if;
|
2023-03-30 17:12:58 +08:00
|
|
|
|
2023-11-24 10:04:54 +08:00
|
|
|
pub fn mount_style(id: &str, content: &'static str) {
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(feature = "ssr")] {
|
|
|
|
use leptos::html::style;
|
|
|
|
use leptos_meta::use_head;
|
|
|
|
let meta = use_head();
|
|
|
|
let style_el = style().attr("csr-id", format!("thaw-{id}")).child(content);
|
|
|
|
meta.tags.register(format!("leptos-thaw-{id}").into(), style_el.into_any());
|
|
|
|
} else {
|
|
|
|
use leptos::document;
|
|
|
|
let head = document().head().expect("head no exist");
|
|
|
|
let style = head
|
|
|
|
.query_selector(&format!("style[csr-id=\"thaw-{id}\"]"))
|
|
|
|
.expect("query style element error");
|
2023-03-30 17:12:58 +08:00
|
|
|
|
2023-11-24 10:04:54 +08:00
|
|
|
#[cfg(feature = "hydrate")]
|
|
|
|
let _ = leptos::leptos_dom::HydrationCtx::id();
|
|
|
|
|
|
|
|
if style.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-30 17:12:58 +08:00
|
|
|
|
2023-11-24 10:04:54 +08:00
|
|
|
let style = document()
|
|
|
|
.create_element("style")
|
|
|
|
.expect("create style element error");
|
|
|
|
_ = style.set_attribute("csr-id", &format!("thaw-{id}"));
|
|
|
|
style.set_text_content(Some(content));
|
2023-12-11 13:21:47 +01:00
|
|
|
_ = head.prepend_with_node_1(&style);
|
2023-11-24 10:04:54 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 17:12:58 +08:00
|
|
|
}
|