thaw/thaw_utils/src/mount_style.rs
luoxiaozero 9bf0f7da1a
Feat/thaw utils (#145)
* feat: extract utils as the library

* refactor: thaw_utils directory

* style: fmt
2024-03-19 22:02:43 +08:00

33 lines
1.2 KiB
Rust

use cfg_if::cfg_if;
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");
#[cfg(feature = "hydrate")]
let _ = leptos::leptos_dom::HydrationCtx::id();
if style.is_some() {
return;
}
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));
_ = head.prepend_with_node_1(&style);
}
}
}