mirror of
https://github.com/adoyle0/thaw.git
synced 2025-02-03 00:54:15 -05:00
21506b2164
* feat: add ssr_axum template * feat: demo added the ssr mode * fix: demo ssr mode * feat(ssr): mount_style * pref: delete some useless fikes * fix(ssr): problems caused by using wasm_bindgen * feat: add hydrate * pref: Demo component * fix(hydrate): teleport component * fix(hydrate): hydrate feature * fix(hydrate): tabs component * feat: GlobalStyle component margin style * docs(ssr): static assets
34 lines
1.1 KiB
Rust
34 lines
1.1 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.append_child(&style);
|
|
}
|
|
}
|
|
}
|