tailwind_css_fixes/modules/borders/ring/ring_width/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Clone, Debug)]
5pub struct TailwindRingWidth {
6    kind: UnitValue,
7}
8
9impl<T> From<T> for TailwindRingWidth
10where
11    T: Into<UnitValue>,
12{
13    fn from(kind: T) -> Self {
14        Self { kind: kind.into() }
15    }
16}
17
18/// Generates the correct class name, e.g., `ring-4` or `ring-[3px]`.
19impl Display for TailwindRingWidth {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        // Special case for the default `ring` class, which has no suffix.
22        if let UnitValue::Keyword(s) = &self.kind {
23            if s == "<DEFAULT>" {
24                return write!(f, "ring");
25            }
26        }
27        write!(f, "ring-{}", self.kind)
28    }
29}
30
31/// Generates the correct layered box-shadow CSS
32impl TailwindInstance for TailwindRingWidth {
33    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
34        // Resolve the UnitValue to a CSS value, defaulting to px.
35        let width = self.kind.get_properties(|f| format!("{}px", f));
36
37        css_attributes! {
38            "--tw-ring-offset-shadow" => "0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color)",
39            "--tw-ring-shadow" => format!("0 0 0 calc({} + var(--tw-ring-offset-width)) var(--tw-ring-color)", width),
40            "box-shadow" => "var(--tw-ring-inset) var(--tw-ring-offset-shadow), var(--tw-ring-inset) var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000)"
41        }
42    }
43}
44
45impl TailwindRingWidth {
46    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
47        let kind = UnitValue::positive_parser("ring", Self::check_valid, true, false, false)(pattern, arbitrary)?;
48        Ok(Self { kind })
49    }
50
51    /// Ring width doesn't have special keywords like medium/thick/thin, so we only
52    /// include the standard CSS ones for validation.
53    /// - https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit#syntax
54
55    pub fn check_valid(mode: &str) -> bool {
56        ["inherit", "initial", "revert", "unset"].contains(&mode)
57    }
58}