Saurus119.Blazor.SwipeBack
1.1.1
dotnet add package Saurus119.Blazor.SwipeBack --version 1.1.1
NuGet\Install-Package Saurus119.Blazor.SwipeBack -Version 1.1.1
<PackageReference Include="Saurus119.Blazor.SwipeBack" Version="1.1.1" />
<PackageVersion Include="Saurus119.Blazor.SwipeBack" Version="1.1.1" />
<PackageReference Include="Saurus119.Blazor.SwipeBack" />
paket add Saurus119.Blazor.SwipeBack --version 1.1.1
#r "nuget: Saurus119.Blazor.SwipeBack, 1.1.1"
#:package Saurus119.Blazor.SwipeBack@1.1.1
#addin nuget:?package=Saurus119.Blazor.SwipeBack&version=1.1.1
#tool nuget:?package=Saurus119.Blazor.SwipeBack&version=1.1.1
Blazor.SwipeBack
The iOS / Messenger back gesture for Blazor. Drag from the edge of the screen, the page follows your finger, let go past a third of the width - or flick - and it slides out and you navigate back. Let go early and it snaps back.
Works in MAUI Blazor Hybrid (iOS, Android), Blazor WebAssembly and Blazor Server. The JavaScript ships inside the package as a static web asset and is imported on demand: no npm, no CDN, no <script> tag.
Nothing native is involved. It is touch events on the element you wrap, which is why the same package serves the app and the web client, and why it works on Android as well as iOS.
Installation
dotnet add package Saurus119.Blazor.SwipeBack
Usage
Wrap the page. The wrapped element is what slides, so give it the page's own layout classes - it has to be the whole page, not a scroller inside it.
@using Saurus119.Blazor.SwipeBack
@inject NavigationManager Navigation
<SwipeBack class="flex flex-col h-full" OnSwipedBack="() => Navigation.NavigateTo("/chats")">
<header>…</header>
<div class="flex-1 overflow-y-auto">…messages…</div>
</SwipeBack>
That is the whole integration. OnSwipedBack fires once the page has left the screen; navigate there. The component does not know where back is.
Options
<SwipeBack Options="_options" OnSwipedBack="…">…</SwipeBack>
@code {
private readonly SwipeBackOptions _options = new()
{
EdgeWidthInPixels = 28, // a touch must start this close to the edge
CommitFraction = 1.0 / 3.0, // drag past this share of the width to leave
FlickVelocityInPixelsPerMillisecond = 0.6, // or flick faster than this
AnimationDurationInMilliseconds = 200, // leave and snap-back animation
ShouldCastShadow = true, // depth cue on the trailing side while dragging
Edge = SwipeBackEdge.Left // Right for right-to-left layouts
};
}
Every option has the default shown. SlopInPixels (8) is how far the finger moves before the touch stops being a tap and commits to an axis.
Callbacks
| Callback | When |
|---|---|
OnSwipedBack |
The page has left the screen. Required. |
OnSwipeStarted |
The drag was recognised as a back gesture and the page started following the finger. |
OnSwipeCancelled |
Released short of the threshold; the page is snapping back. |
OnSwipeProgress |
Every move, with 0..1 of the width travelled. Only wired up when a handler is set - it costs one interop call per touch move. |
OnSwipeProgress is for things only .NET can do with the progress - a haptic at the threshold, a counter. For a scrim or a parallax behind the page use Underlay below instead: it does the same job without a render per touch move.
Two ways to wire it
1. A page that navigates away. The simplest integration, shown above: wrap the routed page, navigate in OnSwipedBack. The page slides out over a plain background (the shadow is the only depth cue), then Blazor swaps in the destination page - a new component instance, possibly a new layout. Fine for a detail page that goes back to somewhere it does not need to show.
2. An overlay on the page it goes back to - the Messenger way. Messenger shows the list you are returning to underneath the conversation as you drag, and when you let go nothing reloads: the conversation was never a page of its own, it was drawn on top of the list. Blazor renders one routed page at a time, so to get this the list page has to own both routes and render the detail as an overlay on itself. Closing the detail is then a parameter change on the same component instance - no re-mount, no layout swap, the list keeps its scroll position, and the screen the swipe reveals is the live one.
@page "/chats"
@page "/chats/{ConversationId:guid}"
<div @ref="_root" class="relative flex flex-col h-full">
<ConversationList />
@if(ConversationId is not null)
{
@* Scrim over the list, fading as the overlay leaves. *@
<div class="absolute inset-0 bg-black pointer-events-none"
style="opacity: calc(0.5 * (1 - var(--swipe-back-progress, 0)))"></div>
}
</div>
@if(ConversationId is not null)
{
<SwipeBack class="fixed inset-0 flex flex-col bg-zinc-950"
Underlay="_root"
OnSwipedBack="() => Navigation.NavigateTo("/chats")">
<ConversationDetail ConversationId="ConversationId.Value" />
</SwipeBack>
}
@code {
[Parameter] public Guid? ConversationId { get; set; }
private ElementReference _root;
}
Hand the element behind the overlay over as Underlay. The package parks it UnderlayParallax (default 0.3) of the width behind the edge at rest - invisible under the opaque overlay - slides it into place as the overlay is dragged out, and sets --swipe-back-progress (0..1) on it for a scrim to fade on. All of that is JavaScript; a drag costs no interop per move. NavigateTo("/chats") at the end only clears ConversationId: same page component, same layout, nothing re-mounts.
Things to get right in the overlay pattern:
- The overlay must be opaque - the underlay sits behind it at rest.
- Layering. The overlay is
fixed, so give it az-indexabove your bottom navigation and below your sheets and modals, and add the safe-area padding your layout would otherwise provide. - Deep links. When the overlay renders in the page's very first pass, the underlay's element reference does not exist yet. Call
StateHasChanged()once fromOnAfterRender(firstRender: true)so the overlay receives the real one; the component picks up a changedUnderlayon any re-render. - Only underlay what you actually return to. If a detail can also go back to some other page (a
returnUrl), passUnderlay="null"for that case - a list peeking out from under a page that leaves for somewhere else is a lie.
Disabling
IsEnabled="false" turns the gesture off without unmounting anything - while a sheet owns the screen, or on a page that has nothing to go back to. A drag in progress is cancelled when it flips to false.
How it behaves
- One touch is one axis. The first move past the slop decides: away from the edge and faster than it climbs is a back gesture, anything else is the page's own scroll and the page never moves. A swipe that begins anywhere but the edge is never a back gesture, whatever it does afterwards, so swiping across a message does nothing.
- A scroll the browser has already started wins. If the vertical pan is under way before the slop is crossed the touchmove is no longer cancelable, and the gesture yields - the page must not scroll and slide at once.
- The page never goes past the edge it came from. Dragging back towards the edge clamps at zero.
- A cancelled touch never leaves.
touchcancel(the OS took the touch, a call came in) snaps back. - If
OnSwipedBackdoes not navigate, the page is put back rather than left parked off-screen - afterRestoreDelayInMilliseconds(default 300), because a navigation started in the callback removes the page only after the callback has returned, and putting it back any sooner would flash it over the screen behind.
Android
With gesture navigation the OS claims the outermost sliver of the screen for its own back gesture, so there the system back fires first - which is also back. On three-button navigation, and on iOS, this gesture owns the edge.
Demo
demo/ is a WebAssembly app wired the overlay way (pattern 2). Run it and open it on a phone, or use the device toolbar in DevTools - the gesture is touch only.
dotnet run --project demo/Blazor.SwipeBack.Demo.csproj
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.Components.Web (>= 10.0.0)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.36)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.20)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fix: the page no longer flashes back over the screen behind after a committed swipe. It stays parked off-screen while the navigation started in OnSwipedBack lands and is put back only if nothing removed it within RestoreDelayInMilliseconds (new option, default 300).