dioxus_use_document/hooks/use_document/
mod.rs

1use super::*;
2use web_sys::DocumentType;
3
4pub struct UseDocument {
5    data: Rc<RefCell<UseDocumentData>>,
6}
7
8struct UseDocumentData {
9    document: Option<Document>,
10}
11
12impl UseDocument {
13    pub fn new(cx: &ScopeState) -> Option<Self> {
14        let document = window()?.document()?;
15        let data = Rc::new(RefCell::new(UseDocumentData { document: Some(document) }));
16
17        Some(Self { data })
18    }
19}
20
21impl UseDocument {
22    /// Title
23    pub fn title(&self) -> String {
24        let document = &self.data.borrow_mut().document;
25        match document {
26            None => String::new(),
27            Some(e) => e.title(),
28        }
29    }
30    ///
31    pub fn set_title(&self, input: &str) -> Option<()> {
32        let document = &self.data.borrow_mut().document;
33        Some(document.as_ref()?.set_title(input))
34    }
35    /// **read-only**
36    pub fn character_set(&self) -> String {
37        let document = &self.data.borrow_mut().document;
38        match document {
39            None => String::from("utf-8"),
40            Some(e) => e.character_set(),
41        }
42    }
43    /// **read-only**
44    pub fn doc_type(&self) -> Option<DocumentType> {
45        let document = &self.data.borrow_mut().document;
46        document.as_ref()?.doctype()
47    }
48}