{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggle-button",
  "title": "Toggle Button",
  "description": "A button that stays pressed: the fill grows out from the middle, icons spin into their 'on' version, labels roll from Follow to Following and counts tick up by one. Outline, soft and ghost styles, icon-only or with text.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/toggle-button/toggle-button.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ToggleButtonVariant = \"outline\" | \"soft\" | \"ghost\";\nexport type ToggleButtonSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface ToggleButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"onChange\" | \"children\" | \"onDrag\" | \"onDragStart\" | \"onDragEnd\" | \"onAnimationStart\"> {\n  pressed?: boolean;\n  defaultPressed?: boolean;\n  onPressedChange?: (pressed: boolean) => void;\n  /** Label when off. Leave empty for an icon-only toggle (then pass aria-label). */\n  children?: React.ReactNode;\n  /** Label when on; rolls in over the off label. */\n  pressedLabel?: React.ReactNode;\n  icon?: React.ReactNode;\n  /** Icon when on; defaults to `icon`. */\n  pressedIcon?: React.ReactNode;\n  /** A number after the label that ticks up by one while pressed (stars, likes). */\n  count?: number;\n  variant?: ToggleButtonVariant;\n  size?: ToggleButtonSize;\n  /** Fill of the pressed state. Defaults to white on dark, black on light. */\n  accentColor?: string;\n  theme?: \"dark\" | \"light\";\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.62)\", border: \"rgba(255,255,255,0.14)\", borderHover: \"rgba(255,255,255,0.28)\", soft: \"rgba(255,255,255,0.06)\", hover: \"rgba(255,255,255,0.08)\", accent: \"#F5F4F1\", divider: \"rgba(255,255,255,0.14)\", focus: \"rgba(245,244,241,0.85)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.6)\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", soft: \"rgba(10,10,10,0.05)\", hover: \"rgba(10,10,10,0.06)\", accent: \"#0A0A0A\", divider: \"rgba(10,10,10,0.14)\", focus: \"rgba(10,10,10,0.85)\" },\n};\n\nconst SIZES: Record<ToggleButtonSize, { h: number; font: number; pad: number; gap: number; radius: number }> = {\n  sm: { h: 32, font: 13, pad: 11, gap: 6, radius: 9 },\n  md: { h: 38, font: 14, pad: 14, gap: 7, radius: 11 },\n  lg: { h: 46, font: 15.5, pad: 18, gap: 8, radius: 13 },\n};\n\n// Black or white text on top of the accent, whichever reads better.\nfunction onColor(hex: string) {\n  const m = /^#?([\\da-f]{6})$/i.exec(hex);\n  if (!m) return \"#0A0A0A\";\n  const n = parseInt(m[1], 16);\n  const l = (0.299 * ((n >> 16) & 255) + 0.587 * ((n >> 8) & 255) + 0.114 * (n & 255)) / 255;\n  return l > 0.6 ? \"#0A0A0A\" : \"#FFFFFF\";\n}\n\nexport function ToggleButton({ pressed, defaultPressed = false, onPressedChange, children, pressedLabel, icon, pressedIcon, count, variant = \"outline\", size = \"md\", accentColor, theme = \"dark\", className, disabled, onClick, style, ...rest }: ToggleButtonProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const reduce = Boolean(useReducedMotion());\n  const [internal, setInternal] = React.useState(defaultPressed);\n  const on = pressed ?? internal;\n  const accent = accentColor ?? p.accent;\n  const ink = onColor(accent);\n  const iconOnly = children == null && pressedLabel == null && count == null;\n  const label = on && pressedLabel != null ? pressedLabel : children;\n  const shownIcon = on && pressedIcon ? pressedIcon : icon;\n  const shownCount = count == null ? null : count + (on ? 1 : 0);\n\n  const spring = reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 520, damping: 32 };\n  const roll = reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 600, damping: 38 };\n  const rest0 = on ? ink : variant === \"ghost\" ? p.muted : p.text;\n\n  return (\n    <motion.button\n      type=\"button\"\n      aria-pressed={on}\n      disabled={disabled}\n      onClick={(e) => {\n        onClick?.(e);\n        if (e.defaultPrevented) return;\n        if (pressed === undefined) setInternal(!on);\n        onPressedChange?.(!on);\n      }}\n      whileTap={disabled || reduce ? undefined : { scale: 0.95 }}\n      transition={spring}\n      className={cn(\n        \"relative inline-flex shrink-0 cursor-pointer items-center justify-center overflow-hidden font-medium whitespace-nowrap outline-none select-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-45\",\n        !on && \"hover:bg-[var(--tb-hover)] hover:[border-color:var(--tb-border-hover)]\",\n        className,\n      )}\n      style={{\n        height: s.h,\n        minWidth: iconOnly ? s.h : undefined,\n        padding: iconOnly ? 0 : `0 ${s.pad}px`,\n        gap: s.gap,\n        borderRadius: s.radius,\n        fontSize: s.font,\n        fontFamily: \"Inter, sans-serif\",\n        color: rest0,\n        background: variant === \"soft\" && !on ? p.soft : \"transparent\",\n        border: `1px solid ${variant === \"outline\" || on ? (on ? accent : p.border) : \"transparent\"}`,\n        transition: \"color 0.2s ease, border-color 0.2s ease, background-color 0.15s ease\",\n        [\"--tb-hover\" as string]: p.hover,\n        [\"--tb-border-hover\" as string]: variant === \"outline\" ? p.borderHover : \"transparent\",\n        [\"--tw-ring-color\" as string]: p.focus,\n        [\"--tw-ring-offset-color\" as string]: \"transparent\",\n        ...style,\n      }}\n      {...rest}\n    >\n      {/* The pressed fill grows out from the middle. */}\n      <motion.span aria-hidden=\"true\" className=\"absolute inset-0\" style={{ background: accent, borderRadius: s.radius - 1 }} initial={false} animate={{ opacity: on ? 1 : 0, scale: on ? 1 : 0.6 }} transition={spring} />\n      {shownIcon && (\n        <span className=\"relative flex\" style={{ width: s.font + 3, height: s.font + 3 }}>\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            <motion.span key={on && pressedIcon ? \"on\" : \"off\"} className=\"absolute inset-0 flex items-center justify-center [&>svg]:h-full [&>svg]:w-full\" initial={reduce ? false : { scale: 0.4, opacity: 0, rotate: -30 }} animate={{ scale: 1, opacity: 1, rotate: 0 }} exit={reduce ? undefined : { scale: 0.4, opacity: 0, rotate: 30 }} transition={roll}>\n              {shownIcon}\n            </motion.span>\n          </AnimatePresence>\n        </span>\n      )}\n      {label != null && (\n        <span className=\"relative grid overflow-hidden\" style={{ height: s.font * 1.4 }}>\n          {/* Both labels share one cell so the button is as wide as the longer one and never jumps. */}\n          {pressedLabel != null && (\n            <span aria-hidden=\"true\" className=\"invisible col-start-1 row-start-1\" style={{ lineHeight: `${s.font * 1.4}px` }}>\n              {String(children ?? \"\").length > String(pressedLabel).length ? children : pressedLabel}\n            </span>\n          )}\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            <motion.span key={on && pressedLabel != null ? \"on\" : \"off\"} className=\"col-start-1 row-start-1 text-center\" style={{ lineHeight: `${s.font * 1.4}px` }} initial={reduce ? false : { y: on ? \"100%\" : \"-100%\", opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={reduce ? undefined : { y: on ? \"-100%\" : \"100%\", opacity: 0 }} transition={roll}>\n              {label}\n            </motion.span>\n          </AnimatePresence>\n        </span>\n      )}\n      {shownCount != null && (\n        <>\n          <span aria-hidden=\"true\" className=\"relative self-stretch\" style={{ width: 1, margin: `${s.h * 0.24}px 0`, background: on ? `color-mix(in srgb, ${ink} 25%, transparent)` : p.divider, transition: \"background 0.2s ease\" }} />\n          <span className=\"relative grid overflow-hidden tabular-nums\" style={{ height: s.font * 1.4 }}>\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.span key={shownCount} className=\"col-start-1 row-start-1\" style={{ lineHeight: `${s.font * 1.4}px` }} initial={reduce ? false : { y: on ? \"100%\" : \"-100%\", opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={reduce ? undefined : { y: on ? \"-100%\" : \"100%\", opacity: 0 }} transition={roll}>\n                {shownCount.toLocaleString(\"en-US\")}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n        </>\n      )}\n    </motion.button>\n  );\n}\n\nconst svg = { width: 24, height: 24, viewBox: \"0 0 24 24\", fill: \"none\", stroke: \"currentColor\", strokeWidth: 1.8, strokeLinecap: \"round\" as const, strokeLinejoin: \"round\" as const, \"aria-hidden\": true };\n\n/** A few ready-made icons for the demo and common toggles. */\nexport const ToggleIcons = {\n  bold: <svg {...svg}><path d=\"M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z\" /></svg>,\n  italic: <svg {...svg}><path d=\"M14 5h-4M14 19h-4M14 5l-4 14M10 5h6M8 19h6\" /></svg>,\n  underline: <svg {...svg}><path d=\"M7 4v7a5 5 0 0 0 10 0V4M5 20h14\" /></svg>,\n  bell: <svg {...svg}><path d=\"M6 16V11a6 6 0 1 1 12 0v5l1.5 2h-15zM10 20.5a2.2 2.2 0 0 0 4 0\" /></svg>,\n  bellRing: <svg {...svg}><path d=\"M6 16V11a6 6 0 1 1 12 0v5l1.5 2h-15zM10 20.5a2.2 2.2 0 0 0 4 0M3 7.5A8 8 0 0 1 5.5 3.5M21 7.5a8 8 0 0 0-2.5-4\" /></svg>,\n  star: <svg {...svg}><path d=\"m12 3.8 2.5 5.1 5.6.8-4 4 1 5.6-5.1-2.7-5 2.7 1-5.6-4.1-4 5.6-.8z\" /></svg>,\n  starFilled: <svg {...svg} fill=\"currentColor\"><path d=\"m12 3.8 2.5 5.1 5.6.8-4 4 1 5.6-5.1-2.7-5 2.7 1-5.6-4.1-4 5.6-.8z\" /></svg>,\n  plus: <svg {...svg}><path d=\"M12 5v14M5 12h14\" /></svg>,\n  check: <svg {...svg}><path d=\"m5 12.5 4.5 4.5L19 7.5\" /></svg>,\n  pin: <svg {...svg}><path d=\"M9 4h6l-1 5 3 3v2H7v-2l3-3zM12 14v6\" /></svg>,\n};\n\nexport function ToggleButtonDemo({ theme = \"dark\" }: { theme?: \"dark\" | \"light\" }) {\n  return (\n    <div className=\"flex flex-col items-center\" style={{ gap: 18 }}>\n      <div className=\"flex items-center\" style={{ gap: 6 }}>\n        <ToggleButton theme={theme} variant=\"ghost\" icon={ToggleIcons.bold} aria-label=\"Bold\" defaultPressed />\n        <ToggleButton theme={theme} variant=\"ghost\" icon={ToggleIcons.italic} aria-label=\"Italic\" />\n        <ToggleButton theme={theme} variant=\"ghost\" icon={ToggleIcons.underline} aria-label=\"Underline\" />\n      </div>\n      <div className=\"flex flex-wrap items-center justify-center\" style={{ gap: 10 }}>\n        <ToggleButton theme={theme} icon={ToggleIcons.plus} pressedIcon={ToggleIcons.check} pressedLabel=\"Following\">\n          Follow\n        </ToggleButton>\n        <ToggleButton theme={theme} variant=\"soft\" icon={ToggleIcons.star} pressedIcon={ToggleIcons.starFilled} count={1284}>\n          Star\n        </ToggleButton>\n      </div>\n      <div className=\"flex flex-wrap items-center justify-center\" style={{ gap: 10 }}>\n        <ToggleButton theme={theme} variant=\"outline\" icon={ToggleIcons.bell} pressedIcon={ToggleIcons.bellRing} pressedLabel=\"Subscribed\" accentColor=\"#FFB547\">\n          Subscribe\n        </ToggleButton>\n        <ToggleButton theme={theme} variant=\"soft\" icon={ToggleIcons.pin} aria-label=\"Pin\" />\n      </div>\n    </div>\n  );\n}\n\nexport default ToggleButton;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}