{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "auth-06",
  "description": "A forgot password form that turns into a check your email page, with buttons to open the mail app and a resend that counts down.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/announcements.json",
    "button",
    "field",
    "input",
    "https://www.dashboardblocks.com/r/radix/auth.json",
    "https://www.dashboardblocks.com/r/radix/forms.json",
    "https://www.dashboardblocks.com/r/radix/rolling-text.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/auth/auth-06.tsx",
      "content": "'use client'\n\nimport {\n  Announcements,\n  useAnnouncements,\n} from '@/registry/components/dashboardblocks/announcements'\nimport {\n  AuthIcon,\n  AuthLayout,\n  AuthResendButton,\n  AuthReveal,\n  AuthSwitch,\n} from '@/registry/components/dashboardblocks/auth'\nimport {\n  type FormErrors,\n  useSimpleForm,\n} from '@/registry/components/dashboardblocks/forms'\nimport { RollingText } from '@/registry/components/dashboardblocks/rolling-text'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { type FormEvent, startTransition, useEffect, useRef, useState } from 'react'\n\nimport { Button, buttonVariants } from '@/components/ui/button'\nimport { Field, FieldError, FieldLabel } from '@/components/ui/field'\nimport { Input } from '@/components/ui/input'\n\n/** Refuse with a sentence for the email field, such as an address with no account. */\ntype Auth6Result = { message: string; status: 'error' } | void\n\ninterface Auth6Props {\n  defaultEmail?: string\n  focusOnMount?: boolean\n  /** How long the link works, read as \"It works for 30 minutes.\". */\n  linkLifetime?: string\n  /** Mail apps to open once the link is sent. */\n  mailLinks?: { href: string; name: string }[]\n  /** Send the reset link. Many products resolve even for an address with no account, so the form doesn't say who has one. */\n  onSend?: (email: string) => Promise<Auth6Result>\n  /** Seconds before the link can be sent again. @default 60 */\n  resendAfter?: number\n  signInHref: string\n}\n\nconst exampleProps: Auth6Props = {\n  defaultEmail: 'amara@acme.co',\n  linkLifetime: '30 minutes',\n  mailLinks: [\n    { href: 'https://mail.google.com', name: 'Gmail' },\n    { href: 'https://outlook.live.com', name: 'Outlook' },\n  ],\n  onSend: async () => {\n    await new Promise((resolve) => setTimeout(resolve, 700))\n  },\n  signInHref: '#sign-in',\n}\n\nconst EMAIL = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/\n\nconst Auth6 = (props: Auth6Props) => {\n  const {\n    defaultEmail = '',\n    focusOnMount = false,\n    linkLifetime,\n    mailLinks = [],\n    onSend,\n    resendAfter = 60,\n    signInHref,\n  } = props\n  const [sentTo, setSentTo] = useState<string | null>(null)\n  const [cooldown, setCooldown] = useState(0)\n  const [resending, setResending] = useState(false)\n  const [message, setMessage] = useState<string | null>(null)\n  const [notices, announce] = useAnnouncements()\n  const refused = useRef(false)\n  const sentRef = useRef<HTMLDivElement>(null)\n  const focused = useRef<string | null | false>(focusOnMount ? false : null)\n\n  const form = useSimpleForm<{ email: string }>({\n    defaultValues: { email: defaultEmail },\n    onSubmit: async ({ email }) => {\n      refused.current = false\n      const result = await onSend?.(email.trim().toLowerCase())\n      if (result?.status !== 'error') return\n      refused.current = true\n      return { email: result.message }\n    },\n    validate: ({ email }): FormErrors<{ email: string }> => ({\n      email: !email.trim()\n        ? 'Enter your email address.'\n        : EMAIL.test(email.trim())\n          ? undefined\n          : 'Enter an email address like name@company.com.',\n    }),\n  })\n\n  useEffect(() => {\n    if (cooldown <= 0) return\n    const timer = setTimeout(() => setCooldown((seconds) => seconds - 1), 1000)\n    return () => clearTimeout(timer)\n  }, [cooldown])\n\n  useEffect(() => {\n    if (sentTo === focused.current) return\n    focused.current = sentTo\n    if (sentTo === null) document.getElementById(form.getId('email'))?.focus()\n    else sentRef.current?.querySelector<HTMLElement>('a, button')?.focus()\n  }, [form, sentTo])\n\n  const submit = async (event: FormEvent<HTMLFormElement>) => {\n    event.preventDefault()\n    if (form.isSubmitting) return\n    setMessage(null)\n    try {\n      const saved = await form.submit(event)\n      if (saved && !refused.current) {\n        const address = form.values.email.trim().toLowerCase()\n        startTransition(() => {\n          setSentTo(address)\n          setCooldown(resendAfter)\n          announce(`We sent a reset link to ${address}.`)\n        })\n      }\n    } catch {\n      startTransition(() =>\n        setMessage('We couldn’t send the email. Check your connection and try again.'),\n      )\n    }\n  }\n\n  const resend = async () => {\n    if (!sentTo) return\n    setResending(true)\n    setMessage(null)\n    try {\n      const result = await onSend?.(sentTo)\n      startTransition(() => {\n        if (result?.status === 'error') setMessage(result.message)\n        else {\n          setCooldown(resendAfter)\n          announce(`We sent another link to ${sentTo}.`)\n        }\n      })\n    } catch {\n      startTransition(() =>\n        setMessage('We couldn’t send the email. Check your connection and try again.'),\n      )\n    } finally {\n      startTransition(() => setResending(false))\n    }\n  }\n\n  const sent = sentTo !== null\n  const pending = form.isSubmitting\n\n  return (\n    <AuthLayout\n      media={\n        <AuthIcon>\n          {sent ? (\n            <IconPlaceholder\n              lucide='MailIcon'\n              tabler='IconMail'\n              hugeicons='MailIcon'\n              phosphor='EnvelopeIcon'\n              remixicon='RiMailLine'\n              aria-hidden\n            />\n          ) : (\n            <IconPlaceholder\n              lucide='KeyRoundIcon'\n              tabler='IconKey'\n              hugeicons='Key01Icon'\n              phosphor='KeyIcon'\n              remixicon='RiKey2Line'\n              aria-hidden\n            />\n          )}\n        </AuthIcon>\n      }\n      title={\n        <RollingText text={sent ? 'Check your email' : 'Reset your password'} wrap />\n      }\n      description={\n        <RollingText\n          wrap\n          text={\n            sent\n              ? `We sent a link to ${sentTo}.${linkLifetime ? ` It works for ${linkLifetime}.` : ''}`\n              : 'Enter your email and we’ll send you a link to set a new one.'\n          }\n        />\n      }\n    >\n      <div className='flex flex-col'>\n        <AuthReveal open={!sent}>\n          <form\n            method='post'\n            noValidate\n            onSubmit={(event) => void submit(event)}\n            className='flex flex-col gap-5'\n          >\n            <Field data-invalid={form.errors.email ? true : undefined}>\n              <FieldLabel htmlFor={form.getId('email')}>Email</FieldLabel>\n              <Input\n                type='email'\n                autoComplete='email'\n                disabled={pending}\n                {...form.field('email')}\n              />\n              <FieldError {...form.error('email')} />\n            </Field>\n            <Button\n              type='submit'\n              size='lg'\n              className='aria-disabled:opacity-50'\n              aria-disabled={pending || undefined}\n            >\n              <RollingText text={pending ? 'Sending link' : 'Send reset link'} />\n            </Button>\n          </form>\n        </AuthReveal>\n        <AuthReveal open={sent}>\n          <div ref={sentRef} className='flex flex-col gap-4'>\n            {mailLinks.length > 0 && (\n              <div\n                className='grid gap-2'\n                style={{\n                  gridTemplateColumns: `repeat(${mailLinks.length}, minmax(0, 1fr))`,\n                }}\n              >\n                {mailLinks.map((link) => (\n                  <a\n                    key={link.name}\n                    href={link.href}\n                    target='_blank'\n                    rel='noreferrer'\n                    className={buttonVariants({ variant: 'outline' })}\n                  >\n                    Open {link.name}\n                    <span className='sr-only'> (opens in a new tab)</span>\n                    <IconPlaceholder\n                      lucide='ArrowRightIcon'\n                      tabler='IconArrowRight'\n                      hugeicons='ArrowRight01Icon'\n                      phosphor='ArrowRightIcon'\n                      remixicon='RiArrowRightLine'\n                      data-icon='inline-end'\n                    />\n                  </a>\n                ))}\n              </div>\n            )}\n            <p className='text-muted-foreground text-sm text-pretty'>\n              Not in your inbox? Check your spam folder, or send it again.\n            </p>\n            <div className='-mx-2.5 flex items-center justify-between gap-2'>\n              <Button\n                type='button'\n                variant='ghost'\n                size='sm'\n                onClick={() => {\n                  setSentTo(null)\n                  setCooldown(0)\n                  setMessage(null)\n                }}\n              >\n                Use another email\n              </Button>\n              <AuthResendButton\n                cooldown={cooldown}\n                total={resendAfter}\n                pending={resending}\n                onClick={() => void resend()}\n              />\n            </div>\n          </div>\n        </AuthReveal>\n        {message && (\n          <p role='alert' className='text-destructive mt-4 text-sm text-pretty'>\n            {message}\n          </p>\n        )}\n        <Announcements said={notices} />\n      </div>\n      <AuthSwitch\n        href={signInHref}\n        title='Remembered it?'\n        description='Back to sign in.'\n      />\n    </AuthLayout>\n  )\n}\n\nexport { Auth6, exampleProps as auth6ExampleProps, type Auth6Props, type Auth6Result }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/auth/auth-06.tsx"
    }
  ],
  "meta": {
    "shows": "A forgot password form that turns into a check your email page, with buttons to open the mail app and a resend that counts down.",
    "size": "page",
    "usedWith": [
      "auth-05",
      "auth-07"
    ],
    "component": "Auth6",
    "props": "{ defaultEmail?: string; focusOnMount?: boolean; linkLifetime?: string; mailLinks?: { href: string; name: string }[]; onSend?: (email: string) => Promise<void | { message: string; status: \"error\" }>; resendAfter?: number; signInHref: string }"
  },
  "type": "registry:component"
}
