feat: provide `leptos_router::Method` via context (#1808) (#2315)

This commit is contained in:
zoomiti 2024-02-26 18:25:53 -08:00 committed by GitHub
parent aa977001c1
commit 4809cf473e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 10 deletions

View File

@ -1215,13 +1215,18 @@ where
let mode = listing.mode();
for method in listing.methods() {
let additional_context = additional_context.clone();
let additional_context_and_method = move || {
provide_context(method);
additional_context();
};
router = if let Some(static_mode) = listing.static_mode() {
router.route(
path,
static_route(
options.clone(),
app_fn.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
method,
static_mode,
),
@ -1233,7 +1238,7 @@ where
SsrMode::OutOfOrder => {
render_app_to_stream_with_context(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
)
@ -1241,7 +1246,7 @@ where
SsrMode::PartiallyBlocked => {
render_app_to_stream_with_context_and_replace_blocks(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
true,
@ -1250,14 +1255,14 @@ where
SsrMode::InOrder => {
render_app_to_stream_in_order_with_context(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
)
}
SsrMode::Async => render_app_async_with_context(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
),

View File

@ -1615,6 +1615,11 @@ where
let path = listing.path();
for method in listing.methods() {
let cx_with_state = cx_with_state.clone();
let cx_with_state_and_method = move || {
provide_context(method);
cx_with_state();
};
router = if let Some(static_mode) = listing.static_mode() {
#[cfg(feature = "default")]
{
@ -1623,7 +1628,7 @@ where
path,
LeptosOptions::from_ref(options),
app_fn.clone(),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
method,
static_mode,
)
@ -1643,7 +1648,7 @@ where
SsrMode::OutOfOrder => {
let s = render_app_to_stream_with_context(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
);
match method {
@ -1657,7 +1662,7 @@ where
SsrMode::PartiallyBlocked => {
let s = render_app_to_stream_with_context_and_replace_blocks(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
true
);
@ -1672,7 +1677,7 @@ where
SsrMode::InOrder => {
let s = render_app_to_stream_in_order_with_context(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
);
match method {
@ -1686,7 +1691,7 @@ where
SsrMode::Async => {
let s = render_app_async_with_context(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
);
match method {

View File

@ -410,6 +410,11 @@ impl RouteContext {
pub fn outlet(&self) -> impl IntoView {
(self.inner.outlet)()
}
/// The http method used to navigate to this route. Defaults to [`Method::Get`] when unavailable like in client side routing
pub fn method(&self) -> Method {
use_context().unwrap_or_default()
}
}
pub(crate) struct RouteContextInner {