"use client"
import { motion, useReducedMotion } from "motion/react"
import { ENTER } from "@/components/site/motion"
import { OTPInput, type SlotProps } from "input-otp"
import { useState } from "react"
const BLINK = {
opacity: [1, 0, 0, 1, 1],
transition: {
duration: 1,
times: [0, 0.2, 0.5, 0.7, 1],
repeat: Number.POSITIVE_INFINITY,
ease: "linear" as const,
},
}
function Slot({ char, isActive, reduced }: SlotProps & { reduced: boolean }) {
return (
<div
className="group relative flex h-10 w-9 items-center justify-center rounded-cell border border-gray-700 font-medium text-base text-gray-1200 contrast-more:border-gray-900"
>
{char !== null && (
<div className="relative flex items-center justify-center overflow-hidden">
<motion.span
animate={ENTER.to}
initial={reduced ? ENTER.to : ENTER.from}
key={char}
transition={reduced ? { duration: 0 } : ENTER.transition}
>
{char}
</motion.span>
</div>
)}
{char === null && isActive && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<motion.span
animate={reduced ? { opacity: 1 } : { opacity: BLINK.opacity }}
className="z-20 h-4.5 w-px bg-gray-1000"
transition={reduced ? { duration: 0 } : BLINK.transition}
/>
</div>
)}
{isActive && (
<div className="absolute inset-0 z-10 rounded-[inherit] ring-2 ring-gray-700 contrast-more:ring-gray-900" />
)}
</div>
)
}
export function InputOtp({
length = 6,
onComplete,
}: {
length?: number
onComplete?: (value: string) => void
}) {
const [value, setValue] = useState("")
const reduced = useReducedMotion() ?? false
const half = Math.ceil(length / 2)
return (
<OTPInput
aria-label="Verification code"
autoComplete="one-time-code"
containerClassName="flex items-center gap-2 has-disabled:opacity-50"
inputMode="numeric"
maxLength={length}
onChange={setValue}
onComplete={onComplete}
pattern="^\d+$"
render={({ slots }) => (
<>
<div className="flex items-center gap-1">
{slots.slice(0, half).map((slot, i) => (
<Slot key={i} {...slot} reduced={reduced} />
))}
</div>
<div className="h-0.5 w-2 rounded-full bg-gray-700 contrast-more:bg-gray-900" />
<div className="flex items-center gap-1">
{slots.slice(half).map((slot, i) => (
<Slot key={i + half} {...slot} reduced={reduced} />
))}
</div>
</>
)}
value={value}
/>
)
}