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
use cfg_if::cfg_if;
use leptos::prelude::*;
/// Reactive [`window.devicePixelRatio`](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio)
///
/// > NOTE: there is no event listener for `window.devicePixelRatio` change.
/// > So this function uses the same mechanism as described in
/// > [this example](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#monitoring_screen_resolution_or_zoom_level_changes).
///
/// ## Demo
///
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_device_pixel_ratio)
///
/// ## Usage
///
/// ```
/// # use leptos::prelude::*;
/// # use leptos_use::use_device_pixel_ratio;
/// #
/// # #[component]
/// # fn Demo() -> impl IntoView {
/// let pixel_ratio = use_device_pixel_ratio();
/// #
/// # view! { }
/// # }
/// ```
///
/// ## Server-Side Rendering
///
/// On the server this function returns a Signal that is always `1.0`.
pub fn use_device_pixel_ratio() -> Signal<f64> {
cfg_if! { if #[cfg(feature = "ssr")] {
Signal::derive(|| 1.0)
} else {
use crate::{use_event_listener_with_options, UseEventListenerOptions};
use leptos::ev::change;
let initial_pixel_ratio = window().device_pixel_ratio();
let (pixel_ratio, set_pixel_ratio) = signal(initial_pixel_ratio);
Effect::new(move |_| {
let media = window().match_media(
&format!("(resolution: {}dppx)", pixel_ratio.get())
).unwrap();
_ = use_event_listener_with_options(
media,
change,
move |_| {
set_pixel_ratio.set(window().device_pixel_ratio());
},
UseEventListenerOptions::default()
.capture(false)
.passive(true)
.once(true),
);
});
pixel_ratio.into()
}}
}