diff --git a/src/app.rs b/src/app.rs index 750f108e31f..9b3e2e18d3f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -20,6 +20,25 @@ where App { scope } } + /// Alias to `mount_in_place` taking a component having a body element at the + /// root of the HTML generated by its `view` method. Use this method when you + /// need to manipulate the body element. For example, adding/removing app-wide + /// CSS classes of the body element. + pub fn replace_body(self) -> Scope { + let html_element = document() + .query_selector("html") + .expect("can't get html node for rendering") + .expect("can't unwrap html node"); + let body_element = document() + .query_selector("body") + .expect("can't get body node for rendering") + .expect("can't unwrap body node"); + html_element + .remove_child(&body_element) + .expect("can't remove body child"); + self.scope.mount_in_place(html_element, None, None, ()) + } + /// Alias to `mount("body", ...)`. pub fn mount_to_body(self) -> Scope { // Bootstrap the component for `Window` environment only (not for `Worker`)