dwind_base/
media_queries.rs1use futures_signals::signal::{Signal, SignalExt};
2
3#[derive(PartialOrd, Debug, Eq, PartialEq, Clone)]
4pub enum Breakpoint {
5 VerySmall,
6 Small,
7 Medium,
8 Large,
9 VeryLarge,
10 MediaQuery(String),
11}
12
13impl TryFrom<&str> for Breakpoint {
14 type Error = ();
15
16 fn try_from(value: &str) -> Result<Self, Self::Error> {
17 match value {
18 "@xs" => Ok(Breakpoint::VerySmall),
19 "@sm" => Ok(Breakpoint::Small),
20 "@md" => Ok(Breakpoint::Medium),
21 "@lg" => Ok(Breakpoint::Large),
22 "@xl" => Ok(Breakpoint::VeryLarge),
23 _ => Err(()),
24 }
25 }
26}
27
28pub fn breakpoint() -> impl Signal<Item = Breakpoint> {
29 dominator::window_size()
30 .map(|v| match v.width {
31 v if v >= 2560.0 => Breakpoint::VeryLarge,
32 v if v >= 1920.0 => Breakpoint::Large,
33 v if v >= 1280.0 => Breakpoint::Medium,
34 v if v >= 640.0 => Breakpoint::Small,
35 _v => Breakpoint::VerySmall,
36 })
37 .dedupe_cloned()
38}
39
40pub fn breakpoint_active_signal(level: Breakpoint) -> impl Signal<Item = bool> {
41 breakpoint().map(move |bp| bp >= level).dedupe()
42}
43
44pub fn breakpoint_less_than_signal(level: Breakpoint) -> impl Signal<Item = bool> {
45 breakpoint().map(move |bp| bp < level).dedupe()
46}