yew_hooks/hooks/
use_window_size.rs1use gloo::utils::window;
2use yew::prelude::*;
3
4use super::{use_event_with_window, use_mount, use_raf_state};
5
6#[hook]
30pub fn use_window_size() -> (f64, f64) {
31 let state = use_raf_state(|| {
32 (
33 window().inner_width().unwrap().as_f64().unwrap(),
34 window().inner_height().unwrap().as_f64().unwrap(),
35 )
36 });
37
38 {
39 let state = state.clone();
40 use_event_with_window("resize", move |_: Event| {
41 state.set((
42 window().inner_width().unwrap().as_f64().unwrap(),
43 window().inner_height().unwrap().as_f64().unwrap(),
44 ));
45 });
46 }
47
48 {
49 let state = state.clone();
50 use_mount(move || {
51 state.set((
52 window().inner_width().unwrap().as_f64().unwrap(),
53 window().inner_height().unwrap().as_f64().unwrap(),
54 ));
55 });
56 }
57
58 *state
59}