rustyle_css/components/
table.rs

1//! Table component styles
2//!
3//! Provides type-safe table styling with variants and sizes.
4
5use super::{ComponentStyle, Size, Variant};
6use crate::css::{Color, Spacing};
7use crate::tokens::{BorderTokens, ColorTokens, SpacingTokens};
8
9/// Table style configuration
10#[derive(Clone, Debug)]
11pub struct TableStyle {
12    pub variant: Variant,
13    pub size: Size,
14    pub tokens: Option<TableTokens>,
15}
16
17/// Table design tokens
18#[derive(Clone, Debug)]
19pub struct TableTokens {
20    pub colors: ColorTokens,
21    pub spacing: SpacingTokens,
22    pub borders: BorderTokens,
23}
24
25impl TableStyle {
26    /// Create a new table style
27    pub fn new(variant: Variant, size: Size) -> Self {
28        Self {
29            variant,
30            size,
31            tokens: None,
32        }
33    }
34
35    /// Set custom tokens
36    pub fn tokens(mut self, tokens: TableTokens) -> Self {
37        self.tokens = Some(tokens);
38        self
39    }
40
41    fn border_color(&self) -> Color {
42        let default_colors = ColorTokens::default();
43        let colors = self
44            .tokens
45            .as_ref()
46            .map(|t| &t.colors)
47            .unwrap_or(&default_colors);
48
49        colors.secondary.c300.clone()
50    }
51
52    fn header_background(&self) -> Color {
53        let default_colors = ColorTokens::default();
54        let colors = self
55            .tokens
56            .as_ref()
57            .map(|t| &t.colors)
58            .unwrap_or(&default_colors);
59
60        match &self.variant {
61            Variant::Primary => colors.primary.c50.clone(),
62            Variant::Secondary => colors.secondary.c50.clone(),
63            _ => colors.background.elevated.clone(),
64        }
65    }
66
67    fn padding(&self) -> Spacing {
68        let default_spacing = SpacingTokens::default();
69        let spacing = self
70            .tokens
71            .as_ref()
72            .map(|t| &t.spacing)
73            .unwrap_or(&default_spacing);
74
75        match &self.size {
76            Size::Small => Spacing::vh(spacing.xs.clone(), spacing.sm.clone()),
77            Size::Medium => Spacing::vh(spacing.sm.clone(), spacing.md.clone()),
78            Size::Large => Spacing::vh(spacing.md.clone(), spacing.lg.clone()),
79            Size::ExtraLarge => Spacing::vh(spacing.lg.clone(), spacing.xl.clone()),
80        }
81    }
82}
83
84impl ComponentStyle for TableStyle {
85    fn to_css(&self) -> String {
86        let mut css = String::new();
87
88        css.push_str("width: 100%; ");
89        css.push_str("border-collapse: collapse; ");
90        css.push_str(&format!(
91            "border: 1px solid {}; ",
92            self.border_color().to_css()
93        ));
94
95        // Add header styling
96        let default_colors = ColorTokens::default();
97        let colors = self
98            .tokens
99            .as_ref()
100            .map(|t| &t.colors)
101            .unwrap_or(&default_colors);
102
103        let header_bg = match &self.variant {
104            Variant::Primary => colors.primary.c50.clone(),
105            Variant::Secondary => colors.secondary.c50.clone(),
106            _ => colors.background.elevated.clone(),
107        };
108
109        css.push_str(&format!(
110            "thead {{ background-color: {}; }} ",
111            header_bg.to_css()
112        ));
113
114        // Add cell padding
115        let default_spacing = SpacingTokens::default();
116        let spacing = self
117            .tokens
118            .as_ref()
119            .map(|t| &t.spacing)
120            .unwrap_or(&default_spacing);
121
122        let cell_padding = match &self.size {
123            Size::Small => spacing.xs.clone(),
124            Size::Medium => spacing.sm.clone(),
125            Size::Large => spacing.md.clone(),
126            Size::ExtraLarge => spacing.lg.clone(),
127        };
128
129        css.push_str(&format!(
130            "td, th {{ padding: {}; }} ",
131            cell_padding.to_css()
132        ));
133
134        css
135    }
136
137    fn class_name(&self) -> &str {
138        "rustyle-table"
139    }
140}
141
142impl Default for TableStyle {
143    fn default() -> Self {
144        Self::new(Variant::Primary, Size::Medium)
145    }
146}