{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pill",
  "title": "usePill",
  "description": "The stretching pill: an active-item capsule whose leading edge springs ahead while the trailing edge follows, so it stretches over the travel and contracts on arrival.",
  "dependencies": [
    "motion"
  ],
  "devDependencies": [],
  "registryDependencies": [
    "https://reactmotionkit.com/r/motion-presets.json"
  ],
  "files": [
    {
      "path": "lib/pill.ts",
      "content": "\"use client\"\n\nimport {\n  animate,\n  type MotionValue,\n  type Transition,\n  useMotionValue,\n  useTransform,\n} from \"motion/react\"\nimport { useCallback, useEffect, useRef, useState } from \"react\"\n\nimport type { MotionTokens } from \"@/lib/motion\"\n\n/**\n * The stretching pill (\"la pillola\"), the second shape of the MotionKit shape language\n * (docs/plan/SHAPE-LANGUAGE.md). A capsule marks the active item of a row (tabs,\n * segmented control, sidebar, menu highlight, date range). When the active item\n * changes, the pill does not slide as a rigid block: its leading edge is thrown toward\n * the destination on the temperament's spring while its trailing edge follows on a\n * slower one, so the pill stretches over the distance (charge and action together),\n * then contracts onto the destination (settle). On a long jump it briefly covers the\n * items in between, which is what makes the travel readable at a glance.\n */\n\n/*\n * The trailing edge takes 1.35x the leading edge's time: enough stretch to read as\n * elastic over one or two items, short enough that the pill is whole again before the\n * leading edge finishes settling. Below 1.2 the stretch is invisible; above 1.6 the pill\n * looks torn from where it came.\n */\nconst TRAIL_LAG = 1.35\n\n/*\n * The charge of a pill: on press, its edge facing the pressed item moves 2px toward it,\n * a hint of where it will go. More than that and it reads as the pill already leaving.\n */\nconst NUDGE_PX = 2\n\nexport type PillAxis = \"x\" | \"y\"\n\nexport interface PillOptions {\n  /** Start edge of the active item along the axis, in px from the container's padding edge. */\n  start: number\n  /** End edge of the active item along the axis, in px. */\n  end: number\n  /** Resolved motion tokens of the component (`useMotionKit(temperament)`). */\n  motion: MotionTokens\n  /** Spring of the leading edge. Defaults to `motion.spring`. */\n  transition?: Transition\n}\n\nexport interface Pill {\n  /** Position of the pill's start edge, in px. Bind to `x` (or `y`). */\n  position: MotionValue<number>\n  /** Length of the pill along the axis, in px. Bind to `width` (or `height`). */\n  size: MotionValue<number>\n  /** The two edges, for consumers that draw the pill themselves. */\n  start: MotionValue<number>\n  end: MotionValue<number>\n  /** Charge beat: on press of an item, the edge facing it nudges 2px toward it. */\n  nudge: (toward: -1 | 1) => void\n  /** End of the charge beat: on release, leave, cancel, blur. */\n  release: () => void\n}\n\n/** The trailing edge's spring: same character, slower. A tween or fade is kept as it is. */\nfunction trailing(transition: Transition): Transition {\n  if (transition.type !== \"spring\") return transition\n  if (\"visualDuration\" in transition && typeof transition.visualDuration === \"number\") {\n    return { ...transition, visualDuration: transition.visualDuration * TRAIL_LAG }\n  }\n  if (\"duration\" in transition && typeof transition.duration === \"number\") {\n    return { ...transition, duration: transition.duration * TRAIL_LAG }\n  }\n  return transition\n}\n\n/**\n * Physics of the stretching pill. Give it the active item's edges (measure them with\n * `measureAlong`) and bind `position` and `size` to an absolutely positioned capsule.\n * With reduced motion both edges glide together in the short fade time: no stretch.\n */\nexport function usePill({ start, end, motion, transition }: PillOptions): Pill {\n  const startValue = useMotionValue(start)\n  const endValue = useMotionValue(end)\n  const size = useTransform([startValue, endValue], ([a, b]) =>\n    Math.max(0, (b as number) - (a as number)),\n  )\n  const previous = useRef({ start, end })\n  const [nudged, setNudged] = useState<-1 | 0 | 1>(0)\n  const nudgePx = motion.reduced ? 0 : NUDGE_PX\n\n  const lead = transition ?? motion.spring\n  useEffect(() => {\n    const forward = start >= previous.current.start\n    previous.current = { start, end }\n    // The edge facing the destination leads; the one facing where it came from lags.\n    const [startTransition, endTransition] = forward\n      ? [trailing(lead), lead]\n      : [lead, trailing(lead)]\n    const controls = [\n      animate(startValue, start + (nudged === -1 ? -nudgePx : 0), startTransition),\n      animate(endValue, end + (nudged === 1 ? nudgePx : 0), endTransition),\n    ]\n    return () => {\n      for (const control of controls) control.stop()\n    }\n  }, [start, end, lead, startValue, endValue, nudged, nudgePx])\n\n  const nudge = useCallback((toward: -1 | 1) => {\n    setNudged(toward)\n  }, [])\n  const release = useCallback(() => {\n    setNudged(0)\n  }, [])\n\n  return { position: startValue, size, start: startValue, end: endValue, nudge, release }\n}\n\n/**\n * Edges of `item` along `axis`, relative to `container`'s padding box (where an absolutely\n * positioned pill lives), from layout offsets so a pressed, squashed item never moves the\n * measurement. Call it on selection change and from a ResizeObserver on the container.\n */\nexport function measureAlong(item: HTMLElement, container: HTMLElement, axis: PillAxis) {\n  let start = 0\n  let node: HTMLElement | null = item\n  while (node && node !== container) {\n    start += axis === \"x\" ? node.offsetLeft : node.offsetTop\n    node = node.offsetParent instanceof HTMLElement ? node.offsetParent : null\n  }\n  const length = axis === \"x\" ? item.offsetWidth : item.offsetHeight\n  return { start, end: start + length }\n}\n",
      "type": "registry:lib"
    }
  ],
  "meta": {
    "tier": "free",
    "collection": "interface",
    "needsImage": false,
    "addedAt": "2026-10-16",
    "tags": [
      "shape",
      "pill",
      "indicator",
      "tabs"
    ],
    "tech": [],
    "presets": [],
    "keyboard": [],
    "heavy": false
  },
  "categories": [
    "internal"
  ],
  "type": "registry:lib"
}