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
use leptos_reactive::Scope;

pub enum Class {
    Value(bool),
    Fn(Box<dyn Fn() -> bool>),
}

pub trait IntoClass {
    fn into_class(self, cx: Scope) -> Class;
}

impl IntoClass for bool {
    fn into_class(self, _cx: Scope) -> Class {
        Class::Value(self)
    }
}

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

impl Class {
    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 {
                    ""
                }
            }
        }
    }
}