Skip to content

Transitions ​

Presets ​

PresetLooks likeDurationEasing
iosUIKit navigation push: the new screen slides in over the old one, which moves 30% left and darkens350msease
androidAndroid 14+ activity transition: the new screen slides in from 10% and fades in early; the old one moves 10% left450mscubic-bezier(0.2, 0, 0, 1)
materialMaterial shared axis X: both screens move 30px and cross-fade300mscubic-bezier(0.4, 0, 0.2, 1)

The default picks per platform: android on Android, ios elsewhere.

Open the demo
Open on a phone

Choosing with animate ​

animate runs for every screen change and returns the transition to play, or false for none. The stack also calls it at rest to check whether a swipe back may start, so keep it pure and cheap.

tsx
import { ios, material } from "route-stack";

<Stack
  animate={(from, to, direction) => {
    if (to.startsWith("/tabs/")) return false; // tab switches: no slide
    if (to.startsWith("/settings")) return material;
    return ios;
  }}
>
ArgumentValue
fromPath with query of the screen leaving
toPath with query of the screen arriving
direction"push", "back" or "replace"
  • The browser's forward button is a "push": it slides in the screen it returns to.
  • history.go(-3) is one "back".
  • A swipe back uses animate(from, to, "back") too; returning false there also turns the swipe off for that pair.
  • false still switches screens and keeps the one left.

Your own transition ​

A transition is data: keyframes for each screen, played forward on push and reversed on back (unless it has a back of its own).

ts
import type { Transition } from "route-stack";

export const fade: Transition = {
  duration: 250,
  easing: "ease-out",
  enter: [{ opacity: 0 }, { opacity: 1 }],
  leave: [{ opacity: 1 }, { opacity: 0 }],
  enterOnTop: true,
};
FieldMeaning
durationLength of the animation in milliseconds. A swipe's release plays the rest over the full duration, whatever the distance left
easingCSS easing of a played slide (push, back, the rest of a swipe after release). A finger follows linearly
enterKeyframes of the screen coming in on push
leaveKeyframes of the screen going out on push
shadeOptional keyframes of a black layer over the lower screen, to darken it (opacity only)
shadowOptional fixed box-shadow of the upper screen during the slide
enterOnTopWhether the screen coming in is drawn above the one leaving
backOptional enter / leave / shade of its own for back, played forward. Without it, back plays push reversed. A back without a shade has none (not the push's)

Keyframes are Web Animations keyframes: any animatable CSS property works. Performance differs:

PropertiesRun byCost
transform (translate, scale, rotate), opacityThe compositorSmooth even when the page is busy; 120Hz on ProMotion iPhones
filter, clip-path, border-radius, background-color, box-shadow, …The main threadRepaint a screen-sized layer every frame; can drop frames on low-end phones
width, height, top, left, margin, …The main threadLay out the screen every frame; avoid
  • The presets use only transform and opacity.
  • shadow is a static style on the upper layer, not animated.
  • Without a back, the keyframes play in reverse on back: the screen coming back takes leave, the one going takes enter.
  • On back the layers stack as they did on push: with enterOnTop, the screen that came in on push stays on top.