thaw/src/utils/mount_style.rs
kandrelczyk 6ab334cc94
Add class param to first components (#39)
* Add class param to first components

* fix: auto complete input failed

---------

Co-authored-by: Cristobal Andrada <kandrelczyk@gmail.com>
Co-authored-by: luoxiao <luoxiaozero@163.com>
2023-12-11 20:21:47 +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);
}
}
}