rustyle_css/components/
tabs.rs

1//! Tabs component styles
2//!
3//! Provides type-safe tab styling with variants and sizes.
4
5use super::{ComponentStyle, Size, Variant};
6use crate::css::{Color, Radius, Spacing};
7use crate::tokens::{BorderTokens, ColorTokens, SpacingTokens};
8
9/// Tabs style configuration
10#[derive(Clone, Debug)]
11pub struct TabsStyle {
12    pub variant: Variant,
13    pub size: Size,
14    pub tokens: Option<TabsTokens>,
15}
16
17/// Tabs design tokens
18#[derive(Clone, Debug)]
19pub struct TabsTokens {
20    pub colors: ColorTokens,
21    pub spacing: SpacingTokens,
22    pub borders: BorderTokens,
23}
24
25impl TabsStyle {
26    /// Create a new tabs 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: TabsTokens) -> Self {
37        self.tokens = Some(tokens);
38        self
39    }
40
41    fn active_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        match &self.variant {
50            Variant::Primary => colors.primary.c600.clone(),
51            Variant::Secondary => colors.secondary.c600.clone(),
52            Variant::Success => colors.semantic.success.clone(),
53            Variant::Error => colors.semantic.error.clone(),
54            Variant::Warning => colors.semantic.warning.clone(),
55            Variant::Info => colors.semantic.info.clone(),
56        }
57    }
58
59    fn padding(&self) -> Spacing {
60        let default_spacing = SpacingTokens::default();
61        let spacing = self
62            .tokens
63            .as_ref()
64            .map(|t| &t.spacing)
65            .unwrap_or(&default_spacing);
66
67        match &self.size {
68            Size::Small => Spacing::vh(spacing.xs.clone(), spacing.sm.clone()),
69            Size::Medium => Spacing::vh(spacing.sm.clone(), spacing.md.clone()),
70            Size::Large => Spacing::vh(spacing.md.clone(), spacing.lg.clone()),
71            Size::ExtraLarge => Spacing::vh(spacing.lg.clone(), spacing.xl.clone()),
72        }
73    }
74}
75
76impl ComponentStyle for TabsStyle {
77    fn to_css(&self) -> String {
78        let mut css = String::new();
79
80        css.push_str("display: flex; ");
81        css.push_str("border-bottom: 2px solid #e5e7eb; ");
82
83        // Tab item styles
84        let padding = self.padding();
85        let active_color = self.active_color();
86
87        css.push_str(&format!(
88            ".rustyle-tab-item {{ padding: {}; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px; }} ",
89            padding.to_css()
90        ));
91
92        css.push_str(&format!(
93            ".rustyle-tab-item.active {{ border-bottom-color: {}; color: {}; }} ",
94            active_color.to_css(),
95            active_color.to_css()
96        ));
97
98        css
99    }
100
101    fn class_name(&self) -> &str {
102        "rustyle-tabs"
103    }
104}
105
106impl Default for TabsStyle {
107    fn default() -> Self {
108        Self::new(Variant::Primary, Size::Medium)
109    }
110}