Skip to main content

leptos_css/
sizing.rs

1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::{
5    CssDimension, CssDimensionExpr, LengthPercentageCalculation, NonNegativeLengthPercentage,
6    NonNegativeLengthPercentageValue, serialization::CssWriteTo,
7};
8
9/// Values supported by the typed `width`, `height`, and minimum-size property APIs.
10///
11/// The `stretch`, bare `fit-content`, and `contain` keywords extend the CSS Sizing Level 3
12/// grammar as defined by CSS Sizing Level 4.
13///
14/// [CSS Sizing Level 3]: https://www.w3.org/TR/css-sizing-3/#sizing-values
15/// [CSS Sizing Level 4]: https://www.w3.org/TR/css-sizing-4/#sizing-values
16#[derive(Clone, Debug, PartialEq)]
17#[non_exhaustive]
18pub enum Size {
19    /// The `auto` keyword.
20    Auto,
21    /// A non-negative length or percentage.
22    LengthPercentage(NonNegativeLengthPercentage),
23    /// A typed length-percentage expression.
24    Calculation(LengthPercentageCalculation),
25    /// The `min-content` keyword.
26    MinContent,
27    /// The `max-content` keyword.
28    MaxContent,
29    /// The bare `fit-content` keyword.
30    FitContent,
31    /// The `fit-content()` function with a non-negative argument.
32    FitContentFunction(NonNegativeLengthPercentageValue),
33    /// The `stretch` keyword.
34    Stretch,
35    /// The `contain` keyword.
36    Contain,
37}
38
39impl From<NonNegativeLengthPercentage> for Size {
40    fn from(value: NonNegativeLengthPercentage) -> Self {
41        Self::LengthPercentage(value)
42    }
43}
44
45impl Size {
46    /// Construct `fit-content()` with a directly non-negative argument.
47    pub fn fit_content(value: CssDimension) -> Self {
48        Self::FitContentFunction(NonNegativeLengthPercentage::new(value).into())
49    }
50
51    /// Construct `fit-content()` with a typed calculation.
52    pub fn fit_content_calculated(expression: impl Into<CssDimensionExpr>) -> Self {
53        Self::FitContentFunction(LengthPercentageCalculation::new(expression).into())
54    }
55}
56
57impl CssWriteTo for Size {
58    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
59        match self {
60            Self::Auto => w.write_str("auto"),
61            Self::LengthPercentage(value) => value.css_fmt(w),
62            Self::Calculation(value) => value.css_fmt(w),
63            Self::MinContent => w.write_str("min-content"),
64            Self::MaxContent => w.write_str("max-content"),
65            Self::FitContent => w.write_str("fit-content"),
66            Self::FitContentFunction(value) => {
67                w.write_str("fit-content(")?;
68                value.css_fmt(w)?;
69                w.write_char(')')
70            }
71            Self::Stretch => w.write_str("stretch"),
72            Self::Contain => w.write_str("contain"),
73        }
74    }
75}
76
77/// Values supported by the typed maximum-size property APIs.
78///
79/// The `stretch`, bare `fit-content`, and `contain` keywords extend the CSS Sizing Level 3
80/// grammar as defined by CSS Sizing Level 4.
81///
82/// [CSS Sizing Level 3]: https://www.w3.org/TR/css-sizing-3/#sizing-values
83/// [CSS Sizing Level 4]: https://www.w3.org/TR/css-sizing-4/#sizing-values
84#[derive(Clone, Debug, PartialEq)]
85#[non_exhaustive]
86pub enum MaxSize {
87    /// The `none` keyword.
88    None,
89    /// A non-negative length or percentage.
90    LengthPercentage(NonNegativeLengthPercentage),
91    /// A typed length-percentage expression.
92    Calculation(LengthPercentageCalculation),
93    /// The `min-content` keyword.
94    MinContent,
95    /// The `max-content` keyword.
96    MaxContent,
97    /// The bare `fit-content` keyword.
98    FitContent,
99    /// The `fit-content()` function with a non-negative argument.
100    FitContentFunction(NonNegativeLengthPercentageValue),
101    /// The `stretch` keyword.
102    Stretch,
103    /// The `contain` keyword.
104    Contain,
105}
106
107impl From<NonNegativeLengthPercentage> for MaxSize {
108    fn from(value: NonNegativeLengthPercentage) -> Self {
109        Self::LengthPercentage(value)
110    }
111}
112
113impl MaxSize {
114    /// Construct `fit-content()` with a directly non-negative argument.
115    pub fn fit_content(value: CssDimension) -> Self {
116        Self::FitContentFunction(NonNegativeLengthPercentage::new(value).into())
117    }
118
119    /// Construct `fit-content()` with a typed calculation.
120    pub fn fit_content_calculated(expression: impl Into<CssDimensionExpr>) -> Self {
121        Self::FitContentFunction(LengthPercentageCalculation::new(expression).into())
122    }
123}
124
125impl CssWriteTo for MaxSize {
126    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
127        match self {
128            Self::None => w.write_str("none"),
129            Self::LengthPercentage(value) => value.css_fmt(w),
130            Self::Calculation(value) => value.css_fmt(w),
131            Self::MinContent => w.write_str("min-content"),
132            Self::MaxContent => w.write_str("max-content"),
133            Self::FitContent => w.write_str("fit-content"),
134            Self::FitContentFunction(value) => {
135                w.write_str("fit-content(")?;
136                value.css_fmt(w)?;
137                w.write_char(')')
138            }
139            Self::Stretch => w.write_str("stretch"),
140            Self::Contain => w.write_str("contain"),
141        }
142    }
143}
144
145impl_checked_value!(Size, MaxSize);