mirror of
https://github.com/adoyle0/thaw.git
synced 2025-02-02 16:44:15 -05:00
feat: add global style component
This commit is contained in:
parent
263db0c3e8
commit
6ab369718f
6 changed files with 49 additions and 33 deletions
|
@ -8,7 +8,7 @@ pub fn App() -> impl IntoView {
|
||||||
let theme = create_rw_signal(Theme::light());
|
let theme = create_rw_signal(Theme::light());
|
||||||
provide_context(theme);
|
provide_context(theme);
|
||||||
view! {
|
view! {
|
||||||
<Provider theme=theme.split().0>
|
<Provider theme>
|
||||||
<Router base="/melt-ui">
|
<Router base="/melt-ui">
|
||||||
<Routes base="/melt-ui".to_string()>
|
<Routes base="/melt-ui".to_string()>
|
||||||
<Route path="/" view=Home/>
|
<Route path="/" view=Home/>
|
||||||
|
@ -54,16 +54,13 @@ pub fn App() -> impl IntoView {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn Provider(theme: ReadSignal<Theme>, children: Children) -> impl IntoView {
|
fn Provider(theme: RwSignal<Theme>, children: Children) -> impl IntoView {
|
||||||
view! {
|
view! {
|
||||||
<ThemeProvider theme>
|
<ThemeProvider theme>
|
||||||
|
<GlobalStyle />
|
||||||
<MessageProvider>
|
<MessageProvider>
|
||||||
{children()}
|
{children()}
|
||||||
</MessageProvider>
|
</MessageProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_rw_theme() -> RwSignal<Theme> {
|
|
||||||
expect_context::<RwSignal<Theme>>()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use crate::app::use_rw_theme;
|
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_router::use_navigate;
|
use leptos_router::use_navigate;
|
||||||
use melt_ui::*;
|
use melt_ui::*;
|
||||||
|
|
23
src/global_style/mod.rs
Normal file
23
src/global_style/mod.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
use crate::{use_theme, Theme};
|
||||||
|
use leptos::*;
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn GlobalStyle() -> impl IntoView {
|
||||||
|
let theme = use_theme(Theme::light);
|
||||||
|
create_effect(move |_| {
|
||||||
|
theme.with(|theme| {
|
||||||
|
if let Some(body) = document().body() {
|
||||||
|
_ = body
|
||||||
|
.style()
|
||||||
|
.set_property("background-color", &theme.common.background_color);
|
||||||
|
_ = body.style().set_property("color", &theme.common.font_color);
|
||||||
|
_ = body
|
||||||
|
.style()
|
||||||
|
.set_property("font-family", &theme.common.font_family);
|
||||||
|
_ = body
|
||||||
|
.style()
|
||||||
|
.set_property("font-size", &theme.common.font_size);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ mod code;
|
||||||
mod color_picker;
|
mod color_picker;
|
||||||
mod components;
|
mod components;
|
||||||
mod divider;
|
mod divider;
|
||||||
|
mod global_style;
|
||||||
mod grid;
|
mod grid;
|
||||||
mod icon;
|
mod icon;
|
||||||
mod image;
|
mod image;
|
||||||
|
@ -45,6 +46,7 @@ pub use checkbox::*;
|
||||||
pub use code::*;
|
pub use code::*;
|
||||||
pub use color_picker::*;
|
pub use color_picker::*;
|
||||||
pub use divider::*;
|
pub use divider::*;
|
||||||
|
pub use global_style::*;
|
||||||
pub use grid::*;
|
pub use grid::*;
|
||||||
pub use icon::*;
|
pub use icon::*;
|
||||||
pub use image::*;
|
pub use image::*;
|
||||||
|
|
|
@ -3,6 +3,8 @@ use super::ThemeMethod;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CommonTheme {
|
pub struct CommonTheme {
|
||||||
pub font_family: String,
|
pub font_family: String,
|
||||||
|
pub font_color: String,
|
||||||
|
pub background_color: String,
|
||||||
|
|
||||||
pub color_primary: String,
|
pub color_primary: String,
|
||||||
pub color_primary_hover: String,
|
pub color_primary_hover: String,
|
||||||
|
@ -38,7 +40,9 @@ pub struct CommonTheme {
|
||||||
impl CommonTheme {
|
impl CommonTheme {
|
||||||
fn common() -> Self {
|
fn common() -> Self {
|
||||||
Self {
|
Self {
|
||||||
font_family: "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans',sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol','Noto Color Emoji'".into(),
|
font_family: r#"Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", Segoe UI Symbol, "Noto Color Emoji""#.into(),
|
||||||
|
font_color: "".into(),
|
||||||
|
background_color: "".into(),
|
||||||
color_primary: "".into(),
|
color_primary: "".into(),
|
||||||
color_primary_hover: "".into(),
|
color_primary_hover: "".into(),
|
||||||
color_primary_active: "".into(),
|
color_primary_active: "".into(),
|
||||||
|
@ -72,6 +76,8 @@ impl CommonTheme {
|
||||||
impl ThemeMethod for CommonTheme {
|
impl ThemeMethod for CommonTheme {
|
||||||
fn light() -> Self {
|
fn light() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
font_color: "#11181c".into(),
|
||||||
|
background_color: "#fff".into(),
|
||||||
color_primary: "#f5222d".into(),
|
color_primary: "#f5222d".into(),
|
||||||
color_primary_hover: "#ff4d4f".into(),
|
color_primary_hover: "#ff4d4f".into(),
|
||||||
color_primary_active: "#cf1322".into(),
|
color_primary_active: "#cf1322".into(),
|
||||||
|
@ -89,6 +95,8 @@ impl ThemeMethod for CommonTheme {
|
||||||
}
|
}
|
||||||
fn dark() -> Self {
|
fn dark() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
font_color: "#ecedee".into(),
|
||||||
|
background_color: "#1a1d1e".into(),
|
||||||
color_primary: "#d32029".into(),
|
color_primary: "#d32029".into(),
|
||||||
color_primary_hover: "#e04648".into(),
|
color_primary_hover: "#e04648".into(),
|
||||||
color_primary_active: "#ad111e".into(),
|
color_primary_active: "#ad111e".into(),
|
||||||
|
|
|
@ -53,49 +53,36 @@ impl Theme {
|
||||||
|
|
||||||
impl ThemeMethod for Theme {
|
impl ThemeMethod for Theme {
|
||||||
fn light() -> Self {
|
fn light() -> Self {
|
||||||
Self {
|
Theme::light()
|
||||||
name: "Light".into(),
|
|
||||||
common: CommonTheme::light(),
|
|
||||||
button: ButtonTheme::light(),
|
|
||||||
input: InputTheme::light(),
|
|
||||||
menu: MenuTheme::light(),
|
|
||||||
table: TableTheme::light(),
|
|
||||||
alert: AlertTheme::light(),
|
|
||||||
skeletion: SkeletionTheme::light(),
|
|
||||||
tag: TagTheme::light(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fn dark() -> Self {
|
fn dark() -> Self {
|
||||||
Self {
|
Theme::dark()
|
||||||
name: "Dark".into(),
|
|
||||||
common: CommonTheme::dark(),
|
|
||||||
button: ButtonTheme::dark(),
|
|
||||||
input: InputTheme::dark(),
|
|
||||||
menu: MenuTheme::dark(),
|
|
||||||
table: TableTheme::dark(),
|
|
||||||
alert: AlertTheme::dark(),
|
|
||||||
skeletion: SkeletionTheme::dark(),
|
|
||||||
tag: TagTheme::dark(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn ThemeProvider(
|
pub fn ThemeProvider(
|
||||||
#[prop(optional, into)] theme: Option<ReadSignal<Theme>>,
|
#[prop(optional, into)] theme: Option<RwSignal<Theme>>,
|
||||||
children: Children,
|
children: Children,
|
||||||
) -> impl IntoView {
|
) -> impl IntoView {
|
||||||
let theme = if let Some(theme) = theme {
|
let theme = if let Some(theme) = theme {
|
||||||
theme
|
theme
|
||||||
} else {
|
} else {
|
||||||
create_signal(Theme::light()).0
|
create_rw_signal(Theme::light())
|
||||||
};
|
};
|
||||||
provide_context(theme);
|
provide_context(theme);
|
||||||
children()
|
children()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_theme(default: impl Fn() -> Theme) -> ReadSignal<Theme> {
|
pub fn use_theme(default: impl Fn() -> Theme) -> ReadSignal<Theme> {
|
||||||
use_context::<ReadSignal<Theme>>().unwrap_or_else(|| create_signal(default()).0)
|
use_context::<RwSignal<Theme>>()
|
||||||
|
.unwrap_or_else(|| create_rw_signal(default()))
|
||||||
|
.split()
|
||||||
|
.0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_rw_theme() -> RwSignal<Theme> {
|
||||||
|
expect_context::<RwSignal<Theme>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue