mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-11 22:44:56 +02:00
84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { OTPInput, OTPInputContext } from "input-otp"
|
|
import { Dot } from "lucide-react"
|
|
|
|
import { cn } from "@app/lib/cn"
|
|
|
|
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}
|
|
/>)
|
|
InputOTP.displayName = "InputOTP"
|
|
|
|
const InputOTPGroup = (
|
|
{
|
|
ref,
|
|
className,
|
|
...props
|
|
}: React.ComponentPropsWithoutRef<"div"> & {
|
|
ref: React.RefObject<React.ElementRef<"div">>;
|
|
}
|
|
) => (<div ref={ref} className={cn("flex items-center", className)} {...props} />)
|
|
InputOTPGroup.displayName = "InputOTPGroup"
|
|
|
|
const InputOTPSlot = (
|
|
{
|
|
ref,
|
|
index,
|
|
className,
|
|
...props
|
|
}
|
|
) => {
|
|
const inputOTPContext = React.useContext(OTPInputContext)
|
|
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index]
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className={cn(
|
|
"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",
|
|
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>
|
|
)
|
|
}
|
|
InputOTPSlot.displayName = "InputOTPSlot"
|
|
|
|
const InputOTPSeparator = (
|
|
{
|
|
ref,
|
|
...props
|
|
}: React.ComponentPropsWithoutRef<"div"> & {
|
|
ref: React.RefObject<React.ElementRef<"div">>;
|
|
}
|
|
) => (<div ref={ref} role="separator" {...props}>
|
|
<Dot />
|
|
</div>)
|
|
InputOTPSeparator.displayName = "InputOTPSeparator"
|
|
|
|
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }
|