tailwind_css_fixes/systems/units/anchor/
mod.rs

1use super::*;
2
3/// Anchor points are used to position elements relative to the viewport.
4#[derive(Clone, Debug)]
5pub enum AnchorPoint {
6    /// `0% 0%` to the viewport.
7    LeftTop,
8    /// `50% 0%` to the viewport.
9    Top,
10    /// `100% 0%` to the viewport.
11    RightTop,
12    /// `0% 50%` to the viewport.
13    Left,
14    /// `50% 50%` to the viewport.
15    Center,
16    /// `100% 50%` to the viewport.
17    Right,
18    /// `0% 100%` to the viewport.
19    LeftBottom,
20    /// `50% 100%` to the viewport.
21    Bottom,
22    /// `100% 100%` to the viewport.
23    RightBottom,
24    Standard(String),
25    Arbitrary(TailwindArbitrary),
26}
27
28impl AnchorPoint {
29    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary, allow_center: bool) -> Result<Self> {
30        let out = match pattern {
31            ["7" | "tl" | "lt"] | ["left", "top"] | ["top", "left"] => Self::LeftTop,
32            ["8" | "t"] | ["top"] => Self::Top,
33            ["9" | "rt" | "tr"] | ["right", "top"] | ["top", "right"] => Self::RightTop,
34            ["4" | "l"] | ["left"] => Self::Left,
35            ["5" | "c"] | ["center"] if allow_center => Self::Center,
36            ["6" | "r"] | ["right"] => Self::Right,
37            ["1" | "lb" | "bl"] | ["left", "bottom"] | ["bottom", "left"] => Self::LeftBottom,
38            ["2" | "b"] | ["bottom"] => Self::Bottom,
39            ["3" | "rb" | "br"] | ["right", "bottom"] | ["bottom", "right"] => Self::RightBottom,
40            [n] if Self::check_valid(n) => Self::Standard(n.to_string()),
41            [] => Self::parse_arbitrary(arbitrary)?,
42            _ => return syntax_error!("Unknown anchor-point instructions: {}", pattern.join("-")),
43        };
44        Ok(out)
45    }
46    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
47        Ok(Self::Arbitrary(TailwindArbitrary::new(arbitrary)?))
48    }
49
50    pub fn get_class(&self) -> String {
51        match self {
52            Self::LeftTop => "7".to_string(),
53            Self::Top => "8".to_string(),
54            Self::RightTop => "9".to_string(),
55            Self::Left => "4".to_string(),
56            Self::Center => "5".to_string(),
57            Self::Right => "6".to_string(),
58            Self::LeftBottom => "1".to_string(),
59            Self::Bottom => "2".to_string(),
60            Self::RightBottom => "3".to_string(),
61            Self::Standard(s) => s.to_string(),
62            Self::Arbitrary(s) => s.get_class(),
63        }
64    }
65
66    pub fn get_properties(&self) -> String {
67        match self {
68            Self::LeftTop => "0% 0%".to_string(),
69            Self::Top => "50% 0%".to_string(),
70            Self::RightTop => "100% 0%".to_string(),
71            Self::Left => "0% 50%".to_string(),
72            Self::Center => "50% 50%".to_string(),
73            Self::Right => "100% 50%".to_string(),
74            Self::LeftBottom => "0% 100%".to_string(),
75            Self::Bottom => "50% 100%".to_string(),
76            Self::RightBottom => "100% 100%".to_string(),
77            Self::Standard(s) => s.to_string(),
78            Self::Arbitrary(s) => s.get_properties(),
79        }
80    }
81    pub fn check_valid(mode: &str) -> bool {
82        ["inherit", "initial", "revert", "unset"].contains(&mode)
83    }
84}