Slide View

Craft

Side-by-side page transition for list ↔ detail and step 1 ↔ step 2 flows.

Motion: Current view slides out one way, next slides in from the other through a soft blur; direction follows the step index.

Installation

npx shadcn@latest add @craft/slide-view

Source

components/ui/slide-view.tsx
"use client"

import * as React from "react"
import { AnimatePresence, motion, type Variants } from "motion/react"
import { cn } from "cn"

import { duration, ease } from "@/lib/motion"

const slide: Variants = {
  enter: (dir: number) => ({ x: dir * 24, opacity: 0, filter: "blur(3px)" }),
  center: { x: 0, opacity: 1, filter: "blur(0px)", transition: { duration: duration.slow, ease: ease.out } },
  exit: (dir: number) => ({ x: dir * -24, opacity: 0, filter: "blur(3px)", transition: { duration: duration.base, ease: ease.in } }),
}

/**
 * Side-by-side page transition: the current view slides out one way while the
 * next slides in from the other, through a soft blur — list ↔ detail, step 1 ↔
 * step 2, a sheet that drills in. Give each view a `view` key and an `index`
 * (its position in the flow); going to a higher index slides left, back slides
 * right. Both views share one grid cell while they cross, so the layout
 * doesn't jump.
 */
function SlideView({
  view,
  index,
  className,
  children,
}: {
  /** Identifies the view being shown — change it to transition */
  view: React.Key
  /** Position in the flow, used for direction (forward = higher index) */
  index: number
  className?: string
  children: React.ReactNode
}) {
  // direction = sign of the change since the last render, kept through the exit animation
  const [state, setState] = React.useState({ index, dir: 1 })
  if (state.index !== index) setState({ index, dir: index > state.index ? 1 : -1 })

  return (
    <div data-slot="slide-view" className={cn("grid overflow-x-clip *:[grid-area:1/1] *:min-w-0", className)}>
      <AnimatePresence initial={false} custom={state.dir}>
        <motion.div key={view} custom={state.dir} variants={slide} initial="enter" animate="center" exit="exit">
          {children}
        </motion.div>
      </AnimatePresence>
    </div>
  )
}

export { SlideView }