dioxus/packages/web/src/cfg.rs

71 lines
2.2 KiB
Rust
Raw Normal View History

2021-09-24 20:11:30 -04:00
/// Configuration for the WebSys renderer for the Dioxus VirtualDOM.
///
/// This struct helps configure the specifics of hydration and render destination for WebSys.
///
/// # Example
2022-01-13 22:51:01 -05:00
///
2021-09-24 20:11:30 -04:00
/// ```rust, ignore
/// dioxus_web::launch(App, Config::new().hydrate(true).root_name("myroot"))
2021-09-24 20:11:30 -04:00
/// ```
pub struct Config {
pub(crate) hydrate: bool,
2021-08-05 22:23:41 -04:00
pub(crate) rootname: String,
2021-12-12 19:47:13 -05:00
pub(crate) cached_strings: Vec<String>,
pub(crate) default_panic_hook: bool,
}
2021-09-24 20:11:30 -04:00
impl Default for Config {
fn default() -> Self {
2021-08-05 22:23:41 -04:00
Self {
hydrate: false,
2021-11-19 00:49:04 -05:00
rootname: "main".to_string(),
2021-12-12 19:47:13 -05:00
cached_strings: Vec::new(),
default_panic_hook: true,
2021-08-05 22:23:41 -04:00
}
}
}
2021-09-24 20:11:30 -04:00
impl Config {
/// Create a new Default instance of the Config.
///
/// This is no different than calling `Config::default()`
pub fn new() -> Self {
Self::default()
}
/// Enable SSR hydration
///
/// This enables Dioxus to pick up work from a pre-renderd HTML file. Hydration will completely skip over any async
/// work and suspended nodes.
///
/// Dioxus will load up all the elements with the `dio_el` data attribute into memory when the page is loaded.
pub fn hydrate(mut self, f: bool) -> Self {
self.hydrate = f;
self
}
2021-08-05 22:23:41 -04:00
2021-09-24 20:11:30 -04:00
/// Set the name of the element that Dioxus will use as the root.
///
/// This is akint to calling React.render() on the element with the specified name.
pub fn rootname(mut self, name: impl Into<String>) -> Self {
2021-08-05 22:23:41 -04:00
self.rootname = name.into();
self
}
2021-12-12 19:47:13 -05:00
/// Set the name of the element that Dioxus will use as the root.
///
/// This is akint to calling React.render() on the element with the specified name.
pub fn with_string_cache(mut self, cache: Vec<String>) -> Self {
2021-12-12 19:47:13 -05:00
self.cached_strings = cache;
self
}
/// Set whether or not Dioxus should use the built-in panic hook or defer to your own.
///
/// The panic hook is set to true normally so even the simplest apps have helpful error messages.
pub fn with_default_panic_hook(mut self, f: bool) -> Self {
self.default_panic_hook = f;
self
}
}