tailwind_rs_core/utilities/effects/
box_shadow.rs

1//! Box shadow utilities for tailwind-rs
2//!
3//! This module provides utilities for box shadow effects.
4
5use crate::classes::ClassBuilder;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// Box shadow values
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum BoxShadow {
12    /// No shadow
13    None,
14    /// Small shadow
15    Sm,
16    /// Default shadow
17    Default,
18    /// Medium shadow
19    Md,
20    /// Large shadow
21    Lg,
22    /// Extra large shadow
23    Xl,
24    /// 2x large shadow
25    Xl2,
26    /// Inner shadow
27    Inner,
28}
29
30impl fmt::Display for BoxShadow {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            BoxShadow::None => write!(f, "shadow-none"),
34            BoxShadow::Sm => write!(f, "shadow-sm"),
35            BoxShadow::Default => write!(f, "shadow"),
36            BoxShadow::Md => write!(f, "shadow-md"),
37            BoxShadow::Lg => write!(f, "shadow-lg"),
38            BoxShadow::Xl => write!(f, "shadow-xl"),
39            BoxShadow::Xl2 => write!(f, "shadow-2xl"),
40            BoxShadow::Inner => write!(f, "shadow-inner"),
41        }
42    }
43}
44
45/// Trait for adding box shadow utilities to a class builder
46pub trait BoxShadowUtilities {
47    fn box_shadow(self, shadow: BoxShadow) -> Self;
48}
49
50impl BoxShadowUtilities for ClassBuilder {
51    fn box_shadow(self, shadow: BoxShadow) -> Self {
52        self.class(shadow.to_string())
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_box_shadow_display() {
62        assert_eq!(BoxShadow::None.to_string(), "shadow-none");
63        assert_eq!(BoxShadow::Sm.to_string(), "shadow-sm");
64        assert_eq!(BoxShadow::Default.to_string(), "shadow");
65        assert_eq!(BoxShadow::Md.to_string(), "shadow-md");
66        assert_eq!(BoxShadow::Lg.to_string(), "shadow-lg");
67        assert_eq!(BoxShadow::Xl.to_string(), "shadow-xl");
68        assert_eq!(BoxShadow::Xl2.to_string(), "shadow-2xl");
69        assert_eq!(BoxShadow::Inner.to_string(), "shadow-inner");
70    }
71
72    #[test]
73    fn test_box_shadow_utilities() {
74        let classes = ClassBuilder::new().box_shadow(BoxShadow::Lg).build();
75
76        assert!(classes.to_css_classes().contains("shadow-lg"));
77    }
78
79    #[test]
80    fn test_box_shadow_serialization() {
81        let box_shadow = BoxShadow::Md;
82        let serialized = serde_json::to_string(&box_shadow).unwrap();
83        let deserialized: BoxShadow = serde_json::from_str(&serialized).unwrap();
84        assert_eq!(box_shadow, deserialized);
85    }
86
87    #[test]
88    fn test_box_shadow_equality_and_hash() {
89        let box_shadow1 = BoxShadow::Lg;
90        let box_shadow2 = BoxShadow::Lg;
91        let box_shadow3 = BoxShadow::Md;
92
93        assert_eq!(box_shadow1, box_shadow2);
94        assert_ne!(box_shadow1, box_shadow3);
95
96        // Test that equal effects have the same hash
97        use std::collections::hash_map::DefaultHasher;
98        use std::hash::{Hash, Hasher};
99
100        let mut hasher1 = DefaultHasher::new();
101        let mut hasher2 = DefaultHasher::new();
102        box_shadow1.hash(&mut hasher1);
103        box_shadow2.hash(&mut hasher2);
104        assert_eq!(hasher1.finish(), hasher2.finish());
105    }
106}