1
0
Эх сурвалжийг харах

fix: namespacing of dynamic attributes

Jonathan Kelley 2 жил өмнө
parent
commit
0bf4725639

+ 1 - 1
docs/guide/examples/hooks_composed.rs

@@ -8,6 +8,6 @@ struct AppSettings {}
 
 // ANCHOR: wrap_context
 fn use_settings(cx: &ScopeState) -> UseSharedState<AppSettings> {
-    use_context::<AppSettings>(cx).expect("App settings not provided")
+    use_shared_state::<AppSettings>(cx).expect("App settings not provided")
 }
 // ANCHOR_END: wrap_context

+ 3 - 4
docs/guide/examples/meme_editor_dark_mode.rs

@@ -1,7 +1,6 @@
 // ANCHOR: all
 #![allow(non_snake_case)]
 
-use dioxus::events::FormData;
 use dioxus::prelude::*;
 
 fn main() {
@@ -14,7 +13,7 @@ struct DarkMode(bool);
 
 pub fn App(cx: Scope) -> Element {
     // ANCHOR: context_provider
-    use_context_provider(cx, || DarkMode(false));
+    use_shared_state_provider(cx, || DarkMode(false));
     // ANCHOR_END: context_provider
 
     let is_dark_mode = use_is_dark_mode(cx);
@@ -37,7 +36,7 @@ pub fn App(cx: Scope) -> Element {
 
 pub fn use_is_dark_mode(cx: &ScopeState) -> bool {
     // ANCHOR: use_context
-    let dark_mode_context = use_context::<DarkMode>(cx);
+    let dark_mode_context = use_shared_state::<DarkMode>(cx);
     // ANCHOR_END: use_context
 
     dark_mode_context
@@ -47,7 +46,7 @@ pub fn use_is_dark_mode(cx: &ScopeState) -> bool {
 
 // ANCHOR: toggle
 pub fn DarkModeToggle(cx: Scope) -> Element {
-    let dark_mode = use_context::<DarkMode>(cx)?;
+    let dark_mode = use_shared_state::<DarkMode>(cx).unwrap();
 
     let style = if dark_mode.read().0 {
         "color:white"

+ 2 - 2
packages/hooks/src/lib.rs

@@ -30,8 +30,8 @@ pub use usestate::{use_state, UseState};
 mod useref;
 pub use useref::*;
 
-// mod use_shared_state;
-// pub use use_shared_state::*;
+mod use_shared_state;
+pub use use_shared_state::*;
 
 mod usecoroutine;
 pub use usecoroutine::*;

+ 3 - 10
packages/hooks/src/use_shared_state.rs

@@ -60,10 +60,10 @@ impl<T> ProvidedStateInner<T> {
 ///
 ///
 ///
-pub fn use_context<T: 'static>(cx: &ScopeState) -> Option<UseSharedState<T>> {
+pub fn use_shared_state<T: 'static>(cx: &ScopeState) -> Option<UseSharedState<T>> {
     let state = cx.use_hook(|| {
         let scope_id = cx.scope_id();
-        let root = cx.consume_context::<ProvidedState<T>>().cloned();
+        let root = cx.consume_context::<ProvidedState<T>>();
 
         if let Some(root) = root.as_ref() {
             root.borrow_mut().consumers.insert(scope_id);
@@ -165,14 +165,7 @@ where
 }
 
 /// Provide some state for components down the hierarchy to consume without having to drill props.
-///
-///
-///
-///
-///
-///
-///
-pub fn use_context_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) {
+pub fn use_shared_state_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) {
     cx.use_hook(|| {
         let state: ProvidedState<T> = Rc::new(RefCell::new(ProvidedStateInner {
             value: Rc::new(RefCell::new(f())),

+ 2 - 0
packages/hooks/src/usecontext.rs

@@ -1,6 +1,8 @@
 use dioxus_core::ScopeState;
 
 /// Consume some context in the tree, providing a sharable handle to the value
+///
+/// Does not regenerate the value if the value is changed at the parent.
 pub fn use_context<T: 'static + Clone>(cx: &ScopeState) -> Option<&T> {
     cx.use_hook(|| cx.consume_context::<T>()).as_ref()
 }

+ 4 - 5
packages/hooks/src/usecoroutine.rs

@@ -2,8 +2,6 @@ use dioxus_core::{ScopeState, TaskId};
 pub use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
 use std::future::Future;
 
-use crate::{use_context, use_context_provider};
-
 /// Maintain a handle over a future that can be paused, resumed, and canceled.
 ///
 /// This is an upgraded form of [`use_future`] with an integrated channel system.
@@ -67,10 +65,10 @@ where
     G: FnOnce(UnboundedReceiver<M>) -> F,
     F: Future<Output = ()> + 'static,
 {
-    use_context_provider(cx, || {
+    cx.use_hook(|| {
         let (tx, rx) = futures_channel::mpsc::unbounded();
         let task = cx.push_future(init(rx));
-        Coroutine { tx, task }
+        cx.provide_context(Coroutine { tx, task })
     })
 }
 
@@ -78,7 +76,8 @@ where
 ///
 /// See the docs for [`use_coroutine`] for more details.
 pub fn use_coroutine_handle<M: 'static>(cx: &ScopeState) -> Option<&Coroutine<M>> {
-    use_context::<Coroutine<M>>(cx)
+    cx.use_hook(|| cx.consume_context::<Coroutine<M>>())
+        .as_ref()
 }
 
 pub struct Coroutine<T> {

+ 3 - 3
packages/router/src/components/router.rs

@@ -39,15 +39,15 @@ pub struct RouterProps<'a> {
 /// Will fallback to HashRouter is BrowserRouter is not available, or through configuration.
 #[allow(non_snake_case)]
 pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
-    let svc = use_context_provider(cx, || {
-        RouterService::new(
+    let svc = cx.use_hook(|| {
+        cx.provide_context(RouterService::new(
             cx,
             RouterCfg {
                 base_url: cx.props.base_url.map(|s| s.to_string()),
                 active_class: cx.props.active_class.map(|s| s.to_string()),
                 initial_url: cx.props.initial_url.clone(),
             },
-        )
+        ))
     });
 
     // next time we run the rout_found will be filled

+ 4 - 4
packages/rsx/src/element.rs

@@ -192,16 +192,16 @@ impl ToTokens for Element {
 
 #[derive(PartialEq, Eq, Clone, Debug, Hash)]
 pub enum ElementAttr {
-    /// attribute: "valuee {}"
+    /// `attribute: "value"`
     AttrText { name: Ident, value: IfmtInput },
 
-    /// attribute: true,
+    /// `attribute: true`
     AttrExpression { name: Ident, value: Expr },
 
-    /// "attribute": "value {}"
+    /// `"attribute": "value"`
     CustomAttrText { name: LitStr, value: IfmtInput },
 
-    /// "attribute": true,
+    /// `"attribute": true`
     CustomAttrExpression { name: LitStr, value: Expr },
 
     // /// onclick: move |_| {}

+ 2 - 2
packages/rsx/src/lib.rs

@@ -199,8 +199,8 @@ impl<'a> DynamicContext<'a> {
                         let value = value.source.as_ref().unwrap();
                         quote! {
                             ::dioxus::core::TemplateAttribute::Static {
-                                name: dioxus_elements::#el_name::#name.0,
-                                namespace: dioxus_elements::#el_name::#name.1,
+                                name: #name,
+                                namespace: None,
                                 value: #value,
 
                                 // todo: we don't diff these so we never apply the volatile flag