leptos_use/
use_document.rs1use cfg_if::cfg_if;
2use js_sys::Function;
3use std::ops::Deref;
4
5use crate::core::impl_ssr_safe_method;
6#[cfg(not(feature = "ssr"))]
7use leptos::prelude::*;
8use wasm_bindgen::JsValue;
9use web_sys::{
10 Document, Element, HtmlCollection, HtmlElement, HtmlHeadElement, Location, NodeList,
11 VisibilityState, Window,
12};
13
14pub fn use_document() -> UseDocument {
37 cfg_if! { if #[cfg(feature = "ssr")] {
38 UseDocument(None)
39 } else {
40 UseDocument(Some(document()))
41 }}
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct UseDocument(Option<Document>);
47
48impl Deref for UseDocument {
49 type Target = Option<Document>;
50 fn deref(&self) -> &Self::Target {
51 &self.0
52 }
53}
54
55impl UseDocument {
56 impl_ssr_safe_method!(
57 body(&self) -> Option<HtmlElement>;
59 .unwrap_or_default()
60 );
61
62 impl_ssr_safe_method!(
63 active_element(&self) -> Option<Element>;
65 .unwrap_or_default()
66 );
67
68 impl_ssr_safe_method!(
69 query_selector(&self, selector: &str) -> Result<Option<Element>, JsValue>;
70 .unwrap_or(Ok(None))
71 );
72
73 impl_ssr_safe_method!(
74 query_selector_all(&self, selectors: &str) -> Option<Result<NodeList, JsValue>>
75 );
76
77 impl_ssr_safe_method!(
78 url(&self) -> Option<Result<String, JsValue>>
79 );
80
81 impl_ssr_safe_method!(
82 document_uri(&self) -> Option<Result<String, JsValue>>
83 );
84
85 impl_ssr_safe_method!(
86 compat_mode(&self) -> Option<String>
87 );
88
89 impl_ssr_safe_method!(
90 character_set(&self) -> Option<String>
91 );
92
93 impl_ssr_safe_method!(
94 charset(&self) -> Option<String>
95 );
96
97 impl_ssr_safe_method!(
98 input_encoding(&self) -> Option<String>
99 );
100
101 impl_ssr_safe_method!(
102 content_type(&self) -> Option<String>
103 );
104
105 impl_ssr_safe_method!(
106 document_element(&self) -> Option<Element>;
107 .unwrap_or_default()
108 );
109
110 impl_ssr_safe_method!(
111 location(&self) -> Option<Location>;
112 .unwrap_or_default()
113 );
114
115 impl_ssr_safe_method!(
116 referrer(&self) -> Option<String>
117 );
118
119 impl_ssr_safe_method!(
120 last_modified(&self) -> Option<String>
121 );
122
123 impl_ssr_safe_method!(
124 ready_state(&self) -> Option<String>
125 );
126
127 impl_ssr_safe_method!(
128 title(&self) -> Option<String>
129 );
130
131 impl_ssr_safe_method!(
132 dir(&self) -> Option<String>
133 );
134
135 impl_ssr_safe_method!(
136 head(&self) -> Option<HtmlHeadElement>;
137 .unwrap_or_default()
138 );
139
140 impl_ssr_safe_method!(
141 images(&self) -> Option<HtmlCollection>
142 );
143
144 impl_ssr_safe_method!(
145 embeds(&self) -> Option<HtmlCollection>
146 );
147
148 impl_ssr_safe_method!(
149 plugins(&self) -> Option<HtmlCollection>
150 );
151
152 impl_ssr_safe_method!(
153 links(&self) -> Option<HtmlCollection>
154 );
155
156 impl_ssr_safe_method!(
157 forms(&self) -> Option<HtmlCollection>
158 );
159
160 impl_ssr_safe_method!(
161 scripts(&self) -> Option<HtmlCollection>
162 );
163
164 impl_ssr_safe_method!(
165 default_view(&self) -> Option<Window>;
166 .unwrap_or_default()
167 );
168
169 impl_ssr_safe_method!(
170 onreadystatechange(&self) -> Option<Function>;
171 .unwrap_or_default()
172 );
173
174 impl_ssr_safe_method!(
175 onbeforescriptexecute(&self) -> Option<Function>;
176 .unwrap_or_default()
177 );
178
179 impl_ssr_safe_method!(
180 onafterscriptexecute(&self) -> Option<Function>;
181 .unwrap_or_default()
182 );
183
184 impl_ssr_safe_method!(
185 onselectionchange(&self) -> Option<Function>;
186 .unwrap_or_default()
187 );
188
189 impl_ssr_safe_method!(
190 current_script(&self) -> Option<Element>;
191 .unwrap_or_default()
192 );
193
194 impl_ssr_safe_method!(
195 anchors(&self) -> Option<HtmlCollection>
196 );
197
198 impl_ssr_safe_method!(
199 applets(&self) -> Option<HtmlCollection>
200 );
201
202 impl_ssr_safe_method!(
203 fullscreen(&self) -> Option<bool>
204 );
205
206 impl_ssr_safe_method!(
207 fullscreen_enabled(&self) -> Option<bool>
208 );
209
210 impl_ssr_safe_method!(
211 onfullscreenchange(&self) -> Option<Function>;
212 .unwrap_or_default()
213 );
214
215 impl_ssr_safe_method!(
216 onfullscreenerror(&self) -> Option<Function>;
217 .unwrap_or_default()
218 );
219
220 impl_ssr_safe_method!(
221 onpointerlockchange(&self) -> Option<Function>;
222 .unwrap_or_default()
223 );
224
225 impl_ssr_safe_method!(
226 onpointerlockerror(&self) -> Option<Function>;
227 .unwrap_or_default()
228 );
229
230 impl_ssr_safe_method!(
231 hidden(&self) -> bool;
233 .unwrap_or(true)
234 );
235
236 impl_ssr_safe_method!(
237 visibility_state(&self) -> Option<VisibilityState>
238 );
239
240 impl_ssr_safe_method!(
241 onvisibilitychange(&self) -> Option<Function>;
242 .unwrap_or_default()
243 );
244
245 impl_ssr_safe_method!(
246 selected_style_sheet_set(&self) -> Option<String>;
247 .unwrap_or_default()
248 );
249
250 impl_ssr_safe_method!(
251 last_style_sheet_set(&self) -> Option<String>;
252 .unwrap_or_default()
253 );
254
255 impl_ssr_safe_method!(
256 preferred_style_sheet_set(&self) -> Option<String>;
257 .unwrap_or_default()
258 );
259}