{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sign-up",
  "title": "Sign up",
  "description": "A sign-up screen built from the ways in your app offers: a name, an email, a password with its strength and rules, and the terms, checked as each field is left, or Google, GitHub and Apple. Once the account is made, the card asks the person to check their email.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/button.json",
    "https://kinoui.com/r/checkbox.json",
    "https://kinoui.com/r/field.json",
    "https://kinoui.com/r/input.json",
    "https://kinoui.com/r/password-input.json",
    "https://kinoui.com/r/password-strength.json",
    "https://kinoui.com/r/social-sign-in.json",
    "https://kinoui.com/r/auth-screen.json",
    "https://kinoui.com/r/settle.json",
    "https://kinoui.com/r/swap.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/sign-up/sign-up.tsx",
      "content": "'use client'\n\nimport { CheckEmail } from '@/registry/components/kino/sign-up/check-email'\nimport {\n  SignUpContext,\n  type SignUpSent,\n} from '@/registry/components/kino/sign-up/sign-up-context'\nimport type { SignUpValues } from '@/registry/components/kino/sign-up/sign-up-flow'\nimport { SignUpForm } from '@/registry/components/kino/sign-up/sign-up-form'\nimport { SignUpSocial } from '@/registry/components/kino/sign-up/sign-up-social'\nimport { AuthDivider, type AuthHeading, AuthScreen } from '@/registry/lib/auth-screen'\nimport { type ReactNode, useCallback, useMemo, useState } from 'react'\n\n/*\n * A sign-up screen, built from the ways in your app offers: a name, an email\n * and a password, and Google, GitHub and Apple, in any order, with\n * SignUpDivider between two. Once the account is made, the card asks the\n * person to check their email, and the form goes with it, as a password\n * manager expects before it offers to save the password. It shares the Sign\n * in block's screen, so the two read as one.\n *\n *   <SignUp footer={signInLink}>\n *     <SignUpForm onSignUp={createAccount} onResendEmail={resendLink} />\n *     <SignUpDivider />\n *     <SignUpSocial onSignUp={openProvider} />\n *   </SignUp>\n */\n\nfunction SignUp({\n  title = 'Create your account',\n  description = 'It takes less than a minute.',\n  footer,\n  heading = 'h1',\n  className,\n  children,\n}: {\n  title?: string\n  /** The title's level: the page's `h1` on a sign-up page, lower inside another page. */\n  heading?: AuthHeading\n  description?: ReactNode\n  /** Under the card: a link to sign in. */\n  footer?: ReactNode\n  className?: string\n  /** The ways in: SignUpForm, SignUpSocial and SignUpDivider. */\n  children: ReactNode\n}) {\n  const [sent, setSent] = useState<SignUpSent | null>(null)\n  // What the form held when it was sent, so it comes back filled.\n  const [draft, setDraft] = useState<SignUpValues | null>(null)\n  const created = useCallback((next: SignUpSent) => {\n    setDraft(next.values)\n    setSent(next)\n  }, [])\n  const context = useMemo(() => ({ created, draft }), [created, draft])\n\n  return (\n    <SignUpContext value={context}>\n      <AuthScreen\n        slot='sign-up'\n        title={title}\n        description={description}\n        heading={heading}\n        footer={footer}\n        className={className}\n        end={\n          sent ? (\n            <CheckEmail\n              email={sent.values.email}\n              onResend={sent.onResend}\n              resendAfter={sent.resendAfter}\n              onBack={() => setSent(null)}\n              heading={heading}\n            />\n          ) : null\n        }\n      >\n        {children}\n      </AuthScreen>\n    </SignUpContext>\n  )\n}\n\n/** \"or\", between two ways in. */\nfunction SignUpDivider({ children = 'or' }: { children?: ReactNode }) {\n  return (\n    <div className='flex flex-col pt-(--surface-gap)'>\n      <AuthDivider>{children}</AuthDivider>\n    </div>\n  )\n}\n\nexport { SignUp, SignUpDivider, SignUpForm, SignUpSocial }\nexport type { SignUpErrors } from '@/registry/components/kino/sign-up/sign-up-flow'\n",
      "type": "registry:component",
      "target": "@components/kino/sign-up/sign-up.tsx"
    },
    {
      "path": "registry/components/kino/sign-up/sign-up-form.tsx",
      "content": "'use client'\n\nimport { Button } from '@/registry/components/kino/button'\nimport { Checkbox } from '@/registry/components/kino/checkbox'\nimport {\n  Field,\n  FieldError,\n  FieldLabel,\n  Form,\n  isFormErrors,\n  useFormSubmission,\n} from '@/registry/components/kino/field'\nimport { Input } from '@/registry/components/kino/input'\nimport { PasswordInput } from '@/registry/components/kino/password-input'\nimport { PasswordStrength } from '@/registry/components/kino/password-strength'\nimport { useSignUp } from '@/registry/components/kino/sign-up/sign-up-context'\nimport {\n  emailError,\n  nameError,\n  passwordError,\n  passwordRules,\n  type SignUpErrors,\n  type SignUpValues,\n  signUpMessages,\n  termsError,\n  userInputs,\n} from '@/registry/components/kino/sign-up/sign-up-flow'\nimport { AuthEmailInput, AuthMessage } from '@/registry/lib/auth-screen'\nimport { type FocusEvent, type ReactNode, useState } from 'react'\n\n/*\n * A new account: a name, an email, a password with its strength and the\n * rules it must meet, and the terms. A field is checked when the person\n * leaves it, and every field when they press Create account; an error goes\n * as soon as they start fixing it. An empty field is asked for only after a\n * press, so tabbing through never fills the form with errors. One form with\n * a real submit, so a password manager offers a new password and saves it\n * with the email once the account is made.\n */\n\ntype FieldName = 'name' | 'email' | 'password' | 'terms'\n\n/** Where the person is with a field: editing it since they last left it, or left it. */\ntype Visit = 'editing' | 'left'\n\nconst linkClassName =\n  'text-foreground font-medium underline underline-offset-4 focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-hidden'\n\nconst defaultTerms = (\n  <>\n    I agree to the{' '}\n    {/* oxlint-disable-next-line next/no-html-link-for-pages -- a plain link works in any app; use your router's link if it has one. */}\n    <a href='/terms' className={linkClassName}>\n      Terms of Service\n    </a>{' '}\n    and {/* oxlint-disable-next-line next/no-html-link-for-pages -- as above. */}\n    <a href='/privacy' className={linkClassName}>\n      Privacy Policy\n    </a>\n  </>\n)\n\nfunction SignUpForm({\n  onSignUp,\n  onResendEmail,\n  resendAfter = 30,\n  terms = defaultTerms,\n  defaultEmail = '',\n}: {\n  /**\n   * Creates the account and emails a link to finish it. Return your\n   * server's errors by field to show them there, such as a password found\n   * in a breach; a throw or a rejected promise says it couldn't be reached.\n   */\n  onSignUp?: (\n    account: Omit<SignUpValues, 'agreed'>,\n  ) => SignUpErrors | void | Promise<SignUpErrors | void>\n  /** Emails the link again. A throw or a rejected promise says it couldn't. */\n  onResendEmail?: (email: string) => unknown\n  /** Seconds before another email can go. */\n  resendAfter?: number\n  /** What the person agrees to, beside the checkbox, with links to read it. */\n  terms?: ReactNode\n  /** The email it opens with, such as the one an invite went to. */\n  defaultEmail?: string\n}) {\n  const screen = useSignUp()\n  const [values, setValues] = useState<SignUpValues>(\n    () => screen?.draft ?? { name: '', email: defaultEmail, password: '', agreed: false },\n  )\n  const set = (change: Partial<SignUpValues>) =>\n    setValues((now) => ({ ...now, ...change }))\n  // Whether Create account has been pressed: until then, an empty field isn't an error.\n  const [tried, setTried] = useState(false)\n\n  return (\n    <div className='flex flex-col pt-(--surface-gap)'>\n      <Form\n        className='flex flex-col gap-(--surface-gap)'\n        // A press checks every field, whether the browser turns the form down or sends it.\n        onInvalidCapture={() => setTried(true)}\n        onSubmitCapture={() => setTried(true)}\n        onFormSubmit={async () => {\n          const { name, email, password } = values\n          const refused = await onSignUp?.({ name, email, password })\n          // Errors by field turn the submit down, and the Form shows them there.\n          if (!isFormErrors(refused)) {\n            screen?.created({ values, onResend: onResendEmail, resendAfter })\n          }\n          return refused\n        }}\n      >\n        <SignUpFields values={values} set={set} tried={tried} terms={terms} />\n      </Form>\n    </div>\n  )\n}\n\n/** The fields and the button, locked while the account is made, with what went wrong under them. */\nfunction SignUpFields({\n  values,\n  set,\n  tried,\n  terms,\n}: {\n  values: SignUpValues\n  set: (change: Partial<SignUpValues>) => void\n  tried: boolean\n  terms: ReactNode\n}) {\n  const status = useFormSubmission()?.status\n  // Still busy once saved: the form leaves for the email screen as it was, spinner and all.\n  const busy = status === 'saving' || status === 'saved'\n  // Each check is the whole of it, so a press finds every problem; what shows\n  // after the person leaves a field waits for a press to ask for an empty one.\n  const check = (asked: boolean): Record<FieldName, string | null> => ({\n    name: nameError(values.name, asked),\n    email: emailError(values.email, asked),\n    password: passwordError(values, asked),\n    terms: termsError(values.agreed, asked),\n  })\n  const errors = check(true)\n  const shown = check(tried)\n  const [visits, setVisits] = useState<Partial<Record<FieldName, Visit>>>({})\n  const visit = (name: FieldName, next: Visit) =>\n    setVisits((last) => (last[name] === next ? last : { ...last, [name]: next }))\n  // A field the person is editing hides its error, and one they left shows\n  // it; until then, a press shows it.\n  const checked = (name: FieldName) => ({\n    name,\n    validate: () => errors[name],\n    invalid:\n      visits[name] === 'editing'\n        ? false\n        : visits[name] === 'left' && shown[name]\n          ? true\n          : undefined,\n    onInputCapture: () => visit(name, 'editing'),\n    onChangeCapture: () => visit(name, 'editing'),\n    onBlur: (event: FocusEvent<HTMLDivElement>) => {\n      if (!event.currentTarget.contains(event.relatedTarget)) visit(name, 'left')\n    },\n  })\n  return (\n    <>\n      <Field {...checked('name')}>\n        <FieldLabel>Name</FieldLabel>\n        <Input\n          autoComplete='name'\n          value={values.name}\n          readOnly={busy}\n          onValueChange={(name) => set({ name })}\n        />\n        <FieldError />\n      </Field>\n      <Field {...checked('email')}>\n        <FieldLabel>Email</FieldLabel>\n        <AuthEmailInput\n          autoComplete='username'\n          value={values.email}\n          readOnly={busy}\n          // Back from the email screen, the address is what needs fixing.\n          data-autofocus=''\n          onValueChange={(email) => set({ email })}\n        />\n        <FieldError />\n      </Field>\n      <Field {...checked('password')}>\n        <FieldLabel>Password</FieldLabel>\n        <PasswordInput\n          autoComplete='new-password'\n          value={values.password}\n          readOnly={busy}\n          onValueChange={(password) => set({ password })}\n        />\n        <PasswordStrength\n          value={values.password}\n          userInputs={userInputs(values)}\n          rules={passwordRules(values)}\n        />\n        <FieldError />\n      </Field>\n      <Field {...checked('terms')}>\n        <FieldLabel className='flex items-start gap-2.5 pb-0 text-sm leading-5 font-normal'>\n          <Checkbox\n            checked={values.agreed}\n            readOnly={busy}\n            className='mt-0.5'\n            onCheckedChange={(agreed) => set({ agreed: agreed === true })}\n          />\n          <span className='text-muted-foreground'>{terms}</span>\n        </FieldLabel>\n        <FieldError className='ps-6.5' />\n      </Field>\n      <div className='flex flex-col'>\n        <Button type='submit' loading={busy}>\n          Create account\n        </Button>\n        <AuthMessage>\n          {status === 'failed' ? signUpMessages['sign-up-failed'] : null}\n        </AuthMessage>\n      </div>\n    </>\n  )\n}\n\nexport { SignUpForm }\n",
      "type": "registry:component",
      "target": "@components/kino/sign-up/sign-up-form.tsx"
    },
    {
      "path": "registry/components/kino/sign-up/sign-up-social.tsx",
      "content": "'use client'\n\nimport type {\n  SocialProvider,\n  SocialSignInLayout,\n} from '@/registry/components/kino/social-sign-in'\nimport { AuthSocial } from '@/registry/lib/auth-screen'\n\n/*\n * Signing up with an account the person already has: Google, GitHub or\n * Apple. A press opens the provider, which makes the account on its way\n * back; one that can't be reached goes back to rest and says so under the\n * buttons.\n */\n\nfunction SignUpSocial({\n  onSignUp,\n  layout,\n}: {\n  /** Opens the provider's sign-up. A throw or a rejected promise says it couldn't be reached. */\n  onSignUp?: (provider: SocialProvider) => unknown\n  /** `stack`: full-width buttons with their names. `row`: the logos alone, side by side. */\n  layout?: SocialSignInLayout\n}) {\n  return (\n    <div className='flex flex-col pt-(--surface-gap)'>\n      <AuthSocial\n        label='Sign up with Google, GitHub or Apple'\n        onOpen={onSignUp}\n        layout={layout}\n      />\n    </div>\n  )\n}\n\nexport { SignUpSocial }\n",
      "type": "registry:component",
      "target": "@components/kino/sign-up/sign-up-social.tsx"
    },
    {
      "path": "registry/components/kino/sign-up/check-email.tsx",
      "content": "'use client'\n\nimport { Button } from '@/registry/components/kino/button'\nimport { signUpMessages } from '@/registry/components/kino/sign-up/sign-up-flow'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport {\n  AuthEnd,\n  type AuthHeading,\n  AuthMessage,\n  AuthResend,\n  useCountdown,\n} from '@/registry/lib/auth-screen'\nimport { settle } from '@/registry/lib/settle'\nimport { useState } from 'react'\n\n/*\n * Once the account is made: where the link went, a resend once the wait is\n * over, and a way back to the form to fix the address, with everything\n * typed still there. It says the same whether or not the email already had\n * an account, so the form never tells anyone who has signed up.\n */\n\nfunction CheckEmail({\n  email,\n  onResend,\n  resendAfter,\n  onBack,\n  heading,\n}: {\n  email: string\n  /** Emails the link again. A throw or a rejected promise says it couldn't. */\n  onResend?: (email: string) => unknown\n  /** Seconds before another email can go. */\n  resendAfter: number\n  /** Back to the form, to use another address. */\n  onBack: () => void\n  heading?: AuthHeading\n}) {\n  const [sent, setSent] = useState(0)\n  const [busy, setBusy] = useState(false)\n  const [failed, setFailed] = useState(false)\n  const countdown = useCountdown(sent, resendAfter)\n\n  const resend = () =>\n    new Promise<boolean>((resolve) => {\n      setBusy(true)\n      setFailed(false)\n      settle(\n        () => onResend?.(email),\n        (ok) => {\n          setBusy(false)\n          if (ok) setSent((count) => count + 1)\n          else setFailed(true)\n          resolve(ok)\n        },\n      )\n    })\n\n  return (\n    <AuthEnd\n      heading={heading}\n      title='Check your email'\n      description={\n        <>\n          We sent a link to <span className='text-foreground font-medium'>{email}</span>.\n          Open it to finish creating your account.\n        </>\n      }\n      icon={\n        <IconPlaceholder\n          lucide='MailIcon'\n          tabler='IconMail'\n          hugeicons='Mail01Icon'\n          phosphor='EnvelopeSimpleIcon'\n          remixicon='RiMailLine'\n          aria-hidden\n        />\n      }\n    >\n      <div className='flex flex-col items-center'>\n        <AuthResend\n          className='justify-center'\n          left={countdown.left}\n          busy={busy}\n          onResend={resend}\n          sent={`We sent another link to ${email}.`}\n        >\n          Resend email\n        </AuthResend>\n        <AuthMessage>{failed ? signUpMessages['resend-failed'] : null}</AuthMessage>\n        <Button\n          type='button'\n          variant='link'\n          size='sm'\n          className='text-muted-foreground hover:text-foreground h-auto min-h-6 px-0 font-normal'\n          onClick={onBack}\n        >\n          Use a different email\n        </Button>\n      </div>\n    </AuthEnd>\n  )\n}\n\nexport { CheckEmail }\n",
      "type": "registry:component",
      "target": "@components/kino/sign-up/check-email.tsx"
    },
    {
      "path": "registry/components/kino/sign-up/sign-up-context.ts",
      "content": "'use client'\n\nimport type { SignUpValues } from '@/registry/components/kino/sign-up/sign-up-flow'\nimport { createContext, use } from 'react'\n\n/*\n * How the sign-up's form tells the screen the account is made, and gets\n * back what it held when the person comes back to fix the address.\n */\n\n/** An account just made: what was sent, and how to send its email again. */\ninterface SignUpSent {\n  values: SignUpValues\n  onResend?: (email: string) => unknown\n  /** Seconds before another email can go. */\n  resendAfter: number\n}\n\ninterface SignUpContextValue {\n  /** Shows the check-your-email screen. */\n  created: (sent: SignUpSent) => void\n  /** What the form held when it was last sent, or null. */\n  draft: SignUpValues | null\n}\n\nconst SignUpContext = createContext<SignUpContextValue | null>(null)\n\nfunction useSignUp() {\n  return use(SignUpContext)\n}\n\nexport { SignUpContext, useSignUp }\nexport type { SignUpContextValue, SignUpSent }\n",
      "type": "registry:component",
      "target": "@components/kino/sign-up/sign-up-context.ts"
    },
    {
      "path": "registry/components/kino/sign-up/sign-up-flow.ts",
      "content": "import {\n  estimatePasswordScore,\n  type PasswordRule,\n  usesUserInputs,\n} from '@/registry/components/kino/password-strength'\nimport { looksLikeEmail } from '@/registry/lib/auth-screen'\nimport { graphemes } from '@/registry/lib/swap'\n\n/*\n * What the sign-up checks, apart from how it looks: each field's rule and\n * what it says when the rule isn't met. An empty field is asked for only\n * once the person has tried to create the account, so tabbing through the\n * form never fills it with errors.\n */\n\n/** Your product's name: a password built on it is easy to guess. */\nconst PRODUCT_NAME = 'Kino'\n\n/** How long a password has to be: 15 characters where a password alone signs in (NIST SP 800-63B). */\nconst MIN_PASSWORD_LENGTH = 15\n\n/** What the person has typed so far. */\ninterface SignUpValues {\n  name: string\n  email: string\n  password: string\n  agreed: boolean\n}\n\n/** What your server refuses, by field, such as a password found in a breach. */\ntype SignUpErrors = Partial<Record<'name' | 'email' | 'password', string>>\n\n/** What the person typed that a password shouldn't be built on, with your product's name. */\nfunction userInputs({ name, email }: Pick<SignUpValues, 'name' | 'email'>) {\n  return [name, email, PRODUCT_NAME].filter(Boolean)\n}\n\n/** The rules a new password must meet, listed under it and ticked as they're met. */\nfunction passwordRules(values: Pick<SignUpValues, 'name' | 'email'>): PasswordRule[] {\n  const inputs = userInputs(values)\n  return [\n    {\n      label: `${MIN_PASSWORD_LENGTH} characters or more`,\n      met: (password) => graphemes(password).length >= MIN_PASSWORD_LENGTH,\n    },\n    {\n      label: `Doesn’t use your name, your email or ${PRODUCT_NAME}`,\n      met: (password) => password.length > 0 && !usesUserInputs(password, inputs),\n    },\n  ]\n}\n\nconst signUpMessages = {\n  'missing-name': 'Enter your name.',\n  'missing-email': 'Enter your email address.',\n  'invalid-email': 'Enter an email address, such as maya@kinoui.com.',\n  'missing-password': 'Choose a password.',\n  'password-rules': 'Meet every rule above to create your account.',\n  'weak-password': 'It’s still easy to guess. Add a word or two.',\n  'missing-terms': 'Agree to the terms to create your account.',\n  'sign-up-failed': 'We couldn’t create your account just now. Try again in a moment.',\n  'resend-failed': 'We couldn’t send another email just now. Try again in a moment.',\n} as const\n\n/** What's wrong with the name, or null; an empty one only once the person has `tried`. */\nfunction nameError(name: string, tried: boolean) {\n  return !name.trim() && tried ? signUpMessages['missing-name'] : null\n}\n\n/** What's wrong with the email, or null; an empty one only once the person has `tried`. */\nfunction emailError(email: string, tried: boolean) {\n  if (!email.trim()) return tried ? signUpMessages['missing-email'] : null\n  return looksLikeEmail(email) ? null : signUpMessages['invalid-email']\n}\n\n/**\n * What's wrong with the password, or null: an empty one once the person has\n * `tried`, then a rule not met, then one that's still easy to guess.\n */\nfunction passwordError(values: Omit<SignUpValues, 'agreed'>, tried: boolean) {\n  const { password } = values\n  if (!password) return tried ? signUpMessages['missing-password'] : null\n  if (!passwordRules(values).every((rule) => rule.met(password))) {\n    return signUpMessages['password-rules']\n  }\n  return estimatePasswordScore(password, userInputs(values)) < 2\n    ? signUpMessages['weak-password']\n    : null\n}\n\n/** What's wrong with the terms, or null; asked for only once the person has `tried`. */\nfunction termsError(agreed: boolean, tried: boolean) {\n  return !agreed && tried ? signUpMessages['missing-terms'] : null\n}\n\nexport {\n  emailError,\n  nameError,\n  passwordError,\n  passwordRules,\n  signUpMessages,\n  termsError,\n  userInputs,\n}\nexport type { SignUpErrors, SignUpValues }\n",
      "type": "registry:component",
      "target": "@components/kino/sign-up/sign-up-flow.ts"
    }
  ],
  "type": "registry:block"
}