feat: optimize Text component

This commit is contained in:
luoxiao 2024-07-12 10:06:43 +08:00
parent f4838a8168
commit 5e6f9ec287
7 changed files with 41 additions and 40 deletions

View file

@ -2,12 +2,7 @@
```rust demo ```rust demo
view! { view! {
<Space> <Tag>"default"</Tag>
<Tag>"default"</Tag>
<Tag variant=TagVariant::Success>"success"</Tag>
<Tag variant=TagVariant::Warning>"warning"</Tag>
<Tag variant=TagVariant::Error>"error"</Tag>
</Space>
} }
``` ```
@ -25,12 +20,7 @@ let success = move |_: SendWrapper<ev::MouseEvent>| {
}; };
view! { view! {
<Space> <Tag closable=true on_close=success>"Default"</Tag>
<Tag closable=true on_close=success>"Default"</Tag>
<Tag closable=true on_close=success variant=TagVariant::Success>"Success"</Tag>
<Tag closable=true on_close=success variant=TagVariant::Warning>"Warning"</Tag>
<Tag closable=true on_close=success variant=TagVariant::Error>"Error"</Tag>
</Space>
} }
``` ```

View file

@ -4,7 +4,7 @@
view! { view! {
<Space> <Space>
<Text>"text"</Text> <Text>"text"</Text>
<Text code=true>"code"</Text> <Text tag=TextTag::Code>"code"</Text>
<Caption1>"Caption1"</Caption1> <Caption1>"Caption1"</Caption1>
<Caption1Strong>"Caption1Strong"</Caption1Strong> <Caption1Strong>"Caption1Strong"</Caption1Strong>
<Body1>"Body1"</Body1> <Body1>"Body1"</Body1>

View file

@ -146,7 +146,7 @@ fn iter_nodes<'a>(
NodeValue::Code(node_code) => { NodeValue::Code(node_code) => {
let code = node_code.literal.clone(); let code = node_code.literal.clone();
quote!( quote!(
<Text code=true> <Text tag=TextTag::Code>
#code #code
</Text> </Text>
) )

View file

@ -2,18 +2,8 @@ use leptos::{either::Either, ev, prelude::*};
use send_wrapper::SendWrapper; use send_wrapper::SendWrapper;
use thaw_utils::{class_list, mount_style, OptionalProp}; use thaw_utils::{class_list, mount_style, OptionalProp};
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum TagVariant {
#[default]
Default,
Success,
Warning,
Error,
}
#[component] #[component]
pub fn Tag( pub fn Tag(
#[prop(optional, into)] variant: MaybeSignal<TagVariant>,
#[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>, #[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>,
#[prop(optional, into)] closable: MaybeSignal<bool>, #[prop(optional, into)] closable: MaybeSignal<bool>,
#[prop(optional, into)] on_close: Option<Callback<SendWrapper<ev::MouseEvent>>>, #[prop(optional, into)] on_close: Option<Callback<SendWrapper<ev::MouseEvent>>>,

View file

@ -51,86 +51,93 @@ pub fn Text(
#[prop(optional, into)] class: MaybeProp<String>, #[prop(optional, into)] class: MaybeProp<String>,
#[prop(optional, into)] style: MaybeProp<String>, #[prop(optional, into)] style: MaybeProp<String>,
#[prop(optional)] tag: TextTag, #[prop(optional)] tag: TextTag,
#[prop(optional)] code: bool,
children: Children, children: Children,
) -> impl IntoView { ) -> impl IntoView {
mount_style("text", include_str!("./text.css")); mount_style("text", include_str!("./text.css"));
let class = class_list!["thaw-text", class];
let style = move || style.get().unwrap_or_default();
match tag { match tag {
TextTag::B => view! { TextTag::B => view! {
<b class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <b class=class style=style>
{children()} {children()}
</b> </b>
} }
.into_any(), .into_any(),
TextTag::Code => view! {
<code class=class style=style>
{children()}
</code>
}
.into_any(),
TextTag::Em => view! { TextTag::Em => view! {
<em class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <em class=class style=style>
{children()} {children()}
</em> </em>
} }
.into_any(), .into_any(),
TextTag::H1 => view! { TextTag::H1 => view! {
<h1 class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <h1 class=class style=style>
{children()} {children()}
</h1> </h1>
} }
.into_any(), .into_any(),
TextTag::H2 => view! { TextTag::H2 => view! {
<h2 class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <h2 class=class style=style>
{children()} {children()}
</h2> </h2>
} }
.into_any(), .into_any(),
TextTag::H3 => view! { TextTag::H3 => view! {
<h3 class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <h3 class=class style=style>
{children()} {children()}
</h3> </h3>
} }
.into_any(), .into_any(),
TextTag::H4 => view! { TextTag::H4 => view! {
<h4 class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <h4 class=class style=style>
{children()} {children()}
</h4> </h4>
} }
.into_any(), .into_any(),
TextTag::H5 => view! { TextTag::H5 => view! {
<h5 class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <h5 class=class style=style>
{children()} {children()}
</h5> </h5>
} }
.into_any(), .into_any(),
TextTag::H6 => view! { TextTag::H6 => view! {
<h6 class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <h6 class=class style=style>
{children()} {children()}
</h6> </h6>
} }
.into_any(), .into_any(),
TextTag::I => view! { TextTag::I => view! {
<i class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <i class=class style=style>
{children()} {children()}
</i> </i>
} }
.into_any(), .into_any(),
TextTag::P => view! { TextTag::P => view! {
<p class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <p class=class style=style>
{children()} {children()}
</p> </p>
} }
.into_any(), .into_any(),
TextTag::Pre => view! { TextTag::Pre => view! {
<pre class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <pre class=class style=style>
{children()} {children()}
</pre> </pre>
} }
.into_any(), .into_any(),
TextTag::Span => view! { TextTag::Span => view! {
<span class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <span class=class style=style>
{children()} {children()}
</span> </span>
} }
.into_any(), .into_any(),
TextTag::Strong => view! { TextTag::Strong => view! {
<strong class=class_list!["thaw-text", class] style=move || style.get().unwrap_or_default()> <strong class=class style=style>
{children()} {children()}
</strong> </strong>
} }
@ -138,9 +145,10 @@ pub fn Text(
} }
} }
#[derive(Default)] #[derive(Default, PartialEq)]
pub enum TextTag { pub enum TextTag {
B, B,
Code,
Em, Em,
H1, H1,
H2, H2,

View file

@ -11,6 +11,15 @@
font-family: var(--fontFamilyBase); font-family: var(--fontFamilyBase);
} }
code.thaw-text {
padding: 1px 4px;
margin: 0 3px 0 3px;
background-color: var(--colorNeutralBackground4);
color: var(--colorNeutralForeground1);
font-family: var(--fontFamilyMonospace);
border-radius: var(--borderRadiusMedium);
}
.thaw-caption-1 { .thaw-caption-1 {
line-height: var(--lineHeightBase200); line-height: var(--lineHeightBase200);
font-weight: var(--fontWeightRegular); font-weight: var(--fontWeightRegular);

View file

@ -3,6 +3,8 @@ use thaw_macro::WriteCSSVars;
#[derive(Clone, WriteCSSVars)] #[derive(Clone, WriteCSSVars)]
pub struct CommonTheme { pub struct CommonTheme {
pub font_family_base: String, pub font_family_base: String,
pub font_family_monospace: String,
pub font_family_numeric: String,
pub font_size_base_100: String, pub font_size_base_100: String,
pub font_size_base_200: String, pub font_size_base_200: String,
@ -67,7 +69,9 @@ impl CommonTheme {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
font_family_base: "'Segoe UI', 'Segoe UI Web (West European)', -apple-system, BlinkMacSystemFont, Roboto, 'Helvetica Neue', sans-serif".into(), font_family_base: "'Segoe UI', 'Segoe UI Web (West European)', -apple-system, BlinkMacSystemFont, Roboto, 'Helvetica Neue', sans-serif".into(),
font_family_monospace: "Consolas, 'Courier New', Courier, monospace".into(),
font_family_numeric: "Bahnschrift, 'Segoe UI', 'Segoe UI Web (West European)', -apple-system, BlinkMacSystemFont, Roboto, 'Helvetica Neue', sans-serif".into(),
font_size_base_100: "10px".into(), font_size_base_100: "10px".into(),
font_size_base_200: "12px".into(), font_size_base_200: "12px".into(),
font_size_base_300: "14px".into(), font_size_base_300: "14px".into(),