Less clumsy configuration for desktop and mobile (#553)
* chore: dont use prebuilt builder pattern for configuring desktop * chore: use regular config pattern for web * Chore: update docs too * chore: clean up some warnings
This commit is contained in:
1 parent
41b450eedf
commit
540e785d8b
28 files changed
+172
-156
No files matched your search
+24
-7
@@ -5,32 +5,41 @@
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// dioxus_web::launch(App, |cfg| cfg.hydrate(true).root_name("myroot"))
|
||||
/// dioxus_web::launch(App, Config::new().hydrate(true).root_name("myroot"))
|
||||
/// ```
|
||||
pub struct WebConfig {
|
||||
pub struct Config {
|
||||
pub(crate) hydrate: bool,
|
||||
pub(crate) rootname: String,
|
||||
pub(crate) cached_strings: Vec<String>,
|
||||
pub(crate) default_panic_hook: bool,
|
||||
}
|
||||
|
||||
impl Default for WebConfig {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hydrate: false,
|
||||
rootname: "main".to_string(),
|
||||
cached_strings: Vec::new(),
|
||||
default_panic_hook: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WebConfig {
|
||||
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) -> &mut Self {
|
||||
pub fn hydrate(mut self, f: bool) -> Self {
|
||||
self.hydrate = f;
|
||||
self
|
||||
}
|
||||
@@ -38,7 +47,7 @@ impl WebConfig {
|
||||
/// 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>) -> &mut Self {
|
||||
pub fn rootname(mut self, name: impl Into<String>) -> Self {
|
||||
self.rootname = name.into();
|
||||
self
|
||||
}
|
||||
@@ -46,8 +55,16 @@ impl WebConfig {
|
||||
/// 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>) -> &mut Self {
|
||||
pub fn with_string_cache(mut self, cache: Vec<String>) -> Self {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ use std::{any::Any, rc::Rc, sync::Arc};
|
||||
use wasm_bindgen::{closure::Closure, JsCast};
|
||||
use web_sys::{Document, Element, Event, HtmlElement};
|
||||
|
||||
use crate::WebConfig;
|
||||
use crate::Config;
|
||||
|
||||
pub struct WebsysDom {
|
||||
pub interpreter: Interpreter,
|
||||
@@ -26,7 +26,7 @@ pub struct WebsysDom {
|
||||
}
|
||||
|
||||
impl WebsysDom {
|
||||
pub fn new(cfg: WebConfig, sender_callback: Rc<dyn Fn(SchedulerMsg)>) -> Self {
|
||||
pub fn new(cfg: Config, sender_callback: Rc<dyn Fn(SchedulerMsg)>) -> Self {
|
||||
// eventually, we just want to let the interpreter do all the work of decoding events into our event type
|
||||
let callback: Box<dyn FnMut(&Event)> = Box::new(move |event: &web_sys::Event| {
|
||||
let mut target = event
|
||||
|
||||
+13
-18
@@ -56,7 +56,7 @@
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
pub use crate::cfg::WebConfig;
|
||||
pub use crate::cfg::Config;
|
||||
pub use crate::util::use_eval;
|
||||
use dioxus_core::prelude::Component;
|
||||
use dioxus_core::SchedulerMsg;
|
||||
@@ -93,7 +93,7 @@ mod util;
|
||||
/// }
|
||||
/// ```
|
||||
pub fn launch(root_component: Component) {
|
||||
launch_with_props(root_component, (), |c| c);
|
||||
launch_with_props(root_component, (), Config::default());
|
||||
}
|
||||
|
||||
/// Launch your app and run the event loop, with configuration.
|
||||
@@ -106,7 +106,7 @@ pub fn launch(root_component: Component) {
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// dioxus_web::launch_with_props(App, |config| config.pre_render(true));
|
||||
/// dioxus_web::launch_with_props(App, Config::new().pre_render(true));
|
||||
/// }
|
||||
///
|
||||
/// fn app(cx: Scope) -> Element {
|
||||
@@ -115,8 +115,8 @@ pub fn launch(root_component: Component) {
|
||||
/// })
|
||||
/// }
|
||||
/// ```
|
||||
pub fn launch_cfg(root: Component, config_builder: impl FnOnce(&mut WebConfig) -> &mut WebConfig) {
|
||||
launch_with_props(root, (), config_builder)
|
||||
pub fn launch_cfg(root: Component, config: Config) {
|
||||
launch_with_props(root, (), config)
|
||||
}
|
||||
|
||||
/// Launches the VirtualDOM from the specified component function and props.
|
||||
@@ -130,7 +130,7 @@ pub fn launch_cfg(root: Component, config_builder: impl FnOnce(&mut WebConfig) -
|
||||
/// dioxus_web::launch_with_props(
|
||||
/// App,
|
||||
/// RootProps { name: String::from("joe") },
|
||||
/// |config| config
|
||||
/// Config::new()
|
||||
/// );
|
||||
/// }
|
||||
///
|
||||
@@ -143,19 +143,10 @@ pub fn launch_cfg(root: Component, config_builder: impl FnOnce(&mut WebConfig) -
|
||||
/// rsx!(cx, div {"hello {cx.props.name}"})
|
||||
/// }
|
||||
/// ```
|
||||
pub fn launch_with_props<T>(
|
||||
root_component: Component<T>,
|
||||
root_properties: T,
|
||||
configuration_builder: impl FnOnce(&mut WebConfig) -> &mut WebConfig,
|
||||
) where
|
||||
pub fn launch_with_props<T>(root_component: Component<T>, root_properties: T, config: Config)
|
||||
where
|
||||
T: Send + 'static,
|
||||
{
|
||||
if cfg!(feature = "panic_hook") {
|
||||
console_error_panic_hook::set_once();
|
||||
}
|
||||
|
||||
let mut config = WebConfig::default();
|
||||
configuration_builder(&mut config);
|
||||
wasm_bindgen_futures::spawn_local(run_with_props(root_component, root_properties, config));
|
||||
}
|
||||
|
||||
@@ -171,9 +162,13 @@ pub fn launch_with_props<T>(
|
||||
/// wasm_bindgen_futures::spawn_local(app_fut);
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn run_with_props<T: 'static + Send>(root: Component<T>, root_props: T, cfg: WebConfig) {
|
||||
pub async fn run_with_props<T: 'static + Send>(root: Component<T>, root_props: T, cfg: Config) {
|
||||
let mut dom = VirtualDom::new_with_props(root, root_props);
|
||||
|
||||
if cfg!(feature = "panic_hook") && cfg.default_panic_hook {
|
||||
console_error_panic_hook::set_once();
|
||||
}
|
||||
|
||||
#[cfg(feature = "hot-reload")]
|
||||
hot_reload::init(&dom);
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ use web_sys::{
|
||||
HtmlOptionElement, HtmlTextAreaElement, Node,
|
||||
};
|
||||
|
||||
use crate::{nodeslab::NodeSlab, WebConfig};
|
||||
use crate::{nodeslab::NodeSlab, Config};
|
||||
|
||||
pub struct WebsysDom {
|
||||
stack: Stack,
|
||||
@@ -39,7 +39,7 @@ pub struct WebsysDom {
|
||||
type ListenerEntry = (usize, Closure<dyn FnMut(&Event)>);
|
||||
|
||||
impl WebsysDom {
|
||||
pub fn new(cfg: WebConfig, sender_callback: Rc<dyn Fn(SchedulerMsg)>) -> Self {
|
||||
pub fn new(cfg: Config, sender_callback: Rc<dyn Fn(SchedulerMsg)>) -> Self {
|
||||
let document = load_document();
|
||||
|
||||
let nodes = NodeSlab::new(2000);
|
||||
|
||||
Reference in new issue
Block a user