diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index eb54bed1a..716f4b3a3 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -75,7 +75,7 @@ pub(crate) mod innerlude { pub use crate::innerlude::{ fc_to_builder, generation, schedule_update, schedule_update_any, use_hook, vdom_is_rendering, - AnyValue, Attribute, AttributeValue, BoxedContext, CapturedError, Component, + AnyValue, Attribute, AttributeValue, BoxedContext, CapturedError, Component, ComponentFunction, CrossPlatformConfig, DynamicNode, Element, ElementId, Event, Fragment, HasAttributes, IntoDynNode, Mutation, Mutations, NoOpMutations, PlatformBuilder, Properties, RenderReturn, ScopeId, ScopeState, Task, Template, TemplateAttribute, TemplateNode, VComponent, VNode, @@ -91,7 +91,7 @@ pub mod prelude { has_context, needs_update, parent_scope, provide_context, provide_root_context, remove_future, schedule_update, schedule_update_any, spawn, spawn_forever, suspend, try_consume_context, use_error_boundary, use_hook, AnyValue, Attribute, Component, - ComponentFn, Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes, + ComponentFunction, Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes, IntoAttributeValue, IntoDynNode, Properties, Runtime, RuntimeGuard, ScopeId, ScopeState, Task, Template, TemplateAttribute, TemplateNode, Throw, VNode, VNodeInner, VirtualDom, }; diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index edea9d025..40d9c1b7a 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -5,7 +5,7 @@ use crate::{ use crate::{arena::ElementId, Element, Event}; use crate::{ innerlude::{ElementRef, EventHandler, MountId}, - properties::ComponentFn, + properties::ComponentFunction, }; use crate::{Properties, VirtualDom}; use core::panic; @@ -533,7 +533,11 @@ impl VComponent { /// fn(Props) -> Element; /// async fn(Scope>) -> Element; /// ``` - pub fn new(component: impl ComponentFn, props: P, fn_name: &'static str) -> Self + pub fn new( + component: impl ComponentFunction, + props: P, + fn_name: &'static str, + ) -> Self where // The properties must be valid until the next bump frame P: Properties + 'static, diff --git a/packages/core/src/platform.rs b/packages/core/src/platform.rs index 79a24151a..ea6e5e06e 100644 --- a/packages/core/src/platform.rs +++ b/packages/core/src/platform.rs @@ -1,6 +1,6 @@ use std::any::Any; -use crate::{properties::ComponentFn, Component, VirtualDom}; +use crate::{properties::ComponentFunction, Component, VirtualDom}; /// A boxed object that can be injected into a component's context. pub struct BoxedContext(Box); @@ -52,12 +52,12 @@ pub struct CrossPlatformConfig { impl CrossPlatformConfig { /// Create a new cross-platform config. pub fn new( - component: impl ComponentFn, + component: impl ComponentFunction, props: Props, root_contexts: Vec, ) -> Self { Self { - component: ComponentFn::as_component(std::rc::Rc::new(component)), + component: ComponentFunction::as_component(std::rc::Rc::new(component)), props, root_contexts, } diff --git a/packages/core/src/properties.rs b/packages/core/src/properties.rs index 73e36ae4b..e0ad12f7b 100644 --- a/packages/core/src/properties.rs +++ b/packages/core/src/properties.rs @@ -54,7 +54,7 @@ impl EmptyBuilder { /// This utility function launches the builder method so rsx! and html! macros can use the typed-builder pattern /// to initialize a component's props. -pub fn fc_to_builder(_: impl ComponentFn) ->

::Builder +pub fn fc_to_builder(_: impl ComponentFunction) ->

::Builder where P: Properties, { @@ -62,20 +62,23 @@ where } /// Any component that implements the `ComponentFn` trait can be used as a component. -pub trait ComponentFn { +pub trait ComponentFunction { + type Props; /// Convert the component to a function that takes props and returns an element. fn as_component(self: Rc) -> Component; } /// Accept pre-formed component render functions as components -impl

ComponentFn for Component

{ +impl

ComponentFunction for Component

{ + type Props = P; fn as_component(self: Rc) -> Component

{ self.as_ref().clone() } } /// Accept any callbacks that take props -impl Element + 'static, P> ComponentFn for F { +impl Element + 'static, P> ComponentFunction for F { + type Props = P; fn as_component(self: Rc) -> Component

{ self } @@ -83,7 +86,8 @@ impl Element + 'static, P> ComponentFn for F { /// Accept any callbacks that take no props pub struct EmptyMarker; -impl Element + 'static> ComponentFn<(), EmptyMarker> for F { +impl Element + 'static> ComponentFunction<(), EmptyMarker> for F { + type Props = (); fn as_component(self: Rc) -> Rc Element> { Rc::new(move |_| self()) } @@ -98,7 +102,8 @@ fn it_works_maybe() { todo!() } - let callable: Rc> = Rc::new(test) as Rc>; - let callable2: Rc> = - Rc::new(test2) as Rc>; + let callable: Rc> = + Rc::new(test) as Rc>; + let callable2: Rc> = + Rc::new(test2) as Rc>; } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 724fae850..d5becf10d 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -11,7 +11,7 @@ use crate::{ }, nodes::RenderReturn, nodes::{Template, TemplateId}, - properties::ComponentFn, + properties::ComponentFunction, runtime::{Runtime, RuntimeGuard}, scopes::ScopeId, AttributeValue, BoxedContext, Element, Event, Mutations, @@ -269,7 +269,7 @@ impl VirtualDom { /// let mutations = dom.rebuild(); /// ``` pub fn new_with_props( - root: impl ComponentFn, + root: impl ComponentFunction, root_props: P, ) -> Self { let (tx, rx) = futures_channel::mpsc::unbounded(); diff --git a/packages/dioxus/src/launch.rs b/packages/dioxus/src/launch.rs index 9b1647edf..f32c423bd 100644 --- a/packages/dioxus/src/launch.rs +++ b/packages/dioxus/src/launch.rs @@ -16,7 +16,7 @@ pub struct LaunchBuilder LaunchBuilder { /// Create a new builder for your application. This will create a launch configuration for the current platform based on the features enabled on the `dioxus` crate. - pub fn new(component: impl ComponentFn) -> Self + pub fn new(component: impl ComponentFunction) -> Self where Props: Default, { @@ -97,7 +97,7 @@ type CurrentPlatform = dioxus_web::WebPlatform; type CurrentPlatform = (); /// Launch your application without any additional configuration. See [`LaunchBuilder`] for more options. -pub fn launch(component: impl ComponentFn) +pub fn launch(component: impl ComponentFunction) where Props: Default + Clone + 'static, { @@ -106,7 +106,7 @@ where #[cfg(feature = "web")] /// Launch your web application without any additional configuration. See [`LaunchBuilder`] for more options. -pub fn launch_web(component: impl ComponentFn) +pub fn launch_web(component: impl ComponentFunction) where Props: Default + Clone + 'static, { @@ -115,7 +115,7 @@ where #[cfg(feature = "desktop")] /// Launch your desktop application without any additional configuration. See [`LaunchBuilder`] for more options. -pub fn launch_desktop(component: impl ComponentFn) +pub fn launch_desktop(component: impl ComponentFunction) where Props: Default + Clone + 'static, {