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
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_hooks::use_get_theme;

#[derive(Props)]
pub struct ScrollBarProps<'a> {
    children: Element<'a>,

    #[props(into)]
    width: String,

    #[props(into)]
    height: String,

    #[props(default = "0".to_string(), into)]
    offset_x: String,

    #[props(default = "0".to_string(), into)]
    offset_y: String,

    clicking_scrollbar: bool,
}

enum ScrollBarStatus {
    Idle,
    Hovering,
}

#[allow(non_snake_case)]
pub fn ScrollBar<'a>(cx: Scope<'a, ScrollBarProps<'a>>) -> Element<'a> {
    let status = use_state(cx, || ScrollBarStatus::Idle);
    let theme = use_get_theme(cx);

    let onmouseenter = |_| status.set(ScrollBarStatus::Hovering);

    let onmouseleave = |_| status.set(ScrollBarStatus::Idle);

    let background = match status.get() {
        _ if cx.props.clicking_scrollbar => theme.scrollbar.background,
        ScrollBarStatus::Hovering => theme.scrollbar.background,
        ScrollBarStatus::Idle => "transparent",
    };

    render!(
        rect {
            overflow: "clip",
            role: "scrollBar",
            width: "{cx.props.width}",
            height: "{cx.props.height}",
            offset_x: "{cx.props.offset_x}",
            offset_y: "{cx.props.offset_y}",
            background: "{background}",
            onmouseenter: onmouseenter,
            onmouseleave: onmouseleave,
            &cx.props.children
        }
    )
}