1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#[cfg(not(feature = "nightly"))]
use leptos_reactive::{
    MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet,
};

/// Represents the different possible values a single class on an element could have,
/// allowing you to do fine-grained updates to single items
/// in [`Element.classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).
///
/// This mostly exists for the [`view`](https://docs.rs/leptos_macro/latest/leptos_macro/macro.view.html)
/// macro’s use. You usually won't need to interact with it directly, but it can be useful for defining
/// permissive APIs for certain components.
pub enum Class {
    /// Whether the class is present.
    Value(bool),
    /// A (presumably reactive) function, which will be run inside an effect to toggle the class.
    Fn(Box<dyn Fn() -> bool>),
}

/// Converts some type into a [`Class`].
pub trait IntoClass {
    /// Converts the object into a [`Class`].
    fn into_class(self) -> Class;

    /// Helper function for dealing with `Box<dyn IntoClass>`.
    fn into_class_boxed(self: Box<Self>) -> Class;
}

impl IntoClass for bool {
    #[inline(always)]
    fn into_class(self) -> Class {
        Class::Value(self)
    }

    fn into_class_boxed(self: Box<Self>) -> Class {
        (*self).into_class()
    }
}

impl<T> IntoClass for T
where
    T: Fn() -> bool + 'static,
{
    #[inline(always)]
    fn into_class(self) -> Class {
        let modified_fn = Box::new(self);
        Class::Fn(modified_fn)
    }

    fn into_class_boxed(self: Box<Self>) -> Class {
        (*self).into_class()
    }
}

impl Class {
    /// Converts the class to its HTML value at that moment so it can be rendered on the server.
    pub fn as_value_string(&self, class_name: &'static str) -> &'static str {
        match self {
            Class::Value(value) => {
                if *value {
                    class_name
                } else {
                    ""
                }
            }
            Class::Fn(f) => {
                let value = f();
                if value {
                    class_name
                } else {
                    ""
                }
            }
        }
    }
}

#[cfg(all(target_arch = "wasm32", feature = "web"))]
use leptos_reactive::Oco;

#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[doc(hidden)]
#[inline(never)]
pub fn class_helper(
    el: &web_sys::Element,
    name: Oco<'static, str>,
    value: Class,
) {
    use leptos_reactive::create_render_effect;

    let class_list = el.class_list();
    match value {
        Class::Fn(f) => {
            create_render_effect(move |old| {
                let new = f();
                if old.as_ref() != Some(&new) && (old.is_some() || new) {
                    class_expression(&class_list, &name, new, true)
                }
                new
            });
        }
        Class::Value(value) => {
            class_expression(&class_list, &name, value, false)
        }
    };
}

#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[inline(never)]
pub(crate) fn class_expression(
    class_list: &web_sys::DomTokenList,
    class_name: &str,
    value: bool,
    force: bool,
) {
    use crate::HydrationCtx;

    if force || !HydrationCtx::is_hydrating() {
        let class_name = wasm_bindgen::intern(class_name);

        if value {
            if let Err(e) = class_list.add_1(class_name) {
                crate::error!("[HtmlElement::class()] {e:?}");
            }
        } else {
            if let Err(e) = class_list.remove_1(class_name) {
                crate::error!("[HtmlElement::class()] {e:?}");
            }
        }
    }
}

macro_rules! class_signal_type {
    ($signal_type:ty) => {
        #[cfg(not(feature = "nightly"))]
        impl IntoClass for $signal_type {
            #[inline(always)]
            fn into_class(self) -> Class {
                let modified_fn = Box::new(move || self.get());
                Class::Fn(modified_fn)
            }

            fn into_class_boxed(self: Box<Self>) -> Class {
                (*self).into_class()
            }
        }
    };
}

macro_rules! class_signal_type_optional {
    ($signal_type:ty) => {
        #[cfg(not(feature = "nightly"))]
        impl IntoClass for $signal_type {
            #[inline(always)]
            fn into_class(self) -> Class {
                let modified_fn = Box::new(move || self.get().unwrap_or(false));
                Class::Fn(modified_fn)
            }

            fn into_class_boxed(self: Box<Self>) -> Class {
                (*self).into_class()
            }
        }
    };
}

class_signal_type!(ReadSignal<bool>);
class_signal_type!(RwSignal<bool>);
class_signal_type!(Memo<bool>);
class_signal_type!(Signal<bool>);
class_signal_type!(MaybeSignal<bool>);
class_signal_type_optional!(MaybeProp<bool>);