{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"strength-field","type":"registry:ui","title":"Strength Field","description":"A password field with a segmented strength meter that fills and warms toward green as rules are met, a rolling verdict word, a rule checklist and a show/hide toggle.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/strength-field.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { Check, Eye, EyeOff } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { useId, useState, type ChangeEvent } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface StrengthRule {\n  label: string;\n  test: (value: string) => boolean;\n}\n\nexport interface StrengthFieldProps {\n  label?: string;\n  /** Controlled value. */\n  value?: string;\n  defaultValue?: string;\n  onChange?: (value: string) => void;\n  placeholder?: string;\n  /** Rules that each add one step to the meter. */\n  rules?: StrengthRule[];\n  /** List the rules under the meter with a check per rule met. */\n  showRules?: boolean;\n  name?: string;\n  className?: string;\n}\n\nexport const defaultRules: StrengthRule[] = [\n  { label: \"8+ characters\", test: (v) => v.length >= 8 },\n  { label: \"Upper and lower case\", test: (v) => /[a-z]/.test(v) && /[A-Z]/.test(v) },\n  { label: \"A number\", test: (v) => /\\d/.test(v) },\n  { label: \"A symbol\", test: (v) => /[^A-Za-z0-9]/.test(v) },\n];\n\nconst LEVELS = [\n  { word: \"Too weak\", color: \"#ef4444\" },\n  { word: \"Weak\", color: \"#f97316\" },\n  { word: \"Okay\", color: \"#eab308\" },\n  { word: \"Strong\", color: \"#22c55e\" },\n  { word: \"Great\", color: \"#10b981\" },\n];\n\n/**\n * A password field with a segmented strength meter: each rule met fills one\n * more segment, the color warms toward green, and the verdict word rolls over.\n */\nexport function StrengthField({\n  label = \"Password\",\n  value,\n  defaultValue = \"\",\n  onChange,\n  placeholder = \"Create a password\",\n  rules = defaultRules,\n  showRules = true,\n  name,\n  className,\n}: StrengthFieldProps) {\n  const id = useId();\n  const [inner, setInner] = useState(defaultValue);\n  const [visible, setVisible] = useState(false);\n  const text = value ?? inner;\n  const passed = rules.map((r) => r.test(text));\n  const score = passed.filter(Boolean).length;\n  const level = LEVELS[Math.round((score / Math.max(1, rules.length)) * (LEVELS.length - 1))];\n\n  const change = (e: ChangeEvent<HTMLInputElement>) => {\n    if (value === undefined) setInner(e.target.value);\n    onChange?.(e.target.value);\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div className={cn(\"grid gap-2\", className)}>\n        <label htmlFor={id} className=\"text-sm font-medium text-foreground\">\n          {label}\n        </label>\n        <div className=\"relative\">\n          <input\n            id={id}\n            name={name}\n            type={visible ? \"text\" : \"password\"}\n            value={text}\n            onChange={change}\n            placeholder={placeholder}\n            autoComplete=\"new-password\"\n            aria-describedby={`${id}-meter`}\n            className=\"h-10 w-full rounded-lg border border-border bg-background pl-3 pr-10 text-sm text-foreground outline-none transition-[border-color,box-shadow] placeholder:text-muted-foreground focus:border-ring focus:ring-[3px] focus:ring-ring/30\"\n          />\n          <button\n            type=\"button\"\n            onClick={() => setVisible((v) => !v)}\n            aria-label={visible ? \"Hide password\" : \"Show password\"}\n            aria-pressed={visible}\n            className=\"absolute right-1 top-1 grid size-8 place-items-center rounded-md text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n          >\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.span\n                key={visible ? \"hide\" : \"show\"}\n                initial={{ opacity: 0, scale: 0.5, rotate: -30, filter: \"blur(3px)\" }}\n                animate={{ opacity: 1, scale: 1, rotate: 0, filter: \"blur(0px)\" }}\n                exit={{ opacity: 0, scale: 0.5, rotate: 30, filter: \"blur(3px)\" }}\n                transition={{ duration: 0.2 }}\n              >\n                {visible ? <EyeOff className=\"size-4\" /> : <Eye className=\"size-4\" />}\n              </motion.span>\n            </AnimatePresence>\n          </button>\n        </div>\n\n        <div id={`${id}-meter`} className=\"flex items-center gap-3\">\n          <div className=\"flex flex-1 gap-1.5\" aria-hidden>\n            {rules.map((_, i) => (\n              <span key={i} className=\"h-1.5 flex-1 overflow-hidden rounded-full bg-muted\">\n                <motion.span\n                  className=\"block h-full origin-left rounded-full\"\n                  initial={false}\n                  animate={{ scaleX: i < score ? 1 : 0, backgroundColor: level.color }}\n                  transition={{ type: \"spring\", bounce: 0.25, duration: 0.4, delay: i < score ? i * 0.04 : 0 }}\n                />\n              </span>\n            ))}\n          </div>\n          <span className=\"relative w-16 overflow-hidden text-right text-xs font-medium\" aria-live=\"polite\">\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.span\n                key={text ? level.word : \"empty\"}\n                className=\"block\"\n                style={{ color: text ? level.color : undefined }}\n                initial={{ y: 10, opacity: 0 }}\n                animate={{ y: 0, opacity: 1 }}\n                exit={{ y: -10, opacity: 0 }}\n                transition={{ duration: 0.2 }}\n              >\n                {text ? level.word : \" \"}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n        </div>\n\n        {showRules && (\n          <ul className=\"grid grid-cols-2 gap-x-3 gap-y-1.5\">\n            {rules.map((r, i) => (\n              <li key={r.label} className={cn(\"flex items-center gap-1.5 text-xs transition-colors\", passed[i] ? \"text-foreground\" : \"text-muted-foreground\")}>\n                <span\n                  className={cn(\n                    \"grid size-3.5 shrink-0 place-items-center rounded-full border transition-colors\",\n                    passed[i] ? \"border-transparent bg-emerald-500 text-white\" : \"border-border\",\n                  )}\n                >\n                  <motion.span initial={false} animate={{ scale: passed[i] ? 1 : 0 }} transition={{ type: \"spring\", bounce: 0.5, duration: 0.35 }}>\n                    <Check className=\"size-2.5\" strokeWidth={3.5} />\n                  </motion.span>\n                </span>\n                {r.label}\n                <span className=\"sr-only\">{passed[i] ? \"(met)\" : \"(not met)\"}</span>\n              </li>\n            ))}\n          </ul>\n        )}\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}