{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "block-modal-form",
  "type": "registry:block",
  "title": "Form modal",
  "description": "A short form in a modal: its fields, a consent to tick, the actions in the footer and keyboard hints under them.",
  "dependencies": [
    "@remixicon/react@^4.9.0"
  ],
  "registryDependencies": [
    "https://wideset.co/r/block-styles.json",
    "https://wideset.co/r/button.json",
    "https://wideset.co/r/checkbox.json",
    "https://wideset.co/r/form-field.json",
    "https://wideset.co/r/icon-button.json",
    "https://wideset.co/r/modal.json",
    "https://wideset.co/r/select.json",
    "https://wideset.co/r/text-input.json",
    "https://wideset.co/r/utils.json"
  ],
  "files": [
    {
      "path": "src/blocks/modal-form/card-brand.tsx",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/card-brand.tsx",
      "content": "'use client';\n\n// TODO(custom): card brand mark in a field's suffix; Wideset has no payment-brand entity (Figma's Text Input \"Entity\" suffix holds a Payment image) and blocks ship no logo artwork.\nimport { RiVisaLine } from '@remixicon/react';\n\n/**\n * The card's brand in the card number field's suffix: Figma's 24px Payment entity, drawn as the\n * Remix mark on the inverse tile instead of logo artwork. Static here: a payment provider's field\n * reports the brand as the number is typed, so swap the mark on that.\n */\nexport function CardBrand() {\n  return (\n    <span\n      role=\"img\"\n      aria-label=\"Visa\"\n      className=\"bg-ws-static-inverse-background text-ws-static-inverse-strong rounded-ws-control-sm size-ws-12 flex shrink-0 items-center justify-center\"\n    >\n      <RiVisaLine aria-hidden=\"true\" className=\"size-ws-10\" />\n    </span>\n  );\n}\n"
    },
    {
      "path": "src/blocks/modal-form/data.ts",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/data.ts",
      "content": "/** The product the card pays for and the workspace paying. */\nexport const PRODUCT = 'Orbit';\nexport const WORKSPACE = 'Lumen Studio';\n\n/** Where the links lead: replace with your routes. */\nexport const ROUTES = {\n  terms: '#terms',\n};\n\n/** The countries a card can be billed in; the first is preselected. */\nexport const COUNTRIES = [\n  { value: 'us', label: 'United States' },\n  { value: 'ca', label: 'Canada' },\n  { value: 'de', label: 'Germany' },\n  { value: 'nl', label: 'Netherlands' },\n  { value: 'pl', label: 'Poland' },\n  { value: 'gb', label: 'United Kingdom' },\n];\n"
    },
    {
      "path": "src/blocks/modal-form/index.tsx",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/index.tsx",
      "content": "'use client';\n\nimport '@/styles/wideset-blocks.css';\n\nimport { Button } from '@/components/ui/wideset/button/Button';\nimport { useState } from 'react';\n\nimport { PaymentFormModal } from './payment-form-modal';\n\n/** Form modal: a short form over the page, here over an empty page that reopens it. */\nexport default function ModalFormBlock() {\n  const [open, setOpen] = useState(true);\n\n  return (\n    <div className=\"ws-block-utility bg-ws-static-foreground flex h-dvh items-center justify-center\">\n      <Button variant=\"secondary\" preset=\"border\" onClick={() => setOpen(true)}>\n        Add payment method\n      </Button>\n      <PaymentFormModal open={open} onOpenChange={setOpen} />\n    </div>\n  );\n}\n"
    },
    {
      "path": "src/blocks/modal-form/keyboard-hints.tsx",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/keyboard-hints.tsx",
      "content": "'use client';\n\n// TODO(custom): keyboard hints (a key chip and what it does) and the keys they name; Wideset has no Kbd / shortcut component, so the key is the box of Button `xs` Border drawn by hand.\nimport {\n  type RemixiconComponentType,\n  RiArrowDownSLine,\n  RiArrowLeftLine,\n  RiArrowUpSLine,\n} from '@remixicon/react';\nimport { cn } from '@/lib/wideset/utils';\nimport type { KeyboardEvent, ReactNode } from 'react';\n\n/**\n * The hints in the modal's footer strip, as Figma draws them: moving through the modal, going\n * back to where it was opened from, and closing. Esc is the Modal's own; `modalKeys` wires the\n * other two on `Modal.Content`, so every hint does what it says.\n */\nexport function KeyboardHints() {\n  return (\n    <>\n      <div className=\"gap-ws-12 flex items-center\">\n        <Hint\n          keys={\n            <span className=\"gap-ws-3 flex\">\n              <Key icon={RiArrowUpSLine} name=\"Up arrow\" />\n              <Key icon={RiArrowDownSLine} name=\"Down arrow\" />\n            </span>\n          }\n        >\n          Navigate\n        </Hint>\n        <Hint keys={<Key icon={RiArrowLeftLine} name=\"Left arrow\" />}>Return to parent</Hint>\n      </div>\n      <Hint keys={<Key>esc</Key>}>Close</Hint>\n    </>\n  );\n}\n\n/** One hint: the keys, then what they do. */\nfunction Hint({ keys, children }: { keys: ReactNode; children: string }) {\n  return (\n    <span className=\"gap-ws-5 typography-body-md text-ws-static-subtle flex items-center\">\n      {keys}\n      {children}\n    </span>\n  );\n}\n\n/** A key as a `kbd`, 24px high. An icon key names itself for screen readers. */\nfunction Key({\n  icon: Icon,\n  name,\n  children,\n}: {\n  icon?: RemixiconComponentType;\n  name?: string;\n  children?: string;\n}) {\n  return (\n    <kbd\n      className={cn(\n        'font-ws-body border-ws-dynamic-secondary-default bg-ws-static-highlight-subtle shadow-ws-controls-control rounded-ws-control-md h-ws-12 typography-caption-strong-md text-ws-dynamic-secondary-default flex items-center justify-center border',\n        Icon ? 'w-ws-12' : 'px-ws-5',\n      )}\n    >\n      {Icon ? (\n        <>\n          <Icon aria-hidden=\"true\" className=\"size-ws-8 shrink-0\" />\n          <span className=\"sr-only\">{name}</span>\n        </>\n      ) : (\n        children\n      )}\n    </kbd>\n  );\n}\n\n/** Places where the arrows already mean something: a list, a choice, tabs, a menu, a number. */\nconst OWN_ARROWS =\n  'textarea, select, [role=\"combobox\"], [aria-haspopup], [role=\"listbox\"], [role=\"radiogroup\"], [role=\"radio\"], [role=\"tablist\"], [role=\"tab\"], [role=\"menu\"], [role=\"slider\"], input[type=\"number\"], input[type=\"range\"]';\n\nconst FOCUSABLE =\n  'a[href], button:not([disabled]), input:not([disabled]):not([type=\"hidden\"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])';\n\n/** In a text field the caret moves with Left, unless it stands at the very start. */\nconst caretAtStart = (target: Element) =>\n  !(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) ||\n  (target.selectionStart === 0 && target.selectionEnd === 0);\n\n/**\n * The keys the hints name, for `Modal.Content`'s `onKeyDown`: Up and Down move to the previous or\n * next control in the modal, Left goes back with `onBack`. Neither takes a key from a place that\n * uses it (a select, a radio group, tabs, a list, a menu), nor one already handled; Left in a text\n * field only once the caret is at its start, so it still moves the caret.\n */\nexport function modalKeys(onBack: () => void) {\n  return (event: KeyboardEvent<HTMLElement>) => {\n    const { key, target, currentTarget } = event;\n    if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return;\n    if (!(target instanceof Element) || target.closest(OWN_ARROWS)) return;\n    if (key === 'ArrowUp' || key === 'ArrowDown') {\n      const controls = [...currentTarget.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(\n        (element) => element.getClientRects().length > 0,\n      );\n      const next =\n        controls[controls.indexOf(target as HTMLElement) + (key === 'ArrowDown' ? 1 : -1)];\n      if (!next) return;\n      event.preventDefault();\n      next.focus();\n    } else if (key === 'ArrowLeft' && !event.shiftKey && caretAtStart(target)) {\n      event.preventDefault();\n      onBack();\n    }\n  };\n}\n"
    },
    {
      "path": "src/blocks/modal-form/payment-form-modal.tsx",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/payment-form-modal.tsx",
      "content": "'use client';\n\nimport { RiArrowLeftLine } from '@remixicon/react';\nimport { Button } from '@/components/ui/wideset/button/Button';\nimport { IconButton } from '@/components/ui/wideset/button/IconButton';\nimport { Modal } from '@/components/ui/wideset/modal/Modal';\nimport { useId } from 'react';\n\nimport { PRODUCT, WORKSPACE } from './data';\nimport { KeyboardHints, modalKeys } from './keyboard-hints';\nimport { PaymentForm } from './payment-form';\n\ntype PaymentFormModalProps = {\n  open: boolean;\n  onOpenChange: (open: boolean) => void;\n};\n\n/**\n * Figma's Modal `Type=Form`: a short form that only makes sense on its own, here adding a card.\n * The header leads back to where the modal was opened from, the form scrolls between the header\n * and the footer when the window is short, and the actions stay in the footer: the submit button\n * points at the form by id. Focus starts in the first field; Esc, Cancel and the X close it.\n * Up and Down move between the fields, Left goes back to billing as the header's arrow does.\n */\nexport function PaymentFormModal({ open, onOpenChange }: PaymentFormModalProps) {\n  const id = useId();\n\n  return (\n    <Modal open={open} onOpenChange={onOpenChange}>\n      <Modal.Content className=\"ws-block-utility\"\n        onOpenAutoFocus={(event) => {\n          event.preventDefault();\n          document.getElementById(`${id}-number`)?.focus();\n        }}\n        onKeyDown={modalKeys(() => onOpenChange(false))}\n      >\n        <Modal.Header className=\"gap-ws-9 flex-row\">\n          <Modal.Close asChild>\n            <IconButton\n              variant=\"secondary\"\n              preset=\"border\"\n              size=\"sm\"\n              icon={RiArrowLeftLine}\n              aria-label=\"Back to billing\"\n            />\n          </Modal.Close>\n          <div className=\"min-w-0 flex-1\">\n            <Modal.Title>Add payment method</Modal.Title>\n            <Modal.Description>Pays for {WORKSPACE}’s plan.</Modal.Description>\n          </div>\n        </Modal.Header>\n        <Modal.Body className=\"gap-ws-11 flex flex-col\">\n          <p className=\"typography-body-md text-ws-static-subtle\">\n            By providing your card information, you allow {PRODUCT} to charge your card for future\n            payments in accordance with their terms.\n          </p>\n          <PaymentForm id={id} onSubmit={() => onOpenChange(false)} />\n        </Modal.Body>\n        <Modal.Footer className=\"items-center\">\n          <p className=\"typography-body-md text-ws-static-subtle mr-ws-9 min-w-0 flex-1\">\n            Charged monthly, cancel any time.\n          </p>\n          <Modal.Close asChild>\n            <Button variant=\"secondary\" preset=\"border\">\n              Cancel\n            </Button>\n          </Modal.Close>\n          <Button type=\"submit\" form={id}>\n            Add card\n          </Button>\n        </Modal.Footer>\n        <Modal.Footer variant=\"extension\" className=\"items-center\">\n          <KeyboardHints />\n        </Modal.Footer>\n      </Modal.Content>\n    </Modal>\n  );\n}\n"
    },
    {
      "path": "src/blocks/modal-form/payment-form.tsx",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/payment-form.tsx",
      "content": "'use client';\n\nimport { Checkbox } from '@/components/ui/wideset/checkbox/Checkbox';\nimport { FormField } from '@/components/ui/wideset/form-field/FormField';\nimport { Select } from '@/components/ui/wideset/select/Select';\nimport { TextInput } from '@/components/ui/wideset/text-input/TextInput';\nimport { type ChangeEvent, type FormEvent, useState } from 'react';\n\nimport { CardBrand } from './card-brand';\nimport { COUNTRIES, PRODUCT, ROUTES } from './data';\nimport { TextLink } from './text-link';\n\nexport type PaymentValues = {\n  number: string;\n  expiry: string;\n  cvc: string;\n  country: string;\n  zip: string;\n  email: string;\n  terms: boolean;\n};\n\ntype Field = keyof PaymentValues;\n\n/** Every field is required: what its error says when it is left empty. */\nconst REQUIRED: Record<Field, string> = {\n  number: 'Enter the card number.',\n  expiry: 'Enter the expiration date.',\n  cvc: 'Enter the security code.',\n  country: 'Choose a country.',\n  zip: 'Enter the ZIP code.',\n  email: 'Enter a billing e-mail.',\n  terms: 'Accept the terms to continue.',\n};\n\nconst filled = (value: string | boolean) =>\n  typeof value === 'string' ? value.trim() !== '' : value;\n\ntype PaymentFormProps = {\n  /** The form's id: the submit button sits in the modal's footer and points at it with `form`. */\n  id: string;\n  /** Called with the values once every field is filled. */\n  onSubmit: (values: PaymentValues) => void;\n};\n\n/**\n * The payment form: a card, where it is billed and the consent to charge it. Fields are\n * controlled; submitting names every empty field at the field and moves focus to the first. It\n * draws no button of its own, so the modal's footer can hold the actions. Nothing here talks to a\n * payment provider: swap the card fields for the provider's own when you connect one.\n */\nexport function PaymentForm({ id, onSubmit }: PaymentFormProps) {\n  const [values, setValues] = useState<PaymentValues>({\n    number: '',\n    expiry: '',\n    cvc: '',\n    country: COUNTRIES[0].value,\n    zip: '',\n    email: '',\n    terms: false,\n  });\n  const [errors, setErrors] = useState<Partial<Record<Field, string>>>({});\n\n  const set = <F extends Field>(field: F, value: PaymentValues[F]) => {\n    setValues((all) => ({ ...all, [field]: value }));\n    setErrors((all) => ({ ...all, [field]: undefined }));\n  };\n\n  const submit = (event: FormEvent<HTMLFormElement>) => {\n    event.preventDefault();\n    const empty = (Object.keys(REQUIRED) as Field[]).filter((field) => !filled(values[field]));\n    setErrors(Object.fromEntries(empty.map((field) => [field, REQUIRED[field]])));\n    if (empty.length) document.getElementById(`${id}-${empty[0]}`)?.focus();\n    else onSubmit(values);\n  };\n\n  /** What every text field shares: its id, value, change handler and error look. */\n  const text = (field: Exclude<Field, 'country' | 'terms'>) => ({\n    id: `${id}-${field}`,\n    name: field,\n    required: true,\n    value: values[field],\n    variant: errors[field] ? ('danger' as const) : ('primary' as const),\n    onChange: (event: ChangeEvent<HTMLInputElement>) => set(field, event.target.value),\n  });\n\n  return (\n    <form id={id} noValidate onSubmit={submit} className=\"gap-ws-11 flex flex-col\">\n      <FormField label=\"Card number\" htmlFor={`${id}-number`} errorText={errors.number}>\n        <TextInput\n          {...text('number')}\n          inputMode=\"numeric\"\n          autoComplete=\"cc-number\"\n          placeholder=\"1234 1234 1234 1234\"\n        >\n          <TextInput.Input />\n          <TextInput.Suffix>\n            <CardBrand />\n          </TextInput.Suffix>\n        </TextInput>\n      </FormField>\n      <div className=\"gap-ws-9 grid grid-cols-2\">\n        <FormField label=\"Expiration date\" htmlFor={`${id}-expiry`} errorText={errors.expiry}>\n          <TextInput\n            {...text('expiry')}\n            inputMode=\"numeric\"\n            autoComplete=\"cc-exp\"\n            placeholder=\"MM / YY\"\n          />\n        </FormField>\n        <FormField label=\"Security code\" htmlFor={`${id}-cvc`} errorText={errors.cvc}>\n          <TextInput {...text('cvc')} inputMode=\"numeric\" autoComplete=\"cc-csc\" placeholder=\"CVC\" />\n        </FormField>\n      </div>\n      <div className=\"gap-ws-9 grid grid-cols-2\">\n        <FormField\n          label=\"Country\"\n          htmlFor={`${id}-country`}\n          tooltip=\"Where the card is billed. It sets the tax on your invoices.\"\n          errorText={errors.country}\n        >\n          <Select\n            id={`${id}-country`}\n            name=\"country\"\n            className=\"w-full\"\n            options={COUNTRIES}\n            placeholder=\"Choose a country\"\n            clearable\n            required\n            value={values.country}\n            onValueChange={(value) => set('country', value)}\n            variant={errors.country ? 'danger' : 'primary'}\n          />\n        </FormField>\n        <FormField label=\"ZIP code\" htmlFor={`${id}-zip`} errorText={errors.zip}>\n          <TextInput {...text('zip')} autoComplete=\"postal-code\" placeholder=\"12345\" />\n        </FormField>\n      </div>\n      <FormField label=\"Billing email\" htmlFor={`${id}-email`} errorText={errors.email}>\n        <TextInput\n          {...text('email')}\n          type=\"email\"\n          autoComplete=\"email\"\n          placeholder=\"e.g. finance@lumenstudio.co\"\n        />\n      </FormField>\n      {/*\n        FormField draws the consent's error under the box. The checkbox carries its own label, so\n        the field's label row is empty: `gap-0` takes back the gap it would leave above the box.\n      */}\n      <FormField label={null} errorText={errors.terms} className=\"gap-0\">\n        <Checkbox\n          id={`${id}-terms`}\n          name=\"terms\"\n          required\n          checked={values.terms}\n          onCheckedChange={(checked) => set('terms', checked === true)}\n          variant={errors.terms ? 'danger' : 'primary'}\n        >\n          <Checkbox.Label>\n            I agree to the {PRODUCT} <TextLink href={ROUTES.terms}>Terms &amp; Conditions</TextLink>\n          </Checkbox.Label>\n        </Checkbox>\n      </FormField>\n    </form>\n  );\n}\n"
    },
    {
      "path": "src/blocks/modal-form/text-link.tsx",
      "type": "registry:file",
      "target": "blocks/wideset/modal-form/text-link.tsx",
      "content": "'use client';\n\n// TODO(custom): a link inside a sentence; Wideset has no inline text link (LinkButton is a control with its own padding and height).\nimport { cn } from '@/lib/wideset/utils';\nimport type { AnchorHTMLAttributes } from 'react';\n\n/**\n * A link inside a sentence, as Figma draws the terms link: the sentence's own type in the primary\n * colour. The underline comes on hover and focus.\n */\nexport function TextLink({\n  className,\n  children,\n  ...props\n}: AnchorHTMLAttributes<HTMLAnchorElement>) {\n  return (\n    <a\n      className={cn(\n        'text-ws-dynamic-primary-default hover:text-ws-dynamic-primary-hover active:text-ws-dynamic-primary-pressed rounded-ws-control-sm focus-visible:wds-focus-visible outline-ws-dynamic-primary-strong-default decoration-from-font hover:underline focus-visible:underline',\n        className,\n      )}\n      {...props}\n    >\n      {children}\n    </a>\n  );\n}\n"
    }
  ],
  "meta": {
    "version": "1.1.0",
    "commit": "a734f9f0"
  }
}
