Skip to main content

orbital_base_components/spacing/
inset.rs

1use super::{SpacingHorizontal, SpacingVertical};
2
3/// Theme-aware padding or margin on all sides using Orbital spacing tokens.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum SpacingInset {
6    /// Same horizontal and vertical theme token (e.g. card body padding).
7    Symmetric {
8        horizontal: SpacingHorizontal,
9        vertical: SpacingVertical,
10    },
11    /// Fixed pixel inset on all sides (escape hatch; prefer theme tokens).
12    UniformPx(u16),
13}
14
15impl SpacingInset {
16    pub fn symmetric(horizontal: SpacingHorizontal, vertical: SpacingVertical) -> Self {
17        Self::Symmetric {
18            horizontal,
19            vertical,
20        }
21    }
22
23    pub fn all_m() -> Self {
24        Self::Symmetric {
25            horizontal: SpacingHorizontal::M,
26            vertical: SpacingVertical::M,
27        }
28    }
29
30    pub fn all_l() -> Self {
31        Self::Symmetric {
32            horizontal: SpacingHorizontal::L,
33            vertical: SpacingVertical::L,
34        }
35    }
36
37    pub fn uniform_px(px: u16) -> Self {
38        Self::UniformPx(px)
39    }
40
41    pub fn padding_css(self) -> String {
42        self.box_css("padding")
43    }
44
45    pub fn margin_css(self) -> String {
46        self.box_css("margin")
47    }
48
49    fn box_css(self, property: &str) -> String {
50        match self {
51            Self::Symmetric {
52                horizontal,
53                vertical,
54            } => format!(
55                "{property}: {} {};",
56                vertical.css_var(),
57                horizontal.css_var()
58            ),
59            Self::UniformPx(px) => format!("{property}: {px}px;"),
60        }
61    }
62}