From 628fd95783b8a30b4ed2cc6fcfe8eaa5307cbe89 Mon Sep 17 00:00:00 2001 From: luoxiao Date: Mon, 20 Nov 2023 21:00:13 +0800 Subject: [PATCH] feat: add text component --- demo/src/app.rs | 1 + demo/src/pages/components.rs | 25 +++++++----- demo/src/pages/mod.rs | 2 + demo/src/pages/typography/mod.rs | 69 ++++++++++++++++++++++++++++++++ src/lib.rs | 2 + src/theme/mod.rs | 5 ++- src/typography/mod.rs | 5 +++ src/typography/text.css | 9 +++++ src/typography/text.rs | 30 ++++++++++++++ src/typography/theme.rs | 20 +++++++++ 10 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 demo/src/pages/typography/mod.rs create mode 100644 src/typography/mod.rs create mode 100644 src/typography/text.css create mode 100644 src/typography/text.rs create mode 100644 src/typography/theme.rs diff --git a/demo/src/app.rs b/demo/src/app.rs index e1ba969..e731092 100644 --- a/demo/src/app.rs +++ b/demo/src/app.rs @@ -75,6 +75,7 @@ fn TheRouter(is_routing: RwSignal) -> impl IntoView { + diff --git a/demo/src/pages/components.rs b/demo/src/pages/components.rs index d04d564..0cf8a8c 100644 --- a/demo/src/pages/components.rs +++ b/demo/src/pages/components.rs @@ -31,9 +31,9 @@ pub fn ComponentsPage() -> impl IntoView { - { - gen_menu_data().into_view() - } + + {gen_menu_data().into_view()} + @@ -53,10 +53,13 @@ impl IntoView for MenuGroupOption { fn into_view(self) -> View { let Self { label, children } = self; view! { - - { - children.into_iter().map(|v| v.into_view()).collect_view() - } + + + {children.into_iter().map(|v| v.into_view()).collect_view()} + } } @@ -70,9 +73,7 @@ pub(crate) struct MenuItemOption { impl IntoView for MenuItemOption { fn into_view(self) -> View { let Self { label, value } = self; - view! { - - } + view! { } } } @@ -105,6 +106,10 @@ pub(crate) fn gen_menu_data() -> Vec { value: "tag".into(), label: "Tag".into(), }, + MenuItemOption { + value: "typography".into(), + label: "Typography".into(), + }, ], }, MenuGroupOption { diff --git a/demo/src/pages/mod.rs b/demo/src/pages/mod.rs index cc05c74..0ff65fc 100644 --- a/demo/src/pages/mod.rs +++ b/demo/src/pages/mod.rs @@ -36,6 +36,7 @@ mod tabs; mod tag; mod theme; mod toast; +mod typography; mod upload; pub use alert::*; @@ -76,4 +77,5 @@ pub use tabs::*; pub use tag::*; pub use theme::*; pub use toast::*; +pub use typography::*; pub use upload::*; diff --git a/demo/src/pages/typography/mod.rs b/demo/src/pages/typography/mod.rs new file mode 100644 index 0000000..5ea26d1 --- /dev/null +++ b/demo/src/pages/typography/mod.rs @@ -0,0 +1,69 @@ +use crate::components::{Demo, DemoCode}; +use leptos::*; +use prisms::highlight_str; +use thaw::*; + +#[component] +pub fn TypographyPage() -> impl IntoView { + view! { +
+

"Typography"

+ + + "text" + "code" + + + + "text" + + + "code" + + + "#, + "rust" + ) + > + + "" + + +

"Text Props"

+ + + + + + + + + + + + + + + + + + + + + + + +
"Name""Type""Default""Description"
"code" + "bool" + + "false" + "Use the code tag and style."
"children" + "Children" + "Text's content."
+
+ } +} diff --git a/src/lib.rs b/src/lib.rs index 3687b98..e74a413 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ mod table; mod tabs; mod tag; mod theme; +mod typography; mod upload; mod utils; @@ -69,5 +70,6 @@ pub use table::*; pub use tabs::*; pub use tag::*; pub use theme::*; +pub use typography::*; pub use upload::*; pub use utils::SignalWatch; diff --git a/src/theme/mod.rs b/src/theme/mod.rs index e2f8d75..ae98102 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -6,7 +6,7 @@ use crate::{ utils::Provider, AlertTheme, AutoCompleteTheme, AvatarTheme, BreadcrumbTheme, ButtonTheme, ColorPickerTheme, InputTheme, MenuTheme, MessageTheme, ProgressTheme, SelectTheme, SkeletionTheme, SliderTheme, - SwitchTheme, TableTheme, TagTheme, UploadTheme, + SwitchTheme, TableTheme, TagTheme, TypographyTheme, UploadTheme, }; use leptos::*; @@ -38,6 +38,7 @@ pub struct Theme { pub color_picker: ColorPickerTheme, pub breadcrumb: BreadcrumbTheme, pub progress: ProgressTheme, + pub typograph: TypographyTheme, } impl Theme { @@ -64,6 +65,7 @@ impl Theme { color_picker: ColorPickerTheme::light(), breadcrumb: BreadcrumbTheme::light(), progress: ProgressTheme::light(), + typograph: TypographyTheme::light(), } } pub fn dark() -> Self { @@ -89,6 +91,7 @@ impl Theme { color_picker: ColorPickerTheme::dark(), breadcrumb: BreadcrumbTheme::dark(), progress: ProgressTheme::dark(), + typograph: TypographyTheme::dark(), } } } diff --git a/src/typography/mod.rs b/src/typography/mod.rs new file mode 100644 index 0000000..e160a6c --- /dev/null +++ b/src/typography/mod.rs @@ -0,0 +1,5 @@ +mod text; +mod theme; + +pub use text::*; +pub use theme::TypographyTheme; diff --git a/src/typography/text.css b/src/typography/text.css new file mode 100644 index 0000000..2645043 --- /dev/null +++ b/src/typography/text.css @@ -0,0 +1,9 @@ +.thaw-text--code { + padding: 0.2em 0.35em; + font-size: 0.93em; + line-height: 1.4; + background-color: var(--thaw-background-color); + border: 1px solid #0000; + border-radius: 2px; + box-sizing: border-box; +} diff --git a/src/typography/text.rs b/src/typography/text.rs new file mode 100644 index 0000000..1ded9e6 --- /dev/null +++ b/src/typography/text.rs @@ -0,0 +1,30 @@ +use crate::{use_theme, utils::mount_style, Theme}; +use leptos::*; + +#[component] +pub fn Text(#[prop(optional)] code: bool, children: Children) -> impl IntoView { + mount_style("text", include_str!("./text.css")); + let theme = use_theme(Theme::light); + let css_vars = create_memo(move |_| { + let mut css_vars = String::new(); + theme.with(|theme| { + css_vars.push_str(&format!( + "--thaw-background-color: {}", + theme.typograph.code_background_color + )); + }); + css_vars + }); + + if code { + return view! { + + {children()} + + } + .into_any(); + } else { + view! { {children()} } + .into_any() + } +} diff --git a/src/typography/theme.rs b/src/typography/theme.rs new file mode 100644 index 0000000..437f0c6 --- /dev/null +++ b/src/typography/theme.rs @@ -0,0 +1,20 @@ +use crate::theme::ThemeMethod; + +#[derive(Clone)] +pub struct TypographyTheme { + pub code_background_color: String, +} + +impl ThemeMethod for TypographyTheme { + fn light() -> Self { + Self { + code_background_color: "#f4f4f8".into(), + } + } + + fn dark() -> Self { + Self { + code_background_color: "#ffffff1f".into(), + } + } +}