{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"swipe-action","type":"registry:ui","title":"Swipe Action","description":"List row you swipe right to archive or left to delete, with rubber-band resistance, an armed pop past the threshold and a collapse on commit. Arrow keys + Enter work too.","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/ui/swipe-action.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useId, useRef, useState, type KeyboardEvent, type ReactNode } from \"react\";\nimport {\n  animate,\n  motion,\n  useMotionValue,\n  useMotionValueEvent,\n  useReducedMotion,\n  useTransform,\n  type PanInfo,\n} from \"motion/react\";\nimport { Archive, Trash2 } from \"lucide-react\";\n\ntype Side = \"archive\" | \"delete\";\n\nexport interface SwipeActionProps {\n  children: ReactNode;\n  /** Accessible name of the row, e.g. the item title. */\n  label?: string;\n  /** Px the row must travel to commit an action on release. */\n  threshold?: number;\n  /** Width of the action button when the row rests half open, px. */\n  actionWidth?: number;\n  archiveColor?: string;\n  deleteColor?: string;\n  archiveLabel?: string;\n  deleteLabel?: string;\n  /** Called after the row has swiped out and collapsed. Remove the item here. Omit to disable the right swipe. */\n  onArchive?: () => void;\n  /** Called after the row has swiped out and collapsed. Remove the item here. Omit to disable the left swipe. */\n  onDelete?: () => void;\n  className?: string;\n}\n\nconst settleSpring = { type: \"spring\", stiffness: 520, damping: 26 } as const;\nconst exitSpring = { type: \"spring\", stiffness: 380, damping: 40 } as const;\n\n// iOS-style rubber band: follows 1:1 at first, then gives less and less.\nconst rubber = (v: number, limit: number) => (1 - 1 / ((v * 0.55) / limit + 1)) * limit;\n\nexport function SwipeAction({\n  children,\n  label,\n  threshold = 110,\n  actionWidth = 80,\n  archiveColor = \"#22c55e\",\n  deleteColor = \"#ef4444\",\n  archiveLabel = \"Archive\",\n  deleteLabel = \"Delete\",\n  onArchive,\n  onDelete,\n  className,\n}: SwipeActionProps) {\n  const reduce = useReducedMotion();\n  const rowRef = useRef<HTMLDivElement>(null);\n  const start = useRef(0);\n  const fired = useRef(false);\n  const hintId = useId();\n  const [open, setOpen] = useState<Side | null>(null);\n  const [armed, setArmed] = useState<Side | null>(null);\n  const [gone, setGone] = useState<Side | null>(null);\n\n  const x = useMotionValue(0);\n  useMotionValueEvent(x, \"change\", (v) => setArmed(v >= threshold ? \"archive\" : v <= -threshold ? \"delete\" : null));\n\n  const leftOpacity = useTransform(x, (v) => (v > 0.5 ? 1 : 0));\n  const rightOpacity = useTransform(x, (v) => (v < -0.5 ? 1 : 0));\n  // Once past the button width, the icon rides along with the row's edge.\n  const leftIconX = useTransform(x, (v) => Math.max(0, v - actionWidth));\n  const rightIconX = useTransform(x, (v) => Math.min(0, v + actionWidth));\n  const leftReveal = useTransform(x, [0, actionWidth], [0.4, 1]);\n  const rightReveal = useTransform(x, [-actionWidth, 0], [1, 0.4]);\n\n  const resist = (raw: number) => {\n    const allowed = raw > 0 ? !!onArchive : !!onDelete;\n    const s = Math.sign(raw);\n    const a = Math.abs(raw);\n    if (!allowed) return s * rubber(a, 36);\n    return a <= threshold ? raw : s * (threshold + rubber(a - threshold, threshold));\n  };\n\n  const settle = (side: Side | null) => {\n    setOpen(side);\n    animate(x, side === \"archive\" ? actionWidth : side === \"delete\" ? -actionWidth : 0, reduce ? { duration: 0 } : settleSpring);\n  };\n\n  const commit = (side: Side) => {\n    const w = (rowRef.current?.offsetWidth ?? 400) + 40;\n    setOpen(null);\n    animate(x, side === \"archive\" ? w : -w, reduce ? { duration: 0 } : exitSpring).then(() => setGone(side));\n  };\n\n  const onPanEnd = (_: unknown, info: PanInfo) => {\n    const v = x.get();\n    const vel = info.velocity.x;\n    if (onArchive && (v >= threshold || (v > actionWidth && vel > 900))) return commit(\"archive\");\n    if (onDelete && (v <= -threshold || (v < -actionWidth && vel < -900))) return commit(\"delete\");\n    if (onArchive && v > actionWidth / 2 && vel > -300) return settle(\"archive\");\n    if (onDelete && v < -actionWidth / 2 && vel < 300) return settle(\"delete\");\n    settle(null);\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLDivElement>) => {\n    if (e.target !== e.currentTarget || gone) return;\n    if (e.key === \"Enter\" && open) {\n      e.preventDefault();\n      return commit(open);\n    }\n    let next: Side | null | undefined;\n    if (e.key === \"ArrowRight\") next = open === \"delete\" ? null : onArchive ? \"archive\" : open;\n    if (e.key === \"ArrowLeft\") next = open === \"archive\" ? null : onDelete ? \"delete\" : open;\n    if (e.key === \"Escape\") next = null;\n    if (next === undefined) return;\n    e.preventDefault();\n    settle(next);\n  };\n\n  const action = (side: Side) => {\n    const isLeft = side === \"archive\";\n    const Icon = isLeft ? Archive : Trash2;\n    return (\n      <motion.div\n        className={`absolute inset-0 flex items-center ${isLeft ? \"justify-start\" : \"justify-end\"}`}\n        style={{ background: isLeft ? archiveColor : deleteColor, opacity: isLeft ? leftOpacity : rightOpacity }}\n      >\n        {/* Dim veil lifts once the swipe is armed, so the color \"floods\" in. */}\n        <motion.span\n          aria-hidden\n          className=\"absolute inset-0 bg-black/25\"\n          animate={{ opacity: armed === side ? 0 : 1 }}\n          transition={{ duration: 0.15 }}\n        />\n        <motion.button\n          type=\"button\"\n          tabIndex={open === side ? 0 : -1}\n          aria-hidden={open !== side}\n          onClick={() => commit(side)}\n          style={{ width: actionWidth, x: isLeft ? leftIconX : rightIconX }}\n          className=\"relative flex h-full flex-col items-center justify-center gap-1 text-xs font-medium text-white outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-inset\"\n        >\n          <motion.span style={{ scale: isLeft ? leftReveal : rightReveal }}>\n            <motion.span\n              className=\"block\"\n              animate={{ scale: armed === side ? 1.3 : 1, rotate: armed === side ? (isLeft ? -12 : 12) : 0 }}\n              transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 600, damping: 12 }}\n            >\n              <Icon className=\"size-5\" />\n            </motion.span>\n          </motion.span>\n          {isLeft ? archiveLabel : deleteLabel}\n        </motion.button>\n      </motion.div>\n    );\n  };\n\n  return (\n    <motion.div\n      className={`relative overflow-hidden ${className ?? \"\"}`}\n      initial={false}\n      animate={gone ? { height: 0, opacity: 0 } : { height: \"auto\", opacity: 1 }}\n      transition={reduce ? { duration: 0 } : { duration: 0.28, ease: [0.4, 0, 0.2, 1] }}\n      onAnimationComplete={() => {\n        if (!gone || fired.current) return;\n        fired.current = true;\n        (gone === \"archive\" ? onArchive : onDelete)?.();\n      }}\n    >\n      {onArchive && action(\"archive\")}\n      {onDelete && action(\"delete\")}\n      <motion.div\n        ref={rowRef}\n        tabIndex={gone ? -1 : 0}\n        role=\"group\"\n        aria-label={label}\n        aria-describedby={hintId}\n        onPanStart={() => {\n          start.current = x.get();\n          setOpen(null);\n        }}\n        onPan={(_, info) => x.set(resist(start.current + info.offset.x))}\n        onPanEnd={onPanEnd}\n        onKeyDown={onKey}\n        style={{ x, touchAction: \"pan-y\" }}\n        className=\"relative cursor-grab bg-card outline-none select-none active:cursor-grabbing focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset\"\n      >\n        {children}\n      </motion.div>\n      <span id={hintId} className=\"sr-only\">\n        Swipe, or press the arrow keys to reveal actions. Enter confirms, Escape closes.\n      </span>\n    </motion.div>\n  );\n}\n"}]}