Feat: add global context functions
Add functions like window() and router() to allow dynamically grabbing global contexts without have to use the hook variants. Deprecates the existing hook variants to discourage folks from adding more noise to their codebases.
This commit is contained in:
16 files changed
+77
-78
No files matched your search
@@ -28,6 +28,15 @@ use wry::webview::WebView;
|
||||
|
||||
pub type ProxyType = EventLoopProxy<UserWindowEvent>;
|
||||
|
||||
/// Get an imperative handle to the current window without using a hook
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This function will panic if it is called outside of the context of a Dioxus App.
|
||||
pub fn window() -> DesktopContext {
|
||||
dioxus_core::prelude::consume_context().unwrap()
|
||||
}
|
||||
|
||||
/// Get an imperative handle to the current window
|
||||
pub fn use_window(cx: &ScopeState) -> &DesktopContext {
|
||||
cx.use_hook(|| cx.consume_context::<DesktopContext>())
|
||||
|
||||
@@ -23,7 +23,7 @@ use crate::query::QueryResult;
|
||||
pub use cfg::{Config, WindowCloseBehaviour};
|
||||
pub use desktop_context::DesktopContext;
|
||||
pub use desktop_context::{
|
||||
use_window, use_wry_event_handler, DesktopService, WryEventHandler, WryEventHandlerId,
|
||||
use_window, use_wry_event_handler, window, DesktopService, WryEventHandler, WryEventHandlerId,
|
||||
};
|
||||
use desktop_context::{EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers};
|
||||
use dioxus_core::*;
|
||||
|
||||
@@ -48,6 +48,15 @@ pub fn use_eval(cx: &ScopeState) -> &EvalCreator {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn eval(script: &str) -> Result<UseEval, EvalError> {
|
||||
let eval_provider = dioxus_core::prelude::consume_context::<Rc<dyn EvalProvider>>()
|
||||
.expect("evaluator not provided");
|
||||
|
||||
eval_provider
|
||||
.new_evaluator(script.to_string())
|
||||
.map(UseEval::new)
|
||||
}
|
||||
|
||||
/// A wrapper around the target platform's evaluator.
|
||||
#[derive(Clone)]
|
||||
pub struct UseEval {
|
||||
|
||||
@@ -66,7 +66,6 @@ fn Route2(cx: Scope, user_id: usize) -> Element {
|
||||
|
||||
#[component]
|
||||
fn Route3(cx: Scope, dynamic: String) -> Element {
|
||||
let navigator = use_navigator(cx);
|
||||
let current_route = use_route(cx)?;
|
||||
let current_route_str = use_ref(cx, String::new);
|
||||
let parsed = Route::from_str(¤t_route_str.read());
|
||||
@@ -78,9 +77,7 @@ fn Route3(cx: Scope, dynamic: String) -> Element {
|
||||
|
||||
render! {
|
||||
input {
|
||||
oninput: move |evt| {
|
||||
*current_route_str.write() = evt.value.clone();
|
||||
},
|
||||
oninput: move |evt| *current_route_str.write() = evt.value.clone(),
|
||||
value: "{current_route_str.read()}"
|
||||
}
|
||||
"dynamic: {dynamic}"
|
||||
@@ -89,7 +86,7 @@ fn Route3(cx: Scope, dynamic: String) -> Element {
|
||||
"hello world link"
|
||||
}
|
||||
button {
|
||||
onclick: move |_| { navigator.push("https://www.google.com"); },
|
||||
onclick: move |_| { dioxus_router::router().push("https://www.google.com"); },
|
||||
"google link"
|
||||
}
|
||||
p { "Site Map" }
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
use crate::prelude::{ExternalNavigationFailure, IntoRoutable, RouterContext};
|
||||
|
||||
/// Acquire the navigator without subscribing to updates.
|
||||
///
|
||||
/// Can be called anywhere in the application provided a Router has been initialized.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if there is no router present.
|
||||
pub fn navigator() -> Navigator {
|
||||
Navigator(
|
||||
dioxus::core::prelude::consume_context::<RouterContext>()
|
||||
.expect("A router must be present to use navigator"),
|
||||
)
|
||||
}
|
||||
|
||||
/// A view into the navigation state of a router.
|
||||
#[derive(Clone)]
|
||||
pub struct Navigator(pub(crate) RouterContext);
|
||||
|
||||
@@ -49,6 +49,7 @@ use crate::prelude::{Navigator, RouterContext};
|
||||
/// # let _ = vdom.rebuild();
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[deprecated = "Prefer acquiring the router directly with `dioxus_router::router()`"]
|
||||
pub fn use_navigator(cx: &ScopeState) -> &Navigator {
|
||||
&*cx.use_hook(|| {
|
||||
let router = cx
|
||||
|
||||
@@ -2,7 +2,7 @@ use dioxus::prelude::ScopeState;
|
||||
|
||||
use crate::{prelude::RouterContext, utils::use_router_internal::use_router_internal};
|
||||
|
||||
#[deprecated = "prefer the use_navigator or use_route functions"]
|
||||
#[deprecated = "prefer the `router()` function or `use_route` functions"]
|
||||
#[must_use]
|
||||
/// A hook that provides access to information about the router.
|
||||
pub fn use_router(cx: &ScopeState) -> &RouterContext {
|
||||
@@ -10,3 +10,8 @@ pub fn use_router(cx: &ScopeState) -> &RouterContext {
|
||||
.as_ref()
|
||||
.expect("use_route must have access to a router")
|
||||
}
|
||||
|
||||
/// Aquire the router without subscribing to updates.
|
||||
pub fn router() -> RouterContext {
|
||||
dioxus::core::prelude::consume_context().unwrap()
|
||||
}
|
||||
@@ -53,6 +53,8 @@ pub mod hooks {
|
||||
pub use use_navigator::*;
|
||||
}
|
||||
|
||||
pub use hooks::router;
|
||||
|
||||
/// A collection of useful items most applications might need.
|
||||
pub mod prelude {
|
||||
pub use crate::components::*;
|
||||
|
||||
Reference in new issue
Block a user