tailwind_css_fixes/modules/typography/leading/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Debug, Clone)]
5pub struct TailwindLeading {
6    kind: UnitValue,
7}
8
9// A convenience implementation to easily create instances.
10impl<T> From<T> for TailwindLeading
11where
12    T: Into<UnitValue>,
13{
14    fn from(kind: T) -> Self {
15        Self { kind: kind.into() }
16    }
17}
18
19
20impl Display for TailwindLeading {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        write!(f, "leading-{}", self.kind)
23    }
24}
25
26impl TailwindInstance for TailwindLeading {
27    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
28        // Map the keywords to their specific values.
29        let line_height = match &self.kind {
30            UnitValue::Keyword(k) => match k.as_str() {
31                // Tailwind v3 keywords that map to unitless values
32                "none" => "1.0",
33                "tight" => "1.25",
34                "snug" => "1.375",
35                "normal" => "1.5",
36                "relaxed" => "1.625",
37                "loose" => "2.0",
38                // Standard CSS keyword
39                "default" => "normal",
40                _ => "1.5", // Default
41            }.to_string(),
42            // For UnitValue, use get_properties_rem to convert numbers to rem.
43            _ => self.kind.get_properties_rem(),
44        };
45
46        css_attributes! {
47            "line-height" => line_height
48        }
49    }
50}
51
52impl TailwindLeading {
53    /// https://v3.tailwindcss.com/docs/line-height
54    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
55        Ok(Self {
56            kind: UnitValue::positive_parser(
57                "leading",
58                Self::check_valid,
59                true,  
60                true,  
61                false, 
62            )(pattern, arbitrary)?,
63        })
64    }
65
66    /// Checks for valid line-height keywords
67    pub fn check_valid(mode: &str) -> bool {
68        // "default" is from https://developer.mozilla.org/en-US/docs/Web/CSS/line-height#normal
69        let set = BTreeSet::from_iter([
70            "none", "tight", "snug", "normal", "relaxed", "loose", "default",
71        ]);
72        set.contains(mode)
73    }
74}