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

use freya_hooks::{use_applied_theme, ScrollBarTheme, ScrollBarThemeWith};

#[derive(Props)]
pub struct ScrollBarProps<'a> {
    /// Theme override.
    pub theme: Option<ScrollBarThemeWith>,
    pub children: Element<'a>,
    #[props(into)]
    pub width: String,
    #[props(into)]
    pub height: String,
    #[props(default = "0".to_string(), into)]
    pub offset_x: String,
    #[props(default = "0".to_string(), into)]
    pub offset_y: String,
    pub 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 ScrollBarTheme { background, .. } = use_applied_theme!(cx, &cx.props.theme, scroll_bar);

    let ScrollBarProps {
        width,
        height,
        clicking_scrollbar,
        offset_x,
        offset_y,
        ..
    } = cx.props;

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

    let background = match status.get() {
        _ if *clicking_scrollbar => background.as_ref(),
        ScrollBarStatus::Hovering => background.as_ref(),
        ScrollBarStatus::Idle => "transparent",
    };

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