tailwind-rs-core 0.15.4

Core types and utilities for tailwind-rs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! New theme system implementation according to API documentation

use crate::color::Color;
use std::collections::HashMap;

/// Theme variant for different component styles
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ThemeVariant {
    Primary,
    Secondary,
    Danger,
    Success,
    Warning,
    Info,
    Light,
    Dark,
}

impl ThemeVariant {
    /// Returns the associated color for the variant
    pub fn color(&self) -> Color {
        match self {
            ThemeVariant::Primary => Color::Blue,
            ThemeVariant::Secondary => Color::Gray,
            ThemeVariant::Danger => Color::Red,
            ThemeVariant::Success => Color::Green,
            ThemeVariant::Warning => Color::Yellow,
            ThemeVariant::Info => Color::Blue,
            ThemeVariant::Light => Color::Gray,
            ThemeVariant::Dark => Color::Gray,
        }
    }
}

/// Spacing size enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SpacingSize {
    Xs,
    Sm,
    Md,
    Lg,
    Xl,
    Xxl,
    Xxxl,
}

/// Spacing scale for consistent spacing values
pub struct SpacingScale {
    values: HashMap<SpacingSize, String>,
}

impl Default for SpacingScale {
    fn default() -> Self {
        Self::new()
    }
}

impl SpacingScale {
    /// Creates a new spacing scale with default values
    pub fn new() -> Self {
        let mut values = HashMap::new();
        values.insert(SpacingSize::Xs, "0.125rem".to_string());
        values.insert(SpacingSize::Sm, "0.25rem".to_string());
        values.insert(SpacingSize::Md, "1rem".to_string());
        values.insert(SpacingSize::Lg, "1.5rem".to_string());
        values.insert(SpacingSize::Xl, "2rem".to_string());
        values.insert(SpacingSize::Xxl, "4rem".to_string());
        values.insert(SpacingSize::Xxxl, "8rem".to_string());

        Self { values }
    }

    /// Creates a custom spacing scale
    pub fn custom(xs: &str, sm: &str, md: &str, lg: &str, xl: &str, xl2: &str, xl3: &str) -> Self {
        let mut values = HashMap::new();
        values.insert(SpacingSize::Xs, xs.to_string());
        values.insert(SpacingSize::Sm, sm.to_string());
        values.insert(SpacingSize::Md, md.to_string());
        values.insert(SpacingSize::Lg, lg.to_string());
        values.insert(SpacingSize::Xl, xl.to_string());
        values.insert(SpacingSize::Xxl, xl2.to_string());
        values.insert(SpacingSize::Xxxl, xl3.to_string());

        Self { values }
    }

    /// Gets spacing value for a specific size
    pub fn get(&self, size: SpacingSize) -> &str {
        self.values.get(&size).map(|s| s.as_str()).unwrap_or("0rem")
    }
}

/// Font family enum
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FontFamily {
    Sans,
    Serif,
    Mono,
    Custom(String),
}

impl FontFamily {
    /// Returns the CSS class for the font family
    pub fn class(&self) -> &str {
        match self {
            FontFamily::Sans => "font-sans",
            FontFamily::Serif => "font-serif",
            FontFamily::Mono => "font-mono",
            FontFamily::Custom(name) => name,
        }
    }
}

/// Font size scale
pub struct FontSizeScale {
    pub xs: String,    // 0.75rem
    pub sm: String,    // 0.875rem
    pub base: String,  // 1rem
    pub lg: String,    // 1.125rem
    pub xl: String,    // 1.25rem
    pub xxl: String,   // 1.5rem
    pub xxxl: String,  // 1.875rem
    pub xxxxl: String, // 2.25rem
}

impl Default for FontSizeScale {
    fn default() -> Self {
        Self::new()
    }
}

impl FontSizeScale {
    /// Creates a new font size scale with default values
    pub fn new() -> Self {
        Self {
            xs: "0.75rem".to_string(),
            sm: "0.875rem".to_string(),
            base: "1rem".to_string(),
            lg: "1.125rem".to_string(),
            xl: "1.25rem".to_string(),
            xxl: "1.5rem".to_string(),
            xxxl: "1.875rem".to_string(),
            xxxxl: "2.25rem".to_string(),
        }
    }
}

/// Font weight scale
pub struct FontWeightScale {
    pub thin: String,       // 100
    pub extralight: String, // 200
    pub light: String,      // 300
    pub normal: String,     // 400
    pub medium: String,     // 500
    pub semibold: String,   // 600
    pub bold: String,       // 700
    pub extrabold: String,  // 800
    pub black: String,      // 900
}

impl Default for FontWeightScale {
    fn default() -> Self {
        Self::new()
    }
}

impl FontWeightScale {
    /// Creates a new font weight scale with default values
    pub fn new() -> Self {
        Self {
            thin: "100".to_string(),
            extralight: "200".to_string(),
            light: "300".to_string(),
            normal: "400".to_string(),
            medium: "500".to_string(),
            semibold: "600".to_string(),
            bold: "700".to_string(),
            extrabold: "800".to_string(),
            black: "900".to_string(),
        }
    }
}

/// Line height scale
pub struct LineHeightScale {
    pub none: String,    // 1
    pub tight: String,   // 1.25
    pub snug: String,    // 1.375
    pub normal: String,  // 1.5
    pub relaxed: String, // 1.625
    pub loose: String,   // 2
}

impl Default for LineHeightScale {
    fn default() -> Self {
        Self::new()
    }
}

impl LineHeightScale {
    /// Creates a new line height scale with default values
    pub fn new() -> Self {
        Self {
            none: "1".to_string(),
            tight: "1.25".to_string(),
            snug: "1.375".to_string(),
            normal: "1.5".to_string(),
            relaxed: "1.625".to_string(),
            loose: "2".to_string(),
        }
    }
}

/// Letter spacing scale
pub struct LetterSpacingScale {
    pub tighter: String, // -0.05em
    pub tight: String,   // -0.025em
    pub normal: String,  // 0em
    pub wide: String,    // 0.025em
    pub wider: String,   // 0.05em
    pub widest: String,  // 0.1em
}

impl Default for LetterSpacingScale {
    fn default() -> Self {
        Self::new()
    }
}

impl LetterSpacingScale {
    /// Creates a new letter spacing scale with default values
    pub fn new() -> Self {
        Self {
            tighter: "-0.05em".to_string(),
            tight: "-0.025em".to_string(),
            normal: "0em".to_string(),
            wide: "0.025em".to_string(),
            wider: "0.05em".to_string(),
            widest: "0.1em".to_string(),
        }
    }
}

/// Typography scale for the theme
pub struct TypographyScale {
    pub font_family: FontFamily,
    pub font_sizes: FontSizeScale,
    pub font_weights: FontWeightScale,
    pub line_heights: LineHeightScale,
    pub letter_spacing: LetterSpacingScale,
}

impl Default for TypographyScale {
    fn default() -> Self {
        Self::new()
    }
}

impl TypographyScale {
    /// Creates a new typography scale with default values
    pub fn new() -> Self {
        Self {
            font_family: FontFamily::Sans,
            font_sizes: FontSizeScale::new(),
            font_weights: FontWeightScale::new(),
            line_heights: LineHeightScale::new(),
            letter_spacing: LetterSpacingScale::new(),
        }
    }

    /// Sets the font family for the typography scale
    pub fn font_family(self, family: FontFamily) -> Self {
        Self {
            font_family: family,
            ..self
        }
    }
}

/// Shadow scale
pub struct ShadowScale {
    pub sm: String,
    pub base: String,
    pub md: String,
    pub lg: String,
    pub xl: String,
    pub xxl: String,
    pub inner: String,
}

impl Default for ShadowScale {
    fn default() -> Self {
        Self::new()
    }
}

impl ShadowScale {
    /// Creates a new shadow scale with default values
    pub fn new() -> Self {
        Self {
            sm: "0 1px 2px 0 rgb(0 0 0 / 0.05)".to_string(),
            base: "0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)".to_string(),
            md: "0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)".to_string(),
            lg: "0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)".to_string(),
            xl: "0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1)".to_string(),
            xxl: "0 25px 50px -12px rgb(0 0 0 / 0.25)".to_string(),
            inner: "inset 0 2px 4px 0 rgb(0 0 0 / 0.05)".to_string(),
        }
    }
}

/// Border scale
pub struct BorderScale {
    pub none: String,
    pub sm: String,
    pub base: String,
    pub md: String,
    pub lg: String,
    pub xl: String,
}

impl Default for BorderScale {
    fn default() -> Self {
        Self::new()
    }
}

impl BorderScale {
    /// Creates a new border scale with default values
    pub fn new() -> Self {
        Self {
            none: "0px".to_string(),
            sm: "1px".to_string(),
            base: "2px".to_string(),
            md: "4px".to_string(),
            lg: "8px".to_string(),
            xl: "16px".to_string(),
        }
    }
}

/// Animation scale
pub struct AnimationScale {
    pub none: String,
    pub spin: String,
    pub ping: String,
    pub pulse: String,
    pub bounce: String,
}

impl Default for AnimationScale {
    fn default() -> Self {
        Self::new()
    }
}

impl AnimationScale {
    /// Creates a new animation scale with default values
    pub fn new() -> Self {
        Self {
            none: "none".to_string(),
            spin: "spin 1s linear infinite".to_string(),
            ping: "ping 1s cubic-bezier(0, 0, 0.2, 1) infinite".to_string(),
            pulse: "pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite".to_string(),
            bounce: "bounce 1s infinite".to_string(),
        }
    }
}

/// Main theme structure according to API documentation
pub struct Theme {
    pub primary_color: Color,
    pub secondary_color: Color,
    pub accent_color: Color,
    pub background_color: Color,
    pub text_color: Color,
    pub border_color: Color,
    pub success_color: Color,
    pub warning_color: Color,
    pub error_color: Color,
    pub info_color: Color,
    pub spacing: SpacingScale,
    pub typography: TypographyScale,
    pub shadows: ShadowScale,
    pub borders: BorderScale,
    pub animations: AnimationScale,
}

impl Default for Theme {
    fn default() -> Self {
        Self::new()
    }
}

impl Theme {
    /// Creates a new theme with default values
    pub fn new() -> Self {
        Self {
            primary_color: Color::Blue,
            secondary_color: Color::Gray,
            accent_color: Color::Blue,
            background_color: Color::Gray, // Using Gray as placeholder for White
            text_color: Color::Gray,
            border_color: Color::Gray,
            success_color: Color::Green,
            warning_color: Color::Yellow,
            error_color: Color::Red,
            info_color: Color::Blue,
            spacing: SpacingScale::new(),
            typography: TypographyScale::new(),
            shadows: ShadowScale::new(),
            borders: BorderScale::new(),
            animations: AnimationScale::new(),
        }
    }

    /// Sets the primary color for the theme
    pub fn primary_color(self, color: Color) -> Self {
        Self {
            primary_color: color,
            ..self
        }
    }

    /// Sets the secondary color for the theme
    pub fn secondary_color(self, color: Color) -> Self {
        Self {
            secondary_color: color,
            ..self
        }
    }

    /// Sets the accent color for the theme
    pub fn accent_color(self, color: Color) -> Self {
        Self {
            accent_color: color,
            ..self
        }
    }

    /// Sets the background color for the theme
    pub fn background_color(self, color: Color) -> Self {
        Self {
            background_color: color,
            ..self
        }
    }

    /// Sets the text color for the theme
    pub fn text_color(self, color: Color) -> Self {
        Self {
            text_color: color,
            ..self
        }
    }

    /// Applies theme to a component
    pub fn apply_to_component(&self, component: &dyn ThemedComponent) -> String {
        component.apply_theme(self)
    }
}

/// Trait for components that support theming
pub trait ThemedComponent {
    /// Returns the base classes for the component
    fn base_classes(&self) -> &str;

    /// Applies the theme to the component
    fn apply_theme(&self, theme: &Theme) -> String;

    /// Returns available theme variants for the component
    fn theme_variants(&self) -> Vec<ThemeVariant> {
        vec![
            ThemeVariant::Primary,
            ThemeVariant::Secondary,
            ThemeVariant::Danger,
            ThemeVariant::Success,
        ]
    }
}

/// Theme preset enum for predefined themes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ThemePreset {
    Light,
    Dark,
    Professional,
    Minimal,
    Vibrant,
}

impl ThemePreset {
    /// Creates a theme from the preset
    pub fn create(&self) -> Theme {
        match self {
            ThemePreset::Light => Theme::new()
                .primary_color(Color::Blue)
                .secondary_color(Color::Gray)
                .background_color(Color::Gray) // Using Gray as placeholder for White
                .text_color(Color::Gray),
            ThemePreset::Dark => Theme::new()
                .primary_color(Color::Blue)
                .secondary_color(Color::Gray)
                .background_color(Color::Gray) // Using Gray as placeholder for Black
                .text_color(Color::Gray), // Using Gray as placeholder for White
            ThemePreset::Professional => Theme::new()
                .primary_color(Color::Blue)
                .secondary_color(Color::Gray)
                .accent_color(Color::Blue),
            ThemePreset::Minimal => Theme::new()
                .primary_color(Color::Gray)
                .secondary_color(Color::Gray)
                .accent_color(Color::Gray),
            ThemePreset::Vibrant => Theme::new()
                .primary_color(Color::Blue)
                .secondary_color(Color::Green)
                .accent_color(Color::Yellow),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_theme_creation() {
        let theme = Theme::new();
        assert_eq!(theme.primary_color, Color::Blue);
        assert_eq!(theme.secondary_color, Color::Gray);
        assert_eq!(theme.accent_color, Color::Blue);
    }

    #[test]
    fn test_theme_primary_color() {
        let theme = Theme::new().primary_color(Color::Green);
        assert_eq!(theme.primary_color, Color::Green);
    }

    #[test]
    fn test_theme_secondary_color() {
        let theme = Theme::new().secondary_color(Color::Purple);
        assert_eq!(theme.secondary_color, Color::Purple);
    }

    #[test]
    fn test_theme_accent_color() {
        let theme = Theme::new().accent_color(Color::Orange);
        assert_eq!(theme.accent_color, Color::Orange);
    }

    #[test]
    fn test_theme_variant_color() {
        assert_eq!(ThemeVariant::Primary.color(), Color::Blue);
        assert_eq!(ThemeVariant::Secondary.color(), Color::Gray);
        assert_eq!(ThemeVariant::Danger.color(), Color::Red);
        assert_eq!(ThemeVariant::Success.color(), Color::Green);
    }

    #[test]
    fn test_spacing_scale_new() {
        let spacing = SpacingScale::new();
        assert_eq!(spacing.get(SpacingSize::Xs), "0.125rem");
        assert_eq!(spacing.get(SpacingSize::Sm), "0.25rem");
        assert_eq!(spacing.get(SpacingSize::Md), "1rem");
        assert_eq!(spacing.get(SpacingSize::Lg), "1.5rem");
    }

    #[test]
    fn test_spacing_scale_custom() {
        let spacing =
            SpacingScale::custom("0.1rem", "0.2rem", "0.5rem", "1rem", "2rem", "4rem", "8rem");
        assert_eq!(spacing.get(SpacingSize::Xs), "0.1rem");
        assert_eq!(spacing.get(SpacingSize::Sm), "0.2rem");
        assert_eq!(spacing.get(SpacingSize::Md), "0.5rem");
    }

    #[test]
    fn test_font_family_class() {
        assert_eq!(FontFamily::Sans.class(), "font-sans");
        assert_eq!(FontFamily::Serif.class(), "font-serif");
        assert_eq!(FontFamily::Mono.class(), "font-mono");
        assert_eq!(
            FontFamily::Custom("custom-font".to_string()).class(),
            "custom-font"
        );
    }

    #[test]
    fn test_typography_scale_new() {
        let typography = TypographyScale::new();
        assert_eq!(typography.font_family, FontFamily::Sans);
        assert_eq!(typography.font_sizes.xs, "0.75rem");
        assert_eq!(typography.font_sizes.base, "1rem");
    }

    #[test]
    fn test_typography_scale_font_family() {
        let typography = TypographyScale::new().font_family(FontFamily::Serif);
        assert_eq!(typography.font_family, FontFamily::Serif);
    }

    #[test]
    fn test_theme_preset_light() {
        let theme = ThemePreset::Light.create();
        assert_eq!(theme.primary_color, Color::Blue);
        assert_eq!(theme.background_color, Color::Gray); // Using Gray as placeholder for White
        assert_eq!(theme.text_color, Color::Gray);
    }

    #[test]
    fn test_theme_preset_dark() {
        let theme = ThemePreset::Dark.create();
        assert_eq!(theme.primary_color, Color::Blue);
        assert_eq!(theme.background_color, Color::Gray); // Using Gray as placeholder for Black
        assert_eq!(theme.text_color, Color::Gray); // Using Gray as placeholder for White
    }

    #[test]
    fn test_theme_preset_professional() {
        let theme = ThemePreset::Professional.create();
        assert_eq!(theme.primary_color, Color::Blue);
        assert_eq!(theme.secondary_color, Color::Gray);
        assert_eq!(theme.accent_color, Color::Blue);
    }

    #[test]
    fn test_theme_preset_minimal() {
        let theme = ThemePreset::Minimal.create();
        assert_eq!(theme.primary_color, Color::Gray);
        assert_eq!(theme.secondary_color, Color::Gray);
        assert_eq!(theme.accent_color, Color::Gray);
    }

    #[test]
    fn test_theme_preset_vibrant() {
        let theme = ThemePreset::Vibrant.create();
        assert_eq!(theme.primary_color, Color::Blue);
        assert_eq!(theme.secondary_color, Color::Green);
        assert_eq!(theme.accent_color, Color::Yellow);
    }

    // Mock component for testing ThemedComponent trait
    struct MockButton {
        variant: ThemeVariant,
    }

    impl MockButton {
        fn new(variant: ThemeVariant) -> Self {
            Self { variant }
        }
    }

    impl ThemedComponent for MockButton {
        fn base_classes(&self) -> &str {
            "px-4 py-2 rounded"
        }

        fn apply_theme(&self, theme: &Theme) -> String {
            match self.variant {
                ThemeVariant::Primary => {
                    format!(
                        "{} bg-{} text-white",
                        self.base_classes(),
                        theme.primary_color.name().to_lowercase()
                    )
                }
                ThemeVariant::Secondary => {
                    format!(
                        "{} bg-{} text-{}",
                        self.base_classes(),
                        theme.secondary_color.name().to_lowercase(),
                        theme.secondary_color.name().to_lowercase()
                    )
                }
                _ => self.base_classes().to_string(),
            }
        }
    }

    #[test]
    fn test_themed_component_primary() {
        let theme = Theme::new().primary_color(Color::Blue);
        let button = MockButton::new(ThemeVariant::Primary);
        let classes = theme.apply_to_component(&button);
        assert!(classes.contains("px-4 py-2 rounded"));
        assert!(classes.contains("bg-blue"));
        assert!(classes.contains("text-white"));
    }

    #[test]
    fn test_themed_component_secondary() {
        let theme = Theme::new().secondary_color(Color::Gray);
        let button = MockButton::new(ThemeVariant::Secondary);
        let classes = theme.apply_to_component(&button);
        assert!(classes.contains("px-4 py-2 rounded"));
        assert!(classes.contains("bg-gray"));
        assert!(classes.contains("text-gray"));
    }

    #[test]
    fn test_themed_component_variants() {
        let button = MockButton::new(ThemeVariant::Primary);
        let variants = button.theme_variants();
        assert!(variants.contains(&ThemeVariant::Primary));
        assert!(variants.contains(&ThemeVariant::Secondary));
        assert!(variants.contains(&ThemeVariant::Danger));
        assert!(variants.contains(&ThemeVariant::Success));
    }
}