From 3f5e96d476c772f97cddd80c96f62f907c202037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Esp=C3=ADn?= Date: Tue, 22 Aug 2023 21:29:17 +0200 Subject: [PATCH] chore: Simplify how the default theme is provided (#287) --- components/src/theme.rs | 8 ++------ hooks/src/use_theme.rs | 13 +++++++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/components/src/theme.rs b/components/src/theme.rs index 47a72353a..c10f0631e 100644 --- a/components/src/theme.rs +++ b/components/src/theme.rs @@ -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)] @@ -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) } diff --git a/hooks/src/use_theme.rs b/hooks/src/use_theme.rs index dad80b496..b4ddb0d37 100644 --- a/hooks/src/use_theme.rs +++ b/hooks/src/use_theme.rs @@ -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. @@ -23,7 +22,7 @@ pub fn use_theme(cx: &ScopeState) -> &UseSharedState { pub fn use_get_theme(cx: &ScopeState) -> Theme { use_shared_state::(cx) .map(|v| v.read().clone()) - .unwrap_or(LIGHT_THEME) + .unwrap_or_default() } /// Theming properties for DropdownItem components. @@ -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",