Files
shakespeare/src/components/ui/password-input.tsx
T
2025-12-03 23:02:50 -06:00

57 lines
2.1 KiB
TypeScript

import * as React from "react"
import { Eye, EyeOff } from "lucide-react"
import { cn } from "@/lib/utils"
interface PasswordInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
/** Whether to show the visibility toggle button. Defaults to true. */
showToggle?: boolean;
}
const PasswordInput = React.forwardRef<HTMLInputElement, PasswordInputProps>(
({ className, showToggle = true, ...props }, ref) => {
const [showPassword, setShowPassword] = React.useState(false)
const [isFocused, setIsFocused] = React.useState(false)
return (
<div className={cn("relative", className)}>
<input
type={showPassword ? "text" : "password"}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-base ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
showToggle && "pr-10",
isFocused && "ring-2 ring-ring ring-offset-2"
)}
ref={ref}
onFocus={(e) => {
setIsFocused(true)
props.onFocus?.(e)
}}
onBlur={(e) => {
setIsFocused(false)
props.onBlur?.(e)
}}
{...props}
/>
{showToggle && (
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
disabled={props.disabled}
aria-label={showPassword ? "Hide password" : "Show password"}
>
{showPassword ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</button>
)}
</div>
)
}
)
PasswordInput.displayName = "PasswordInput"
export { PasswordInput }