cabin_tailwind/utilities/
ring.rs

1//! Control the style of an element's outline (`outline-style`).
2//!
3//! <https://developer.mozilla.org/en-US/docs/Web/CSS/outline-style>
4
5use std::fmt;
6
7pub use offset::width as offset;
8
9use crate::{Length, Property, Utility};
10
11const BOX_SHADOW: &str = "box-shadow";
12
13pub struct RingColor<V = &'static str>(pub(crate) V);
14
15include!(concat!(env!("OUT_DIR"), "/ring-color.rs"));
16
17pub mod offset {
18    use super::*;
19
20    pub struct RingOffsetColor<V = &'static str>(pub(crate) V);
21
22    include!(concat!(env!("OUT_DIR"), "/ring-offset-color.rs"));
23
24    /// ```css
25    /// --tw-ring-offset-width: {x}px;
26    /// box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);
27    /// ```
28    pub fn width(x: i16) -> RingOffsetWidth {
29        RingOffsetWidth(Length::Px(f32::from(x)))
30    }
31
32    /// ```css
33    /// --tw-ring-offset-width: {x}px;
34    /// box-shadow: 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color), var(--tw-ring-shadow);
35    /// ```
36    pub fn widthf(x: f32) -> RingOffsetWidth {
37        RingOffsetWidth(Length::Px(x))
38    }
39
40    pub struct RingOffsetWidth(pub Length);
41
42    impl Utility for RingOffsetWidth {
43        fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
44            write!(f, "--tw-ring-offset-width: {};", self.0)?;
45            write!(
46                f,
47                "box-shadow: 0 0 0 var(--tw-ring-offset-width) \
48                var(--tw-ring-offset-color), var(--tw-ring-shadow);",
49            )?;
50            Ok(())
51        }
52    }
53
54    impl<V: fmt::Display> Utility for RingOffsetColor<V> {
55        fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
56            writeln!(f, "--tw-ring-offset-color: {};", self.0)?;
57            writeln!(
58                f,
59                "box-shadow: 0 0 0 var(--tw-ring-offset-width) \
60                 var(--tw-ring-offset-color), var(--tw-ring-shadow);"
61            )?;
62            Ok(())
63        }
64    }
65}
66
67/// Force a ring to render on the inside of an element instead of the outside.
68/// ```css
69/// --tw-ring-inset: inset;
70/// ```
71pub const INSET: Property = Property("--tw-ring-inset", "inset");
72
73pub const DEFAULT: Property<RingWidth> = Property(BOX_SHADOW, RingWidth(Length::Px(3.0)));
74
75/// ```css
76/// box-shadow: var(--tw-ring-inset) 0 0 0 calc({x}px + var(--tw-ring-offset-width)) var(--tw-ring-color);
77/// ```
78pub fn width(x: i16) -> RingWidth {
79    RingWidth(Length::Px(f32::from(x)))
80}
81
82/// ```css
83/// box-shadow: var(--tw-ring-inset) 0 0 0 calc({x}px + var(--tw-ring-offset-width)) var(--tw-ring-color);
84/// ```
85pub fn widthf(x: f32) -> RingWidth {
86    RingWidth(Length::Px(x))
87}
88
89pub struct RingWidth(pub Length);
90
91impl Utility for RingWidth {
92    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
93        write!(f, "--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);")?;
94        write!(f, "--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc({} + var(--tw-ring-offset-width)) var(--tw-ring-color);", self.0)?;
95        write!(f, "box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);")?;
96        Ok(())
97    }
98}
99
100impl<V: fmt::Display> Utility for RingColor<V> {
101    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
102        writeln!(f, "--tw-ring-color: {};", self.0)?;
103        Ok(())
104    }
105}