dioxus_use_window/hooks/
mod.rs

1#![allow(non_snake_case)]
2mod builder;
3mod use_browser;
4// mod use_full_screen;
5mod use_height;
6mod use_layout;
7mod use_width;
8mod use_window;
9
10use self::use_window::data::*;
11pub use self::{
12    builder::UseWindowBuilder, use_browser::UseBrowser, use_height::UseWindowHeight, use_layout::UseWindowLayout,
13    use_width::UseWindowWidth, use_window::UseWindow,
14};
15use crate::ResponsiveLayout;
16use dioxus::core::ScopeState;
17use gloo_events::EventListener;
18use log::info;
19use std::{
20    cell::{Ref, RefCell},
21    fmt::{Debug, Display, Formatter},
22    marker::PhantomData,
23    rc::Rc,
24};
25use wasm_bindgen::JsValue;
26use web_sys::{window, Window};
27
28/// hooks for window's size
29///
30/// # Arguments
31///
32/// returns: [`UseWindowSize`]
33///
34/// # Examples
35///
36/// ```
37/// use dioxus::prelude::*;
38/// use dioxus_use_window::use_window_size;
39///
40/// fn App(cx: Scope) -> Element {
41///     let size = use_window_size(&cx);
42///
43///     cx.render(rsx!(
44///         h1 { "Window size: {size}" }
45///     ))
46/// }
47/// ```
48#[inline]
49pub fn use_window_size(cx: &ScopeState) -> &UseWindow {
50    UseWindowBuilder::default().use_window(cx)
51}
52
53/// hooks for window's width
54///
55/// # Arguments
56///
57/// returns: [`UseWindowWidth`]
58///
59/// # Examples
60///
61/// ```
62/// use dioxus::prelude::*;
63/// use dioxus_use_window::use_window_width;
64///
65/// fn App(cx: Scope) -> Element {
66///     let width = use_window_width(&cx);
67///
68///     cx.render(rsx!(
69///         h1 { "Window width: {width}" }
70///     ))
71/// }
72/// ```
73#[inline]
74pub fn use_window_width(cx: &ScopeState) -> &UseWindowWidth {
75    UseWindowBuilder::default().use_width(cx)
76}
77
78/// hooks for window's height
79///
80/// # Arguments
81///
82/// returns: [`UseWindowHeight`]
83///
84/// # Examples
85///
86/// ```
87/// use dioxus::prelude::*;
88/// use dioxus_use_window::use_window_height;
89///
90/// fn App(cx: Scope) -> Element {
91///     let height = use_window_height(&cx);
92///
93///     cx.render(rsx!(
94///         h1 { "Window height: {height}" }
95///     ))
96/// }
97/// ```
98#[inline]
99pub fn use_window_height(cx: &ScopeState) -> &UseWindowHeight {
100    UseWindowBuilder::default().use_height(cx)
101}
102
103/// hooks for window's layout
104///
105/// # Arguments
106///
107/// returns: [`WindowLayout`]
108///
109/// # Examples
110///
111/// ```
112/// use dioxus::prelude::*;
113/// use dioxus_use_window::{ResponsiveLayout, use_window_layout};
114///
115/// fn App(cx: Scope) -> Element {
116///     let layout = use_window_layout::<ResponsiveLayout>(&cx);
117///
118///     cx.render(rsx!(
119///         h1 { "Window layout: {layout}" }
120///     ))
121/// }
122/// ```
123#[inline]
124pub fn use_window_layout<T>(cx: &ScopeState) -> &UseWindowLayout<T>
125where
126    T: From<usize>,
127    T: 'static,
128{
129    UseWindowBuilder::default().use_layout(cx)
130}
131
132/// hooks for window's layout
133///
134/// # Arguments
135///
136/// returns: [`WindowLayout`]
137///
138/// # Examples
139///
140/// ```
141/// use dioxus::prelude::*;
142/// use dioxus_use_window::{use_responsive_layout};
143///
144/// fn App(cx: Scope) -> Element {
145///     let layout = use_responsive_layout(&cx);
146///
147///     cx.render(rsx!(
148///         h1 { "Window layout: {layout}" }
149///     ))
150/// }
151/// ```
152#[inline]
153pub fn use_responsive_layout(cx: &ScopeState) -> &UseWindowLayout<ResponsiveLayout> {
154    UseWindowBuilder::default().use_responsive_layout(cx)
155}
156
157/// hooks for window's size
158///
159/// # Arguments
160///
161/// returns: [`UseBrowser`]
162///
163/// # Examples
164///
165/// ```
166/// use dioxus::prelude::*;
167/// use dioxus_use_window::use_browser;
168///
169/// fn App(cx: Scope) -> Element {
170///     let size = use_browser(&cx);
171///
172///     cx.render(rsx!(
173///         h1 { "Window size: {size}" }
174///     ))
175/// }
176/// ```
177#[inline]
178pub fn use_browser(cx: &ScopeState) -> &UseBrowser {
179    UseWindowBuilder::default().use_browser(cx)
180}