{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "newsletter",
  "title": "Newsletter",
  "description": "A form that signs an email address up for letters, as the Footer’s newsletter and the Changelog’s release emails do: the box and its button, the wait while it sends, the line that says where the letters will go, and a way back to use another address.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/auth-screen.json",
    "https://kinoui.com/r/button.json",
    "https://kinoui.com/r/field.json",
    "https://kinoui.com/r/notice.json",
    "https://kinoui.com/r/swap.json"
  ],
  "files": [
    {
      "path": "registry/lib/newsletter.tsx",
      "content": "'use client'\n\nimport { Button } from '@/registry/components/kino/button'\nimport {\n  Field,\n  FieldError,\n  FieldLabel,\n  Form,\n  useFormSubmission,\n} from '@/registry/components/kino/field'\nimport {\n  AuthEmailInput,\n  AuthMessage,\n  looksLikeEmail,\n  useFocusStep,\n} from '@/registry/lib/auth-screen'\nimport { NoticeGlyph } from '@/registry/lib/notice'\nimport { Swap } from '@/registry/lib/swap'\nimport { useRef, useState } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A form that signs an email address up for letters: the Footer's\n * newsletter and the Changelog's release emails. It lays out by its own\n * width, the button beside the box once there is room.\n */\n\n/** What the newsletter says when the address is missing or doesn't read as one. */\nfunction newsletterError(email: string): string | null {\n  const address = email.trim()\n  if (!address) return 'Enter your email address.'\n  if (!looksLikeEmail(address)) return 'Enter an email address, like name@company.com.'\n  return null\n}\n\n/**\n * The newsletter's form: an email box and a button, the box's label for\n * screen readers alone. While it sends, the button spins and a second press\n * does nothing; once it's done, the form gives way to a line that says where\n * the letters will go, as the Sign in's form gives way to its end, and a way\n * back to use another address. A failure says so under the box, and the address stays to try\n * again.\n */\nfunction NewsletterForm({\n  onSubscribe,\n  submitLabel = 'Subscribe',\n  className,\n}: {\n  /** Signs the address up. A throw or a rejected promise says it couldn't. */\n  onSubscribe: (email: string) => unknown\n  submitLabel?: string\n  className?: string\n}) {\n  const [email, setEmail] = useState('')\n  // The address last sent, so a failure shows only until the address changes.\n  const [sent, setSent] = useState('')\n  const triedRef = useRef(false)\n  return (\n    <Form\n      data-newsletter=''\n      className={cn('@container/newsletter min-w-0', className)}\n      onSubmitCapture={() => {\n        triedRef.current = true\n      }}\n      onFormSubmit={() => {\n        setSent(email)\n        return onSubscribe(email.trim())\n      }}\n    >\n      <NewsletterFields\n        email={email}\n        sent={sent}\n        setEmail={setEmail}\n        triedRef={triedRef}\n        submitLabel={submitLabel}\n      />\n    </Form>\n  )\n}\n\nfunction NewsletterFields({\n  email,\n  sent,\n  setEmail,\n  triedRef,\n  submitLabel,\n}: {\n  email: string\n  sent: string\n  setEmail: (email: string) => void\n  triedRef: { current: boolean }\n  submitLabel: string\n}) {\n  const submission = useFormSubmission()\n  const status = submission?.status\n  const done = status === 'saved'\n  const failed = status === 'failed' && email === sent\n  // The form that held focus goes: focus moves to the way back once the line arrives, and back to the box after.\n  const focusRef = useFocusStep<HTMLDivElement>(done ? 'done' : 'form')\n  return (\n    <div ref={focusRef} className='min-w-0'>\n      {/* There before the line arrives, so a screen reader reads it out. */}\n      <p role='status' className='sr-only'>\n        {done ? `You’re on the list. The next letter goes to ${email.trim()}.` : ''}\n      </p>\n      <Swap fill block wait id={done ? 'done' : 'form'}>\n        {done ? (\n          <div className='flex min-w-0 flex-col items-start gap-1'>\n            <p className='flex min-w-0 items-start gap-2 text-sm leading-5'>\n              <NoticeGlyph type='success' />\n              <span className='min-w-0 wrap-break-word'>\n                You’re on the list. The next letter goes to{' '}\n                <bdi dir='ltr' className='font-medium'>\n                  {email.trim()}\n                </bdi>\n                .\n              </span>\n            </p>\n            <Button\n              data-autofocus=''\n              type='button'\n              variant='link'\n              size='sm'\n              className='text-muted-foreground hover:text-foreground h-auto min-h-6 px-0 ps-6 font-normal'\n              onClick={() => {\n                triedRef.current = false\n                setEmail('')\n                submission?.reset()\n              }}\n            >\n              Use another email\n            </Button>\n          </div>\n        ) : (\n          <div className='flex w-full min-w-0 flex-col'>\n            <Field\n              name='email'\n              validate={() => (triedRef.current ? newsletterError(email) : null)}\n              className='w-full'\n            >\n              <FieldLabel className='sr-only'>Email</FieldLabel>\n              {/* 21rem: room for the box and the button side by side, the width a 24rem footer leaves it. */}\n              <div className='flex min-w-0 flex-col gap-2 [grid-area:control] @[21rem]/newsletter:flex-row [&>[data-slot=input-group]]:flex-1'>\n                <AuthEmailInput\n                  data-autofocus=''\n                  autoComplete='email'\n                  value={email}\n                  readOnly={status === 'saving'}\n                  onValueChange={setEmail}\n                />\n                <Button type='submit' loading={status === 'saving'} className='shrink-0'>\n                  {submitLabel}\n                </Button>\n              </div>\n              <FieldError />\n            </Field>\n            {/* The list couldn't be reached: nothing is wrong with the address, so it says so away from the box, as the Sign up does. */}\n            <AuthMessage>\n              {failed ? 'We couldn’t sign you up just now. Try again in a moment.' : null}\n            </AuthMessage>\n          </div>\n        )}\n      </Swap>\n    </div>\n  )\n}\n\nexport { NewsletterForm, newsletterError }\n",
      "type": "registry:lib",
      "target": "@lib/newsletter.tsx"
    }
  ],
  "type": "registry:lib"
}