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
use dioxus::prelude::*;
use std::{fmt::Display, str::FromStr};

/// This trait can be derived using the `#[derive(Slidable)]` macro.
pub trait Slidable: FromStr + Display + Clone + 'static {
    fn render<'a>(&self, cx: &'a ScopeState) -> Element<'a>;
    fn next(&self) -> Option<Self>;
    fn prev(&self) -> Option<Self>;
    fn slide_number(&self) -> usize;
    fn number_of_slides(&self) -> usize;
}

#[derive(Props)]
pub struct SlideProps<'a> {
    content: Element<'a>,
}

pub fn Slide<'a, S: Slidable + Clone + 'static>(cx: Scope<'a, SlideProps<'a>>) -> Element<'a> {
    let deck = use_shared_state::<S>(cx).expect("Failed to get shared state");

    let prev = deck.read().prev();
    let next = deck.read().next();

    cx.render(rsx! {
        div {
            style: "position: relative; min-height: 100vh; min-width: 100vw;",
            div {
                style: "z-index: 0; height: 100%; width: 100%; position: absolute; top: 0; left: 0;",
                cx.props.content.clone()
            }
            if let Some(prev) = prev.clone() {
                render! {
                    div {
                        // show on the left side of the screen for 20% of the screen
                        style: "z-index: 10; height: 100%; width: 20%; position: absolute; top: 0; left: 0;",
                        onclick: move |_| {
                            let mut deck = deck.write();
                            *deck = prev.clone();
                        }
                    }
                }
            }
            if let Some(next) = next.clone() {
                render! {
                    div {
                        style: "z-index: 10; height: 100%; width: 20%; position: absolute; top: 0; left: 80%;",
                        onclick: move |_| {
                            let mut deck = deck.write();
                            *deck = next.clone();
                        }
                    }
                }
            }
        }
    })
}