Skip to content

Commit

Permalink
chore: Simplify how the default theme is provided (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 authored Aug 22, 2023
1 parent e0e0d6b commit 3f5e96d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 2 additions & 6 deletions components/src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dioxus::prelude::*;
use freya_hooks::{use_init_default_theme, use_init_theme, Theme};
use freya_hooks::{use_init_theme, Theme};

/// [`ThemeProvider`] component properties.
#[derive(Props)]
Expand All @@ -18,11 +18,7 @@ pub struct ThemeProviderProps<'a> {
///
#[allow(non_snake_case)]
pub fn ThemeProvider<'a>(cx: Scope<'a, ThemeProviderProps<'a>>) -> Element<'a> {
if let Some(theme) = cx.props.theme.as_ref() {
use_init_theme(cx, theme.clone());
} else {
use_init_default_theme(cx);
}
use_init_theme(cx, cx.props.theme.clone().unwrap_or_default());

render!(&cx.props.children)
}
13 changes: 9 additions & 4 deletions hooks/src/use_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ pub fn use_init_theme(cx: &ScopeState, theme: Theme) {
}

/// Provide the default [`Theme`].
pub fn use_init_default_theme(cx: &ScopeState) -> Theme {
use_shared_state_provider(cx, || LIGHT_THEME);
LIGHT_THEME
pub fn use_init_default_theme(cx: &ScopeState) {
use_shared_state_provider(cx, Theme::default);
}

/// Subscribe to [`Theme`] changes.
Expand All @@ -23,7 +22,7 @@ pub fn use_theme(cx: &ScopeState) -> &UseSharedState<Theme> {
pub fn use_get_theme(cx: &ScopeState) -> Theme {
use_shared_state::<Theme>(cx)
.map(|v| v.read().clone())
.unwrap_or(LIGHT_THEME)
.unwrap_or_default()
}

/// Theming properties for DropdownItem components.
Expand Down Expand Up @@ -133,6 +132,12 @@ pub struct Theme {
pub loader: LoaderTheme,
}

impl Default for Theme {
fn default() -> Self {
LIGHT_THEME
}
}

/// `Light` theme
pub const LIGHT_THEME: Theme = Theme {
name: "light",
Expand Down

0 comments on commit 3f5e96d

Please sign in to comment.