fosrl.pangolin/src/components/ui/input-otp.tsx

85 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-10-12 23:00:36 -04:00
"use client"
import * as React from "react"
import { OTPInput, OTPInputContext } from "input-otp"
import { Dot } from "lucide-react"
2025-01-01 21:41:31 -05:00
import { cn } from "@app/lib/cn"
2024-10-12 23:00:36 -04:00
2025-04-12 12:40:52 -04:00
const InputOTP = (
{
ref,
className,
containerClassName,
...props
}: React.ComponentPropsWithoutRef<typeof OTPInput> & {
ref: React.RefObject<React.ElementRef<typeof OTPInput>>;
}
) => (<OTPInput
ref={ref}
containerClassName={cn(
"flex items-center gap-2 has-disabled:opacity-50",
containerClassName
)}
className={cn("disabled:cursor-not-allowed", className)}
{...props}
/>)
2024-10-12 23:00:36 -04:00
InputOTP.displayName = "InputOTP"
2025-04-12 12:40:52 -04:00
const InputOTPGroup = (
{
ref,
className,
...props
}: React.ComponentPropsWithoutRef<"div"> & {
ref: React.RefObject<React.ElementRef<"div">>;
}
) => (<div ref={ref} className={cn("flex items-center", className)} {...props} />)
2024-10-12 23:00:36 -04:00
InputOTPGroup.displayName = "InputOTPGroup"
2025-04-12 12:40:52 -04:00
const InputOTPSlot = (
{
ref,
index,
className,
...props
}
) => {
2024-10-12 23:00:36 -04:00
const inputOTPContext = React.useContext(OTPInputContext)
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index]
return (
<div
ref={ref}
className={cn(
2025-03-01 17:45:38 -05:00
"relative flex h-10 w-10 items-center justify-center border-y-2 border-r-2 border-input text-base md:text-sm transition-all first:rounded-l-md first:border-l-2 last:rounded-r-md",
2024-10-12 23:00:36 -04:00
isActive && "z-10 ring-2 ring-ring ring-offset-background",
className
)}
{...props}
>
{char}
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
</div>
)}
</div>
)
2025-04-12 12:40:52 -04:00
}
2024-10-12 23:00:36 -04:00
InputOTPSlot.displayName = "InputOTPSlot"
2025-04-12 12:40:52 -04:00
const InputOTPSeparator = (
{
ref,
...props
}: React.ComponentPropsWithoutRef<"div"> & {
ref: React.RefObject<React.ElementRef<"div">>;
}
) => (<div ref={ref} role="separator" {...props}>
<Dot />
</div>)
2024-10-12 23:00:36 -04:00
InputOTPSeparator.displayName = "InputOTPSeparator"
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }