{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "beats",
  "title": "Beats",
  "description": "The three beats as helpers: press charge (usePress, sink, stretch), the cascade of secondary parts after a hero movement, the put-down of a landing part and the reflow spring for siblings.",
  "dependencies": [
    "motion"
  ],
  "devDependencies": [],
  "registryDependencies": [
    "https://reactmotionkit.com/r/motion-presets.json"
  ],
  "files": [
    {
      "path": "lib/beats.ts",
      "content": "\"use client\"\n\nimport type { TargetAndTransition, Transition } from \"motion/react\"\nimport type { FocusEvent, KeyboardEvent, PointerEvent } from \"react\"\nimport { useCallback, useState } from \"react\"\n\nimport type { MotionTokens } from \"@/lib/motion\"\n\n/**\n * The three beats as reusable pieces (docs/plan/SHAPE-LANGUAGE.md): the charge of a\n * press (`usePress`, `sink`, `stretch`), the cascade of secondary parts after a hero\n * movement (`cascade`), the put-down of a part that lands (`putDown`) and the reflow of\n * siblings around a change (`reflow`).\n */\n\n/* -------------------------------------------------------------------------------------------------\n * Press: the charge beat\n * -----------------------------------------------------------------------------------------------*/\n\nexport interface PressHandlers<E extends Element = HTMLElement> {\n  onPointerDown?: (event: PointerEvent<E>) => void\n  onPointerUp?: (event: PointerEvent<E>) => void\n  onPointerLeave?: (event: PointerEvent<E>) => void\n  onPointerCancel?: (event: PointerEvent<E>) => void\n  onKeyDown?: (event: KeyboardEvent<E>) => void\n  onKeyUp?: (event: KeyboardEvent<E>) => void\n  onBlur?: (event: FocusEvent<E>) => void\n}\n\nexport interface PressOptions<E extends Element = HTMLElement> extends PressHandlers<E> {\n  /** A disabled control never charges. */\n  disabled?: boolean\n  /** Keys that charge the control, default Space and Enter (buttons, switches, checkboxes). */\n  keys?: readonly string[]\n}\n\nexport interface Press<E extends Element = HTMLElement> {\n  /** True while the person holds the control down (pointer or key). */\n  pressed: boolean\n  /** Handlers to spread on the control. The customer's handlers in `options` still fire. */\n  handlers: Required<PressHandlers<E>>\n}\n\nconst PRESS_KEYS = [\" \", \"Enter\"] as const\n\n/**\n * Tracks the charge beat of a press. The control is \"pressed\" from pointer down (main\n * button) or key down (Space/Enter, not repeated) until pointer up, leave, cancel, key up\n * or blur, whichever comes first. Pair it with `sink` or `stretch` for the pose.\n */\nexport function usePress<E extends Element = HTMLElement>({\n  disabled = false,\n  keys = PRESS_KEYS,\n  onPointerDown,\n  onPointerUp,\n  onPointerLeave,\n  onPointerCancel,\n  onKeyDown,\n  onKeyUp,\n  onBlur,\n}: PressOptions<E> = {}): Press<E> {\n  const [pressed, setPressed] = useState(false)\n  const release = useCallback(() => {\n    setPressed(false)\n  }, [])\n\n  return {\n    pressed,\n    handlers: {\n      onPointerDown: (event) => {\n        if (event.button === 0 && !disabled) setPressed(true)\n        onPointerDown?.(event)\n      },\n      onPointerUp: (event) => {\n        release()\n        onPointerUp?.(event)\n      },\n      onPointerLeave: (event) => {\n        release()\n        onPointerLeave?.(event)\n      },\n      onPointerCancel: (event) => {\n        release()\n        onPointerCancel?.(event)\n      },\n      onKeyDown: (event) => {\n        if (keys.includes(event.key) && !event.repeat && !disabled) setPressed(true)\n        onKeyDown?.(event)\n      },\n      onKeyUp: (event) => {\n        release()\n        onKeyUp?.(event)\n      },\n      onBlur: (event) => {\n        release()\n        onBlur?.(event)\n      },\n    },\n  }\n}\n\n/**\n * Pose of a surface under the finger: it sinks by the temperament's squash, uniformly,\n * so text inside is not distorted. For cards, rows, buttons with text.\n */\nexport function sink(motion: MotionTokens, pressed: boolean): TargetAndTransition {\n  return { scale: pressed ? 1 - motion.values.squash : 1 }\n}\n\n/*\n * Small round things (thumbs, dots, icon buttons) get the squash amplified: a fraction\n * of a 20-36px circle is otherwise invisible. The Switch and the Morphing Dialog's close\n * button use 2x; the travelling ball uses 4x along its axis.\n */\nexport const SMALL_SQUASH = 2\n\n/**\n * Pose of a small shape charging toward a direction: wider along `axis`, flatter across.\n * `amplify` multiplies the temperament's squash (see `SMALL_SQUASH`).\n */\nexport function stretch(\n  motion: MotionTokens,\n  pressed: boolean,\n  axis: \"x\" | \"y\" = \"x\",\n  amplify = SMALL_SQUASH,\n): TargetAndTransition {\n  if (!pressed) return { scaleX: 1, scaleY: 1 }\n  const along = 1 + motion.values.squash * amplify\n  const across = 1 - motion.values.squash\n  return axis === \"x\" ? { scaleX: along, scaleY: across } : { scaleX: across, scaleY: along }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Cascade: secondary parts arrive after the hero movement\n * -----------------------------------------------------------------------------------------------*/\n\n/*\n * Rising parts travel about half a line of body text: enough to read as arriving from\n * below the surface they belong to, short enough not to look like a slide.\n */\nconst RISE_PX = 12\n\n/*\n * Secondary parts start once the hero movement is clearly under way, at 35% of its\n * perceived duration: earlier and they compete with it, later and the surface sits empty.\n */\nconst AFTER_SHARE = 0.35\n\n/*\n * On the way back they leave in the first 40% of the hero movement: long enough to read\n * as leaving with the surface, short enough that nothing of them is left on a shrinking one.\n */\nconst EXIT_SHARE = 0.4\n\nexport interface CascadeOptions {\n  /** Delay before the first part, in seconds, after the time scale. Default: 35% of the spring's perceived duration. */\n  after?: number\n  /** Cap on the total stagger of the group, in seconds. Default 0.6. */\n  maxTotal?: number\n  /** Where the parts come from. Default `below` (they rise). */\n  from?: \"below\" | \"above\" | \"left\" | \"right\"\n  /**\n   * Where they go on exit: `back` (default) returns where they came from, as parts of a\n   * surface that is closing; `through` continues in the same direction, as a tab panel\n   * that leaves while the next one arrives.\n   */\n  exit?: \"back\" | \"through\"\n}\n\nexport interface Cascade {\n  initial: TargetAndTransition\n  animate: TargetAndTransition\n  exit: TargetAndTransition\n  transition: Transition\n}\n\n/**\n * Variants for part `index` of a group that follows a hero movement (the contents of a\n * morphed panel, menu items, toast description, sidebar labels). Spread the result on a\n * `motion` element inside `AnimatePresence`. With reduced motion the parts fade in place,\n * all at once (`staggerDelay` is 0 when reduced).\n */\nexport function cascade(motion: MotionTokens, index = 0, options: CascadeOptions = {}): Cascade {\n  const after = options.after ?? motion.seconds(motion.values.duration * AFTER_SHARE)\n  const delay = after + motion.staggerDelay(index, options.maxTotal)\n  const exitDuration = motion.seconds(motion.values.duration * EXIT_SHARE)\n  if (motion.reduced) {\n    /*\n     * Every state names the final pose explicitly: a part that was mid-rise (or whose\n     * component spreads its own offset) must not keep a 12 px offset under reduced motion.\n     */\n    const inPlace = { x: 0, y: 0, scale: 1 }\n    return {\n      initial: { opacity: 0, ...inPlace },\n      animate: { opacity: 1, ...inPlace },\n      exit: { opacity: 0, ...inPlace },\n      transition: { ...motion.fade, delay },\n    }\n  }\n  const from = options.from ?? \"below\"\n  const offset =\n    from === \"below\"\n      ? { y: RISE_PX }\n      : from === \"above\"\n        ? { y: -RISE_PX }\n        : from === \"left\"\n          ? { x: -RISE_PX }\n          : { x: RISE_PX }\n  const exitOffset =\n    options.exit === \"through\" ? { x: -(offset.x ?? 0), y: -(offset.y ?? 0) } : offset\n  return {\n    initial: { opacity: 0, ...offset },\n    animate: { opacity: 1, x: 0, y: 0 },\n    exit: { opacity: 0, ...exitOffset, transition: { duration: exitDuration, ease: \"easeIn\" } },\n    transition: { ...motion.tween, delay },\n  }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Put-down: a part lands with weight\n * -----------------------------------------------------------------------------------------------*/\n\n/*\n * The box squashes by 2x the temperament's squash (the small-shape amplification: a 5%\n * squash is invisible on a tile), capped so the far edge never moves more than 6 px: the\n * text sits on an unscaled layer with 16-20 px of padding, and 6 px is the most a tile can\n * give before the box visibly crosses it (playful reaches the cap on a 170 px tile).\n */\nconst PUTDOWN_AMPLIFY = 2\nconst PUTDOWN_MAX_PX = 6\n/* The squash lasts 60% of the temperament's duration: shorter reads as a flicker. */\nconst PUTDOWN_SHARE = 0.6\n/* Fast in (35% of the time to the flattest frame), slow back: an impact, then the rebound. */\nconst PUTDOWN_TIMES = [0, 0.35, 1]\n\nexport interface PutDownOptions {\n  /** Height of the box that lands, in px (measured at the landing frame). */\n  height: number\n  /** The edge it lands on, which stays still. Default `bottom`. */\n  origin?: \"bottom\" | \"top\"\n}\n\nexport interface PutDown {\n  /** Keyframes for the box's `scaleY`: `[1, 1 - amount, 1]`. */\n  scaleY: [number, number, number]\n  /** `originY` for the box: 1 for `bottom`, 0 for `top`. */\n  originY: 0 | 1\n  /** Keyframed transition, after the time scale. */\n  transition: Transition\n}\n\n/**\n * The settle of a part that lands on a surface (a result tile, a card from a deck, a cell\n * in a bento): its box, never its text, squashes against the edge it lands on and rounds\n * back. Start it on the landing frame. Returns `null` when there is nothing to play: the\n * calm temperament (no overshoot), reduced motion, or a box not measured yet.\n */\nexport function putDown(\n  motion: MotionTokens,\n  { height, origin = \"bottom\" }: PutDownOptions,\n): PutDown | null {\n  if (motion.reduced || motion.values.overshoot <= 0 || height <= 0) return null\n  const amount = Math.min(motion.values.squash * PUTDOWN_AMPLIFY, PUTDOWN_MAX_PX / height)\n  return {\n    scaleY: [1, 1 - amount, 1],\n    originY: origin === \"bottom\" ? 1 : 0,\n    transition: {\n      duration: motion.seconds(motion.values.duration * PUTDOWN_SHARE),\n      times: PUTDOWN_TIMES,\n      ease: \"easeOut\",\n    },\n  }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Reflow: siblings make room\n * -----------------------------------------------------------------------------------------------*/\n\nexport interface ReflowOptions {\n  /**\n   * Dense, serious contexts (tables, long lists, forms with errors, payments) never\n   * bounce whatever the temperament: they use the calm spring.\n   */\n  calm?: boolean\n}\n\n/**\n * Transition for Motion `layout` animations of siblings around a change (a chip\n * added, a row sorted, a toast dismissed). One spring for the whole group, so every\n * item arrives together.\n */\nexport function reflow(motion: MotionTokens, { calm = false }: ReflowOptions = {}): Transition {\n  return calm ? motion.springCalm : motion.spring\n}\n",
      "type": "registry:lib"
    }
  ],
  "meta": {
    "tier": "free",
    "collection": "interface",
    "needsImage": false,
    "addedAt": "2026-10-16",
    "tags": [
      "shape",
      "press",
      "cascade",
      "put-down",
      "reflow"
    ],
    "tech": [],
    "presets": [],
    "keyboard": [],
    "heavy": false
  },
  "categories": [
    "internal"
  ],
  "type": "registry:lib"
}