{"version":3,"file":"index.cjs","sources":["../src/vertical.tsx","../src/index.tsx"],"sourcesContent":["import React from \"react\";\nimport { motion } from \"framer-motion\";\n\ninterface VerticalProps {\n  letter: string;\n}\n\nconst chars = [\"9\", \"8\", \"7\", \"6\", \"5\", \"4\", \"3\", \"2\", \"1\", \"0\", \",\", \".\", \"-\"];\nconst amountOfItems = chars.length + 1;\nconst containerHeight = `${amountOfItems}em`;\n\n// we make a map of char to index: 9 => 0, 8 => 1, 7 => 2, etc.\nconst charsIndex = new Map<string, number>(\n  Array(chars.length).fill(true).map((_, index) => [chars[index], index])\n)\n\nconst children = chars.map((char) => <div key={char}>{char}</div>);\n\nexport function Vertical({ letter }: VerticalProps) {\n  const charIndex = charsIndex.get(letter);\n\n  if (charIndex === undefined) {\n    return <React.Fragment>{letter}</React.Fragment>;\n  }\n\n  const y = `${(-charIndex / (amountOfItems - 1)) * 100}%`;\n\n  return (\n    <div style={{ height: containerHeight, position: \"relative\" }}>\n      <motion.div\n        initial={{ y, opacity: 0 }}\n        animate={{ y, opacity: 1 }}\n        exit={{ y, opacity: 0 }}\n        transition={{ ease: \"easeOut\" }}\n        style={{\n          position: `absolute`,\n          left: 0,\n        }}\n      >\n        {children}\n      </motion.div>\n    </div>\n  );\n}\n","import React, { useMemo, useEffect, useRef, useState } from \"react\";\nimport { motion, AnimatePresence } from \"framer-motion\";\nimport { Vertical } from \"./vertical\";\n\nexport interface MechanicalCounterProps {\n  text: string | number;\n  height?: string | number;\n}\n\nconst transition = { ease: \"easeOut\" };\n\nexport function MechanicalCounter({\n  text,\n  height = \"1em\",\n}: MechanicalCounterProps) {\n  const [isLoaded, set] = useState(false);\n  const ref = useRef<HTMLDivElement>(null);\n  const getTextStats = useMemo(() => generateTextStats(ref), [ref]);\n\n  useEffect(() => {\n    if (\n      typeof document !== \"undefined\" &&\n      typeof document.fonts.ready === \"object\"\n    ) {\n      document.fonts.ready.finally(() => set(true));\n    } else {\n      set(true);\n    }\n  }, []);\n\n  const baseStyles = {\n    height,\n    lineHeight: typeof height === \"string\" ? height : `${height}px`,\n  };\n\n  if (!isLoaded) {\n    // we need the height to calculate the font stats\n    // so we need opacity 0 to hide the font until we have it\n    return (\n      <div style={{ ...baseStyles, opacity: 0 }}>\n        <span ref={ref}>{text}</span>\n      </div>\n    );\n  } \n\n  const textArray = String(text).split(\"\");\n  const stats = textArray.map(getTextStats);\n  const totalWidth = Math.ceil(stats.reduce(count, 0));\n\n  return (\n    <motion.div\n      initial={{\n        width: totalWidth,\n      }}\n      animate={{\n        width: totalWidth,\n      }}\n      transition={transition}\n      style={{\n        ...baseStyles,\n        overflow: \"hidden\",\n        position: \"relative\",\n      }}\n    >\n      {/* this is the text that the user can select and copy */}\n      <span\n        style={{\n          color: \"transparent\",\n          position: `absolute`,\n          top: 0,\n          left: 0,\n        }}\n        ref={ref}\n      >\n        {text}\n      </span>\n\n      <AnimatePresence initial={false}>\n        {textArray.map((letter, index) => {\n          const x = stats.slice(0, index).reduce(count, 0);\n          const width = stats[index];\n\n          // animate from the right to left, so we need to invert the index\n          const key = `${textArray.length - index}`;\n\n          return (\n            <motion.span\n              key={key}\n              layoutId={key}\n              animate={{ x, width, opacity: 1 }}\n              initial={{ x, width, opacity: 0 }}\n              exit={{ width: 0, opacity: 0 }}\n              transition={{ ease: \"easeOut\" }}\n              style={{\n                position: \"absolute\",\n                top: 0,\n                left: 0,\n                pointerEvents: \"none\",\n              }}\n              aria-hidden=\"true\"\n            >\n              <Vertical letter={letter} />\n            </motion.span>\n          );\n        })}\n      </AnimatePresence>\n    </motion.div>\n  );\n}\n\nfunction count(acc: number, curr: number) {\n  return acc + curr;\n}\n\nfunction generateTextStats(ref: React.RefObject<HTMLDivElement>) {\n  const cache = new Map<string, number>();\n\n  // safety for nodejs/ssr\n  if (typeof document === \"undefined\") {\n    return function (letter: string): number {\n      return cache.get(letter) ?? 0;\n    };\n  }\n\n  let hasCalculatedFont = false;\n  const canvas = document.createElement(\"canvas\");\n  const context = canvas.getContext(\"2d\") as CanvasRenderingContext2D;\n\n  return function (letter: string): number {\n    if (!cache.has(letter)) {\n      if (!hasCalculatedFont) {\n        context.font = getFont(ref.current ?? document.body);\n        hasCalculatedFont = true;\n      }\n      cache.set(letter, context.measureText(letter)?.width ?? 0);\n    }\n\n    return cache.get(letter) ?? 0;\n  };\n}\n\nfunction getFont(element: HTMLElement) {\n  const font = getComputedStyle(element).getPropertyValue(\"font\");\n\n  if (font) {\n    return font;\n  }\n\n  const fontFamily = getComputedStyle(element).getPropertyValue(\"font-family\");\n  const fontSize = getComputedStyle(element).getPropertyValue(\"font-size\");\n\n  return `${fontSize} / ${fontSize} ${fontFamily}`;\n}\n"],"names":["chars","amountOfItems","length","containerHeight","charsIndex","Map","Array","fill","map","_","index","children","char","React","createElement","key","Vertical","_ref","letter","charIndex","get","undefined","Fragment","y","style","height","position","motion","div","initial","opacity","animate","exit","transition","ease","left","count","acc","curr","text","_ref$height","_useState","useState","isLoaded","set","useRef","getTextStats","useMemo","ref","cache","document","_cache$get","hasCalculatedFont","context","getContext","_cache$get2","_context$measureText$","_context$measureText","_ref$current","has","font","element","getComputedStyle","getPropertyValue","fontFamily","fontSize","getFont","current","body","measureText","width","generateTextStats","useEffect","fonts","ready","baseStyles","lineHeight","_extends","textArray","String","split","Math","ceil","stats","reduce","totalWidth","overflow","color","top","AnimatePresence","slice","span","layoutId","x","pointerEvents"],"mappings":"sXAOA,IAAWA,EAAG,CAAC,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,KACrEC,EAAgBD,EAAME,OAAS,EAC/BC,EAAqBF,EAA3B,KAGMG,EAAa,IAAIC,IACrBC,MAAMN,EAAME,QAAQK,MAAK,GAAMC,IAAI,SAACC,EAAGC,GAAJ,MAAc,CAACV,EAAMU,GAAQA,EAA7B,IAGvBC,EAAGX,EAAMQ,IAAI,SAACI,gBAAS,OAAAC,EAAA,QAAAC,cAAA,MAAA,CAAKC,IAAKH,GAAO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