fix: namespacing of dynamic attributes

This commit is contained in:
Jonathan Kelley 2022-12-07 15:11:51 -08:00
parent db5b65b6cb
commit 0bf4725639
9 changed files with 24 additions and 31 deletions

View file

@ -8,6 +8,6 @@ struct AppSettings {}
// ANCHOR: wrap_context // ANCHOR: wrap_context
fn use_settings(cx: &ScopeState) -> UseSharedState<AppSettings> { 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 // ANCHOR_END: wrap_context

View file

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

View file

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

View file

@ -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 state = cx.use_hook(|| {
let scope_id = cx.scope_id(); 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() { if let Some(root) = root.as_ref() {
root.borrow_mut().consumers.insert(scope_id); 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. /// Provide some state for components down the hierarchy to consume without having to drill props.
/// pub fn use_shared_state_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) {
///
///
///
///
///
///
pub fn use_context_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) {
cx.use_hook(|| { cx.use_hook(|| {
let state: ProvidedState<T> = Rc::new(RefCell::new(ProvidedStateInner { let state: ProvidedState<T> = Rc::new(RefCell::new(ProvidedStateInner {
value: Rc::new(RefCell::new(f())), value: Rc::new(RefCell::new(f())),

View file

@ -1,6 +1,8 @@
use dioxus_core::ScopeState; use dioxus_core::ScopeState;
/// Consume some context in the tree, providing a sharable handle to the value /// 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> { pub fn use_context<T: 'static + Clone>(cx: &ScopeState) -> Option<&T> {
cx.use_hook(|| cx.consume_context::<T>()).as_ref() cx.use_hook(|| cx.consume_context::<T>()).as_ref()
} }

View file

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

View file

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

View file

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

View file

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