{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "auth-01",
  "title": "Sign in with a one-time code",
  "description": "An email, then a six-digit code, then signed in: the card resizes between steps, the button turns into its working state and a wrong code says what to do next.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/animated-size.json",
    "https://ui.ndekocode.dev/r/button.json",
    "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/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/blocks/auth/auth-01/index.tsx",
      "content": "'use client';\n\nimport { Fragment, useEffect, useRef, useState } from 'react';\n\nimport { MailIcon, ShieldCheckIcon } from '@/registry/ndeko/lib/icons';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { Field, FieldLabel } from '@/registry/ndeko/ui/field';\nimport { Input } from '@/registry/ndeko/ui/input';\nimport {\n  OTPField,\n  OTPFieldInput,\n  OTPFieldSeparator,\n} from '@/registry/ndeko/ui/otp-field';\nimport { StatusOrb } from '@/registry/ndeko/ui/status-orb';\n\nimport {\n  AuthFrame,\n  CardHead,\n  ErrorPresence,\n  MarkTile,\n  StepSwap,\n  SubmitButton,\n  SwapLabel,\n  clockOf,\n  useCountdown,\n} from './auth-kit';\nimport {\n  CHECK_DELAY_MS,\n  CODE_LENGTH,\n  CODE_LIFETIME_MINUTES,\n  EMAIL_PLACEHOLDER,\n  PREVIEW_CODE,\n  PREVIEW_CODE_LABEL,\n  RESEND_SECONDS,\n  RESENT_NOTICE_MS,\n  SEND_DELAY_MS,\n  WORKSPACE_NAME,\n} from './data';\n\ntype Step = 'email' | 'code' | 'done';\n\nconst EMAIL_PATTERN = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n\nconst SLOTS = Array.from({ length: CODE_LENGTH }, (_, index) => index);\n\nconst SEPARATOR_AFTER = 2;\n\nfunction emailProblemOf(email: string) {\n  const trimmed = email.trim();\n  if (trimmed.length === 0) {\n    return 'Enter your work email';\n  }\n  const isValid = EMAIL_PATTERN.test(trimmed);\n  if (!isValid) {\n    return 'Enter an email like name@company.com';\n  }\n  return null;\n}\n\nfunction slotLabelOf(index: number) {\n  const position = index + 1;\n  return `Digit ${position} of ${CODE_LENGTH}`;\n}\n\nfunction resendLabelOf(seconds: number, isResent: boolean) {\n  if (isResent) {\n    return 'New code sent';\n  }\n  if (seconds > 0) {\n    const clock = clockOf(seconds);\n    return `Send a new code in ${clock}`;\n  }\n  return 'Send a new code';\n}\n\nexport default function Auth01() {\n  const [step, setStep] = useState<Step>('email');\n  const [hasMoved, setHasMoved] = useState(false);\n  const [email, setEmail] = useState('');\n  const [emailError, setEmailError] = useState<string | null>(null);\n  const [isSending, setIsSending] = useState(false);\n  const [code, setCode] = useState('');\n  const [codeError, setCodeError] = useState<string | null>(null);\n  const [checkingCode, setCheckingCode] = useState<string | null>(null);\n  const [isResent, setIsResent] = useState(false);\n  const { seconds: resendSeconds, start: startCountdown } = useCountdown();\n  const emailRef = useRef<HTMLInputElement>(null);\n  const trimmedEmail = email.trim();\n  const hasEmailError = Boolean(emailError);\n  const hasCodeError = Boolean(codeError);\n  const isChecking = checkingCode !== null;\n  const isResendWaiting = resendSeconds > 0;\n  const resendLabel = resendLabelOf(resendSeconds, isResent);\n\n  useEffect(() => {\n    if (!isSending) {\n      return;\n    }\n    const timer = window.setTimeout(() => {\n      setIsSending(false);\n      setCode('');\n      setCodeError(null);\n      setStep('code');\n      setHasMoved(true);\n      startCountdown(RESEND_SECONDS);\n    }, SEND_DELAY_MS);\n    return () => window.clearTimeout(timer);\n  }, [isSending, startCountdown]);\n\n  useEffect(() => {\n    if (checkingCode === null) {\n      return;\n    }\n    const timer = window.setTimeout(() => {\n      const isRight = checkingCode === PREVIEW_CODE;\n      setCheckingCode(null);\n      if (isRight) {\n        setStep('done');\n        return;\n      }\n      setCodeError(\n        'That code does not match. Check the latest email or send a new one'\n      );\n    }, CHECK_DELAY_MS);\n    return () => window.clearTimeout(timer);\n  }, [checkingCode]);\n\n  useEffect(() => {\n    if (!isResent) {\n      return;\n    }\n    const timer = window.setTimeout(() => setIsResent(false), RESENT_NOTICE_MS);\n    return () => window.clearTimeout(timer);\n  }, [isResent]);\n\n  function changeEmail(event: React.ChangeEvent<HTMLInputElement>) {\n    setEmail(event.target.value);\n    setEmailError(null);\n  }\n\n  function submitEmail(event: React.FormEvent<HTMLFormElement>) {\n    event.preventDefault();\n    if (isSending) {\n      return;\n    }\n    const problem = emailProblemOf(email);\n    setEmailError(problem);\n    if (problem) {\n      emailRef.current?.focus();\n      return;\n    }\n    setIsSending(true);\n  }\n\n  function changeCode(value: string) {\n    setCode(value);\n    setCodeError(null);\n  }\n\n  function completeCode(value: string) {\n    setCheckingCode(value);\n  }\n\n  function submitCode(event: React.FormEvent<HTMLFormElement>) {\n    event.preventDefault();\n    if (isChecking) {\n      return;\n    }\n    const isComplete = code.length === CODE_LENGTH;\n    if (!isComplete) {\n      setCodeError(`Enter the ${CODE_LENGTH} digits from the email`);\n      return;\n    }\n    setCheckingCode(code);\n  }\n\n  function resendCode() {\n    setCode('');\n    setCodeError(null);\n    setIsResent(true);\n    startCountdown(RESEND_SECONDS);\n  }\n\n  function switchEmail() {\n    setStep('email');\n    setHasMoved(true);\n  }\n\n  function signOut() {\n    setCode('');\n    setStep('email');\n    setHasMoved(true);\n  }\n\n  return (\n    <AuthFrame footer='Trouble signing in? A workspace admin can send you a new invitation.'>\n      <StepSwap stepKey={step}>\n        {step === 'email' && (\n          <>\n            <CardHead\n              mark={<MarkTile icon={MailIcon} />}\n              title='Sign in to Tidewell'\n            >\n              We email you a 6-digit code, so there is no password to remember.\n            </CardHead>\n            <form\n              noValidate\n              onSubmit={submitEmail}\n              className='flex flex-col gap-5'\n            >\n              <Field invalid={hasEmailError} className='gap-0'>\n                <FieldLabel className='mb-1.5'>Work email</FieldLabel>\n                <Input\n                  ref={emailRef}\n                  type='email'\n                  inputMode='email'\n                  autoComplete='email'\n                  placeholder={EMAIL_PLACEHOLDER}\n                  value={email}\n                  readOnly={isSending}\n                  autoFocus={hasMoved}\n                  onChange={changeEmail}\n                />\n                <ErrorPresence error={emailError} />\n              </Field>\n              <div className='flex flex-col-reverse gap-3 sm:flex-row sm:items-center sm:justify-between'>\n                <p className='text-muted-foreground text-xs'>\n                  {`Codes expire after ${CODE_LIFETIME_MINUTES} minutes`}\n                </p>\n                <SubmitButton\n                  label='Send code'\n                  workingLabel='Sending code'\n                  isWorking={isSending}\n                />\n              </div>\n            </form>\n          </>\n        )}\n        {step === 'code' && (\n          <>\n            <CardHead\n              mark={<MarkTile icon={ShieldCheckIcon} />}\n              title='Check your email'\n            >\n              Enter the code sent to{' '}\n              <span className='text-foreground font-medium break-all'>\n                {trimmedEmail}\n              </span>\n              .{' '}\n              <Button\n                variant='link'\n                onClick={switchEmail}\n                className='h-auto px-0 text-[13px]'\n              >\n                Use another email\n              </Button>\n            </CardHead>\n            <form\n              noValidate\n              onSubmit={submitCode}\n              className='flex flex-col gap-5'\n            >\n              <Field invalid={hasCodeError} className='gap-0'>\n                <FieldLabel className='mb-1.5'>\n                  {`${CODE_LENGTH}-digit code`}\n                </FieldLabel>\n                <OTPField\n                  length={CODE_LENGTH}\n                  value={code}\n                  autoComplete='one-time-code'\n                  readOnly={isChecking}\n                  onValueChange={changeCode}\n                  onValueComplete={completeCode}\n                  className='gap-1.5 sm:gap-2'\n                >\n                  {SLOTS.map((index) => {\n                    const isFirst = index === 0;\n                    const isSeparated = index === SEPARATOR_AFTER;\n\n                    return (\n                      <Fragment key={index}>\n                        <OTPFieldInput\n                          autoFocus={isFirst}\n                          aria-label={slotLabelOf(index)}\n                          className='sm:size-11'\n                        />\n                        {isSeparated && <OTPFieldSeparator />}\n                      </Fragment>\n                    );\n                  })}\n                </OTPField>\n                <ErrorPresence error={codeError} />\n              </Field>\n              <p className='bg-foreground/[0.03] text-muted-foreground rounded-lg px-3 py-2 text-xs'>\n                This preview sends no email. The code it accepts is{' '}\n                <span className='text-foreground font-medium tabular-nums'>\n                  {PREVIEW_CODE_LABEL}\n                </span>\n              </p>\n              <div className='flex flex-col-reverse gap-3 sm:flex-row sm:items-center sm:justify-between'>\n                <Button\n                  type='button'\n                  variant='ghost'\n                  size='sm'\n                  disabled={isResendWaiting}\n                  onClick={resendCode}\n                  className='text-muted-foreground self-center sm:self-auto'\n                >\n                  <SwapLabel text={resendLabel} />\n                </Button>\n                <SubmitButton\n                  label='Sign in'\n                  workingLabel='Checking'\n                  isWorking={isChecking}\n                />\n              </div>\n            </form>\n          </>\n        )}\n        {step === 'done' && (\n          <>\n            <CardHead\n              mark={<StatusOrb tone='success' />}\n              title='Signed in'\n              autoFocusTitle\n            >\n              <span className='text-foreground font-medium break-all'>\n                {trimmedEmail}\n              </span>{' '}\n              {`is signed in to ${WORKSPACE_NAME} on this device.`}\n            </CardHead>\n            <div className='flex flex-col-reverse gap-3 sm:flex-row sm:items-center sm:justify-between'>\n              <p className='text-muted-foreground text-xs'>\n                Sessions on this device last 30 days\n              </p>\n              <Button variant='outline' size='lg' onClick={signOut}>\n                Sign out\n              </Button>\n            </div>\n          </>\n        )}\n      </StepSwap>\n    </AuthFrame>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/blocks/auth-01/index.tsx"
    },
    {
      "path": "registry/ndeko/blocks/auth/auth-01/auth-kit.tsx",
      "content": "'use client';\n\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useEffect, useRef, useState } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { LoaderCircleIcon, type Icon } from '@/registry/ndeko/lib/icons';\nimport { AnimatedSize } from '@/registry/ndeko/ui/animated-size';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { FieldError } from '@/registry/ndeko/ui/field';\n\nexport const PRODUCT_NAME = 'Tidewell';\n\nconst STEP_IN: Transition = { duration: 0.2, ease: [0.23, 1, 0.32, 1] };\n\nconst STEP_OUT: Transition = { duration: 0.15, ease: [0.23, 1, 0.32, 1] };\n\nconst SWAP_TRANSITION: Transition = { duration: 0.15, ease: [0.2, 0, 0, 1] };\n\nconst STEP_VARIANTS: Variants = {\n  enter: { opacity: 0, scale: 0.98, filter: 'blur(2px)' },\n  visible: { opacity: 1, scale: 1, filter: 'blur(0px)', transition: STEP_IN },\n  exit: { opacity: 0, scale: 0.98, filter: 'blur(2px)', transition: STEP_OUT },\n};\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 ERROR_VARIANTS: Variants = {\n  enter: { height: 0, opacity: 0 },\n  visible: { height: 'auto', opacity: 1, transition: STEP_IN },\n  exit: { height: 0, opacity: 0, transition: STEP_OUT },\n};\n\nconst FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1, transition: STEP_IN },\n  exit: { opacity: 0, transition: STEP_OUT },\n};\n\nfunction stepVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return STEP_VARIANTS;\n}\n\nfunction swapVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return SWAP_VARIANTS;\n}\n\nfunction errorVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return ERROR_VARIANTS;\n}\n\nfunction BrandMark() {\n  return (\n    <svg viewBox='0 0 24 24' aria-hidden='true' className='size-6 shrink-0'>\n      <rect width='24' height='24' rx='6' className='fill-primary' />\n      <path\n        d='M5 10q1.75-2.6 3.5 0t3.5 0 3.5 0 3.5 0'\n        fill='none'\n        stroke='white'\n        strokeOpacity='0.7'\n        strokeWidth='1.8'\n        strokeLinecap='round'\n      />\n      <path\n        d='M5 15.5q1.75-2.6 3.5 0t3.5 0 3.5 0 3.5 0'\n        fill='none'\n        stroke='white'\n        strokeWidth='1.8'\n        strokeLinecap='round'\n      />\n    </svg>\n  );\n}\n\ntype AuthFrameProps = {\n  footer?: React.ReactNode;\n  children: React.ReactNode;\n};\n\n/** A focused page: the window colour, the mark and its name, one card in the middle. */\nexport function AuthFrame({ footer, children }: AuthFrameProps) {\n  return (\n    <div className='bg-sidebar text-foreground flex min-h-dvh w-full flex-col'>\n      <header className='flex h-14 shrink-0 items-center gap-2 px-4 sm:px-6'>\n        <BrandMark />\n        <span className='text-sm font-semibold'>{PRODUCT_NAME}</span>\n      </header>\n      <main className='flex flex-1 flex-col items-center px-4 pt-4 pb-16 sm:justify-center sm:pt-0'>\n        <div className='flex w-full max-w-[25rem] flex-col gap-4'>\n          <div className='bg-card hairline-ring ring-border dark:bg-foreground/[0.03] rounded-xl p-6 shadow-[0_1px_2px_rgb(0_0_0/0.04)] sm:p-7'>\n            <AnimatedSize contentClassName='relative'>{children}</AnimatedSize>\n          </div>\n          {footer && (\n            <p className='text-muted-foreground px-2 text-center text-xs text-balance'>\n              {footer}\n            </p>\n          )}\n        </div>\n      </main>\n    </div>\n  );\n}\n\ntype StepSwapProps = {\n  stepKey: string;\n  children: React.ReactNode;\n};\n\n/** The card resizes around it while one step gives way to the next. */\nexport function StepSwap({ stepKey, children }: StepSwapProps) {\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const variants = stepVariantsOf(shouldReduceMotion);\n\n  return (\n    <AnimatePresence mode='popLayout' initial={false}>\n      <motion.div\n        key={stepKey}\n        variants={variants}\n        initial='enter'\n        animate='visible'\n        exit='exit'\n        className='flex flex-col gap-6'\n      >\n        {children}\n      </motion.div>\n    </AnimatePresence>\n  );\n}\n\ntype MarkTileProps = { icon: Icon };\n\nexport function MarkTile({ icon: TileIcon }: MarkTileProps) {\n  return (\n    <span className='keycap-3d text-muted-foreground flex size-10 items-center justify-center rounded-lg'>\n      <TileIcon className='size-5' />\n    </span>\n  );\n}\n\ntype CardHeadProps = {\n  mark: React.ReactNode;\n  title: string;\n  /** Focuses the title when a step opens, so screen readers hear where they are. */\n  autoFocusTitle?: boolean;\n  children: React.ReactNode;\n};\n\nexport function CardHead({\n  mark,\n  title,\n  autoFocusTitle,\n  children,\n}: CardHeadProps) {\n  const titleRef = useRef<HTMLHeadingElement>(null);\n\n  useEffect(() => {\n    if (autoFocusTitle) {\n      titleRef.current?.focus();\n    }\n  }, [autoFocusTitle]);\n\n  return (\n    <div className='flex flex-col gap-4'>\n      {mark}\n      <div className='flex flex-col gap-1.5'>\n        <h1\n          ref={titleRef}\n          tabIndex={-1}\n          className='text-xl font-semibold text-balance outline-none'\n        >\n          {title}\n        </h1>\n        <p className='text-muted-foreground text-[13px] text-pretty'>\n          {children}\n        </p>\n      </div>\n    </div>\n  );\n}\n\ntype ErrorPresenceProps = {\n  error: string | null;\n};\n\n/** Rendered inside a `Field`, so the error is tied to its control. It grows in and folds away. */\nexport function ErrorPresence({ error }: ErrorPresenceProps) {\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const variants = errorVariantsOf(shouldReduceMotion);\n\n  return (\n    <AnimatePresence initial={false}>\n      {error && (\n        <motion.div\n          key='error'\n          variants={variants}\n          initial='enter'\n          animate='visible'\n          exit='exit'\n          className='overflow-hidden'\n        >\n          <FieldError match className='pt-1.5'>\n            {error}\n          </FieldError>\n        </motion.div>\n      )}\n    </AnimatePresence>\n  );\n}\n\ntype SubmitButtonProps = {\n  label: string;\n  workingLabel: string;\n  isWorking: boolean;\n};\n\n/** Becomes its working state in place: a spinner joins the label and the pill resizes to it. */\nexport function SubmitButton({\n  label,\n  workingLabel,\n  isWorking,\n}: SubmitButtonProps) {\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const variants = swapVariantsOf(shouldReduceMotion);\n  const shownLabel = workingLabelOf(isWorking, label, workingLabel);\n\n  return (\n    <Button\n      type='submit'\n      size='lg'\n      disabled={isWorking}\n      focusableWhenDisabled\n      className='w-full data-disabled:opacity-100 sm:w-auto'\n    >\n      <AnimatedSize\n        as='span'\n        axis='width'\n        contentClassName='relative flex items-center gap-2'\n      >\n        <AnimatePresence mode='popLayout' initial={false}>\n          {isWorking && (\n            <motion.span\n              key='spinner'\n              variants={variants}\n              initial='enter'\n              animate='visible'\n              exit='exit'\n              transition={SWAP_TRANSITION}\n              className='flex'\n            >\n              <LoaderCircleIcon className='animate-spin' />\n            </motion.span>\n          )}\n        </AnimatePresence>\n        <AnimatePresence mode='popLayout' initial={false}>\n          <motion.span\n            key={shownLabel}\n            variants={variants}\n            initial='enter'\n            animate='visible'\n            exit='exit'\n            transition={SWAP_TRANSITION}\n          >\n            {shownLabel}\n          </motion.span>\n        </AnimatePresence>\n      </AnimatedSize>\n    </Button>\n  );\n}\n\nfunction workingLabelOf(\n  isWorking: boolean,\n  label: string,\n  workingLabel: string\n) {\n  if (isWorking) {\n    return workingLabel;\n  }\n  return label;\n}\n\ntype SwapLabelProps = { text: string };\n\nexport function SwapLabel({ text }: SwapLabelProps) {\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const variants = swapVariantsOf(shouldReduceMotion);\n\n  return (\n    <AnimatedSize as='span' axis='width' contentClassName='relative flex'>\n      <AnimatePresence mode='popLayout' initial={false}>\n        <motion.span\n          key={text}\n          variants={variants}\n          initial='enter'\n          animate='visible'\n          exit='exit'\n          transition={SWAP_TRANSITION}\n          className='tabular-nums'\n        >\n          {text}\n        </motion.span>\n      </AnimatePresence>\n    </AnimatedSize>\n  );\n}\n\n/** Counts down once a second while it is above zero. */\nexport function useCountdown() {\n  const [seconds, setSeconds] = useState(0);\n\n  useEffect(() => {\n    if (seconds <= 0) {\n      return;\n    }\n    const timer = window.setTimeout(() => {\n      setSeconds((current) => current - 1);\n    }, 1000);\n    return () => window.clearTimeout(timer);\n  }, [seconds]);\n\n  return { seconds, start: setSeconds };\n}\n\nexport function clockOf(totalSeconds: number) {\n  const rawMinutes = totalSeconds / 60;\n  const minutes = Math.floor(rawMinutes);\n  const seconds = totalSeconds % 60;\n  const paddedSeconds = String(seconds).padStart(2, '0');\n  return `${minutes}:${paddedSeconds}`;\n}\n",
      "type": "registry:component",
      "target": "components/blocks/auth-01/auth-kit.tsx"
    },
    {
      "path": "registry/ndeko/blocks/auth/auth-01/data.ts",
      "content": "export const WORKSPACE_NAME = 'Tidewell Studio';\n\nexport const CODE_LENGTH = 6;\n\n/** The preview sends no email, so it says which code it accepts. */\nexport const PREVIEW_CODE = '428913';\n\nexport const PREVIEW_CODE_LABEL = '428 913';\n\nexport const SEND_DELAY_MS = 900;\n\nexport const CHECK_DELAY_MS = 700;\n\nexport const RESEND_SECONDS = 30;\n\nexport const RESENT_NOTICE_MS = 1600;\n\nexport const CODE_LIFETIME_MINUTES = 10;\n\nexport const EMAIL_PLACEHOLDER = 'name@company.com';\n",
      "type": "registry:component",
      "target": "components/blocks/auth-01/data.ts"
    }
  ],
  "categories": [
    "blocks",
    "auth"
  ],
  "type": "registry:block"
}