A provider for document-related functionality. Provides things like a history API, a title, a way to run JS, and some other basics/essentials used by nearly every platform. An integration with some kind of navigation history. Depending on your use case, your implementation may deviate from the described procedure. This is fine, as long as both `current_route` and `current_query` match the descr
| 62 | /// The described behaviors are designed to mimic a web browser, which most users should already |
| 63 | /// know. Deviations might confuse them. |
| 64 | pub trait Document: 'static { |
| 65 | /// Run `eval` against this document, returning an [`Eval`] that can be used to await the result. |
| 66 | fn eval(&self, js: String) -> Eval; |
| 67 | |
| 68 | /// Set the title of the document |
| 69 | fn set_title(&self, title: String) { |
| 70 | self.eval(format!("document.title = {title:?};")); |
| 71 | } |
| 72 | |
| 73 | /// Create a new element in the head |
| 74 | fn create_head_element( |
| 75 | &self, |
| 76 | name: &str, |
| 77 | attributes: &[(&str, String)], |
| 78 | contents: Option<String>, |
| 79 | ) { |
| 80 | // This default implementation remains to make the trait compatible with the 0.6 version, but it should not be used |
| 81 | // The element should only be created inside an effect so it is not called while the component is suspended |
| 82 | self.eval(create_element_in_head(name, attributes, contents)); |
| 83 | } |
| 84 | |
| 85 | /// Create a new meta tag in the head |
| 86 | fn create_meta(&self, props: MetaProps) { |
| 87 | let attributes = props.attributes(); |
| 88 | self.create_head_element("meta", &attributes, None); |
| 89 | } |
| 90 | |
| 91 | /// Create a new script tag in the head |
| 92 | fn create_script(&self, props: ScriptProps) { |
| 93 | let attributes = props.attributes(); |
| 94 | self.create_head_element("script", &attributes, props.script_contents().ok()); |
| 95 | } |
| 96 | |
| 97 | /// Create a new style tag in the head |
| 98 | fn create_style(&self, props: StyleProps) { |
| 99 | let attributes = props.attributes(); |
| 100 | self.create_head_element("style", &attributes, props.style_contents().ok()); |
| 101 | } |
| 102 | |
| 103 | /// Create a new link tag in the head |
| 104 | fn create_link(&self, props: LinkProps) { |
| 105 | let attributes = props.attributes(); |
| 106 | self.create_head_element("link", &attributes, None); |
| 107 | } |
| 108 | |
| 109 | /// Check if we should create a new head component at all. If it returns false, the head component will be skipped. |
| 110 | /// |
| 111 | /// This runs once per head component and is used to hydrate head components in fullstack. |
| 112 | fn create_head_component(&self) -> bool { |
| 113 | true |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// A document that does nothing |
| 118 | #[derive(Default)] |
no outgoing calls
no test coverage detected