Skip to main content

leptos_css/
length.rs

1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::{FiniteF64, InvalidCssNumber, serialization::CssWriteTo};
5
6/// CSS length values with absolute and relative units.
7#[derive(Clone, Copy, Debug, PartialEq)]
8#[non_exhaustive]
9pub enum CssLength {
10    /// Pixels.
11    Px(FiniteF64),
12    /// Em units (relative to element font size).
13    Em(FiniteF64),
14    /// Rem units (relative to root font size).
15    Rem(FiniteF64),
16    /// Viewport width units.
17    Vw(FiniteF64),
18    /// Viewport height units.
19    Vh(FiniteF64),
20    /// Viewport minimum dimension units.
21    Vmin(FiniteF64),
22    /// Viewport maximum dimension units.
23    Vmax(FiniteF64),
24    /// Character width units (width of the `0` glyph in the element's font).
25    Ch(FiniteF64),
26    /// Dynamic viewport width units (1dvw = 1% of the dynamic viewport width).
27    /// Unlike `vw`, adjusts when browser UI elements (e.g. mobile address bar) appear or disappear.
28    Dvw(FiniteF64),
29    /// Dynamic viewport height units (1dvh = 1% of the dynamic viewport height).
30    /// Unlike `vh`, adjusts when browser UI elements (e.g. mobile address bar) appear or disappear.
31    Dvh(FiniteF64),
32    /// Small viewport width units (1svw = 1% of the smallest possible viewport width).
33    /// Represents the viewport size when all dynamic browser UI is expanded/visible.
34    Svw(FiniteF64),
35    /// Small viewport height units (1svh = 1% of the smallest possible viewport height).
36    /// Represents the viewport size when all dynamic browser UI is expanded/visible.
37    Svh(FiniteF64),
38    /// Large viewport width units (1lvw = 1% of the largest possible viewport width).
39    /// Represents the viewport size when all dynamic browser UI is retracted/hidden.
40    Lvw(FiniteF64),
41    /// Large viewport height units (1lvh = 1% of the largest possible viewport height).
42    /// Represents the viewport size when all dynamic browser UI is retracted/hidden.
43    Lvh(FiniteF64),
44    /// Container query width units (1cqw = 1% of the nearest size container's width).
45    /// Used with CSS container queries (`@container`).
46    Cqw(FiniteF64),
47    /// Container query height units (1cqh = 1% of the nearest size container's height).
48    /// Used with CSS container queries (`@container`).
49    Cqh(FiniteF64),
50}
51
52impl CssLength {
53    /// Returns the inner numeric value regardless of unit.
54    pub fn value(self) -> f64 {
55        match self {
56            Self::Px(v)
57            | Self::Em(v)
58            | Self::Rem(v)
59            | Self::Vw(v)
60            | Self::Vh(v)
61            | Self::Vmin(v)
62            | Self::Vmax(v)
63            | Self::Ch(v)
64            | Self::Dvw(v)
65            | Self::Dvh(v)
66            | Self::Svw(v)
67            | Self::Svh(v)
68            | Self::Lvw(v)
69            | Self::Lvh(v)
70            | Self::Cqw(v)
71            | Self::Cqh(v) => v.get(),
72        }
73    }
74
75    /// Attempt to transform the value while preserving its unit and the finite-value invariant.
76    ///
77    /// # Errors
78    ///
79    /// Returns [`InvalidCssNumber::NonFinite`] if `f` produces `NaN` or infinity.
80    pub fn try_map_value(self, f: impl FnOnce(f64) -> f64) -> Result<Self, InvalidCssNumber> {
81        let mapped = FiniteF64::try_new(f(self.value()))?;
82        Ok(match self {
83            Self::Px(_) => Self::Px(mapped),
84            Self::Em(_) => Self::Em(mapped),
85            Self::Rem(_) => Self::Rem(mapped),
86            Self::Vw(_) => Self::Vw(mapped),
87            Self::Vh(_) => Self::Vh(mapped),
88            Self::Vmin(_) => Self::Vmin(mapped),
89            Self::Vmax(_) => Self::Vmax(mapped),
90            Self::Ch(_) => Self::Ch(mapped),
91            Self::Dvw(_) => Self::Dvw(mapped),
92            Self::Dvh(_) => Self::Dvh(mapped),
93            Self::Svw(_) => Self::Svw(mapped),
94            Self::Svh(_) => Self::Svh(mapped),
95            Self::Lvw(_) => Self::Lvw(mapped),
96            Self::Lvh(_) => Self::Lvh(mapped),
97            Self::Cqw(_) => Self::Cqw(mapped),
98            Self::Cqh(_) => Self::Cqh(mapped),
99        })
100    }
101
102    /// Transform the value while preserving its unit.
103    ///
104    /// # Panics
105    ///
106    /// Panics if `f` produces `NaN` or infinity. Use [`CssLength::try_map_value`] when the
107    /// transformed value can be invalid at runtime.
108    #[must_use]
109    #[track_caller]
110    pub fn map_value(self, f: impl FnOnce(f64) -> f64) -> Self {
111        self.try_map_value(f)
112            .unwrap_or_else(|error| panic!("{error}"))
113    }
114}
115
116impl CssWriteTo for CssLength {
117    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
118        match self {
119            Self::Px(v) => write!(w, "{v}px"),
120            Self::Em(v) => write!(w, "{v}em"),
121            Self::Rem(v) => write!(w, "{v}rem"),
122            Self::Vw(v) => write!(w, "{v}vw"),
123            Self::Vh(v) => write!(w, "{v}vh"),
124            Self::Vmin(v) => write!(w, "{v}vmin"),
125            Self::Vmax(v) => write!(w, "{v}vmax"),
126            Self::Ch(v) => write!(w, "{v}ch"),
127            Self::Dvw(v) => write!(w, "{v}dvw"),
128            Self::Dvh(v) => write!(w, "{v}dvh"),
129            Self::Svw(v) => write!(w, "{v}svw"),
130            Self::Svh(v) => write!(w, "{v}svh"),
131            Self::Lvw(v) => write!(w, "{v}lvw"),
132            Self::Lvh(v) => write!(w, "{v}lvh"),
133            Self::Cqw(v) => write!(w, "{v}cqw"),
134            Self::Cqh(v) => write!(w, "{v}cqh"),
135        }
136    }
137}
138
139impl std::ops::Neg for CssLength {
140    type Output = Self;
141
142    fn neg(self) -> Self {
143        self.map_value(|v| -v)
144    }
145}
146
147impl_checked_value!(CssLength);