Skip to main content

ThemeState

Struct ThemeState 

Source
pub struct ThemeState {
    pub theme: Signal<String>,
    pub root_class: Signal<String>,
}
Expand description

Holds the reactive state for the application theme.

Provides the current theme signal and the derived root element class name signal that combines the app root class with the active theme class.

Fields§

§theme: Signal<String>

The current theme name signal (“light” or “dark”).

§root_class: Signal<String>

The derived root element class name signal combining the app root class and the active theme class.

Implementations§

Source§

impl ThemeState

Implementation of theme state functionality.

Provides methods for creating theme state, detecting system theme changes, and managing theme-related reactive state.

Source

pub fn use_theme_state(mobile_signal: Signal<bool>) -> ThemeState

Creates theme state for managing light/dark mode and root CSS classes.

Detects the system color scheme preference at startup and initializes the theme signal accordingly. Uses watch! to reactively update the root class whenever the theme or mobile signal changes.

§Arguments
  • Signal<bool> - The reactive signal indicating whether the viewport is mobile-sized.
§Returns
  • ThemeState - The reactive theme state containing the theme signal and root class signal.
Source

pub fn use_system_theme_change(theme_signal: Signal<String>)

Subscribes to system color scheme changes and updates the theme signal.

Creates a MediaQueryList for prefers-color-scheme: dark and listens for change events. When the system theme changes, the theme signal is updated accordingly. The listener is automatically cleaned up when the hook context is cleared.

§Arguments
  • Signal<String> - The theme signal to update when the system theme changes.
Source

pub fn detect_system_theme() -> String

Detects the current system color scheme preference.

Uses window.matchMedia("(prefers-color-scheme: dark)") to check whether the operating system is in dark mode. Falls back to light theme if the detection fails.

§Returns
  • String - The detected system theme name (“light” or “dark”).
Source

pub fn toggle(theme_signal: Signal<String>) -> Option<Rc<dyn Fn(Event)>>

Creates a click event handler that toggles the theme between “light” and “dark”.

The switch stays smooth even on pages with many elements (e.g. the list page) because individual components no longer carry their own colour (background / color / border-color) transitions — only the app root container animates its colour change, so the page fades as a whole while each component snaps to the new theme instantly. This avoids hundreds of elements running simultaneous colour transitions, which previously caused dropped frames.

§Arguments
  • Signal<String> - The theme signal to toggle.
§Returns
  • Option<Rc<dyn Fn(Event)>> - A click event handler that flips the theme value.
Source§

impl ThemeState

Source

pub fn get_theme(&self) -> Signal<String>

Source

pub fn get_mut_theme(&mut self) -> &mut Signal<String>

Source

pub fn set_theme(&mut self, val: Signal<String>) -> &mut Self

Source

pub fn get_root_class(&self) -> Signal<String>

Source

pub fn get_mut_root_class(&mut self) -> &mut Signal<String>

Source

pub fn set_root_class(&mut self, val: Signal<String>) -> &mut Self

Source§

impl ThemeState

Source

pub fn new(theme: Signal<String>, root_class: Signal<String>) -> Self

Trait Implementations§

Source§

impl Clone for ThemeState

Source§

fn clone(&self) -> ThemeState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ThemeState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more