feat: rewrite code component

This commit is contained in:
luoxiao 2024-01-31 20:53:43 +08:00 committed by luoxiaozero
parent 80d190e15d
commit 9c7c24b102
4 changed files with 36 additions and 16 deletions

View file

@ -42,7 +42,7 @@ pub fn Demo(demo_code: DemoCode, children: Children) -> impl IntoView {
let content_class = create_memo(move |_| { let content_class = create_memo(move |_| {
theme.with(|theme| { theme.with(|theme| {
format!( format!(
"thaw-demo__content color-scheme--{}", "color-scheme--{}",
theme.common.color_scheme theme.common.color_scheme
) )
}) })
@ -66,21 +66,17 @@ pub fn Demo(demo_code: DemoCode, children: Children) -> impl IntoView {
</Style> </Style>
<div style=move || style.get()>{children()}</div> <div style=move || style.get()>{children()}</div>
<div style=move || code_style.get() class=move || content_class.get()> <div style=move || code_style.get() class=move || content_class.get()>
<Code>
{ {
if is_highlight { if is_highlight {
view! { view! {
<pre style="margin: 0" inner_html=html></pre> <Code inner_html=html />
} }
} else { } else {
view! { view! {
<pre style="margin: 0"> <Code text=html />
{html}
</pre>
} }
} }
} }
</Code>
</div> </div>
} }
} }

View file

@ -1,8 +1,3 @@
.thaw-demo__content pre {
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas,
Liberation Mono, monospace;
}
/** https://github.com/AmjadHD/sublime_one_theme */ /** https://github.com/AmjadHD/sublime_one_theme */
/** light */ /** light */

View file

@ -1,3 +1,12 @@
.thaw-code { .thaw-code {
font-size: 14px; font-size: 14px;
} font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas,
Liberation Mono, monospace;
}
.thaw-code pre {
margin: 0;
line-height: inherit;
font-size: inherit;
font-family: inherit;
}

View file

@ -1,8 +1,28 @@
use crate::utils::mount_style; use crate::utils::{class_list::class_list, mount_style, OptionalProp};
use leptos::*; use leptos::*;
#[component] #[component]
pub fn Code(children: Children) -> impl IntoView { pub fn Code(
#[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>,
#[prop(optional, into)] text: Option<String>,
#[prop(optional, into)] inner_html: Option<String>,
) -> impl IntoView {
mount_style("code", include_str!("./code.css")); mount_style("code", include_str!("./code.css"));
view! { <code class="thaw-code">{children()}</code> } view! {
<code class=class_list!["thaw-code", class.map(|c| move || c.get())]>
{
if let Some(inner_html) = inner_html {
view! {
<pre inner_html=inner_html></pre>
}.into()
} else if let Some(text) = text {
view! {
<pre>{text}</pre>
}.into()
} else {
None
}
}
</code>
}
} }