use crate::*;
pub(crate) fn use_theme(mobile_signal: Signal<bool>) -> ThemeState {
let theme: Signal<String> = use_signal(|| "light".to_string());
let initial_mobile: bool = mobile_signal.get();
let initial_root: &'static str = if initial_mobile {
c_mobile_app_root().get_name()
} else {
c_app_root().get_name()
};
let root_class: Signal<String> =
use_signal(|| format!("{} {}", initial_root, theme_class_name("light")));
watch!(mobile_signal, theme, |mobile, theme_value| {
let root: &'static str = if mobile {
c_mobile_app_root().get_name()
} else {
c_app_root().get_name()
};
root_class.set(format!("{} {}", root, theme_class_name(&theme_value)));
});
ThemeState { theme, root_class }
}
pub(crate) fn toggle_theme(theme_signal: Signal<String>) -> NativeEventHandler {
NativeEventHandler::create(NativeEventName::Click, move |_event: Event| {
let current: String = theme_signal.get();
if current == "light" {
theme_signal.set("dark".to_string());
} else {
theme_signal.set("light".to_string());
}
})
}
pub(crate) fn theme_class_name(theme: &str) -> &'static str {
if theme == "dark" {
c_theme_dark().get_name()
} else {
c_theme_light().get_name()
}
}