{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"squash-press-button","type":"registry:component","title":"Squash Press Button","description":"A pill that squashes under your finger and pops back: it sinks onto its base where you press, deeper the longer you hold, keeps its area as it squashes, and springs back with one overshoot while its tinted shadows collapse and return.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/squash-press-button.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState, useSyncExternalStore } from \"react\";\nimport type { CSSProperties, KeyboardEvent as ReactKeyboardEvent, PointerEvent as ReactPointerEvent, ReactNode, RefObject } from \"react\";\n\n/**\n * SquashPressButton — a pill that squashes under your finger and pops back.\n *\n * Press and it squashes onto its base where you pressed, its area kept;\n * hold and it sinks further, up to 600ms; let go and it springs back past\n * rest, one overshoot, settled inside 450ms. Its shadows (a contact and a\n * wide one tinted by its own colour) collapse to one line while pressed.\n * Swipe across it and it jiggles the way you pushed. Gum, ink and soft.\n *\n * Part of the Tactile Maximalism kit: dough #f7f0e4, warm ink #17130f and\n * one loud accent, bubblegum #ff2d78, filled once per screen; grain fixed to\n * the material; soft shadows tinted by what casts them, never a hard offset.\n * Everything soft keeps its area as it squashes and settles inside 450ms.\n * Unbounded through var(--font-unbounded) for display over Geist. Respects\n * prefers-reduced-motion. No dependencies beyond React. Paste it as its own\n * file: it repeats the kit's small helpers, which would clash in one module.\n * Load Unbounded with next/font (variable: \"--font-unbounded\") on a\n * parent, or from Google Fonts or @fontsource.\n */\n\nconst DISPLAY = 'var(--font-unbounded, \"Unbounded\", var(--font-sans, \"Geist\"), ui-sans-serif, system-ui, sans-serif)';\n\n/** Dough, warm ink, and one loud accent. */\nconst DOUGH = {\n  ground: \"#f7f0e4\",\n  surface: \"#fffaf1\",\n  well: \"#efe5d3\",\n  solid: \"#17130f\",\n  ink: \"rgba(23,19,15,0.92)\",\n  ink2: \"rgba(23,19,15,0.64)\",\n  ink3: \"rgba(23,19,15,0.42)\",\n  line: \"rgba(23,19,15,0.1)\",\n  line2: \"rgba(23,19,15,0.18)\",\n  gum: \"#ff2d78\",\n  gumDeep: \"#d8155d\",\n  lilac: \"#e8e1f6\",\n} as const;\n\n/** A soft object resting in dough: a tight contact shadow and a wide one tinted by the object. */\nfunction softShadow(tint = \"23,19,15\", lift = 1): string {\n  return `0 ${(2 * lift).toFixed(1)}px ${(6 * lift).toFixed(1)}px rgba(23,19,15,0.1), 0 ${(18 * lift).toFixed(1)}px ${(40 * lift).toFixed(1)}px -8px rgba(${tint},${(0.2 * Math.min(1, lift)).toFixed(3)})`;\n}\n\n/** Pressed into the dough: both shadows collapse to one tight line. */\nconst PRESSED_SHADOW = \"0 1px 3px rgba(23,19,15,0.14)\";\n\nfunction clamp(v: number, lo: number, hi: number): number {\n  return v < lo ? lo : v > hi ? hi : v;\n}\n\n/** Links: strip tabs and newlines, refuse backslashes, allow http(s), mailto, tel and same-site paths. */\nfunction safeHref(raw: string): string {\n  const v = raw.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (!v || v.includes(\"\\\\\")) return \"#\";\n  if (/^(https?:|mailto:|tel:)/i.test(v)) return v;\n  if (/^[/#?]/.test(v) && !/^\\/\\//.test(v)) return v;\n  return \"#\";\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\n/** True when the visitor asked for reduced motion. False on the server. */\nfunction useReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/**\n * Squash and stretch for one element, on a spring. `press(depth)` squashes it\n * onto its foot with its area kept (as short as it gets, it gets as wide);\n * `release()` lets it spring back past rest, one visible overshoot, settled\n * inside 450ms; `flick(v)` gives it a sideways jiggle (a skew) from a push.\n * The transform is written straight to the element; nothing re-renders.\n */\nfunction useSquish(ref: RefObject<HTMLElement | null>, origin = \"50% 100%\") {\n  const reduced = useReducedMotion();\n  const st = useRef({ y: 1, v: 0, t: 1, sk: 0, skv: 0, raf: 0, last: 0 });\n  const tickRef = useRef<(now: number) => void>(() => undefined);\n  useEffect(() => {\n    const s = st.current;\n    const el = ref.current;\n    if (el) {\n      el.style.transformOrigin = origin;\n      // The spring owns the transform; a stylesheet's transition would smear it.\n      el.style.transitionProperty = el.style.transitionProperty && el.style.transitionProperty !== \"all\" ? el.style.transitionProperty : \"none\";\n    }\n    tickRef.current = (now: number) => {\n      s.raf = 0;\n      const dt = s.last ? Math.min(1 / 30, (now - s.last) / 1000) : 1 / 60;\n      s.last = now;\n      // k 420, c 18: one overshoot of about 20%, settled inside ~440ms.\n      for (let i = 0; i < 3; i++) {\n        const h = dt / 3;\n        s.v += (420 * (s.t - s.y) - 18 * s.v) * h;\n        s.y += s.v * h;\n        s.skv += (-380 * s.sk - 20 * s.skv) * h;\n        s.sk += s.skv * h;\n      }\n      const e = ref.current;\n      const y = clamp(s.y, 0.5, 1.6);\n      if (e) e.style.transform = `scale(${(1 / y).toFixed(4)}, ${y.toFixed(4)}) skewX(${s.sk.toFixed(3)}deg)`;\n      const moving = Math.abs(s.v) > 0.002 || Math.abs(s.y - s.t) > 0.0015 || Math.abs(s.sk) > 0.02 || Math.abs(s.skv) > 0.05;\n      if (moving) s.raf = requestAnimationFrame((t) => tickRef.current(t));\n      else {\n        s.last = 0;\n        if (e && s.t === 1) {\n          s.y = 1;\n          s.sk = 0;\n          e.style.transform = \"\";\n        }\n      }\n    };\n    return () => {\n      cancelAnimationFrame(s.raf);\n      s.raf = 0;\n      s.last = 0;\n    };\n  }, [ref, origin]);\n  const kick = () => {\n    const s = st.current;\n    if (!s.raf) s.raf = requestAnimationFrame((t) => tickRef.current(t));\n  };\n  return {\n    press: (depth = 0.86) => {\n      if (reduced) return;\n      st.current.t = clamp(depth, 0.6, 1);\n      kick();\n    },\n    release: () => {\n      st.current.t = 1;\n      if (!reduced) kick();\n    },\n    flick: (v: number) => {\n      if (reduced) return;\n      st.current.skv += clamp(v, -1, 1) * 160;\n      kick();\n    },\n  };\n}\n\n// Each part carries only the CSS it needs, so any one of them works on its own.\n// revert-layer keeps an element's own rounded corners when a host page's focus rule flattens them.\nconst FOCUS_CSS = \".tm-scope :focus-visible,.tm-scope:focus-visible{outline:2.5px solid var(--tm-ring,#17130f)!important;outline-offset:3px;border-radius:revert-layer}\";\n\nconst TM_BUTTON_INK = {\n  gum: { bg: DOUGH.gum, fg: \"#fffaf1\", tint: \"255,45,120\", border: \"transparent\" },\n  ink: { bg: DOUGH.solid, fg: \"#fffaf1\", tint: \"23,19,15\", border: \"transparent\" },\n  soft: { bg: DOUGH.surface, fg: DOUGH.solid, tint: \"23,19,15\", border: \"rgba(23,19,15,0.1)\" },\n} as const;\n\nexport type TmButtonVariant = keyof typeof TM_BUTTON_INK;\n\n/**\n * SquashPressButton — a pill that squashes under your finger and pops back.\n *\n * Press and it squashes onto its base where you pressed, its area kept (as it\n * gets shorter it gets wider); hold and it sinks further, up to a deep press\n * at 600ms; let go and it springs back past rest, one overshoot, settled\n * inside 450ms. Its two shadows (a contact and a wide one tinted by the pill's\n * own colour) collapse to one tight line while pressed. Swipe across it and it\n * jiggles the way you pushed it. Gum, ink and soft; a link when given an href.\n */\nexport function SquashPressButton({\n  children,\n  href,\n  onClick,\n  type = \"button\",\n  variant = \"ink\",\n  size = \"lg\",\n  disabled = false,\n  ariaLabel,\n  className = \"\",\n}: {\n  children: ReactNode;\n  href?: string;\n  onClick?: () => void;\n  type?: \"button\" | \"submit\";\n  variant?: TmButtonVariant;\n  size?: \"lg\" | \"md\" | \"sm\";\n  disabled?: boolean;\n  ariaLabel?: string;\n  className?: string;\n}) {\n  const ink = TM_BUTTON_INK[variant] ?? TM_BUTTON_INK.ink;\n  const bodyRef = useRef<HTMLSpanElement>(null);\n  const squish = useSquish(bodyRef);\n  const [state, setState] = useState<\"rest\" | \"hover\" | \"down\">(\"rest\");\n  const hold = useRef({ t0: 0, raf: 0, x: 0, t: 0 });\n\n  // Holding sinks it further: 0.9 at the press, 0.8 by 600ms.\n  useEffect(() => {\n    const h = hold.current;\n    return () => {\n      cancelAnimationFrame(h.raf);\n      h.raf = 0;\n    };\n  }, []);\n  const deepen = () => {\n    const h = hold.current;\n    const run = (now: number) => {\n      h.raf = 0;\n      const k = clamp((now - h.t0) / 600, 0, 1);\n      squish.press(0.9 - 0.1 * k);\n      if (k < 1) h.raf = requestAnimationFrame(run);\n    };\n    cancelAnimationFrame(h.raf);\n    h.raf = requestAnimationFrame(run);\n  };\n  const down = (xPct: number) => {\n    // It squashes onto its base under your finger, not about its middle.\n    if (bodyRef.current) bodyRef.current.style.transformOrigin = `${clamp(xPct, 15, 85).toFixed(0)}% 100%`;\n    setState(\"down\");\n    hold.current.t0 = performance.now();\n    squish.press(0.9);\n    deepen();\n  };\n  const up = (to: \"rest\" | \"hover\") => {\n    cancelAnimationFrame(hold.current.raf);\n    hold.current.raf = 0;\n    setState(to);\n    squish.release();\n  };\n\n  const pad = size === \"sm\" ? \"h-9 px-4 text-[12.5px]\" : size === \"md\" ? \"h-11 px-5 text-[13.5px]\" : \"h-14 px-7 text-[15px]\";\n  const shadow = state === \"down\" ? PRESSED_SHADOW : softShadow(ink.tint, state === \"hover\" ? 1.3 : 1);\n  const handlers = {\n    \"aria-label\": ariaLabel,\n    className: \"tm-scope group relative inline-flex select-none rounded-full no-underline outline-none \" + (disabled ? \"pointer-events-none opacity-40 \" : \"\") + className,\n    style: { WebkitTapHighlightColor: \"transparent\", touchAction: \"manipulation\" } as CSSProperties,\n    onPointerEnter: (e: ReactPointerEvent<HTMLElement>) => {\n      setState(\"hover\");\n      hold.current.x = e.clientX;\n      hold.current.t = performance.now();\n    },\n    onPointerMove: (e: ReactPointerEvent<HTMLElement>) => {\n      hold.current.x = e.clientX;\n      hold.current.t = performance.now();\n    },\n    onPointerLeave: (e: ReactPointerEvent<HTMLElement>) => {\n      const dt = Math.max(8, performance.now() - hold.current.t);\n      // A quick swipe out leaves it jiggling the way it was pushed.\n      squish.flick(((e.clientX - hold.current.x) / dt) * 0.6);\n      up(\"rest\");\n    },\n    onPointerDown: (e: ReactPointerEvent<HTMLElement>) => {\n      if (e.button !== 0) return;\n      const r = e.currentTarget.getBoundingClientRect();\n      down(((e.clientX - r.left) / Math.max(1, r.width)) * 100);\n    },\n    onPointerUp: () => up(\"hover\"),\n    onPointerCancel: () => up(\"rest\"),\n    onKeyDown: (e: ReactKeyboardEvent<HTMLElement>) => {\n      if (e.repeat) return;\n      if (e.key === \"Enter\" || (e.key === \" \" && !href)) down(50);\n    },\n    onKeyUp: () => {\n      if (state === \"down\") up(\"rest\");\n    },\n    onBlur: () => {\n      if (state === \"down\") up(\"rest\");\n    },\n  };\n  // The pill that squashes and its shadow live on an inner span, so the hit area never moves.\n  const pill = (\n    <span\n      ref={bodyRef}\n      className={\"relative inline-flex items-center justify-center rounded-full \" + pad}\n      style={{\n        background: ink.bg,\n        color: ink.fg,\n        border: `1px solid ${ink.border}`,\n        boxShadow: shadow,\n        fontFamily: DISPLAY,\n        fontWeight: 600,\n        letterSpacing: \"-0.01em\",\n        transition: \"box-shadow 180ms cubic-bezier(0.2, 0.8, 0.2, 1)\",\n        willChange: \"transform\",\n      }}\n    >\n      <span className=\"whitespace-nowrap\">{children}</span>\n    </span>\n  );\n  if (href) {\n    return (\n      <>\n        <style>{FOCUS_CSS}</style>\n        <a href={safeHref(href)} onClick={onClick} {...handlers}>\n          {pill}\n        </a>\n      </>\n    );\n  }\n  return (\n    <>\n      <style>{FOCUS_CSS}</style>\n      <button type={type} onClick={onClick} disabled={disabled} {...handlers}>\n        {pill}\n      </button>\n    </>\n  );\n}\n","type":"registry:component","target":"components/viberdy/squash-press-button.tsx"}],"meta":{"tier":"free","tags":["button","squash and stretch","spring","press","jelly","shadow"],"href":"https://viberdy.dev/library/squash-press-button","aiPrompt":"Build a React + Tailwind button, `SquashPressButton({ children, href, onClick, type, variant, size, disabled, ariaLabel, className })`.\n\nTactile Maximalism grammar: loud but composed. A dough ground #f7f0e4 over most of the page (surfaces #fffaf1, a pressed well #efe5d3), warm ink #17130f at 92/64/42%, hairlines at 10% and 18%; ONE loud accent, bubblegum #ff2d78 (deep #d8155d), as a filled thing once per screen, and one quiet tint, lavender #e8e1f6, only on small chips, never a second loud colour. Grain at 4-6%, monochrome, static, fixed to the material. Depth is soft and tinted: a contact shadow 0 2px 6px rgba(23,19,15,0.1) plus a wide one 0 18px 40px -8px tinted by what casts it (a gum chip casts pink); pressed, both collapse to 0 1px 3px rgba(23,19,15,0.14). Never a hard offset shadow (that is neo-brutalism). Type: Unbounded at 800 through var(--font-unbounded, \"Unbounded\", ...) for one oversized headline per screen (up to ~15cqw, leading 0.92, tracking -0.045em), Geist for reading at 16-17px/1.5, Geist Mono uppercase at 11-12px for labels. Full pills for every control and chip, 28px cards, 20px nested media. Motion: everything soft keeps its area as it squashes (scaleX = 1 / scaleY about its foot), deforms in proportion to the input (where, how long, how fast), overshoots once and settles inside 450ms (a spring near k 420, c 18), and always returns to one legible rest shape. Reduced motion keeps the states and drops the springs.\n\nThe hit area (a button, or a link when href is given) holds an inner pill span that squashes, so the hit area never moves. A hook `useSquish(ref)` runs one spring on the pill's height factor y (k 420, c 18, three substeps a frame) and one on a skew (k 380, c 20), writing transform: scale(1/y, y) skewX(skew) straight to the element, and stops when settled. Pointer down sets the transform origin to the pressed x (15-85%) at the bottom and presses to 0.9, deepening to 0.8 over a 600ms hold; up or cancel releases to 1 (the spring overshoots once). Leaving with a quick sideways swipe flicks the skew by the pointer's speed. Enter (and Space on a button) presses; key up and blur release. Shadows: rest = a 0 2px 6px contact at 10% plus a 0 18px 40px -8px tinted by the pill's colour at 20%; hover widens it (x1.3); pressed = 0 1px 3px at 14%; transition 180ms. Inks: gum (#ff2d78, paper text, pink shadow), ink (#17130f, paper text), soft (#fffaf1 with a 10% hairline, ink text). Label in Unbounded 600, -0.01em; sizes lg 56px, md 44px, sm 36px tall.","license":"https://viberdy.dev/license"}}