{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dither-credit-card","title":"Dither Credit Card","author":"CoDesign-Spa27","description":"A pointer-responsive payment card with a restrained dithering shader and spring-smoothed depth.","dependencies":["@paper-design/shaders-react","motion"],"files":[{"path":"components/ui-components/dither-credit-card.tsx","content":"\"use client\";\n\nimport { Dithering, type DitheringProps } from \"@paper-design/shaders-react\";\nimport { motion, useMotionValue, useReducedMotion, useSpring } from \"motion/react\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport type DitherCreditCardColors = {\n  accent?: string;\n  background?: string;\n  details?: string;\n  foreground?: string;\n};\n\nexport type DitherCreditCardContent = {\n  brandName?: string;\n  cardNumber?: string;\n  cardholder?: string;\n};\n\nexport type DitherCreditCardDither = {\n  offsetX?: number;\n  offsetY?: number;\n  rotation?: number;\n  scale?: number;\n  shape?: DitheringProps[\"shape\"];\n  size?: number;\n  speed?: number;\n  type?: DitheringProps[\"type\"];\n};\n\nexport type DitherCreditCardPhysicsMode = \"tilt\" | \"magnetic\" | \"zero-gravity\";\n\nexport type DitherCreditCardSpring = {\n  bounce?: number;\n  damping?: number;\n  mass?: number;\n  stiffness?: number;\n  visualDuration?: number;\n};\n\nexport type DitherCreditCardPhysics = {\n  glare?: number;\n  lift?: number;\n  magnetism?: number;\n  mode?: DitherCreditCardPhysicsMode;\n  perspective?: number;\n  scale?: number;\n  spring?: DitherCreditCardSpring;\n  tilt?: number;\n};\n\nexport type DitherCreditCardProps = {\n  ariaLabel?: string;\n  className?: string;\n  colors?: DitherCreditCardColors;\n  content?: DitherCreditCardContent;\n  dither?: DitherCreditCardDither;\n  interactive?: boolean;\n  physics?: DitherCreditCardPhysics;\n  /** @deprecated Use `interactive` to enable or disable the complete physics system. */\n  tilt?: boolean;\n};\n\nconst CARD_DOTS = [\"dot-1\", \"dot-2\", \"dot-3\", \"dot-4\"];\nconst DEFAULT_PHYSICS = {\n  glare: 0.28,\n  lift: 8,\n  magnetism: 14,\n  mode: \"tilt\",\n  perspective: 1200,\n  scale: 1,\n  spring: {\n    bounce: 0.08,\n    visualDuration: 0.32,\n  },\n  tilt: 7,\n} satisfies Required<DitherCreditCardPhysics>;\nconst DEFAULT_COLORS = {\n  accent: \"#ffc831\",\n  background: \"#faa200\",\n  details: \"#5f5f5f\",\n  foreground: \"#ffffff\",\n} satisfies Required<DitherCreditCardColors>;\nconst DEFAULT_CONTENT = {\n  brandName: \"Skecher-ui\",\n  cardNumber: \"4325 2535\",\n  cardholder: \"@roohbuilds\",\n} satisfies Required<DitherCreditCardContent>;\nconst DEFAULT_DITHER = {\n  offsetX: 0.6,\n  offsetY: 0.46,\n  rotation: 8,\n  scale: 2.52,\n  shape: \"wave\",\n  size: 2,\n  speed: 0.26,\n  type: \"8x8\",\n} satisfies Required<DitherCreditCardDither>;\n\nconst SHADER_STYLE = { width: \"100%\", height: \"100%\" } as const;\n\nfunction resolveSpring(spring: DitherCreditCardSpring) {\n  if (spring.visualDuration !== undefined || spring.bounce !== undefined) {\n    return {\n      bounce: spring.bounce ?? 0,\n      visualDuration: spring.visualDuration ?? 0.32,\n    };\n  }\n\n  return {\n    damping: spring.damping ?? 22,\n    mass: spring.mass ?? 0.8,\n    stiffness: spring.stiffness ?? 180,\n  };\n}\n\nfunction useFinePointer() {\n  const [hasFinePointer, setHasFinePointer] = useState(false);\n\n  useEffect(() => {\n    const query = window.matchMedia(\"(hover: hover) and (pointer: fine)\");\n    const updatePointer = () => setHasFinePointer(query.matches);\n\n    updatePointer();\n    query.addEventListener(\"change\", updatePointer);\n\n    return () => query.removeEventListener(\"change\", updatePointer);\n  }, []);\n\n  return hasFinePointer;\n}\n\nfunction SkecherMark({ accent, foreground }: { accent: string; foreground: string }) {\n  return (\n    <svg width=\"41\" height=\"39\" viewBox=\"0 0 41 39\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n      <rect width=\"40\" height=\"39\" rx=\"10\" fill={foreground} />\n      <path\n        d=\"M5.29221 15.9984C5.56471 15.5953 6.13438 15.4456 6.62038 15.6493L29.1293 25.0859C29.6153 25.2897 30.185 25.1399 30.4575 24.7369L36.4991 15.7999C36.9561 15.1239 36.3153 14.2553 35.4349 14.3575L27.0571 15.3291C26.7476 15.365 26.4755 15.5233 26.3149 15.761L16.4275 30.3865C16.0495 30.9456 15.1552 30.9772 14.6985 30.4476L7.14633 21.6888L4.38808 18.5339C4.11343 18.2198 4.08075 17.7904 4.30612 17.457L5.29221 15.9984Z\"\n        fill={accent}\n      />\n    </svg>\n  );\n}\n\nfunction VisaMark({ color }: { color: string }) {\n  return (\n    <svg aria-label=\"Visa\" className=\"h-auto w-full\" role=\"img\" viewBox=\"0 0 69 23\">\n      <text\n        fill={color}\n        fontFamily=\"Arial Black, Arial, sans-serif\"\n        fontSize=\"26\"\n        fontStyle=\"italic\"\n        fontWeight=\"900\"\n        letterSpacing=\"-1.7\"\n        x=\"0\"\n        y=\"22\"\n      >\n        VISA\n      </text>\n    </svg>\n  );\n}\n\nexport function DitherCreditCard({\n  ariaLabel,\n  className,\n  colors,\n  content,\n  dither,\n  interactive = true,\n  physics,\n  tilt = true,\n}: DitherCreditCardProps) {\n  const shouldReduceMotion = useReducedMotion();\n  const hasFinePointer = useFinePointer();\n  const [isHovered, setIsHovered] = useState(false);\n  const pointerBoundsRef = useRef<DOMRect | null>(null);\n  const rotateXTarget = useMotionValue(0);\n  const rotateYTarget = useMotionValue(0);\n  const xTarget = useMotionValue(0);\n  const yTarget = useMotionValue(0);\n  const scaleTarget = useMotionValue(1);\n  const glareXTarget = useMotionValue(0);\n  const glareYTarget = useMotionValue(0);\n  const glareOpacityTarget = useMotionValue(0);\n  const resolvedColors = { ...DEFAULT_COLORS, ...colors };\n  const resolvedContent = { ...DEFAULT_CONTENT, ...content };\n  const resolvedDither = { ...DEFAULT_DITHER, ...dither };\n  const resolvedPhysics = {\n    ...DEFAULT_PHYSICS,\n    ...physics,\n    spring: { ...DEFAULT_PHYSICS.spring, ...physics?.spring },\n  };\n  const spring = resolveSpring(resolvedPhysics.spring);\n  const rotateX = useSpring(rotateXTarget, spring);\n  const rotateY = useSpring(rotateYTarget, spring);\n  const x = useSpring(xTarget, spring);\n  const y = useSpring(yTarget, spring);\n  const scale = useSpring(scaleTarget, spring);\n  const glareX = useSpring(glareXTarget, spring);\n  const glareY = useSpring(glareYTarget, spring);\n  const glareOpacity = useSpring(glareOpacityTarget, {\n    bounce: 0,\n    visualDuration: 0.2,\n  });\n  const physicsEnabled = interactive && tilt && hasFinePointer && !shouldReduceMotion;\n  const lastFourDigits = resolvedContent.cardNumber.replace(/\\D/g, \"\").slice(-4);\n  const resolvedAriaLabel =\n    ariaLabel ??\n    `${resolvedContent.brandName} Visa card${lastFourDigits ? ` ending in ${lastFourDigits}` : \"\"}`;\n\n  const resetPhysics = useCallback(() => {\n    pointerBoundsRef.current = null;\n    rotateXTarget.set(0);\n    rotateYTarget.set(0);\n    xTarget.set(0);\n    yTarget.set(0);\n    scaleTarget.set(1);\n    glareXTarget.set(0);\n    glareYTarget.set(0);\n    glareOpacityTarget.set(0);\n  }, [\n    glareOpacityTarget,\n    glareXTarget,\n    glareYTarget,\n    rotateXTarget,\n    rotateYTarget,\n    scaleTarget,\n    xTarget,\n    yTarget,\n  ]);\n\n  useEffect(() => {\n    if (!physicsEnabled) {\n      setIsHovered(false);\n      resetPhysics();\n    }\n  }, [physicsEnabled, resetPhysics]);\n\n  const handlePointerMove = useCallback(\n    (event: React.PointerEvent<HTMLElement>) => {\n      if (!physicsEnabled || event.pointerType !== \"mouse\") return;\n\n      const bounds = pointerBoundsRef.current ?? event.currentTarget.getBoundingClientRect();\n      pointerBoundsRef.current = bounds;\n\n      const normalizedX = Math.max(\n        -1,\n        Math.min(1, ((event.clientX - bounds.left) / bounds.width) * 2 - 1),\n      );\n      const normalizedY = Math.max(\n        -1,\n        Math.min(1, ((event.clientY - bounds.top) / bounds.height) * 2 - 1),\n      );\n      const mode = resolvedPhysics.mode;\n      const rotationFactor = mode === \"magnetic\" ? 0.45 : 1;\n      const magnetismFactor = mode === \"tilt\" ? 0 : mode === \"zero-gravity\" ? 1.35 : 1;\n      const liftFactor = mode === \"zero-gravity\" ? 1.45 : 1;\n\n      rotateXTarget.set(normalizedY * resolvedPhysics.tilt * -rotationFactor);\n      rotateYTarget.set(normalizedX * resolvedPhysics.tilt * rotationFactor);\n      xTarget.set(normalizedX * resolvedPhysics.magnetism * magnetismFactor);\n      yTarget.set(\n        normalizedY * resolvedPhysics.magnetism * magnetismFactor -\n          resolvedPhysics.lift * liftFactor,\n      );\n      glareXTarget.set(normalizedX * 88);\n      glareYTarget.set(normalizedY * 56);\n    },\n    [\n      glareXTarget,\n      glareYTarget,\n      physicsEnabled,\n      resolvedPhysics.lift,\n      resolvedPhysics.magnetism,\n      resolvedPhysics.mode,\n      resolvedPhysics.tilt,\n      rotateXTarget,\n      rotateYTarget,\n      xTarget,\n      yTarget,\n    ],\n  );\n\n  return (\n    <motion.article\n      aria-label={resolvedAriaLabel}\n      className={cn(\n        \"relative isolate aspect-[121/73] w-full max-w-[605px] origin-center overflow-hidden rounded-[clamp(24px,7.6%,6px)] border border-neutral-300/70 [backface-visibility:hidden] [container-type:inline-size] [transform-style:preserve-3d]\",\n        className,\n      )}\n      onPointerEnter={(event) => {\n        if (physicsEnabled && event.pointerType === \"mouse\") {\n          pointerBoundsRef.current = event.currentTarget.getBoundingClientRect();\n          setIsHovered(true);\n          const liftFactor = resolvedPhysics.mode === \"zero-gravity\" ? 1.45 : 1;\n          yTarget.set(resolvedPhysics.lift * -liftFactor);\n          scaleTarget.set(resolvedPhysics.scale);\n          glareOpacityTarget.set(resolvedPhysics.glare);\n        }\n      }}\n      onPointerLeave={() => {\n        setIsHovered(false);\n        resetPhysics();\n      }}\n      onPointerMove={handlePointerMove}\n      style={{\n        backgroundColor: resolvedColors.background,\n        rotateX,\n        rotateY,\n        scale,\n        transformPerspective: resolvedPhysics.perspective,\n        willChange: isHovered ? \"transform\" : \"auto\",\n        x,\n        y,\n      }}\n    >\n      <Dithering\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 size-full\"\n        width={1280}\n        height={720}\n        colorBack={resolvedColors.background}\n        colorFront={resolvedColors.foreground}\n        shape={resolvedDither.shape}\n        type={resolvedDither.type}\n        size={resolvedDither.size}\n        speed={shouldReduceMotion ? 0 : resolvedDither.speed}\n        scale={resolvedDither.scale}\n        rotation={resolvedDither.rotation}\n        offsetX={resolvedDither.offsetX}\n        offsetY={resolvedDither.offsetY}\n        style={SHADER_STYLE}\n      />\n\n      <motion.span\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute -inset-[28%] z-[5] rounded-full mix-blend-soft-light\"\n        style={{\n          background: `radial-gradient(circle, color-mix(in srgb, ${resolvedColors.foreground} 68%, transparent) 0%, transparent 64%)`,\n          opacity: glareOpacity,\n          x: glareX,\n          y: glareY,\n        }}\n      />\n\n      <div\n        className=\"absolute left-[7.27%] top-[8.77%] z-10 flex items-center gap-[clamp(7px,2cqw,12px)]\"\n        style={{ color: resolvedColors.foreground }}\n      >\n        <span className=\"block w-[clamp(28px,6.78cqw,41px)] shrink-0\">\n          <SkecherMark accent={resolvedColors.accent} foreground={resolvedColors.foreground} />\n        </span>\n        <span className=\"whitespace-nowrap text-[clamp(20px,4.96cqw,30px)] leading-none font-semibold\">\n          {resolvedContent.brandName}\n        </span>\n      </div>\n\n      <span className=\"absolute right-[7.1%] top-[11.5%] z-10 w-[11.4%] min-w-12\">\n        <VisaMark color={resolvedColors.foreground} />\n      </span>\n\n      <p\n        className=\"absolute bottom-[10.25%] left-[7.27%] z-10 m-0 flex items-center gap-[clamp(5px,1.15cqw,7px)] font-sans text-[clamp(15px,3.97cqw,24px)] font-semibold leading-none tracking-[-0.02em]\"\n        style={{ color: resolvedColors.details }}\n      >\n        <span aria-hidden=\"true\" className=\"flex gap-[clamp(4px,1cqw,6px)]\">\n          {CARD_DOTS.map((dot) => (\n            <span className=\"size-[clamp(5px,1.32cqw,8px)] rounded-full bg-current\" key={dot} />\n          ))}\n        </span>\n        <span>{resolvedContent.cardNumber}</span>\n      </p>\n\n      <p\n        className=\"absolute bottom-[10.25%] right-[7.1%] z-10 m-0 font-sans text-[clamp(15px,3.97cqw,24px)] font-semibold leading-none tracking-[-0.035em]\"\n        style={{ color: resolvedColors.details }}\n      >\n        {resolvedContent.cardholder}\n      </p>\n    </motion.article>\n  );\n}\n","type":"registry:ui"}],"categories":["card","shader"],"type":"registry:ui"}