Stackflow-style stack navigation for the Next.js App Router, with server rendering left intact.
Demo · Architecture · npm · GitHub
Built on Stackflow's core, it brings app-style stack navigation to the Next.js App Router: slides that know forward from Back, an edge-swipe Back gesture, scroll restored per screen and a scrollbar that respects your fixed bars. Each screen stays an ordinary Next.js route.
- Server Components, SSR and prefetching stay intact. Stackflow itself renders every screen inside one client
<Stack />, and its README marks the Next.js example "Not Recommended". Here every screen remains its own route:StackflowProviderrenders server-rendered children as passed, the onlyuseSearchParamscall sits in its own<Suspense>, andStackLinkrendersnext/link, so prefetching and RSC payloads are untouched. - Back and Forward animate in the right direction, automatically. The Next.js guide states that browser Back/Forward, history swipes and
router.back()do not trigger view transition animations, and its directional slides needtransitionTypeson each link. This package reads direction from the history position, so browser Back plays the reverse slide with no per-link setup. - Swipe Back with the previous screen visible. An edge swipe drags the page and shows the screen underneath before committing.
- Scroll position per history entry, restored on Back and Forward.
- The scroller is never replaced. The document keeps scrolling, so
window.scrollTo,scrollYand scroll listeners behave as before. Only the scrollbar changes: an OverlayScrollbars bar you can drag or click, kept between fixed headers, footers and tab bars, with a horizontal bar only when the page overflows sideways. - Light at runtime. All exports bundled, minified and gzipped come to about 31.5 KB (OverlayScrollbars 15.3 KB,
@stackflow/core6.0 KB, this package about 10 KB); the same measurement of@ssgoi/reactwith its view-transitions entry is 44.0 KB. The scrollbar handle follows aScrollTimelinewhere the browser supports it, the track is re-measured only when layout-relevant DOM changes (class,style,hidden), and UI state attributes such asaria-*cause no layout reads. - Your Next.js config wins.
withStackflowturns on Cache Components only if you have not set it. With it on, screens keep their state on Back; with it off, transitions, swipe Back and scroll restoration still work.
Not made by or affiliated with the Stackflow team at Daangn.
- Slide transition between routes; vertical slide for routes in
modalRoutes. - Edge-swipe Back (
swipeBack) with a preview of the previous screen. - Scroll offset restored per history entry (
preserveScroll). - Document scrolling by default (
window.scrollTo,window.scrollYkeep working); an independent scroll container also works (scrollTarget). - Only the scrollbar changes, never the scroller: the document still scrolls, so
window.scrollTo,window.scrollY,scrollTopandscrollevents behave as without the package. The browser's own scrollbar is hidden on<html>and<body>, and the drawn scrollbar replaces it. - A vertical scrollbar drawn with OverlayScrollbars: its track stays between fixed headers, footers and tab bars, follows whichever scroller is moving, and can be dragged or clicked. A horizontal bar appears along the bottom of the track only when the page overflows sideways.
scrollbar={{ autoHide, autoHideDelay, autoHideSuspend }}sets when it hides. edgeBounce: optional touch bounce at scroll boundaries on non-Apple touch devices.skipSlideQueryKeys: skip the page slide when only a popup query parameter changes.fixedTargetskeeps chosen elements still during the slide.waitForPageandskeletonRoutescontrol when the slide starts.StackMotionapplies the same slides to local show/hide state.StackLink,useStackRouter,useStack,useStackScroll,useStackState.persistent,headerandfooterslots outside the transition.- Loading text in ten languages;
prefers-reduced-motionis honored. ./domentry withcreateApelike, a DOM renderer that does not use React.
- Not tested on real devices. Tests run in a simulated DOM and the demo.
- Parallel and intercepting routes are untested.
- No state retention of its own. State survives only while Next.js keeps the route, and not across a reload.
- No fallback animation. Browsers without View Transitions use ordinary navigation.
- The swipe preview is a DOM clone; canvas, video and embedded content may differ.
- Next.js 13 to 15 do not keep ordinary component state on Back; that needs Cache Components in Next.js 16.
useStackStateworks on every version. - No benchmarks against other libraries.
- No physics or spring animation. Motion is CSS and the Web Animations API.
- The drawn scrollbar no longer shrinks during elastic overscroll;
edgeBouncestill compresses it.
npm install @uiwwsw/stackflow-nextWorks with Next.js 13.4 or later (App Router) and React 18.2 or later. Builds and navigation were checked on Next.js 13.4, 13.5, 14.2, 15.1, 15.5, 16.0 and 16.4 with React 18.2, 18.3, 19.0, 19.2 and 19.3.
// next.config.mjs
import { withStackflow } from '@uiwwsw/stackflow-next/config';
export default withStackflow({});// app/layout.tsx
import { StackflowProvider } from '@uiwwsw/stackflow-next';
import '@uiwwsw/stackflow-next/style.css';
export default function Layout({ children }) {
return <html lang="en"><body>
<StackflowProvider swipeBack preserveScroll>{children}</StackflowProvider>
</body></html>;
}Use StackLink and useStackRouter in Client Components in place of next/link and the Next router.
- Full reference: options, behavior, scroll container, modal pages, back gesture, limits.
- Architecture and credits
- Demo, with and without the package: with, without.
MIT. See LICENSE.