Skip to content

API ​

<Stack> ​

ts
import { Stack } from "route-stack/react-router"; // React Router
import { Stack } from "route-stack/tanstack-router"; // TanStack Router
import { Stack } from "route-stack/next"; // Next.js App Router
import { Stack } from "route-stack"; // any other router: pass `path`
PropTypeDefault
childrenReactNode—What the router renders
animateAnimatedefaultAnimateThe transition for each screen change. Transitions
swipeSwipeMode | (from: string, to: string) => SwipeMode"edge", false on AndroidWhere a swipe back can start: the edge, anywhere, or nowhere; per back with a function. Swipe Back
sameScreenSameScreendefaultSameScreenWhether a push or replace stays on the screen shown. Screens & History
enableProgressCssVarbooleanfalseWrites --route-stack-progress during slides. Progress
pathstring—Core Stack only: the rendered path with query. Adapters fill it
view() => View() => "current"Core Stack only. Writing an Adapter

One <Stack> per document.

Presets ​

ts
import { ios, android, material, defaultAnimate, onAndroid } from "route-stack";
Export
iosTransition: slide over, old screen 30% left and darkened, 350ms
androidTransition: Android 14+ activity transition, 450ms
materialTransition: Material shared axis X, 300ms
defaultAnimateAnimate: android on Android, ios elsewhere
onAndroid()boolean: the page runs on Android

Screens ​

ts
import { defaultSameScreen } from "route-stack";
Export
defaultSameScreenSameScreen: a push to the same pathname and query (only the #fragment changed), a replace to the same pathname

Next.js ​

ts
import { onRouterTransitionStart } from "route-stack/next";

Re-export it from instrumentation-client.ts. Routers

Types ​

ts
type Direction = "push" | "back" | "replace";

type Animate = (
  from: string,
  to: string,
  direction: Direction,
) => Transition | false;

interface Transition {
  duration: number;
  easing: string;
  enter: Keyframe[];
  leave: Keyframe[];
  shade?: Keyframe[];
  shadow?: string;
  enterOnTop: boolean;
  back?: Pick<Transition, "enter" | "leave" | "shade">;
}

type NavigationType = "push" | "replace" | "traverse";

type SwipeMode = "edge" | "full" | false;

// from/to: pathname, query and #fragment, as the address bar has them
type SameScreen = (
  from: string,
  to: string,
  type: "push" | "replace",
) => boolean;

type View =
  | "current"
  | "stale"
  | "held"
  | { ahead: "push" | "replace"; url: string };

interface StackProps {
  path: string;
  animate?: Animate;
  swipe?: SwipeMode | ((from: string, to: string) => SwipeMode);
  sameScreen?: SameScreen;
  enableProgressCssVar?: boolean;
  children: ReactNode;
}

interface AdapterProps {
  view?: () => View;
}

CSS variables ​

VariableSet onWhen
--route-stack-progress<html>During a slide, with enableProgressCssVar. 0 to 1

Data attributes ​

AttributeElement
data-stack-rootThe stack root
data-stack-screenEach screen host (the layer a slide moves)
data-stack-contentEach screen's content (what React hides)
data-stack-animatingThe two lifted screens, during a slide. Scroll & Layers

Set by the app:

AttributeElement
data-stack-no-swipeAn element whose drags are its own: no swipe starts on it. Swipe Back

Debug log ​

Set a function on Symbol.for("route-stack.log") to receive the stack's events (commit, begin, release, finished, settle, blocked, returned, late-scroll):

ts
(globalThis as Record<symbol, unknown>)[Symbol.for("route-stack.log")] = (
  event: string,
  data?: object,
) => console.debug("[route-stack]", event, data);