{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "input-52",
  "title": "Password strength",
  "description": "A meter and a checklist that update as the password grows.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/field.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/input.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/input/input-52.tsx",
      "content": "'use client';\n\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useId, useState, type ChangeEvent } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  CheckIcon,\n  EyeIcon,\n  EyeOffIcon,\n  XIcon,\n} from '@/registry/ndeko/lib/icons';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { Field, FieldLabel } from '@/registry/ndeko/ui/field';\nimport { Input } from '@/registry/ndeko/ui/input';\n\ntype Requirement = { pattern: RegExp; label: string };\n\nconst REQUIREMENTS: Requirement[] = [\n  { pattern: /.{8,}/, label: 'At least 8 characters' },\n  { pattern: /[0-9]/, label: 'At least 1 number' },\n  { pattern: /[a-z]/, label: 'At least 1 lowercase letter' },\n  { pattern: /[A-Z]/, label: 'At least 1 uppercase letter' },\n];\n\nconst MAX_SCORE = REQUIREMENTS.length;\n\nconst FIELD_BUTTON =\n  'text-muted-foreground hover:bg-foreground/5 hover:text-foreground focus-visible:ring-ring/25 absolute inset-y-0 end-1 my-auto flex size-6 items-center justify-center rounded-md outline-none transition-[color,background-color,box-shadow] focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50';\n\nconst STRENGTH_FILL = [\n  'h-full w-full rounded-full',\n  '-translate-x-(--strength-remaining) rtl:translate-x-(--strength-remaining)',\n  'transition-[translate,background-color] duration-300 ease-(--ease-out-strong)',\n].join(' ');\n\nconst SWAP_TRANSITION: Transition = { duration: 0.15, ease: [0.2, 0, 0, 1] };\n\nconst SWAP_VARIANTS: Variants = {\n  enter: { opacity: 0, scale: 0.6, filter: 'blur(2px)' },\n  visible: { opacity: 1, scale: 1, filter: 'blur(0px)' },\n  exit: { opacity: 0, scale: 0.6, filter: 'blur(2px)' },\n};\n\nconst FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1 },\n  exit: { opacity: 0 },\n};\n\nfunction swapVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return SWAP_VARIANTS;\n}\n\nfunction inputTypeOf(isVisible: boolean) {\n  if (isVisible) {\n    return 'text';\n  }\n  return 'password';\n}\n\nfunction visibilityGlyphOf(isVisible: boolean) {\n  if (isVisible) {\n    return { key: 'hide', icon: <EyeOffIcon className='size-4' /> };\n  }\n  return { key: 'show', icon: <EyeIcon className='size-4' /> };\n}\n\nfunction requirementGlyphOf(isMet: boolean) {\n  if (isMet) {\n    return {\n      key: 'met',\n      icon: <CheckIcon className='text-success-foreground size-4' />,\n    };\n  }\n  return {\n    key: 'unmet',\n    icon: <XIcon className='text-muted-foreground size-4' />,\n  };\n}\n\nfunction requirementStatusOf(isMet: boolean) {\n  if (isMet) {\n    return 'met';\n  }\n  return 'not met';\n}\n\nfunction strengthToneOf(score: number) {\n  if (score === 0) {\n    return 'bg-transparent';\n  }\n  if (score <= 2) {\n    return 'bg-destructive';\n  }\n  if (score === 3) {\n    return 'bg-warning';\n  }\n  return 'bg-success';\n}\n\nfunction strengthLabelOf(score: number) {\n  if (score === 0) {\n    return 'Enter a password';\n  }\n  if (score <= 2) {\n    return 'Weak password';\n  }\n  if (score === 3) {\n    return 'Medium password';\n  }\n  return 'Strong password';\n}\n\nexport default function Input52() {\n  const inputId = useId();\n  const descriptionId = useId();\n  const [password, setPassword] = useState('');\n  const [isVisible, setIsVisible] = useState(false);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const swapVariants = swapVariantsOf(shouldReduceMotion);\n  const visibilityGlyph = visibilityGlyphOf(isVisible);\n  const checks = REQUIREMENTS.map((requirement) => ({\n    label: requirement.label,\n    isMet: requirement.pattern.test(password),\n  }));\n  const score = checks.filter((check) => check.isMet).length;\n  const strengthRatio = score / MAX_SCORE;\n  const strengthPercent = strengthRatio * 100;\n  const strengthRemaining = 100 - strengthPercent;\n  const strengthLabel = strengthLabelOf(score);\n  const fillStyle = {\n    '--strength-remaining': `${strengthRemaining}%`,\n  } as React.CSSProperties;\n\n  function changePassword(event: ChangeEvent<HTMLInputElement>) {\n    setPassword(event.currentTarget.value);\n  }\n\n  function toggleVisibility() {\n    setIsVisible((current) => !current);\n  }\n\n  return (\n    <div className='flex w-full max-w-xs flex-col'>\n      <Field>\n        <FieldLabel>Password</FieldLabel>\n        <div className='relative'>\n          <Input\n            id={inputId}\n            className='pe-8'\n            type={inputTypeOf(isVisible)}\n            placeholder='Choose a password'\n            value={password}\n            onChange={changePassword}\n            aria-describedby={descriptionId}\n          />\n          <button\n            type='button'\n            className={FIELD_BUTTON}\n            onClick={toggleVisibility}\n            aria-label='Show password'\n            aria-pressed={isVisible}\n            aria-controls={inputId}\n          >\n            <AnimatePresence mode='popLayout' initial={false}>\n              <motion.span\n                key={visibilityGlyph.key}\n                className='flex'\n                variants={swapVariants}\n                initial='enter'\n                animate='visible'\n                exit='exit'\n                transition={SWAP_TRANSITION}\n              >\n                {visibilityGlyph.icon}\n              </motion.span>\n            </AnimatePresence>\n          </button>\n        </div>\n      </Field>\n      <div\n        className='bg-foreground/[0.08] mt-3 mb-4 h-1 w-full overflow-hidden rounded-full'\n        role='progressbar'\n        aria-valuenow={score}\n        aria-valuemin={0}\n        aria-valuemax={MAX_SCORE}\n        aria-label='Password strength'\n      >\n        <div\n          className={cn(STRENGTH_FILL, strengthToneOf(score))}\n          style={fillStyle}\n        />\n      </div>\n      <p id={descriptionId} className='mb-2 text-[13px] font-medium'>\n        <span className='sr-only'>{strengthLabel}. Must contain:</span>\n        <span aria-hidden='true'>\n          <span className='relative inline-flex'>\n            <AnimatePresence mode='popLayout' initial={false}>\n              <motion.span\n                key={strengthLabel}\n                className='inline-flex'\n                variants={swapVariants}\n                initial='enter'\n                animate='visible'\n                exit='exit'\n                transition={SWAP_TRANSITION}\n              >\n                {strengthLabel}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n          . Must contain:\n        </span>\n      </p>\n      <ul className='flex flex-col gap-1.5' aria-label='Password requirements'>\n        {checks.map((check) => {\n          const glyph = requirementGlyphOf(check.isMet);\n          return (\n            <li key={check.label} className='flex items-center gap-2'>\n              <span className='relative flex'>\n                <AnimatePresence mode='popLayout' initial={false}>\n                  <motion.span\n                    key={glyph.key}\n                    className='flex'\n                    variants={swapVariants}\n                    initial='enter'\n                    animate='visible'\n                    exit='exit'\n                    transition={SWAP_TRANSITION}\n                  >\n                    {glyph.icon}\n                  </motion.span>\n                </AnimatePresence>\n              </span>\n              <span\n                className={cn(\n                  'text-muted-foreground text-xs transition-colors',\n                  check.isMet && 'text-success-foreground'\n                )}\n              >\n                {check.label}\n                <span className='sr-only'>\n                  , requirement {requirementStatusOf(check.isMet)}\n                </span>\n              </span>\n            </li>\n          );\n        })}\n      </ul>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "input"
  ],
  "type": "registry:component"
}