cabin_tailwind/utilities/
space.rs

1//! Add space between child elements.
2
3use std::fmt;
4
5pub use x::{unit as x, unitf as xf};
6pub use y::{unit as y, unitf as yf};
7
8use crate::{Length, Utility};
9
10pub struct SpaceX(pub Length);
11pub struct SpaceReverseX;
12pub struct SpaceY(pub Length);
13pub struct SpaceReverseY;
14
15pub mod x {
16    use super::*;
17
18    pub const ZERO: SpaceX = SpaceX(Length::Px(0.0));
19    pub const PX: SpaceX = SpaceX(Length::Px(1.0));
20
21    pub const REVERSE: SpaceReverseX = SpaceReverseX;
22
23    /// Multiple of `0.25rem` (`4px` by default):
24    pub fn unit(x: i16) -> SpaceX {
25        SpaceX(Length::Rem(f32::from(x) * 0.25))
26    }
27
28    /// Multiple of `0.25rem` (`4px` by default):
29    pub fn unitf(x: f32) -> SpaceX {
30        SpaceX(Length::Rem(x * 0.25))
31    }
32
33    pub fn rem(x: i16) -> SpaceX {
34        SpaceX(Length::Rem(f32::from(x)))
35    }
36
37    pub fn remf(x: f32) -> SpaceX {
38        SpaceX(Length::Rem(x))
39    }
40
41    pub fn px(x: i16) -> SpaceX {
42        SpaceX(Length::Px(f32::from(x)))
43    }
44
45    pub fn pxf(x: f32) -> SpaceX {
46        SpaceX(Length::Px(x))
47    }
48}
49
50pub mod y {
51    use super::*;
52
53    pub const ZERO: SpaceY = SpaceY(Length::Px(0.0));
54    pub const PX: SpaceY = SpaceY(Length::Px(1.0));
55
56    pub const REVERSE: SpaceReverseY = SpaceReverseY;
57
58    /// Multiple of `0.25rem` (`4px` by default):
59    pub fn unit(x: i16) -> SpaceY {
60        SpaceY(Length::Rem(f32::from(x) * 0.25))
61    }
62
63    /// Multiple of `0.25rem` (`4px` by default):
64    pub fn unitf(x: f32) -> SpaceY {
65        SpaceY(Length::Rem(x * 0.25))
66    }
67
68    pub fn rem(x: i16) -> SpaceY {
69        SpaceY(Length::Rem(f32::from(x)))
70    }
71
72    pub fn remf(x: f32) -> SpaceY {
73        SpaceY(Length::Rem(x))
74    }
75
76    pub fn px(x: i16) -> SpaceY {
77        SpaceY(Length::Px(f32::from(x)))
78    }
79
80    pub fn pxf(x: f32) -> SpaceY {
81        SpaceY(Length::Px(x))
82    }
83}
84
85impl Utility for SpaceX {
86    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
87        f.write_str("--tw-space-x-reverse: 0;")?;
88        write!(
89            f,
90            "margin-inline-end: calc({} * var(--tw-space-x-reverse));",
91            self.0
92        )?;
93        write!(
94            f,
95            "margin-inline-start: calc({} * calc(1 - var(--tw-space-x-reverse)));",
96            self.0
97        )?;
98        Ok(())
99    }
100
101    fn selector_suffix(&self, f: &mut dyn fmt::Write) -> fmt::Result {
102        f.write_str(" > :not([hidden]) ~ :not([hidden])")
103    }
104
105    fn hash_modifier(&self, hasher: &mut dyn std::hash::Hasher) {
106        hasher.write(b" > :not([hidden]) ~ :not([hidden])");
107    }
108}
109
110impl Utility for SpaceReverseX {
111    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
112        f.write_str("--tw-space-x-reverse: 1;")
113    }
114
115    fn selector_suffix(&self, f: &mut dyn fmt::Write) -> fmt::Result {
116        f.write_str(" > :not([hidden]) ~ :not([hidden])")
117    }
118
119    fn hash_modifier(&self, hasher: &mut dyn std::hash::Hasher) {
120        hasher.write(b" > :not([hidden]) ~ :not([hidden])");
121    }
122}
123
124impl Utility for SpaceY {
125    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
126        f.write_str("--tw-space-y-reverse: 0;")?;
127        write!(
128            f,
129            "margin-top: calc({} * calc(1 - var(--tw-space-y-reverse)));",
130            self.0
131        )?;
132        write!(
133            f,
134            "margin-bottom: calc({} * var(--tw-space-y-reverse));",
135            self.0
136        )?;
137        Ok(())
138    }
139
140    fn selector_suffix(&self, f: &mut dyn fmt::Write) -> fmt::Result {
141        f.write_str(" > :not([hidden]) ~ :not([hidden])")
142    }
143
144    fn hash_modifier(&self, hasher: &mut dyn std::hash::Hasher) {
145        hasher.write(b" > :not([hidden]) ~ :not([hidden])");
146    }
147}
148
149impl Utility for SpaceReverseY {
150    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
151        f.write_str("--tw-space-y-reverse: 1;")
152    }
153
154    fn selector_suffix(&self, f: &mut dyn fmt::Write) -> fmt::Result {
155        f.write_str(" > :not([hidden]) ~ :not([hidden])")
156    }
157
158    fn hash_modifier(&self, hasher: &mut dyn std::hash::Hasher) {
159        hasher.write(b" > :not([hidden]) ~ :not([hidden])");
160    }
161}