{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"health-bar","type":"registry:ui","title":"Health Bar","description":"An HP bar that drops instantly on a hit while a lighter chip bar trails behind, and glows on heal.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/health-bar.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { motion, useAnimate, useReducedMotion } from \"motion/react\";\nimport { useEffect, useState } from \"react\";\n\ntype HealthBarProps = {\n  /** Current health. Lower it to deal damage, raise it to heal. */\n  value: number;\n  max?: number;\n  /** Fill color of the health bar. */\n  color?: string;\n  /** Color of the chip bar that trails behind after damage. */\n  chipColor?: string;\n  /** Glow and fill color shown while healing. */\n  healColor?: string;\n  /** Show the current and maximum values next to the label. */\n  showNumbers?: boolean;\n  label?: string;\n  className?: string;\n};\n\ntype Change = { n: number; kind: \"damage\" | \"heal\"; delta: number };\n\n/** HP bar that drops instantly on damage while a lighter chip bar catches up, and glows on heal. */\nexport function HealthBar({\n  value,\n  max = 100,\n  color = \"#ff784f\",\n  chipColor = \"#ffc24a\",\n  healColor = \"#6ee09a\",\n  showNumbers = true,\n  label = \"HP\",\n  className = \"\",\n}: HealthBarProps) {\n  const reduce = useReducedMotion();\n  const [scope, animate] = useAnimate();\n  const top = Math.max(1, max);\n  const hp = Math.round(Math.min(top, Math.max(0, value)));\n  const pct = (hp / top) * 100;\n\n  // Compare with the previous value during render to know whether this change is a hit or a heal.\n  const [prev, setPrev] = useState(hp);\n  const [change, setChange] = useState<Change | null>(null);\n  if (hp !== prev) {\n    setPrev(hp);\n    setChange({ n: (change?.n ?? 0) + 1, kind: hp < prev ? \"damage\" : \"heal\", delta: hp - prev });\n  }\n\n  const damage = change?.kind === \"damage\";\n  const heal = change?.kind === \"heal\";\n  const low = pct <= 25 && hp > 0;\n\n  useEffect(() => {\n    if (!change || change.kind !== \"damage\" || reduce || !scope.current) return;\n    animate(scope.current, { x: [0, -5, 5, -3, 3, 0] }, { duration: 0.32, ease: \"easeOut\" });\n  }, [change, reduce, animate, scope]);\n\n  return (\n    <div className={`w-full ${className}`}>\n      <div className=\"mb-1.5 flex items-baseline justify-between gap-3\">\n        <span className=\"font-pixel text-xs uppercase tracking-wide\" style={{ color }}>\n          {label}\n        </span>\n        {showNumbers ? (\n          <span className=\"font-pixel text-xs tabular-nums text-bone\">\n            {hp === 0 ? <span style={{ color }}>KO</span> : hp}\n            <span className=\"text-bone-dim\"> / {top}</span>\n          </span>\n        ) : null}\n      </div>\n\n      <div className=\"relative\">\n        <div\n          ref={scope}\n          className=\"relative h-4 overflow-hidden rounded-[4px] bg-ink-800 ring-1 ring-inset ring-white/10\"\n          role=\"progressbar\"\n          aria-label={label}\n          aria-valuemin={0}\n          aria-valuemax={top}\n          aria-valuenow={hp}\n          aria-valuetext={`${hp} of ${top}`}\n        >\n          {/* Chip: holds the old value for a beat after a hit, then drains down. */}\n          <motion.div\n            className=\"absolute inset-y-0 left-0\"\n            style={{ backgroundColor: chipColor }}\n            initial={false}\n            animate={{ width: `${pct}%` }}\n            transition={damage ? { duration: 0.55, delay: 0.45, ease: [0.45, 0, 0.2, 1] } : { duration: 0 }}\n          />\n          {/* Heal preview: jumps to the new value while the main bar fills up to it. */}\n          <motion.div\n            className=\"absolute inset-y-0 left-0 opacity-60\"\n            style={{ backgroundColor: healColor }}\n            initial={false}\n            animate={{ width: `${pct}%` }}\n            transition={{ duration: 0 }}\n          />\n          <motion.div\n            className=\"absolute inset-y-0 left-0 overflow-hidden\"\n            style={{ backgroundColor: color }}\n            initial={false}\n            animate={{ width: `${pct}%` }}\n            transition={heal ? { duration: 0.6, delay: 0.15, ease: \"easeOut\" } : { duration: 0 }}\n          >\n            <span className=\"absolute inset-x-0 top-0 h-1/3 bg-white/25\" />\n            {low && !reduce ? (\n              <motion.span\n                className=\"absolute inset-0 bg-white\"\n                initial={{ opacity: 0 }}\n                animate={{ opacity: [0, 0.3, 0] }}\n                transition={{ duration: 0.9, repeat: Infinity, ease: \"easeInOut\" }}\n              />\n            ) : null}\n          </motion.div>\n          {/* Segment ticks every tenth of the bar. */}\n          <span\n            className=\"pointer-events-none absolute inset-0\"\n            style={{\n              backgroundImage:\n                \"repeating-linear-gradient(90deg, transparent 0 calc(10% - 2px), rgb(7 6 6 / 0.55) calc(10% - 2px) 10%)\",\n            }}\n          />\n          {damage && !reduce ? (\n            <motion.span\n              key={change.n}\n              className=\"pointer-events-none absolute inset-0 bg-white\"\n              initial={{ opacity: 0.8 }}\n              animate={{ opacity: 0 }}\n              transition={{ duration: 0.35, ease: \"easeOut\" }}\n            />\n          ) : null}\n        </div>\n\n        {heal ? (\n          <motion.span\n            key={change.n}\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute -inset-1 rounded-md\"\n            style={{ boxShadow: `0 0 18px 2px ${healColor}, inset 0 0 0 1px ${healColor}` }}\n            initial={{ opacity: 1 }}\n            animate={{ opacity: 0 }}\n            transition={{ duration: reduce ? 0.2 : 0.9, ease: \"easeOut\" }}\n          />\n        ) : null}\n\n        {change && change.delta !== 0 ? (\n          <motion.span\n            key={`n${change.n}`}\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute -top-1 font-pixel text-sm tabular-nums\"\n            style={{ left: `${pct}%`, color: heal ? healColor : color, textShadow: \"0 2px 0 #070606\" }}\n            initial={{ opacity: 0, y: 0, x: \"-50%\" }}\n            animate={{ opacity: [0, 1, 1, 0], y: reduce ? 0 : -22, x: \"-50%\" }}\n            transition={{ duration: 0.9, ease: \"easeOut\" }}\n          >\n            {change.delta > 0 ? `+${change.delta}` : change.delta}\n          </motion.span>\n        ) : null}\n      </div>\n    </div>\n  );\n}\n"}]}