dioxus_web/cfg.rs
1use std::rc::Rc;
2
3use dioxus_core::LaunchConfig;
4use wasm_bindgen::JsCast as _;
5
6///  Configuration for the WebSys renderer for the Dioxus VirtualDOM.
7///
8/// This struct helps configure the specifics of hydration and render destination for WebSys.
9///
10/// # Example
11///
12/// ```rust, ignore
13/// dioxus_web::launch(App, Config::new().hydrate(true).root_name("myroot"))
14/// ```
15pub struct Config {
16    pub(crate) hydrate: bool,
17    #[allow(dead_code)]
18    pub(crate) panic_hook: bool,
19    pub(crate) root: ConfigRoot,
20    #[cfg(feature = "document")]
21    pub(crate) history: Option<Rc<dyn dioxus_history::History>>,
22}
23
24impl LaunchConfig for Config {}
25
26pub(crate) enum ConfigRoot {
27    RootName(String),
28    RootNode(web_sys::Node),
29}
30
31impl Config {
32    /// Create a new Default instance of the Config.
33    ///
34    /// This is no different than calling `Config::default()`
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    #[cfg(feature = "hydrate")]
40    /// Enable SSR hydration
41    ///
42    /// This enables Dioxus to pick up work from a pre-rendered HTML file. Hydration will completely skip over any async
43    /// work and suspended nodes.
44    ///
45    /// Dioxus will load up all the elements with the `dio_el` data attribute into memory when the page is loaded.
46    pub fn hydrate(mut self, f: bool) -> Self {
47        self.hydrate = f;
48        self
49    }
50
51    /// Set the name of the element that Dioxus will use as the root.
52    ///
53    /// This is akin to calling React.render() on the element with the specified name.
54    /// Note that this only works on the current document, i.e. `window.document`.
55    /// To use a different document (popup, iframe, ...) use [Self::rootelement] instead.
56    pub fn rootname(mut self, name: impl Into<String>) -> Self {
57        self.root = ConfigRoot::RootName(name.into());
58        self
59    }
60
61    /// Set the element that Dioxus will use as root.
62    ///
63    /// This is akin to calling React.render() on the given element.
64    pub fn rootelement(mut self, elem: web_sys::Element) -> Self {
65        self.root = ConfigRoot::RootNode(elem.unchecked_into());
66        self
67    }
68
69    /// Set the node that Dioxus will use as root.
70    ///
71    /// This is akin to calling React.render() on the given element.
72    pub fn rootnode(mut self, node: web_sys::Node) -> Self {
73        self.root = ConfigRoot::RootNode(node);
74        self
75    }
76
77    /// Set the history provider for the application.
78    ///
79    /// `dioxus-web` provides two history providers:
80    /// - `dioxus_history::WebHistory`: A history provider that uses the browser history API.
81    /// - `dioxus_history::HashHistory`: A history provider that uses the `#` url fragment.
82    ///
83    /// By default, `dioxus-web` uses the `WebHistory` provider, but this method can be used to configure
84    /// a different history provider.
85    pub fn history(mut self, history: Rc<dyn dioxus_history::History>) -> Self {
86        #[cfg(feature = "document")]
87        {
88            self.history = Some(history);
89        }
90        self
91    }
92}
93
94impl Default for Config {
95    fn default() -> Self {
96        Self {
97            hydrate: false,
98            root: ConfigRoot::RootName("main".to_string()),
99            #[cfg(feature = "document")]
100            history: None,
101            panic_hook: true,
102        }
103    }
104}