Skip to main content

orbital_base_components/drawer/
sizes.rs

1use orbital_motion::{PresenceMotion, SlideFrom};
2
3#[derive(Clone, Copy, Default, PartialEq, Eq)]
4pub enum DrawerPosition {
5    Top,
6    Bottom,
7    #[default]
8    Left,
9    Right,
10}
11
12impl DrawerPosition {
13    pub fn as_str(&self) -> &'static str {
14        match self {
15            Self::Top => "top",
16            Self::Bottom => "bottom",
17            Self::Left => "left",
18            Self::Right => "right",
19        }
20    }
21}
22
23#[derive(Clone, Copy, Default, PartialEq, Eq)]
24pub enum DrawerSize {
25    #[default]
26    Small,
27    Medium,
28    Large,
29    Full,
30}
31
32impl DrawerSize {
33    pub fn as_str(&self) -> &'static str {
34        match self {
35            Self::Small => "small",
36            Self::Medium => "medium",
37            Self::Large => "large",
38            Self::Full => "full",
39        }
40    }
41}
42
43#[derive(Debug, Default, PartialEq, Eq)]
44pub enum DrawerModalType {
45    #[default]
46    Modal,
47    NonModal,
48}
49
50/// Slide preset for drawer panels from [`DrawerPosition`].
51pub const fn drawer_presence_motion(position: DrawerPosition) -> PresenceMotion {
52    PresenceMotion::slide(match position {
53        DrawerPosition::Top => SlideFrom::Top,
54        DrawerPosition::Bottom => SlideFrom::Bottom,
55        DrawerPosition::Left => SlideFrom::Left,
56        DrawerPosition::Right => SlideFrom::Right,
57    })
58}
59
60/// CSS dimension for drawer panel along the opening axis.
61pub fn drawer_size_css(size: DrawerSize, position: DrawerPosition) -> &'static str {
62    match size {
63        DrawerSize::Small => "320px",
64        DrawerSize::Medium => "592px",
65        DrawerSize::Large => "940px",
66        DrawerSize::Full => match position {
67            DrawerPosition::Top | DrawerPosition::Bottom => "100vh",
68            DrawerPosition::Left | DrawerPosition::Right => "100vw",
69        },
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn drawer_size_css_values() {
79        assert_eq!(
80            drawer_size_css(DrawerSize::Small, DrawerPosition::Left),
81            "320px"
82        );
83        assert_eq!(
84            drawer_size_css(DrawerSize::Full, DrawerPosition::Top),
85            "100vh"
86        );
87        assert_eq!(
88            drawer_size_css(DrawerSize::Full, DrawerPosition::Right),
89            "100vw"
90        );
91    }
92}