#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Breakpoint {
Sm,
Md,
Lg,
Xl,
}
impl Breakpoint {
pub const fn min_width_px(self) -> u32 {
match self {
Self::Sm => 640,
Self::Md => 1024,
Self::Lg => 1280,
Self::Xl => 1536,
}
}
pub const fn down_max_px(self) -> u32 {
self.min_width_px().saturating_sub(1)
}
pub const fn css_var(self) -> &'static str {
match self {
Self::Sm => "--orb-breakpoint-sm",
Self::Md => "--orb-breakpoint-md",
Self::Lg => "--orb-breakpoint-lg",
Self::Xl => "--orb-breakpoint-xl",
}
}
pub const fn all() -> [Self; 4] {
[Self::Sm, Self::Md, Self::Lg, Self::Xl]
}
pub fn up_query(self) -> String {
format!("(min-width: {}px)", self.min_width_px())
}
pub fn down_query(self) -> String {
format!("(max-width: {}px)", self.down_max_px())
}
}
pub fn write_orb_breakpoint_css_vars(css_vars: &mut String) {
for bp in Breakpoint::all() {
css_vars.push_str(&format!("{}: {}px;", bp.css_var(), bp.min_width_px()));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn breakpoint_tokens_emit() {
let mut css = String::new();
write_orb_breakpoint_css_vars(&mut css);
assert!(css.contains("--orb-breakpoint-sm: 640px;"));
assert!(css.contains("--orb-breakpoint-md: 1024px;"));
assert!(css.contains("--orb-breakpoint-lg: 1280px;"));
assert!(css.contains("--orb-breakpoint-xl: 1536px;"));
}
#[test]
fn down_max_is_one_below_min() {
assert_eq!(Breakpoint::Md.down_max_px(), 1023);
assert_eq!(Breakpoint::Sm.down_query(), "(max-width: 639px)");
assert_eq!(Breakpoint::Md.up_query(), "(min-width: 1024px)");
}
}