Skip to content

Commit

Permalink
Create Theme component
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Oct 22, 2023
1 parent 1048b4f commit cc11096
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions examples/button.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use dioxus::prelude::*;
use dioxus_material::{use_theme_provider, Button, TextButton, Theme};
use dioxus_material::{Button, TextButton, Theme};

fn app(cx: Scope) -> Element {
use_theme_provider(cx, Theme::default());

render!(
Button { onpress: |_| log::info!("clicked!"), "Click me!" }
TextButton { onpress: |_| log::info!("clicked!"), "Click me!" }
Theme {
Button { onpress: |_| log::info!("clicked!"), "Click me!" }
TextButton { onpress: |_| log::info!("clicked!"), "Click me!" }
}
)
}

Expand Down
32 changes: 16 additions & 16 deletions examples/navigation_rail.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use dioxus::prelude::*;
use dioxus_material::{use_theme_provider, NavigationRail, NavigationRailItem, Theme};
use dioxus_material::{NavigationRail, NavigationRailItem, Theme};

fn app(cx: Scope) -> Element {
use_theme_provider(cx, Theme::default());

render!(
NavigationRail {
NavigationRailItem {
icon: render!("A"),
label: render!("All files"),
is_selected: false,
onselect: |_| {}
}
NavigationRailItem { icon: render!("B"), label: render!("Recent"), is_selected: true, onselect: |_| {} }
NavigationRailItem {
icon: render!("C"),
label: render!("Photos"),
is_selected: false,
onselect: |_| {}
Theme {
NavigationRail {
NavigationRailItem {
icon: render!("A"),
label: render!("All files"),
is_selected: false,
onselect: |_| {}
}
NavigationRailItem { icon: render!("B"), label: render!("Recent"), is_selected: true, onselect: |_| {} }
NavigationRailItem {
icon: render!("C"),
label: render!("Photos"),
is_selected: false,
onselect: |_| {}
}
}
}
)
Expand Down
24 changes: 15 additions & 9 deletions examples/tab_row.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use dioxus::prelude::*;
use dioxus_material::{Tab, TabRow};
use dioxus_material::{Tab, TabRow, Theme};

fn app(cx: Scope) -> Element {
render!(TabRow {
onselect: |idx| log::info!("{}", idx),
tabs: cx.bump().alloc([
render!(Tab { "Tab 1" }),
render!(Tab { "Tab 2" }),
render!(Tab { "Tab 3" }),
])
})
render!(
Theme {
TabRow {
onselect: |idx| log::info!("{}", idx),
tabs: cx
.bump()
.alloc([
render!(Tab { "Tab 1" }),
render!(Tab { "Tab 2" }),
render!(Tab { "Tab 3" }),
])
}
}
)
}

fn main() {
Expand Down
18 changes: 10 additions & 8 deletions examples/text_field.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use dioxus::prelude::*;
use dioxus_material::{use_theme_provider, TextField, Theme};
use dioxus_material::{TextField, Theme};

fn app(cx: Scope) -> Element {
use_theme_provider(cx, Theme::default());

let value = use_state(cx, || String::from("Filled"));
render!(TextField {
label: "Text field",
value: "{value}",
onchange: move |event: FormEvent| value.set(event.value.clone())
})
render!(
Theme {
TextField {
label: "Text field",
value: "{value}",
onchange: move |event: FormEvent| value.set(event.value.clone())
}
}
)
}

fn main() {
Expand Down
14 changes: 5 additions & 9 deletions lookbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,20 @@ fn Home(cx: Scope) -> Element {
h5 { "Material You design library for dioxus." }
a { href: "https://github.com/matthunz/dioxus-material", "Github" }

div {
margin_top: "20px",
div { margin_top: "20px",
"Made with "
a { href: "https://github.com/matthunz/lookbook", "Lookbook" }
"."
}

}
)
}

fn app(cx: Scope) -> Element {
render!(
LookBook {
home: Home,
previews: [ButtonPreview, TextButtonPreview, TextFieldPreview]
}
)
render!(LookBook {
home: Home,
previews: [ButtonPreview, TextButtonPreview, TextFieldPreview]
})
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod tab_row;
pub use tab_row::TabRow;

mod theme;
pub use theme::{use_theme, use_theme_provider, Theme};
pub use theme::{use_theme, Theme, UseTheme};

mod text_field;
pub use text_field::TextField;
Expand Down
67 changes: 47 additions & 20 deletions src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
use dioxus::prelude::*;
use std::{borrow::Cow, rc::Rc};

pub struct Theme {
#[component]
pub fn Theme<'a>(
cx: Scope<'a>,

/// Primary color.
#[props(into, default = Cow::Borrowed("#6750A4"))]
primary_color: Cow<'static, str>,

/// Background color.
#[props(into, default = Cow::Borrowed("#eeeeee"))]
background_color: Cow<'static, str>,

/// Secondary container color.
#[props(into, default = Cow::Borrowed("#E8DEF8"))]
secondary_container_color: Cow<'static, str>,

/// Border radius.
#[props(into, default = Cow::Borrowed("25px"))]
border_radius: Cow<'static, str>,

/// Small label font size.
#[props(default = 12.)]
label_small: f32,

/// Medium label font size.
#[props(default = 16.)]
label_medium: f32,

children: Element<'a>,
) -> Element<'a> {
use_context_provider(cx, move || {
Rc::new(UseTheme {
primary_color: primary_color.clone(),
background_color: background_color.clone(),
secondary_container_color: secondary_container_color.clone(),
border_radius: border_radius.clone(),
label_small: *label_small,
label_medium: *label_medium,
})
});

render!(children)
}

pub struct UseTheme {
pub primary_color: Cow<'static, str>,
pub background_color: Cow<'static, str>,
pub secondary_container_color: Cow<'static, str>,
Expand All @@ -10,24 +54,7 @@ pub struct Theme {
pub label_medium: f32,
}

impl Default for Theme {
fn default() -> Self {
Self {
primary_color: Cow::Borrowed("#6750A4"),
background_color: Cow::Borrowed("#eeeeee"),
secondary_container_color: Cow::Borrowed("#E8DEF8"),
border_radius: Cow::Borrowed("25px"),
label_small: 12.,
label_medium: 16.,
}
}
}

pub fn use_theme_provider<T>(cx: Scope<T>, theme: Theme) {
use_context_provider(cx, move || Rc::new(theme));
}

pub fn use_theme<T>(cx: Scope<T>) -> &Theme {
let rc: &Rc<Theme> = use_context(cx).unwrap();
pub fn use_theme<T>(cx: Scope<T>) -> &UseTheme {
let rc: &Rc<UseTheme> = use_context(cx).unwrap();
rc.as_ref()
}

0 comments on commit cc11096

Please sign in to comment.