From 0573eafe244300efa8b5b83567e41dfd3e113945 Mon Sep 17 00:00:00 2001 From: luoxiao Date: Wed, 11 Oct 2023 23:02:04 +0800 Subject: [PATCH] feat: add alert component --- Cargo.toml | 3 ++ demo/src/app.rs | 1 + demo/src/pages/alert/mod.rs | 34 ++++++++++++++++ demo/src/pages/components.rs | 1 + demo/src/pages/mod.rs | 2 + src/alert/alert.css | 21 ++++++++++ src/alert/mod.rs | 79 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 + 8 files changed, 143 insertions(+) create mode 100644 demo/src/pages/alert/mod.rs create mode 100644 src/alert/alert.css create mode 100644 src/alert/mod.rs diff --git a/Cargo.toml b/Cargo.toml index ed2059d..9319331 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ icondata = { version = "0.0.7", features = [ "AiCheckOutlined", "AiLeftOutlined", "AiLoadingOutlined", + "AiCheckCircleFilled", + "AiExclamationCircleFilled", + "AiCloseCircleFilled" ] } [workspace] diff --git a/demo/src/app.rs b/demo/src/app.rs index be05e83..32baddf 100644 --- a/demo/src/app.rs +++ b/demo/src/app.rs @@ -24,6 +24,7 @@ pub fn App() -> impl IntoView { + diff --git a/demo/src/pages/alert/mod.rs b/demo/src/pages/alert/mod.rs new file mode 100644 index 0000000..682d30b --- /dev/null +++ b/demo/src/pages/alert/mod.rs @@ -0,0 +1,34 @@ +use crate::components::{Demo, DemoCode}; +use leptos::*; +use melt_ui::*; +use prisms::highlight_str; + +#[component] +pub fn AlertPage() -> impl IntoView { + view! { +
+

"Alert"

+ + + "success" + "warning" + "error" + + "success" + "warning" + "error" + "#, + "rust" + ) + > + + "" + + +
+ } +} diff --git a/demo/src/pages/components.rs b/demo/src/pages/components.rs index e5e03b2..baeff1a 100644 --- a/demo/src/pages/components.rs +++ b/demo/src/pages/components.rs @@ -43,6 +43,7 @@ pub fn ComponentsPage() -> impl IntoView { + diff --git a/demo/src/pages/mod.rs b/demo/src/pages/mod.rs index f801e1e..b42f759 100644 --- a/demo/src/pages/mod.rs +++ b/demo/src/pages/mod.rs @@ -1,3 +1,4 @@ +mod alert; mod button; mod checkbox; mod color_picker; @@ -17,6 +18,7 @@ mod table; mod tabs; mod toast; +pub use alert::*; pub use button::*; pub use checkbox::*; pub use color_picker::*; diff --git a/src/alert/alert.css b/src/alert/alert.css new file mode 100644 index 0000000..98e82ff --- /dev/null +++ b/src/alert/alert.css @@ -0,0 +1,21 @@ +.melt-alert { + position: relative; + padding: 14px 20px 14px 42px; + background-color: var(--background-color); + border: 1px solid var(--border-color); + border-radius: 3px; +} + +.melt-alert__icon { + position: absolute; + top: 12px; + left: 8px; +} + +.melt-alert__header { + font-size: 16px; +} + +.melt-alert__header + .alert-body__content { + margin-top: 8px; +} diff --git a/src/alert/mod.rs b/src/alert/mod.rs new file mode 100644 index 0000000..052648c --- /dev/null +++ b/src/alert/mod.rs @@ -0,0 +1,79 @@ +use crate::{theme::use_theme, utils::mount_style::mount_style, Icon, Theme}; +use icondata::AiIcon; +use leptos::*; + +#[derive(Clone)] +pub enum AlertVariant { + SUCCESS, + WARNING, + ERROR, +} + +impl AlertVariant { + pub fn theme_border_color(&self, theme: &Theme) -> String { + match self { + AlertVariant::SUCCESS => theme.common.color_success.clone(), + AlertVariant::WARNING => theme.common.color_warning.clone(), + AlertVariant::ERROR => theme.common.color_error.clone(), + } + } +} + +#[component] +pub fn Alert( + #[prop(optional, into)] title: MaybeSignal, + #[prop(into)] variant: MaybeSignal, + children: Children, +) -> impl IntoView { + mount_style("alert", include_str!("./alert.css")); + let theme = use_theme(Theme::light); + + let css_vars = create_memo({ + let variant = variant.clone(); + + move |_| { + let mut css_vars = String::new(); + + theme.with(|theme| { + let variant = variant.get(); + let border_color = variant.theme_border_color(&theme); + css_vars.push_str(&format!("--background-color: {border_color}aa;")); + css_vars.push_str(&format!("--border-color: {border_color};")); + }); + + css_vars + } + }); + let icon = create_memo(move |_| { + match variant.get() { + AlertVariant::SUCCESS => AiIcon::AiCheckCircleFilled, + AlertVariant::WARNING => AiIcon::AiExclamationCircleFilled, + AlertVariant::ERROR => AiIcon::AiCloseCircleFilled, + } + .into() + }); + view! { +
+ +
+ { + move || { + let title = title.get(); + if title.is_empty() { + None + } else { + view! { +
+ {title} +
+ }.into() + } + } + } +
+ { children() } +
+
+
+ } +} diff --git a/src/lib.rs b/src/lib.rs index 4063edb..db5e652 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod alert; mod button; mod card; mod checkbox; @@ -22,6 +23,7 @@ mod theme; mod utils; mod wave; +pub use alert::*; pub use button::*; pub use card::*; pub use checkbox::*;