feat: alert component theme

This commit is contained in:
luoxiao 2023-10-12 15:30:10 +08:00
parent 0573eafe24
commit 574fa671e2
4 changed files with 80 additions and 8 deletions

View file

@ -4,18 +4,24 @@
background-color: var(--background-color); background-color: var(--background-color);
border: 1px solid var(--border-color); border: 1px solid var(--border-color);
border-radius: 3px; border-radius: 3px;
line-height: 1.6;
} }
.melt-alert__icon { .melt-alert__icon {
position: absolute; position: absolute;
top: 12px; top: 12px;
left: 8px; left: 10px;
font-size: 24px;
color: var(--icon-color);
} }
.melt-alert__header { .melt-alert__header {
font-size: 16px; font-size: 16px;
line-height: 19px;
font-weight: 500;
} }
.melt-alert__header + .alert-body__content { .melt-alert__header + .melt-alert__content {
margin-top: 8px; margin-top: 8px;
font-size: 14px;
} }

View file

@ -1,6 +1,9 @@
mod theme;
use crate::{theme::use_theme, utils::mount_style::mount_style, Icon, Theme}; use crate::{theme::use_theme, utils::mount_style::mount_style, Icon, Theme};
use icondata::AiIcon; use icondata::AiIcon;
use leptos::*; use leptos::*;
pub use theme::AlertTheme;
#[derive(Clone)] #[derive(Clone)]
pub enum AlertVariant { pub enum AlertVariant {
@ -10,13 +13,27 @@ pub enum AlertVariant {
} }
impl AlertVariant { impl AlertVariant {
pub fn theme_border_color(&self, theme: &Theme) -> String { pub fn theme_icon_color(&self, theme: &Theme) -> String {
match self { match self {
AlertVariant::SUCCESS => theme.common.color_success.clone(), AlertVariant::SUCCESS => theme.common.color_success.clone(),
AlertVariant::WARNING => theme.common.color_warning.clone(), AlertVariant::WARNING => theme.common.color_warning.clone(),
AlertVariant::ERROR => theme.common.color_error.clone(), AlertVariant::ERROR => theme.common.color_error.clone(),
} }
} }
pub fn theme_background_color(&self, theme: &Theme) -> String {
match self {
AlertVariant::SUCCESS => theme.alert.success_background_color.clone(),
AlertVariant::WARNING => theme.alert.warning_background_color.clone(),
AlertVariant::ERROR => theme.alert.error_background_color.clone(),
}
}
pub fn theme_border_color(&self, theme: &Theme) -> String {
match self {
AlertVariant::SUCCESS => theme.alert.success_border_color.clone(),
AlertVariant::WARNING => theme.alert.warning_border_color.clone(),
AlertVariant::ERROR => theme.alert.error_border_color.clone(),
}
}
} }
#[component] #[component]
@ -36,9 +53,18 @@ pub fn Alert(
theme.with(|theme| { theme.with(|theme| {
let variant = variant.get(); let variant = variant.get();
let border_color = variant.theme_border_color(&theme); css_vars.push_str(&format!(
css_vars.push_str(&format!("--background-color: {border_color}aa;")); "--icon-color: {};",
css_vars.push_str(&format!("--border-color: {border_color};")); variant.theme_icon_color(&theme)
));
css_vars.push_str(&format!(
"--background-color: {};",
variant.theme_background_color(&theme)
));
css_vars.push_str(&format!(
"--border-color: {};",
variant.theme_border_color(&theme)
));
}); });
css_vars css_vars
@ -70,7 +96,7 @@ pub fn Alert(
} }
} }
} }
<div class="melt-alert-body__content"> <div class="melt-alert__content">
{ children() } { children() }
</div> </div>
</div> </div>

35
src/alert/theme.rs Normal file
View file

@ -0,0 +1,35 @@
use crate::theme::ThemeMethod;
#[derive(Clone)]
pub struct AlertTheme {
pub success_background_color: String,
pub success_border_color: String,
pub warning_background_color: String,
pub warning_border_color: String,
pub error_background_color: String,
pub error_border_color: String,
}
impl ThemeMethod for AlertTheme {
fn light() -> Self {
Self {
success_background_color: "#edf7f2".into(),
success_border_color: "#c5e7d5".into(),
warning_background_color: "#fef7ed".into(),
warning_border_color: "#fae0b5".into(),
error_background_color: "#fbeef1".into(),
error_border_color: "#f3cbd3".into(),
}
}
fn dark() -> Self {
Self {
success_background_color: "#edf7f2".into(),
success_border_color: "#c5e7d5".into(),
warning_background_color: "#fef7ed".into(),
warning_border_color: "#fae0b5".into(),
error_background_color: "#fbeef1".into(),
error_border_color: "#f3cbd3".into(),
}
}
}

View file

@ -2,7 +2,7 @@ mod common;
use leptos::*; use leptos::*;
use self::common::CommonTheme; use self::common::CommonTheme;
use crate::{ButtonTheme, InputTheme, MenuTheme, TableTheme}; use crate::{AlertTheme, ButtonTheme, InputTheme, MenuTheme, TableTheme};
pub trait ThemeMethod { pub trait ThemeMethod {
fn light() -> Self; fn light() -> Self;
@ -16,6 +16,7 @@ pub struct Theme {
pub input: InputTheme, pub input: InputTheme,
pub menu: MenuTheme, pub menu: MenuTheme,
pub table: TableTheme, pub table: TableTheme,
pub alert: AlertTheme,
} }
impl Theme { impl Theme {
@ -26,6 +27,7 @@ impl Theme {
input: InputTheme::light(), input: InputTheme::light(),
menu: MenuTheme::light(), menu: MenuTheme::light(),
table: TableTheme::light(), table: TableTheme::light(),
alert: AlertTheme::light(),
} }
} }
pub fn dark() -> Self { pub fn dark() -> Self {
@ -35,6 +37,7 @@ impl Theme {
input: InputTheme::dark(), input: InputTheme::dark(),
menu: MenuTheme::dark(), menu: MenuTheme::dark(),
table: TableTheme::dark(), table: TableTheme::dark(),
alert: AlertTheme::dark(),
} }
} }
} }
@ -47,6 +50,7 @@ impl ThemeMethod for Theme {
input: InputTheme::light(), input: InputTheme::light(),
menu: MenuTheme::light(), menu: MenuTheme::light(),
table: TableTheme::light(), table: TableTheme::light(),
alert: AlertTheme::light(),
} }
} }
fn dark() -> Self { fn dark() -> Self {
@ -56,6 +60,7 @@ impl ThemeMethod for Theme {
input: InputTheme::dark(), input: InputTheme::dark(),
menu: MenuTheme::dark(), menu: MenuTheme::dark(),
table: TableTheme::dark(), table: TableTheme::dark(),
alert: AlertTheme::dark(),
} }
} }
} }