tailwind_rs_core/utilities/typography/
text_alignment.rs

1//! Text alignment utilities
2//!
3//! This module provides text alignment utilities for typography.
4
5use crate::classes::ClassBuilder;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// Text alignment values
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum TextAlign {
12    /// Left align
13    Left,
14    /// Center align
15    Center,
16    /// Right align
17    Right,
18    /// Justify align
19    Justify,
20    /// Start align (left in LTR, right in RTL)
21    Start,
22    /// End align (right in LTR, left in RTL)
23    End,
24}
25
26impl TextAlign {
27    pub fn to_class_name(&self) -> String {
28        match self {
29            TextAlign::Left => "left".to_string(),
30            TextAlign::Center => "center".to_string(),
31            TextAlign::Right => "right".to_string(),
32            TextAlign::Justify => "justify".to_string(),
33            TextAlign::Start => "start".to_string(),
34            TextAlign::End => "end".to_string(),
35        }
36    }
37
38    pub fn to_css_value(&self) -> String {
39        match self {
40            TextAlign::Left => "left".to_string(),
41            TextAlign::Center => "center".to_string(),
42            TextAlign::Right => "right".to_string(),
43            TextAlign::Justify => "justify".to_string(),
44            TextAlign::Start => "start".to_string(),
45            TextAlign::End => "end".to_string(),
46        }
47    }
48}
49
50impl fmt::Display for TextAlign {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "text-{}", self.to_class_name())
53    }
54}
55
56/// Trait for text alignment utilities
57pub trait TextAlignUtilities {
58    fn text_align(&mut self, align: TextAlign) -> &mut Self;
59    fn text_left(&mut self) -> &mut Self;
60    fn text_center(&mut self) -> &mut Self;
61    fn text_right(&mut self) -> &mut Self;
62    fn text_justify(&mut self) -> &mut Self;
63    fn text_start(&mut self) -> &mut Self;
64    fn text_end(&mut self) -> &mut Self;
65}
66
67impl TextAlignUtilities for ClassBuilder {
68    fn text_align(&mut self, align: TextAlign) -> &mut Self {
69        *self = self
70            .clone()
71            .class(format!("text-{}", align.to_class_name()));
72        self
73    }
74
75    fn text_left(&mut self) -> &mut Self {
76        self.text_align(TextAlign::Left)
77    }
78
79    fn text_center(&mut self) -> &mut Self {
80        self.text_align(TextAlign::Center)
81    }
82
83    fn text_right(&mut self) -> &mut Self {
84        self.text_align(TextAlign::Right)
85    }
86
87    fn text_justify(&mut self) -> &mut Self {
88        self.text_align(TextAlign::Justify)
89    }
90
91    fn text_start(&mut self) -> &mut Self {
92        self.text_align(TextAlign::Start)
93    }
94
95    fn text_end(&mut self) -> &mut Self {
96        self.text_align(TextAlign::End)
97    }
98}