dioxus_web/cfg.rs
1use dioxus_core::LaunchConfig;
2use wasm_bindgen::JsCast as _;
3
4/// Configuration for the WebSys renderer for the Dioxus VirtualDOM.
5///
6/// This struct helps configure the specifics of hydration and render destination for WebSys.
7///
8/// # Example
9///
10/// ```rust, ignore
11/// dioxus_web::launch(App, Config::new().hydrate(true).root_name("myroot"))
12/// ```
13pub struct Config {
14 pub(crate) hydrate: bool,
15 pub(crate) root: ConfigRoot,
16}
17
18impl LaunchConfig for Config {}
19
20pub(crate) enum ConfigRoot {
21 RootName(String),
22 RootNode(web_sys::Node),
23}
24
25impl Config {
26 /// Create a new Default instance of the Config.
27 ///
28 /// This is no different than calling `Config::default()`
29 pub fn new() -> Self {
30 Self::default()
31 }
32
33 #[cfg(feature = "hydrate")]
34 /// Enable SSR hydration
35 ///
36 /// This enables Dioxus to pick up work from a pre-rendered HTML file. Hydration will completely skip over any async
37 /// work and suspended nodes.
38 ///
39 /// Dioxus will load up all the elements with the `dio_el` data attribute into memory when the page is loaded.
40 pub fn hydrate(mut self, f: bool) -> Self {
41 self.hydrate = f;
42 self
43 }
44
45 /// Set the name of the element that Dioxus will use as the root.
46 ///
47 /// This is akin to calling React.render() on the element with the specified name.
48 /// Note that this only works on the current document, i.e. `window.document`.
49 /// To use a different document (popup, iframe, ...) use [Self::rootelement] instead.
50 pub fn rootname(mut self, name: impl Into<String>) -> Self {
51 self.root = ConfigRoot::RootName(name.into());
52 self
53 }
54
55 /// Set the element that Dioxus will use as root.
56 ///
57 /// This is akin to calling React.render() on the given element.
58 pub fn rootelement(mut self, elem: web_sys::Element) -> Self {
59 self.root = ConfigRoot::RootNode(elem.unchecked_into());
60 self
61 }
62
63 /// Set the node that Dioxus will use as root.
64 ///
65 /// This is akin to calling React.render() on the given element.
66 pub fn rootnode(mut self, node: web_sys::Node) -> Self {
67 self.root = ConfigRoot::RootNode(node);
68 self
69 }
70}
71
72impl Default for Config {
73 fn default() -> Self {
74 Self {
75 hydrate: false,
76 root: ConfigRoot::RootName("main".to_string()),
77 }
78 }
79}