biji_ui/components/dialog/
context.rs

1use std::time::Duration;
2
3use leptos::{
4    html::{Button, Div},
5    prelude::*,
6};
7
8#[derive(Copy, Clone)]
9pub struct DialogContext {
10    pub trigger_ref: NodeRef<Button>,
11    pub open: RwSignal<bool>,
12    pub root: RwSignal<RootContext>,
13    pub prevent_scroll: bool,
14    pub hide_delay: Duration,
15}
16
17impl Default for DialogContext {
18    fn default() -> Self {
19        Self {
20            trigger_ref: NodeRef::default(),
21            open: RwSignal::new(false),
22            root: RwSignal::new(RootContext::default()),
23            prevent_scroll: true,
24            hide_delay: Duration::from_millis(200),
25        }
26    }
27}
28
29impl DialogContext {
30    pub fn open(&self) {
31        self.root.set(RootContext::default());
32        self.open.set(true);
33    }
34
35    pub fn close(&self) {
36        self.open.set(false);
37    }
38
39    pub fn toggle(&self) {
40        if self.open.get() {
41            self.close();
42        } else {
43            self.open();
44        }
45    }
46}
47
48#[derive(Copy, Clone)]
49pub struct RootContext {
50    pub close_ref: NodeRef<Button>,
51    pub overlay_ref: NodeRef<Div>,
52}
53
54impl Default for RootContext {
55    fn default() -> Self {
56        Self {
57            overlay_ref: NodeRef::default(),
58            close_ref: NodeRef::default(),
59        }
60    }
61}