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

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

#[derive(Props, Clone, PartialEq)]
pub struct ScrollBarProps {
    /// Theme override.
    pub theme: Option<ScrollBarThemeWith>,
    pub children: Element,
    #[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(
    ScrollBarProps {
        width,
        height,
        clicking_scrollbar,
        offset_x,
        offset_y,
        theme,
        children,
    }: ScrollBarProps,
) -> Element {
    let mut status = use_signal(|| ScrollBarStatus::Idle);
    let ScrollBarTheme { background, .. } = use_applied_theme!(&theme, scroll_bar);

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

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

    rsx!(
        rect {
            overflow: "clip",
            role: "scrollBar",
            width: "{width}",
            height: "{height}",
            offset_x: "{offset_x}",
            offset_y: "{offset_y}",
            background: "{background}",
            onmouseenter,
            onmouseleave,
            {children}
        }
    )
}