{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dialog-12",
  "title": "Confirmation code dialog",
  "description": "A 4-digit code that swaps the dialog to its confirmed step.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/dialog.json",
    "https://ui.ndekocode.dev/r/otp-field.json",
    "https://ui.ndekocode.dev/r/status-orb.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/dialog/dialog-12.tsx",
      "content": "'use client';\n\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useEffect, useId, useRef, useState } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport {\n  Dialog,\n  DialogClose,\n  DialogContent,\n  DialogDescription,\n  DialogHeader,\n  DialogTitle,\n  DialogTrigger,\n} from '@/registry/ndeko/ui/dialog';\nimport { OTPField, OTPFieldInput } from '@/registry/ndeko/ui/otp-field';\nimport { StatusOrb } from '@/registry/ndeko/ui/status-orb';\n\ntype CodeStatus = 'entry' | 'invalid' | 'verified';\n\ntype Step = 'entry' | 'verified';\n\nconst CORRECT_CODE = '6548';\nconst CODE_LENGTH = 4;\nconst LATER_POSITIONS = [2, 3, 4];\n\nconst STEP_COPY: Record<Step, { title: string; description: string }> = {\n  entry: {\n    title: 'Enter the confirmation code',\n    description: `We sent a 4-digit code to amara@example.com. For this preview, use ${CORRECT_CODE}.`,\n  },\n  verified: {\n    title: 'Email confirmed',\n    description: 'Your address is verified. You can close this window.',\n  },\n};\n\nconst BRAND_STYLE = { '--gem': 'var(--primary)' } as React.CSSProperties;\n\nconst SWAP_TRANSITION: Transition = { duration: 0.15, ease: [0.2, 0, 0, 1] };\n\nconst MARK_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 CONTENT_VARIANTS: Variants = {\n  enter: { opacity: 0, filter: 'blur(2px)' },\n  visible: { opacity: 1, filter: 'blur(0px)' },\n  exit: { opacity: 0, filter: 'blur(2px)' },\n};\n\nconst FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1 },\n  exit: { opacity: 0 },\n};\n\nfunction markVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return MARK_VARIANTS;\n}\n\nfunction contentVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return CONTENT_VARIANTS;\n}\n\nfunction stepOf(status: CodeStatus): Step {\n  if (status === 'verified') {\n    return 'verified';\n  }\n  return 'entry';\n}\n\nfunction AcmeMark() {\n  return (\n    <span\n      aria-hidden='true'\n      style={BRAND_STYLE}\n      className='gem-plate flex size-10 shrink-0 items-center justify-center rounded-xl'\n    >\n      <svg\n        viewBox='0 0 24 24'\n        fill='none'\n        stroke='currentColor'\n        strokeWidth={2.25}\n        strokeLinecap='round'\n        strokeLinejoin='round'\n        className='size-5'\n      >\n        <path d='M12 4.5 20 19H4l8-14.5Z' />\n        <path d='M8.75 14h6.5' />\n      </svg>\n    </span>\n  );\n}\n\nexport default function Dialog12() {\n  const otpId = useId();\n  const [code, setCode] = useState('');\n  const [status, setStatus] = useState<CodeStatus>('entry');\n  const closeRef = useRef<HTMLButtonElement>(null);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const markVariants = markVariantsOf(shouldReduceMotion);\n  const contentVariants = contentVariantsOf(shouldReduceMotion);\n  const step = stepOf(status);\n  const copy = STEP_COPY[step];\n  const isVerified = status === 'verified';\n  const isInvalid = status === 'invalid';\n\n  useEffect(() => {\n    if (isVerified) {\n      closeRef.current?.focus();\n    }\n  }, [isVerified]);\n\n  function resetOnOpen(open: boolean) {\n    if (open) {\n      setCode('');\n      setStatus('entry');\n    }\n  }\n\n  function updateCode(nextCode: string) {\n    setCode(nextCode);\n    const isRetyping = nextCode.length > 0;\n    if (isInvalid && isRetyping) {\n      setStatus('entry');\n    }\n  }\n\n  function verifyCode(completeCode: string) {\n    if (completeCode === CORRECT_CODE) {\n      setStatus('verified');\n      return;\n    }\n    setStatus('invalid');\n    setCode('');\n    document.getElementById(otpId)?.focus();\n  }\n\n  return (\n    <Dialog onOpenChange={resetOnOpen}>\n      <DialogTrigger render={<Button variant='outline' />}>\n        Confirm email\n      </DialogTrigger>\n      <DialogContent className='max-w-md' contentClassName='relative gap-5'>\n        <div className='flex flex-col items-center gap-4'>\n          <span className='relative flex size-10 items-center justify-center'>\n            <AnimatePresence mode='popLayout' initial={false}>\n              <motion.span\n                key={step}\n                className='flex'\n                variants={markVariants}\n                initial='enter'\n                animate='visible'\n                exit='exit'\n                transition={SWAP_TRANSITION}\n              >\n                {isVerified && <StatusOrb tone='success' />}\n                {!isVerified && <AcmeMark />}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n          <DialogHeader className='px-8 text-center'>\n            <DialogTitle className='relative'>\n              <AnimatePresence mode='popLayout' initial={false}>\n                <motion.span\n                  key={step}\n                  className='block'\n                  variants={contentVariants}\n                  initial='enter'\n                  animate='visible'\n                  exit='exit'\n                  transition={SWAP_TRANSITION}\n                >\n                  {copy.title}\n                </motion.span>\n              </AnimatePresence>\n            </DialogTitle>\n            <DialogDescription className='relative'>\n              <AnimatePresence mode='popLayout' initial={false}>\n                <motion.span\n                  key={step}\n                  className='block'\n                  variants={contentVariants}\n                  initial='enter'\n                  animate='visible'\n                  exit='exit'\n                  transition={SWAP_TRANSITION}\n                >\n                  {copy.description}\n                </motion.span>\n              </AnimatePresence>\n            </DialogDescription>\n          </DialogHeader>\n        </div>\n        <AnimatePresence mode='popLayout' initial={false}>\n          {isVerified && (\n            <motion.div\n              key='verified'\n              className='flex justify-center'\n              variants={contentVariants}\n              initial='enter'\n              animate='visible'\n              exit='exit'\n              transition={SWAP_TRANSITION}\n            >\n              <DialogClose ref={closeRef} render={<Button size='lg' />}>\n                Close\n              </DialogClose>\n            </motion.div>\n          )}\n          {!isVerified && (\n            <motion.div\n              key='entry'\n              className='flex flex-col items-center gap-3'\n              variants={contentVariants}\n              initial='enter'\n              animate='visible'\n              exit='exit'\n              transition={SWAP_TRANSITION}\n            >\n              <label htmlFor={otpId} className='sr-only'>\n                Confirmation code\n              </label>\n              <OTPField\n                id={otpId}\n                length={CODE_LENGTH}\n                value={code}\n                onValueChange={updateCode}\n                onValueComplete={verifyCode}\n              >\n                <OTPFieldInput aria-invalid={isInvalid} />\n                {LATER_POSITIONS.map((position) => (\n                  <OTPFieldInput\n                    key={position}\n                    aria-label={`Digit ${position} of ${CODE_LENGTH}`}\n                    aria-invalid={isInvalid}\n                  />\n                ))}\n              </OTPField>\n              <AnimatePresence initial={false}>\n                {isInvalid && (\n                  <motion.p\n                    key='error'\n                    role='alert'\n                    className='text-destructive-ink text-center text-xs'\n                    variants={FADE_VARIANTS}\n                    initial='enter'\n                    animate='visible'\n                    exit='exit'\n                    transition={SWAP_TRANSITION}\n                  >\n                    That code does not match. Check the email and try again.\n                  </motion.p>\n                )}\n              </AnimatePresence>\n              <Button type='button' variant='link' size='sm'>\n                Resend code\n              </Button>\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </DialogContent>\n    </Dialog>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "dialog"
  ],
  "type": "registry:component"
}