cabin_tailwind/utilities/
text.rs

1//! Utilities for controlling text layout, behaviour and color, including:
2//!
3//! - `color` <https://developer.mozilla.org/en-US/docs/Web/CSS/color>
4//! - `font-size` <https://developer.mozilla.org/en-US/docs/Web/CSS/font-size>
5//! - `line-height` <https://developer.mozilla.org/en-US/docs/Web/CSS/line-height>
6//! - `text-align` <https://developer.mozilla.org/en-US/docs/Web/CSS/text-align>
7
8use core::fmt;
9
10use crate::{Length, Property, Utility};
11
12const COLOR: &str = "color";
13
14include!(concat!(env!("OUT_DIR"), "/text-color.rs"));
15
16/// Set a custom foreground color.
17pub fn color(color: &'static str) -> Property {
18    Property(COLOR, color)
19}
20
21include!(concat!(env!("OUT_DIR"), "/font-size.rs"));
22
23pub struct FontSize {
24    font_size: Length,
25    line_height: LineHeight,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq)]
29pub enum LineHeight {
30    Length(Length),
31    Multiple(u16),
32}
33
34/// Set a custom `font-size` and `line-height`.
35pub const fn size(font_size: Length, line_height: LineHeight) -> FontSize {
36    FontSize {
37        font_size,
38        line_height,
39    }
40}
41
42impl Utility for FontSize {
43    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
44        writeln!(f, "font-size: {};", self.font_size)?;
45        writeln!(f, "line-height: {};", self.line_height)?;
46        Ok(())
47    }
48}
49
50impl fmt::Display for LineHeight {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            LineHeight::Length(l) => l.fmt(f),
54            LineHeight::Multiple(x) => write!(f, "{x}"),
55        }
56    }
57}
58
59const TEXT_ALIGN: &str = "text-align";
60
61/// ```css
62/// text-align: left;
63/// ```
64pub const LEFT: Property = Property(TEXT_ALIGN, "left");
65
66/// ```css
67/// text-align: center;
68/// ```
69pub const CENTER: Property = Property(TEXT_ALIGN, "center");
70
71/// ```css
72/// text-align: right;
73/// ```
74pub const RIGHT: Property = Property(TEXT_ALIGN, "right");
75
76/// ```css
77/// text-align: justify;
78/// ```
79pub const JUSTIFY: Property = Property(TEXT_ALIGN, "justify");
80
81/// ```css
82/// text-align: start;
83/// ```
84pub const START: Property = Property(TEXT_ALIGN, "start");
85
86/// ```css
87/// text-align: end;
88/// ```
89pub const END: Property = Property(TEXT_ALIGN, "end");
90
91const TEXT_TRANSFORM: &str = "text-transform";
92
93/// ```css
94/// text-transform: uppercase;
95/// ```
96pub const UPPERCASE: Property = Property(TEXT_TRANSFORM, "uppercase");
97
98/// ```css
99/// text-transform: lowercase;
100/// ```
101pub const LOWERCASE: Property = Property(TEXT_TRANSFORM, "lowercase");
102
103/// ```css
104/// text-transform: capitalize;
105/// ```
106pub const CAPITALIZE: Property = Property(TEXT_TRANSFORM, "capitalize");
107
108/// ```css
109/// text-transform: none;
110/// ```
111pub const NORMAL_CASE: Property = Property(TEXT_TRANSFORM, "none");
112
113const TEXT_OVERFLOW: &str = "text-overflow";
114
115/// ```css
116/// text-overflow: ellipsis;
117/// ```
118pub const ELLIPSIS: Property = Property(TEXT_OVERFLOW, "ellipsis");
119
120/// ```css
121/// text-overflow: clip;
122/// ```
123pub const CLIP: Property = Property(TEXT_OVERFLOW, "clip");
124
125/// ```css
126/// overflow: hidden;
127/// text-overflow: ellipsis;
128/// white-space: nowrap;
129/// ```
130pub const TRUNCATE: Truncate = Truncate;
131
132pub struct Truncate;
133
134impl Utility for Truncate {
135    fn declarations(&self, f: &mut dyn fmt::Write) -> fmt::Result {
136        f.write_str("overflow: hidden;")?;
137        f.write_str("text-overflow: ellipsis;")?;
138        f.write_str("white-space: nowrap;")?;
139        Ok(())
140    }
141}