{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sunui-motion","type":"registry:lib","title":"SunUI Motion","description":"The three SunUI springs (snappy, gentle, stamp) and the few motions the design rules allow: press, stamp, error shake, drawer panels, list collapse and number roll. Every helper has a reduced-motion path. There is deliberately no fade-up, blur-in, pulse or confetti; see /docs/motion.","dependencies":["framer-motion"],"files":[{"path":"registry/lib/sunui-motion/motion.ts","type":"registry:lib","target":"lib/sunui-motion.ts","content":"\"use client\";\n\n// SunUI 的動態節奏。數值與 DESIGN.md 第 4 節、現有 block 完全一致，改這裡等於改整套手感。\n//\n// 刻意沒有的東西：fadeUp、blurIn、bounceIn、pulse、confetti。\n// SunUI 只在回應使用者動作或資料改變時動，不做進場淡入與常駐循環，慶祝只有蓋章。\n// 完整對照見 /docs/motion。\n\nimport { useAnimate, useReducedMotion, type Transition, type Variants } from \"framer-motion\";\nimport { useCallback } from \"react\";\n\n/** 三種彈簧，整套只用這三種 */\nexport const springs = {\n  /** 按壓回饋、小元件位移 */\n  snappy: { type: \"spring\", stiffness: 520, damping: 34 },\n  /** 狀態切換、展開收合 */\n  gentle: { type: \"spring\", stiffness: 240, damping: 30 },\n  /** 小元件落定一次的回彈：結果圓片、徽章數字 */\n  pop: { type: \"spring\", stiffness: 620, damping: 20, mass: 0.8 },\n  /** 舊名，等於 pop */\n  stamp: { type: \"spring\", stiffness: 620, damping: 20, mass: 0.8 },\n} as const satisfies Record<string, Transition>;\n\n/** 減少動態時用的立即切換 */\nexport const instant: Transition = { duration: 0 };\n\n/**\n * 按壓：按鈕、卡片被按下時輕微內縮。\n * 用法：<motion.button {...press(reduce)} />\n */\nexport function press(reduce?: boolean | null) {\n  return reduce ? {} : { whileTap: { scale: 0.97 }, transition: springs.snappy };\n}\n\n/**\n * 結果出現：150ms 淡入，不縮放、不旋轉。block 裡顯示結果請直接用 `Result`（registry/free/stamp，舊名 Stamp）；\n * 這組 variants 只留給需要自訂結果樣式的特殊情況。名稱沿用 stamp 以相容舊程式。\n * 用法：<motion.span variants={stamp} initial={reduce ? false : \"lifted\"} animate=\"pressed\" exit=\"gone\" />\n */\nexport const stamp: Variants = {\n  lifted: { opacity: 0 },\n  pressed: { opacity: 1, transition: { duration: 0.15, ease: \"easeOut\" } },\n  gone: { opacity: 0, transition: { duration: 0.12 } },\n};\n\n/** 錯誤輕搖的關鍵影格：有質量的物件被推回來 */\nexport const shakeKeyframes = { x: [0, -6, 5, -3, 2, 0] };\nexport const shakeTransition: Transition = { duration: 0.32, ease: \"easeOut\" };\n\n/**\n * 錯誤輕搖：回傳 [ref, shake]。輸入錯誤時呼叫 shake()；減少動態時不搖，錯誤訊息照常顯示。\n */\nexport function useShake<T extends HTMLElement | SVGElement = HTMLDivElement>() {\n  const [scope, animate] = useAnimate<T>();\n  const reduce = useReducedMotion();\n  const shake = useCallback(() => {\n    if (reduce || !scope.current) return;\n    void animate(scope.current as HTMLElement, { x: shakeKeyframes.x }, { duration: 0.32, ease: \"easeOut\" });\n  }, [animate, reduce, scope]);\n  return [scope, shake] as const;\n}\n\nexport type PanelSide = \"left\" | \"right\" | \"bottom\";\n\n/**\n * 抽屜與底部面板：從邊緣滑入、滑回邊緣。不淡入，紙是被推進來的。\n * 用法：<motion.aside variants={panel(\"right\")} initial=\"closed\" animate=\"open\" exit=\"closed\" />\n */\nexport function panel(side: PanelSide, reduce?: boolean | null): Variants {\n  const offscreen = side === \"bottom\" ? { y: \"100%\" } : { x: side === \"left\" ? \"-100%\" : \"100%\" };\n  const settle = side === \"bottom\" ? { y: 0 } : { x: 0 };\n  const transition = reduce ? instant : springs.gentle;\n  return {\n    closed: { ...offscreen, transition },\n    open: { ...settle, transition },\n  };\n}\n\n/** 面板後的遮罩：只調不透明度，不模糊 */\nexport const scrim: Variants = {\n  closed: { opacity: 0, transition: { duration: 0.18 } },\n  open: { opacity: 1, transition: { duration: 0.22 } },\n};\n\n/**\n * 清單項目的增刪：高度收合，讓下面的項目自然補位。搭配 AnimatePresence 與 layout。\n * 用法：<motion.li variants={listItem(reduce)} initial=\"collapsed\" animate=\"expanded\" exit=\"collapsed\" />\n */\nexport function listItem(reduce?: boolean | null): Variants {\n  if (reduce) {\n    return { collapsed: { opacity: 0, transition: instant }, expanded: { opacity: 1, transition: instant } };\n  }\n  return {\n    collapsed: { opacity: 0, height: 0, transition: springs.gentle },\n    expanded: { opacity: 1, height: \"auto\", transition: springs.gentle },\n  };\n}\n\n/**\n * 數字換值：舊值往變動方向離開、新值從另一側進來。direction 1 為增加，-1 為減少。\n * 用法：<AnimatePresence custom={dir} initial={false}><motion.span key={value} variants={roll} custom={dir} initial=\"enter\" animate=\"center\" exit=\"exit\" /></AnimatePresence>\n */\nexport const roll: Variants = {\n  enter: (direction: number) => ({ y: direction >= 0 ? \"60%\" : \"-60%\", opacity: 0 }),\n  center: { y: 0, opacity: 1, transition: springs.snappy },\n  exit: (direction: number) => ({ y: direction >= 0 ? \"-60%\" : \"60%\", opacity: 0, transition: { duration: 0.12 } }),\n};\n"}]}