pub mod breakpoints;
pub mod flexbox;
pub mod grid;
pub mod responsive_builder;
pub mod responsive_config;
pub mod responsive_values;
pub mod states;
pub use breakpoints::Breakpoint;
pub use flexbox::{AlignItems, FlexDirection, FlexWrap, JustifyContent, ResponsiveFlex};
pub use grid::ResponsiveGrid;
pub use responsive_builder::ResponsiveBuilder;
pub use responsive_config::{Responsive, ResponsiveConfig};
pub use responsive_values::ResponsiveValue;
pub use states::State;
pub fn create_responsive_config() -> ResponsiveConfig {
ResponsiveConfig::default()
}
pub fn create_responsive_builder() -> ResponsiveBuilder {
ResponsiveBuilder::default()
}
pub fn create_responsive_flex() -> ResponsiveFlex {
ResponsiveFlex::default()
}
pub fn create_responsive_grid() -> ResponsiveGrid {
ResponsiveGrid::default()
}
pub mod utils {
use super::*;
pub fn is_breakpoint_active(breakpoint: Breakpoint, screen_width: u32) -> bool {
screen_width >= breakpoint.min_width()
}
pub fn get_breakpoint_for_width(screen_width: u32) -> Breakpoint {
if screen_width >= Breakpoint::Xl2.min_width() {
Breakpoint::Xl2
} else if screen_width >= Breakpoint::Xl.min_width() {
Breakpoint::Xl
} else if screen_width >= Breakpoint::Lg.min_width() {
Breakpoint::Lg
} else if screen_width >= Breakpoint::Md.min_width() {
Breakpoint::Md
} else if screen_width >= Breakpoint::Sm.min_width() {
Breakpoint::Sm
} else {
Breakpoint::Base
}
}
pub fn generate_responsive_classes<T: ToString>(
base: T,
sm: Option<T>,
md: Option<T>,
lg: Option<T>,
xl: Option<T>,
xl2: Option<T>,
) -> String {
let mut classes = Vec::new();
classes.push(base.to_string());
if let Some(sm_val) = sm {
classes.push(format!("sm:{}", sm_val.to_string()));
}
if let Some(md_val) = md {
classes.push(format!("md:{}", md_val.to_string()));
}
if let Some(lg_val) = lg {
classes.push(format!("lg:{}", lg_val.to_string()));
}
if let Some(xl_val) = xl {
classes.push(format!("xl:{}", xl_val.to_string()));
}
if let Some(xl2_val) = xl2 {
classes.push(format!("2xl:{}", xl2_val.to_string()));
}
classes.join(" ")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_responsive_config() {
let config = create_responsive_config();
assert_eq!(config.breakpoints.len(), 6); }
#[test]
fn test_create_responsive_builder() {
let builder = create_responsive_builder();
assert!(builder.is_empty());
}
#[test]
fn test_create_responsive_flex() {
let flex = create_responsive_flex();
assert_eq!(
flex.direction.get_breakpoint(Breakpoint::Base),
Some(&FlexDirection::Row)
);
}
#[test]
fn test_create_responsive_grid() {
let grid = create_responsive_grid();
assert_eq!(grid.columns.get(&Breakpoint::Base), Some(&1));
}
#[test]
fn test_is_breakpoint_active() {
assert!(utils::is_breakpoint_active(Breakpoint::Base, 0));
assert!(utils::is_breakpoint_active(Breakpoint::Sm, 640));
assert!(utils::is_breakpoint_active(Breakpoint::Md, 768));
assert!(!utils::is_breakpoint_active(Breakpoint::Sm, 639));
}
#[test]
fn test_get_breakpoint_for_width() {
assert_eq!(utils::get_breakpoint_for_width(0), Breakpoint::Base);
assert_eq!(utils::get_breakpoint_for_width(640), Breakpoint::Sm);
assert_eq!(utils::get_breakpoint_for_width(768), Breakpoint::Md);
assert_eq!(utils::get_breakpoint_for_width(1024), Breakpoint::Lg);
assert_eq!(utils::get_breakpoint_for_width(1280), Breakpoint::Xl);
assert_eq!(utils::get_breakpoint_for_width(1536), Breakpoint::Xl2);
}
#[test]
fn test_generate_responsive_classes() {
let classes = utils::generate_responsive_classes(
"text-sm",
Some("text-base"),
Some("text-lg"),
Some("text-xl"),
None,
None,
);
assert!(classes.contains("text-sm"));
assert!(classes.contains("sm:text-base"));
assert!(classes.contains("md:text-lg"));
assert!(classes.contains("lg:text-xl"));
assert!(!classes.contains("xl:"));
assert!(!classes.contains("2xl:"));
}
}