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" => format!("{}rem", 1.0),
33                "tight" => format!("{}rem", 1.25),
34                "snug" => format!("{}rem", 1.375),
35                "relaxed" => format!("{}rem", 1.625),
36                "loose" => format!("{}rem", 2.0),
37                // Standard CSS keyword
38                "normal" => "normal".to_string(),
39                _ => format!("{}rem", 1.5), // Default
40            },
41            // For UnitValue, use get_properties_rem to convert numbers to rem.
42            _ => self.kind.get_properties_rem(),
43        };
44
45        css_attributes! {
46            "line-height" => line_height
47        }
48    }
49}
50
51impl TailwindLeading {
52    /// https://v3.tailwindcss.com/docs/line-height
53    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
54        Ok(Self {
55            kind: UnitValue::positive_parser(
56                "leading",
57                Self::check_valid,
58                true,  
59                true,  
60                false, 
61            )(pattern, arbitrary)?,
62        })
63    }
64
65    /// Checks for valid line-height keywords
66    pub fn check_valid(mode: &str) -> bool {
67        // "default" is from https://developer.mozilla.org/en-US/docs/Web/CSS/line-height#normal
68        let set = BTreeSet::from_iter([
69            "none", "tight", "snug", "normal", "relaxed", "loose", "default",
70        ]);
71        set.contains(mode)
72    }
73}