thaw/demo/src/components/demo.rs

93 lines
3.2 KiB
Rust
Raw Normal View History

2023-09-17 22:19:54 +08:00
use leptos::*;
2023-11-13 16:17:45 +08:00
use leptos_meta::Style;
2023-11-05 16:03:58 +08:00
use thaw::*;
2023-09-17 22:19:54 +08:00
#[slot]
pub struct DemoCode {
#[prop(default = true)]
is_highlight: bool,
2023-09-17 22:19:54 +08:00
children: Children,
}
#[component]
pub fn Demo(demo_code: DemoCode, children: Children) -> impl IntoView {
2023-10-26 16:53:51 +08:00
let theme = use_theme(Theme::light);
let style = create_memo(move |_| {
2023-11-05 16:03:58 +08:00
let mut style = String::from("background-image: url(/thaw/grid_dot.svg); background-repeat: repeat; background-size: 1.5rem; margin-top: 1rem; padding: 1rem; border-top-left-radius: 0.5rem; border-top-right-radius: 0.5rem;");
2023-10-26 16:53:51 +08:00
theme.with(|theme| {
if theme.common.color_scheme == "dark" {
style.push_str("border: 1px solid #383f52;");
} else {
style.push_str(&format!("border: 1px solid {};", theme.common.border_color));
}
});
style
});
let code_style = create_memo(move |_| {
let mut style = String::from("font-weight: 400; font-size: 0.875em; line-height: 1.7142857;margin-bottom: 1rem; padding: 1rem; border-bottom-left-radius: 0.5rem; border-bottom-right-radius: 0.5rem;");
theme.with(|theme| {
if theme.common.color_scheme == "dark" {
style.push_str("border: 1px solid #383f52; border-top-width: 0;");
style.push_str("background-color: #242832;");
} else {
style.push_str(&format!(
"border: 1px solid {}; border-top-width: 0;",
theme.common.border_color
));
style.push_str("background-color: #f9fafb;");
}
});
style
});
let content_class = create_memo(move |_| {
theme.with(|theme| {
format!(
"thaw-demo__content color-scheme--{}",
theme.common.color_scheme
)
})
});
let is_highlight = demo_code.is_highlight;
let frag = (demo_code.children)();
let mut html = String::new();
for node in frag.nodes {
match node {
View::Text(text) => html.push_str(&text.content),
2023-11-28 15:49:02 +08:00
_ => {
leptos::logging::warn!("Only text nodes are supported as children of <DemoCode />.")
}
}
}
2023-09-17 22:19:54 +08:00
view! {
<Style id="leptos-thaw-prism-css">{prisms::prism_css!()}</Style>
<Style id="leptos-thaw-syntect-css">
{include_str!("./syntect-css.css")}
</Style>
<Style id="leptos-thaw-prism-css-fix">
".token.operator {
background: hsla(0, 0%, 100%, 0) !important;
}"
2023-11-13 16:17:45 +08:00
</Style>
<div style=move || style.get()>{children()}</div>
<div style=move || code_style.get() class=move || content_class.get()>
2023-09-17 22:19:54 +08:00
<Code>
{
if is_highlight {
view! {
<pre style="margin: 0" inner_html=html></pre>
}
} else {
view! {
<pre style="margin: 0">
{html}
</pre>
}
}
}
2023-09-17 22:19:54 +08:00
</Code>
2023-10-08 09:28:13 +08:00
</div>
2023-09-17 22:19:54 +08:00
}
}